it编程 > 数据库 > Mysql

一文搞懂 MySQL CRUD:增删改查语法详解与实战

73人参与 • 2026-09-29 • Mysql

1. 引言

增删改查(crud)是数据库操作中最基础、最核心的四个动作,分别对应 sql 中的 insert(新增)、select(查询)、update(修改)和 delete(删除)。本文将从语法结构、常用写法、注意事项和实战示例四个维度,系统讲解 mysql 中增删改查的完整用法。

2. 准备工作

在开始讲解之前,先创建一个示例表,后续所有示例都基于这张表展开。

create table student (
    id int primary key auto_increment comment '主键',
    name varchar(50) not null comment '姓名',
    age int comment '年龄',
    class_name varchar(50) comment '班级',
    score decimal(5,2) comment '成绩',
    created_at datetime default current_timestamp comment '创建时间'
) engine=innodb default charset=utf8mb4;

3. 新增数据(insert)

3.1 基本语法

insert into 表名 (列1, 列2, ...) values (值1, 值2, ...);

3.2 常用写法

写法一:指定列插入,只给部分列赋值,未指定的列使用默认值。

insert into student (name, age, class_name, score)
values ('张三', 18, '高三(1)班', 92.5);

写法二:省略列名,此时必须按表结构顺序给所有列赋值。

insert into student values (null, '李四', 19, '高三(2)班', 88.0, now());

写法三:一次插入多行,用逗号分隔多组值,减少 sql 执行次数。

insert into student (name, age, class_name, score) values
('王五', 17, '高三(3)班', 95.0),
('赵六', 18, '高三(1)班', 87.5),
('孙七', 19, '高三(2)班', 90.0);

写法四:insert ... set,可读性更强,适合字段较少的场景。

insert into student set name = '周八', age = 18, class_name = '高三(4)班', score = 89.0;

3.3 注意事项

4. 查询数据(select)

4.1 基本语法

select 列1, 列2, ... from 表名 [where 条件] [order by 排序] [limit 限制];

4.2 常用写法

查询全部列,使用星号通配符。

select * from student;

查询指定列,只返回需要的字段,减少数据传输量。

select name, age, score from student;

带条件查询,使用 where 过滤记录。

select name, score from student where score >= 90;

多条件组合,使用 and、or、not 连接多个条件。

select * from student
where class_name = '高三(1)班' and age >= 18;

模糊查询,使用 like 配合通配符 %(任意多个字符)和 _(单个字符)。

select * from student where name like '张%';

范围查询,使用 between ... and 或 in。

select * from student where score between 85 and 95;
select * from student where class_name in ('高三(1)班', '高三(2)班');

排序,使用 order by 指定排序字段和方向。

select * from student order by score desc;  -- 成绩从高到低
select * from student order by class_name asc, score desc;  -- 多字段排序

分页查询,使用 limit 限制返回行数,offset 表示跳过多少行。

select * from student limit 5;        -- 返回前 5 行
select * from student limit 5, 10;     -- 跳过 5 行,返回 10 行(第 6 到第 15 行)

去重查询,使用 distinct 去除重复值。

select distinct class_name from student;

聚合查询,配合 count、sum、avg、max、min 使用。

select count(*) as total_count from student;
select avg(score) as avg_score, max(score) as max_score from student;

分组查询,使用 group by 按字段分组,常与聚合函数配合。

select class_name, count(*) as cnt, avg(score) as avg_score
from student
group by class_name
having avg(score) >= 88;

4.3 注意事项

5. 修改数据(update)

5.1 基本语法

update 表名 set 列1 = 值1, 列2 = 值2, ... [where 条件];

5.2 常用写法

修改单条记录,通过主键精确定位。

update student set score = 96.0 where id = 1;

批量修改,通过条件匹配多条记录。

update student set class_name = '高三(1)班' where age = 18;

同时修改多个字段,用逗号分隔多个赋值。

update student set age = 19, score = 91.0 where name = '张三';

基于原值更新,在原有值基础上做运算。

update student set score = score + 2 where class_name = '高三(3)班';

5.3 注意事项

6. 删除数据(delete)

6.1 基本语法

delete from 表名 [where 条件];

6.2 常用写法

删除单条记录,按主键删除。

delete from student where id = 1;

按条件批量删除。

delete from student where score < 60;

清空整张表,删除所有记录。

delete from student;

使用 truncate 清空表,速度更快,但无法回滚,且会重置自增主键。

truncate table student;

6.3 delete 与 truncate 的区别

对比项deletetruncate
是否可加 where可以,按条件删除不可以,只能全表清空
是否可回滚在事务内可回滚不可回滚
执行速度较慢,逐行删除很快,直接释放数据页
自增主键保留当前计数重置为初始值
触发器会触发 delete 触发器不会触发

6.4 注意事项

7. 综合实战示例

下面用一个完整的业务场景串联四个操作:新增学生、查询成绩、修改分数、删除不合格记录。

-- 1. 新增三名学生
insert into student (name, age, class_name, score) values
('张三', 18, '高三(1)班', 92.5),
('李四', 19, '高三(2)班', 88.0),
('王五', 17, '高三(3)班', 95.0);

-- 2. 查询所有成绩大于等于 90 的学生
select name, class_name, score from student where score >= 90;

-- 3. 将张三的成绩加 2 分
update student set score = score + 2 where name = '张三';

-- 4. 删除成绩低于 60 的学生
delete from student where score < 60;

-- 5. 查看最终结果
select * from student order by score desc;

8. 总结

mysql 的增删改查是数据库操作的基石,掌握好这四类语句的语法和细节,是后续学习复杂查询、事务、索引优化和存储过程的前提。日常开发中要特别注意:insert 注意字段约束,select 注意查询效率,update 和 delete 务必带 where 条件,并在关键操作前做好数据备份。

到此这篇关于一文搞懂 mysql crud:增删改查语法详解与实战的文章就介绍到这了,更多相关mysql crud增删改查内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

赞 (0)

您想发表意见!!点此发布评论

推荐阅读

从零开始学 MySQL SQL:DDL、DML、DQL 一本通

09-29

MySQL数据库事务的ACID之隔离性实验

09-29

SQL里的“套娃”与“拼图”之MySQL子查询和合并查询

09-29

mysql数据库中null是什么意思详解

09-29

MySQL面试核心知识点总结与原理详细解析

09-29

MySQL表约束的实现

09-29

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论