Mysql表的增删改查
🏝️专栏:Mysql_猫咪-9527的博客-CSDN博客
🌅主页:猫咪-9527-CSDN博客“欲穷千里目,更上一层楼。会当凌绝顶,一览众山小。”
CRUD | |||
create | retrieve | update | delete |
1. 创建(create)
create
语句用于向数据库中插入新数据。理解如何高效地插入数据是数据库操作的基本技能。
语法:
这是MySQL中 INSERT
语句的基本语法,用于向数据库表中插入数据。它的结构如下:
基本语法
INSERT [INTO] table_name
[(column1, column2, ...)]
VALUES (value1, value2, ...) [, (value1, value2, ...)] ...
各部分解释:
INSERT INTO:表示插入数据到指定表。
table_name:指定要插入数据的表名。
(column1, column2, ...):可选部分,列出要插入数据的列名。如果省略列名,默认插入表中的所有列。
VALUES:后面跟随插入的值。每一组值对应表中的列。
value_list:插入的值,可以是单个值,也可以是多个值,多个值之间用逗号隔开。
小知识:
插入大量数据时,使用批量插入可以提高效率,而不是一次插入一条记录。
如果插入数据时,某些字段没有提供值,它们会使用默认值(例如
null
或字段定义时的默认值)。其中insert into 可以写为insert,into可以省略。
示例:
-
全列插入
全列插入:所要插入的列可以省略不写,但是必须插入表的全部列。
指定列插入:可以选择要插入列
-
省略into插入
下面两条语句达到的效果一样:
-
插入单条记录
insert into students values (8, 130,'李明','123');
-
插入多条记录
insert into students values
(9, 131,'刘亮','166'),
(10,132,'王强','124');
-
处理重复键
使用on duplicate key update
来更新已有记录。
insert into students
values (200, 20010, '张伟','111111')
on duplicate key update name = 'zhangwe';
未加(修改失败) | |
![]() | |
添加(修改成功) | |
![]() | |
修改前 | 修改后 |
![]() | ![]() |
注:
1 rows affected(1行数据收到影响):当前表中无数据冲突,直接插入 |
![]() |
0 rows affected(0行数据收到影响):当前表中有数据冲突,但是插入值与原有值相同 |
![]() |
2rows affected(2行数据收到影响):当先列表数据冲突插入并修改 |
![]() |
-
replace into
当主键或唯一键冲突时,删除旧记录并插入新记录。
replace into employees (emp_id, emp_name) values (30001, '陈晨');
数据冲突,直接修改成功(覆盖式插入,如果数据冲突,原数据会被删除,重新插入) | |
![]() | |
插入前 | 插入后 |
![]() | ![]() |
2. 读取(retrieve)
select
语句用于查询数据库中的数据。可以通过过滤条件、排序和分页等操作,精确获取所需数据。
基本语法:
SELECT
[DISTINCT] {* | {column [, column] ...}
[FROM table_name]
[WHERE ...]
[ORDER BY column [ASC | DESC], ...]
LIMIT ...
各部分解释:
SELECT
:用于选择查询的数据。
DISTINCT
(可选):如果指定了DISTINCT
,则只返回不同的(唯一的)结果。
*
:表示选择所有列。
column [, column] ...
:表示选择一个或多个特定列。
FROM table_name
:指定从哪个表中获取数据。
WHERE ...
(可选):可以指定查询的条件,筛选符合条件的记录。
ORDER BY column [ASC | DESC]
(可选):对结果进行排序,ASC
是升序,DESC
是降序。
LIMIT ...
(可选):限制返回的记录数。
小知识:
distinct
关键字用于去重,查询结果中只返回唯一的记录。使用
join
可以将多个表的数据结合起来,特别适用于处理关系型数据。
示例:
-- 创建表结构
create table exam_result (id int unsigned primary key auto_increment,name varchar(20) not null comment '同学姓名',chinese float default 0.0 comment '语文成绩',math float default 0.0 comment '数学成绩',english float default 0.0 comment '英语成绩'
);-- 插入测试数据
insert into exam_result (name, chinese, math, english) values
('唐三藏', 67, 98, 56),
('孙悟空', 87, 78, 77),
('猪悟能', 88, 98, 90),
('曹孟德', 82, 84, 67),
('刘玄德', 55, 85, 45),
('孙权', 70, 73, 78),
('宋公明', 75, 65, 30);
-
查询所有列
select * from exam_result;
-
查询特定列
select id,name from exam_result;
-
重命名查询
select id 学号 ,name 姓名 ,math+chinese+english 总分 from exam_result;
-
表达式查询
| |
![]() | ![]() |
| |
![]() | ![]() |
-
条件查询
select emp_name, salary from employee_details where salary < 5000;
比较运算符:
逻辑比较符:
-
排序查询
select emp_name, department from employee_details order by department desc;
-
分页查询
select emp_id, emp_name from employee_details limit 5 offset 0;
-
聚合查询
select count(*) from employees;
3. 更新(update)
update
语句用于修改数据库中已存在的数据。
小知识:
-
更新时最好通过
where
条件进行精确匹配,避免误操作影响到所有记录。 -
使用
order by
和limit
可以控制更新的记录数,避免批量更新时产生的风险。
示例:
-
更新单列:
update employee_details set salary = 6000 where emp_name = '李明';
-
更新多列:
update employee_details set salary = 5500, department = '销售' where emp_name = '王丽';
-
基于表达式更新:
update employee_details set salary = salary + 500 where department = '技术';
4. 删除(delete)
delete
语句用于删除数据库中的记录。
小知识:
-
使用
where
条件时要特别小心,避免误删除整个表的数据。 -
truncate
语句比delete
语句更高效,且不生成日志,但它无法回滚且会重置自增列。
示例:
-
删除单条记录:
delete from employee_details where emp_name = '李明';
-
删除整表数据:
delete from temp_table;
-
删除所有记录并重置自增列:
truncate table temp_table;
简单测试:
查询英语成绩不及格的同学(<60)
select name 姓名 ,english 英语 from exam_result where english<60;
语文成绩在[80,90]分的同学
方案一:
select name 姓名, chinese 语文 from exam_result where chinese>=80 AND chinese<=90;
方案二:
select name 姓名, chinese 语文 from exam_result where chinese between 80 and 90;
数学成绩是58或者59或者98或者99的同学及数学成绩
方案一:
select name 名字,math 数学 from exam_result where math =99 or math =98
or math =59 or math =58;
方案二:
select name 姓名, math 数学 from exam_result where math in (98,99,59,58);