当前位置: 代码网 > it编程>数据库>Mysql > 一文搞懂 MySQL CRUD:增删改查语法详解与实战

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

2026年09月29日 • Mysql •我要评论
1. 引言增删改查(crud)是数据库操作中最基础、最核心的四个动作,分别对应 sql 中的 insert(新增)、select(查询)、update(修改)和 delete(删除)。本文将从语法结构

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 注意事项

  • 字符串和日期类型必须加单引号,数字类型不需要。
  • 自增主键(auto_increment)可以传 null 或省略,由数据库自动生成。
  • 插入的数据必须满足字段约束,如 not null 字段不能为空。
  • 使用 insert ignore 可忽略因主键或唯一索引冲突导致的错误。
  • 使用 on duplicate key update 可在冲突时改为更新操作。

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 注意事项

  • where 用于分组前过滤,having 用于分组后过滤。
  • like 模糊查询以 % 开头时无法利用索引,数据量大时性能较差。
  • order by 默认升序(asc),降序需显式指定 desc。
  • limit 的 offset 从 0 开始计数。

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 注意事项

  • 务必带 where 条件,否则会更新表中所有记录,这是最常见的误操作。
  • update 语句执行后,可通过 row_count() 获取受影响的行数。
  • 更新操作会触发数据库的行锁,大批量更新时注意锁等待和事务控制。
  • 建议在事务中执行 update,配合 commit 和 rollback 保证数据安全。

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 注意事项

  • 务必带 where 条件,否则会清空整张表。
  • 删除操作不可逆,建议先 select 确认要删除的数据范围。
  • 生产环境删除数据前,建议先备份或使用软删除(增加 is_deleted 字段)。

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)

相关文章:

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

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com