MySQL 零基础全套入门教程|DDL+DML + 五大约束 + DQL 查询(超详细代码笔记)
·
📌 简介
本文整理MySQL 完整基础知识点,包含 SQL 注释、DDL 库表操作、DML 增删改、数据库五大约束、DQL 条件 / 模糊 / 排序 / 聚合 / 分组 / 分页查询,全程可运行代码 + 逐行文字解析,适合零基础入门、期末复习、后端开发入门收藏。
一、SQL 三种注释语法
SQL 注释用于代码说明,不会被执行,MySQL 支持三种写法:
sql
-- 单行注释(通用标准写法)
# MySQL专属单行注释
/*
多行注释
可跨越多行书写
*/
二、DDL 数据定义语言
DDL:操作数据库、表结构,负责创建、修改、删除库和表
2.1 数据库常用操作
sql
-- 查看所有数据库
SHOW DATABASES;
-- 创建数据库
create DATABASE spt2603;
-- 切换/使用指定数据库
use test;
-- 直接删除数据库
drop DATABASE test;
-- 安全删除:存在才删除,避免报错
drop DATABASE if EXISTS test;
-- 修改数据库编码
alter database spt2603 CHARACTER set utf8;
2.2 数据表基础操作
sql
-- 切换数据库
use SPT2603;
-- 创建学生表
CREATE TABLE STU(
id int,
name varchar(10),
age INT,
birthday DATE,
adddress VARCHAR(20),
score DOUBLE
);
-- 查看当前库所有表
show TABLES;
-- 查看表创建语句
show create table stu;
-- 快速查看表结构(字段、类型、约束)
desc stu;
-- 删除数据表
drop table stu2;
2.3 修改表结构
sql
-- 1. 添加新字段
alter table student add school varchar(20);
-- 2. 修改字段名+字段类型
alter table stu change school newschool VARCHAR(20);
alter table stu change newschool school INT;
-- 3. 删除指定字段
alter table student drop school;
-- 4. 修改表名
rename table stu to student;
三、DML 数据操作语言
DML:操作表中数据,包含
INSERT插入、UPDATE修改、DELETE删除
3.1 插入数据 INSERT
sql
-- 方式1:指定字段插入单条数据
insert into student(id,name,age,birthday,adddress,score)
values(1001,'ABC',19,'2005-2-28','晋中',83);
-- 批量插入多条数据
insert into student(id,name,age,birthday,adddress,score)
values(1001,'ABC',19,'2005-2-28','晋中',83),
(1002,'MCN',19,'2005-2-28','晋中',83),
(1003,'QBZ',19,'2005-2-28','晋中',83);
-- 方式2:省略字段,全字段插入
INSERT into student values(1006,'JKL',19,'2006-09-09','河北',100);
-- 只插入部分字段
insert into student(ID)values(1004);
3.2 更新数据 UPDATE
sql
-- 不带条件:修改全表所有数据(高危操作)
update student set adddress = '榆次',age = 20;
-- 带条件修改:精准修改指定数据
update student set adddress = '杭州', score = 100, age = 16 where id=1003;
update student set adddress = '浙江' where id = 1004;
3.3 删除数据 DELETE & TRUNCATE
sql
-- 根据条件删除单条数据
delete from student where id=1005;
-- 删除表中全部数据,保留表结构
delete from student;
-- 清空全表:销毁原表并重建空表
truncate table student;
truncate student;
✅ 核心区别:
delete:只删数据,自增不重置,支持事务回滚truncate:重建数据表,自增归零,效率更高
3.4 综合练习:员工表操作
sql
-- 创建员工表
create table emp(
id INT,
name varchar(10),
gender VARCHAR(10),
salary int
);
-- 批量插入员工数据
insert into emp values
(1001,'张三','男',5000),
(1002,'李四','女',6000),
(1003,'王五','男',7000);
-- 统一修改所有人薪资
UPDATE emp set salary = 8000;
-- 条件修改:单独修改张三薪资
UPDATE emp set salary = 3000 where name = '张三';
-- 查询员工表所有数据
SELECT * from emp;
四、MySQL 五大核心约束
约束作用:限制字段数据,保证数据完整性、合法性五大约束:主键、自增、非空、唯一、默认约束
4.1 主键约束 PRIMARY KEY
- 规则:非空 + 唯一,一张表只能有一个主键
- 联合主键:多个字段组合唯一且非空
sql
-- 普通主键
create table if not exists test1(
id int primary key,
name VARCHAR(10),
gender varchar(10)
);
-- 联合主键
creaTE table if not exists test3(
id int,
name VARCHAR(10),
gender varchar(10),
primary key(id,name)
);
-- 后期修改表添加主键
alter table test5 add primary key(id);
-- 删除主键
alter table test3 drop PRIMARY KEY;
4.2 自增约束 AUTO_INCREMENT
- 必须配合主键使用,整数类型自动累加
delete删除数据,自增延续;truncate重置自增
sql
-- 主键 + 自增 标配
create table user1(
id int primary key auto_increment,
name VARCHAR(10)
);
-- 自增字段填 null 自动生成编号
insert into user1 value(null,'ABC');
-- 指定自增初始值
create table user2(
id int primary key auto_increment,
name VARCHAR(10)
)auto_increment =100;
-- 后期修改自增起始值
alter table user3 auto_increment = 1;
4.3 非空约束 NOT NULL
- 限制字段不能为 NULL
- 空字符串
''不属于空值,可以正常插入
sql
-- 建表时添加非空约束
create table user4(
id int,
name VARCHAR(10) not NULL,
address VARCHAR(10) not NULL
);
-- 后期添加非空约束
alter table user5 modify name VARCHAR(10) NOT NULL;
-- 删除非空约束(去掉 not null)
alter table user5 modify name VARCHAR(10);
4.4 唯一约束 UNIQUE
- 字段数据不能重复,允许为 NULL
sql
-- 建表直接添加唯一约束
create table user6(
id int,
name VARCHAR(10) unique,
address VARCHAR(10)
);
-- 后期添加唯一约束
ALTER table user7 add constraint uni_name unique(name);
-- 删除唯一约束
alter table user7 drop index uni_name;
4.5 默认约束 DEFAULT
- 字段不传值时,自动使用默认值
sql
-- 建表设置默认值
create table user8(
id INT,
name VARCHAR(10),
address VARCHAR(10) default '地球'
);
-- 后期添加默认约束
alter table user9 modify address VARCHAR(10) default '杭州';
-- 删除默认约束
alter table user9 modify address VARCHAR(10);
五、DQL 数据查询语言
DQL:数据库重中之重,用于查询表数据,支持条件、模糊、排序、聚合、分组、分页
5.1 基础查询
sql
-- 查询所有字段
select * from product;
-- 查询指定字段
select name,price from product;
-- 字段别名 / 表别名 as 可省略
select id,name as '名字',price '价格' from product;
5.2 去重查询 DISTINCT
sql
-- 去除重复数据
select distinct price from product;
5.3 条件查询
sql
-- 等值查询
select * from product where price = 500;
-- 范围查询
select * from product where price between 500 and 1000;
-- 多值匹配
select * from product where price in (530,630);
-- 逻辑运算 and / or
select * from product where price > 500 and price < 1000;
5.4 模糊查询 LIKE
%:匹配任意多个字符_:匹配单个字符
sql
-- 包含「海」字
select * from product where name like '%海%';
-- 以海开头
select * from product where name like '海%';
-- 第二个字符为海
select * from product where name like '_海%';
5.5 空值查询
sql
-- 查询字段为 null
select * from product where classify is null;
-- 查询字段不为 null
select * from product where classify is not null;
5.6 排序查询 ORDER BY
asc升序(默认),desc降序
sql
-- 价格降序
select * from product order by price DESC;
-- 多字段联合排序
select * from product order by price desc,classify desc;
5.7 聚合函数
常用:count统计、sum求和、max最大值、min最小值、avg平均值
sql
-- 统计数据条数
select count(*) as '总数' from product;
-- 价格求和
select sum(price) from product;
-- 最高/最低/平均价格
select max(price),min(price),avg(price) from product;
5.8 分组查询 GROUP BY
- 分组后筛选用
having,不能使用 where
sql
-- 根据分类分组统计数量
select classify,count(*) from product group by classify;
-- 分组后条件过滤
select classify,count(*) from product group by classify having count(*) >5;
5.9 分页查询 LIMIT
sql
-- 只查询前5条
select * from product limit 5;
-- 分页语法:limit 起始下标,条数
select * from product limit 0,5; -- 第一页
select * from product limit 5,5; -- 第二页
5.10 结果集迁移
将查询结果直接插入新表
sql
-- 创建新表
create table res(
name VARCHAR(10),
price DOUBLE
);
-- 子查询插入数据
insert into res(name,price) select name,price from product;
六、高频易错点总结
- SQL 语句结尾必须加
;,否则执行失效 - 条件修改 / 删除必须加 where,否则操作全表
- 数据库
NULL≠ 空字符串'',空值判断只能用is null - 自增约束只能用在主键 / 唯一键整数字段
- 分组查询:
where过滤原始数据,having过滤分组后数据 - 聚合函数自动忽略
NULL值 - 拼写易错:
update、address、unique不要写错
七、全文总结
- DDL:库、表结构操作(增删改表)
- DML:表数据操作(增删改数据)
- 五大约束:规范数据,企业开发必用
- DQL:数据查询核心,日常开发使用最多
- 所有代码均可直接复制到 Navicat / MySQL 客户端运行,适合零基础快速上手。
更多推荐




所有评论(0)