作者:IvanCodes
日期:2025年6月6日
核心目标: 学习如何在表中插入、更新和删除数据行。
前提: 操作前需使用 use database_name;
选择目标数据库。以下示例假设已选定数据库。
示例表结构 (供参考):
create table students (
student_id int,
first_name varchar(50),
last_name varchar(50),
major varchar(50),
gpa decimal(4, 2)
);
create table products (
product_id int,
product_name varchar(100),
price decimal(10, 2),
stock_quantity int
);
AI 代码解读
一、插入数据 (insert
)
作用: 向表中添加新的数据行。
指定列插入 (推荐):
明确指定要插入数据的列名和对应的值。列与值的顺序必须一致。
insert into table_name (column1, column2, ...)
values (value1, value2, ...);
AI 代码解读
示例:
insert into students (student_id, first_name, last_name, major, gpa)
values (101, '张', '三', '计算机科学', 3.50);
insert into products (product_id, product_name, price)
values (501, '笔记本电脑', 5999.99); -- stock_quantity 会是 NULL 或默认值
AI 代码解读
- 按表结构顺序插入所有列 (不推荐):
不指定列名,按表定义顺序提供所有列的值。
insert into table_name values (value_for_col1, value_for_col2, ...);
AI 代码解读
示例: (假设 students 列顺序为 id, first, last, major, gpa)
insert into students values (102, '李', '四', '物理学', 3.20);
AI 代码解读
- 一次插入多行 (效率更高):
在values
后提供多个用逗号分隔的值元组。
insert into table_name (column1, column2, ...)
values
(value1a, value2a, ...),
(value1b, value2b, ...),
(value1c, value2c, ...);
AI 代码解读
示例:
insert into products (product_id, product_name, price, stock_quantity)
values
(502, '无线鼠标', 129.00, 150),
(503, '机械键盘', 499.50, 80),
(504, '显示器', 1200.00, 50);
AI 代码解读
二、更新数据 (update
)
作用: 修改表中已存在的数据行。
语法:
update table_name
set column1 = new_value1,
column2 = new_value2,
...
[where condition;] -- !!! 极其重要:指定要更新哪些行 !!!
AI 代码解读
!!! 警告:如果省略
where
子句,将更新表中的所有
行!务必万分小心 !!!示例:
-- 更新学号为 101 的学生的专业
update students
set major = '软件工程'
where student_id = 101;
-- 将所有计算机科学专业学生的 GPA 增加 0.1
update students
set gpa = gpa + 0.1
where major = '计算机科学';
-- 更新产品 ID 为 502 的价格和库存
update products
set price = 135.00,
stock_quantity = stock_quantity - 10
where product_id = 502;
-- !!! 危险示例:将所有产品价格设为 0 (因为没有 WHERE) !!!
-- update products set price = 0.00; -- 极度危险操作!
AI 代码解读
三、删除数据 (delete
)
作用: 从表中删除数据行。
语法:
delete from table_name [where condition]; -- !!! 极其重要:指定要删除哪些行 !!!
AI 代码解读
!!! 警告:如果省略
where
子句,将删除表中的所有
行!务必万分小心 !!!示例:
-- 删除学号为 102 的学生记录
delete from students where student_id = 102;
-- 删除所有 GPA 低于 2.0 的学生记录
delete from students where gpa < 2.0;
-- 删除库存为 0 的产品记录
delete from products where stock_quantity = 0;
-- !!! 危险示例:删除 students 表中的所有记录 (因为没有 WHERE) !!!
-- delete from students; -- 极度危险操作!
AI 代码解读
四、清空表数据 (truncate table
)
作用: 快速删除表中的 所有 行。通常比无
where
条件的delete
更快。语法:
truncate table table_name;
AI 代码解读
特点:
- 速度快。
- 通常会重置自增列的计数器。
- 操作可能无法回滚(属于 DDL 特性)。
- 不触发
delete
触发器。
示例:
-- 快速清空临时日志表
truncate table temp_logs;
AI 代码解读
练习题
假设你已创建 students
和 products
表,并可能已插入部分数据。
初始数据参考 (仅供理解题目背景):
students
表 (可能包含):
student_id | first_name | last_name | major | gpa |
---|---|---|---|---|
101 | 张 | 三 | 计算机科学 | 3.50 |
103 | 王 | 五 | 计算机科学 | 3.80 |
104 | 赵 | 六 | 化学 | 2.90 |
products
表 (可能包含):
product_id | product_name | price | stock_quantity |
---|---|---|---|
501 | 笔记本电脑 | 5999.99 | 20 |
502 | 无线鼠标 | 129.00 | 150 |
503 | 机械键盘 | 499.50 | 80 |
505 | U盘 64GB | 65.00 | 200 |
请编写 SQL 语句完成以下 DML 操作:
- 向
students
表插入一条新记录:学号 107,姓名 '钱 七',专业 '金融学',GPA 3.75。
insert into students (student_id, first_name, last_name, major, gpa) values (107, '钱', '七', '金融学', 3.75);
AI 代码解读
向
products
表一次性插入两条记录:- 产品 ID 506, 名称 '蓝牙耳机', 价格 299.00, 库存 120
- 产品 ID 507, 名称 '充电宝', 价格 150.00, 库存 300
insert into products (product_id, product_name, price, stock_quantity) values
(506, '蓝牙耳机', 299.00, 120),
(507, '充电宝', 150.00, 300);
AI 代码解读
- 将
students
表中学号为 103 的学生的 GPA 更新为 3.90。
update students set gpa = 3.90 where student_id = 103;
AI 代码解读
- 将
products
表中所有价格低于 200 元的产品的库存量增加 50。
update products set stock_quantity = stock_quantity + 50 where price < 200.00;
AI 代码解读
- 将
products
表中产品 ID 为 503 '机械键盘' 的价格降低 50 元,同时名称修改为 '游戏机械键盘'。
update products set price = price - 50, product_name = '游戏机械键盘' where product_id = 503;
AI 代码解读
- 删除
students
表中学号为 104 的学生记录。
delete from students where student_id = 104;
AI 代码解读
- 删除
products
表中库存量少于 100 的所有产品记录。
delete from products where stock_quantity < 100;
AI 代码解读
- (如果需要练习清空) 使用
truncate
命令清空students
表。
-- truncate table students;
AI 代码解读