MySQL

数据的增删改

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 是什么

类型代表语句作用
DDLCREATE / ALTER / DROP改表结构
DMLINSERT / UPDATE / DELETE改表里的数据
DQLSELECT查数据

直觉: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 类型与引号对照

列类型示例值
int1
double5999 或 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 看到的列顺序不一致。

小结

  1. INSERT 写入数据:推荐写明列名;批量用多组 values。
  2. UPDATE / DELETE 必须想清楚 where;先 select 再改。
  3. TRUNCATE 清空数据但保留表;DROP 连表都没了。
  4. 查看结果用 select * from 表;——下一篇系统学习 SELECT。

系列导航:总目录 · 上一篇:修改表结构 · 下一篇:SELECT 查询入门

相关阅读

全部文章 →
← 返回列表更多「MySQL」