数据的增删改
MySQL DML:INSERT 插入、批量插入、UPDATE 修改、DELETE 删除行、TRUNCATE 清空;WHERE 与安全习惯。
本页目录
表建好之后,下一步是往里放数据、改数据、删数据——这是 DML(Data Manipulation Language)。今天学 INSERT / UPDATE / DELETE,并区分 DELETE 与 TRUNCATE。
系列:MySQL 入门到查询 · 第 6 / 17 篇(第 6 天 · 4 月 26 日)
上一篇:修改表结构
下一篇:SELECT 查询入门
总目录:系列索引
环境说明
- 练习库:
train - 建议表结构(没有就先建):
create database if not exists train charset utf8mb4;
use train;
drop table if exists goods;
create table goods(
id int,
name varchar(30),
price double
);
- 预览数据可用(细讲在第 7 天):
select * from goods;
1. DML 是什么
| 类型 | 代表语句 | 作用 |
|---|---|---|
| DDL | CREATE / ALTER / DROP | 改表结构 |
| DML | INSERT / UPDATE / DELETE | 改表里的数据 |
| DQL | SELECT | 查数据 |
直觉:DDL 改「表格本身」;DML 改「表格里的字」。
2. INSERT:插入数据
2.1 给所有列赋值
insert into goods values(1, 'iPhone', 5999);
select * from goods;
| 顺序 | 必须与表中列的顺序一致 |
|---|---|
| 字符 / 日期 | 用英文引号:'iPhone' |
| 数字 | 直接写:5999 |
2.2 只给部分列赋值(推荐写法)
insert into goods(id, name) values(2, 'MacBook');
select * from goods;
没写的列会是 NULL(空值)或列的默认值(若有)。
insert into goods(id, name, price) values(3, 'AirPods', 999);
2.3 批量插入多行
insert into goods(id, name, price) values
(4, 'iPad', 3299),
(5, 'Watch', 2499);
select * from goods;
每行一对 ( ... ),中间用逗号,最后仍以 ; 结束。文末完整脚本里会一次插入更多行。
2.4 类型与引号对照
| 列类型 | 示例值 |
|---|---|
int | 1 |
double | 5999 或 5999.00 |
varchar | 'iPhone' |
datetime | '2026-04-26 10:00:00' |
date | '2026-04-26' |
2.5 中文数据
库和表编码为 utf8mb4 时通常可直接插入中文:
insert into goods(id, name, price) values(6, '华为手机', 4999);
select * from goods;
若出现乱码,回到第 3~5 天:检查 show create database train; 与客户端/终端编码。
3. UPDATE:修改数据
3.1 语法
update 表名 set 列名=新值 where 条件;
update 表名 set 列1=值1, 列2=值2 where 条件;
3.2 修改某一行
update goods set price=5499 where id=1;
select * from goods where id=1;
3.3 一次修改多列
update goods set name='MacBook Pro', price=8999 where id=2;
select * from goods where id=2;
3.4 忘写 WHERE 会怎样?(必懂)
-- 危险示例:下面这条会把所有行的价格都改掉
-- update goods set price=0;
| 写法 | 结果 |
|---|---|
有 where | 只改满足条件的行 |
无 where | 整表所有行都会被改 |
安全习惯:先写查询确认范围,再改:
select * from goods where id=3;
update goods set price=899 where id=3;
select * from goods where id=3;
条件里可用比较运算:= != > < >= <=(第 8 天细讲)。
4. DELETE:删除行
delete from 表名 where 条件;
delete from goods where id=6;
select * from goods;
同样:
-- 危险:删除整表每一行的数据
-- delete from goods;
| 语句 | 删的是什么 |
|---|---|
delete from goods where id=6 | 只删 id=6 的行 |
delete from goods | 删光所有行(表结构还在) |
drop table goods | 连表结构一起删(第 4 天讲过) |
易错:
delete后面要跟 from:delete from 表名。
5. TRUNCATE:清空表数据
truncate table goods;
-- 或
truncate goods;
select * from goods;
show tables;
| 语句 | 表结构 | 数据 |
|---|---|---|
truncate table 表 | 保留 | 全部清空 |
delete from 表(无 where) | 保留 | 全部删行 |
drop table 表 | 删除 | 数据也没了 |
入门记忆:
- 想「清空表、从头再练」→
truncate - 想「删几行」→
delete ... where - 想「表都不要了」→
drop
差异了解即可:
truncate通常更快、且往往不能按条件过滤;delete可带where。约束与事务更深的差异后面章节再提。
6. 四类操作对比速查
| 目标 | 语句 | 危险等级 |
|---|---|---|
| 插入一行 | insert into t(cols) values(...); | 低 |
| 批量插入 | insert into t(...) values(...),(...); | 低 |
| 改某行 | update t set c=v where ...; | 中(忘 where 则高) |
| 删某行 | delete from t where ...; | 中高(忘 where 则高) |
| 清空表 | truncate table t; | 高 |
| 删表 | drop table t; | 高 |
7. 完整跟练脚本
use train;
drop table if exists goods;
create table goods(
id int,
name varchar(30),
price double
);
-- 插入
insert into goods values(1, 'iPhone', 5999);
insert into goods(id, name) values(2, 'MacBook');
insert into goods(id, name, price) values(3, 'AirPods', 999);
insert into goods(id, name, price) values
(4, 'iPad', 3299),
(5, 'Watch', 2499),
(6, '华为手机', 4999);
select * from goods;
-- 修改
update goods set price=5499 where id=1;
update goods set name='MacBook Pro', price=8999 where id=2;
select * from goods;
-- 删除
delete from goods where id=6;
select * from goods;
-- 清空
truncate table goods;
select * from goods;
show tables;
期望
- 插入后能看到多行商品
- id=1 价格变为
5499 - id=6 被删掉
truncate后goods表还在,但数据为空
8. 实用技巧
8.1 重复执行脚本
insert 同一 id 多次,表里可能有多行相同 id(今天还没有主键约束)。练习时可:
delete from goods where id=3;
insert into goods(id, name, price) values(3, 'AirPods', 899);
或先 truncate 再整段插入。
8.2 批量插入来自另一张表(预告)
-- create table goods_bak select * from goods;
-- insert into goods_bak select * from goods where 1=1;
第 5 天已见复制表;这里只要知道数据也能「从表到表」。
8.3 插入 NULL
insert into goods(id, name) values(7, 'Keyboard');
-- price 为 NULL
select * from goods where id=7;
NULL 表示「没有值」,不是 0,也不是空字符串 ''(细节在查询篇)。
9. 速查表
| 目标 | 语句 |
|---|---|
| 全列插入 | insert into 表 values(值...); |
| 指定列插入 | insert into 表(列...) values(值...); |
| 批量插入 | insert into 表(...) values(...),(...); |
| 修改 | update 表 set 列=值 where 条件; |
| 修改多列 | update 表 set 列1=值1,列2=值2 where 条件; |
| 删除行 | delete from 表 where 条件; |
| 清空数据 | truncate table 表; |
| 看数据 | select * from 表;(第 7 天系统讲) |
10. 今天的练习清单
- 准备
train.goods表 - 单行
insert+ 指定列insert - 至少一次批量插入 3 行
-
update ... where id=...并用select验证 -
delete from goods where id=... -
truncate清空后show tables仍在 - 用注释体验「无 where 的 update/delete」不要执行,只读懂有多危险
常见错误
易错:
insert列数与values个数不一致。
易错:字符/日期没加引号;或误用了中文引号“”。
易错:update/delete忘写where,改/删了整表。
易错:delete goods(少了from)。
易错:把truncate当成只删一行。
易错:列顺序用insert into 表 values时与desc看到的列顺序不一致。
小结
- INSERT 写入数据:推荐写明列名;批量用多组
values。 - UPDATE / DELETE 必须想清楚
where;先select再改。 - TRUNCATE 清空数据但保留表;DROP 连表都没了。
- 查看结果用
select * from 表;——下一篇系统学习 SELECT。
系列导航:总目录 · 上一篇:修改表结构 · 下一篇:SELECT 查询入门