使用UPDATE语句可以对数据表中的现有数据进行更新
语法结构
UPDATE 表名 SET 列1=值1,列2=值2... [WHERE 条件] [ORDER BY...] [LIMIT row_count];
SET命令用来将新值赋给被更新的列
ORDER BY: 按照指定的顺序对行进行更新
LIMIT: 限制可更新的行数
基本的UPDATE语句主要由3部分组成,分别是:
1.要更新的表
2.列名和它们的新值
3.确定要更新行的过滤条件
在更新多个列时,只需要使用单个SET命令,每个“列=值”对之间用逗号分隔(最后一列之后不用逗号)
如果WHERE条件没有匹配到任何记录,UPDATE语句不会报错,也不会有任何记录被更新
练习案例
导入数据
DROP TABLE IF EXISTS `prod_info`;
CREATE TABLE `prod_info` (
`prod_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`prod_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`brand` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`class` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`cost` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`sale_price` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`supplier_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `prod_info` VALUES ('10001', '抽纸', '洁柔', '120抽*8', '日用品', '20.11', '28.8', 'NJ0001');
INSERT INTO `prod_info` VALUES ('10002', '抽纸', '维达', '120抽*8', '日用品', '18.02', '25.5', 'NJ0002');
INSERT INTO `prod_info` VALUES ('10003', '抽纸', '洁云', '120抽*8', '日用品', '19.11', '26.8', 'SH0001');
INSERT INTO `prod_info` VALUES ('10004', '洗衣液', '蓝月亮', '2.5kg', '日用品', '33.43', '43.8', 'SH0002');
INSERT INTO `prod_info` VALUES ('10005', '洗衣液', '奥妙', '2.5kg', '日用品', '35.82', '49.9', 'SZ0001');
prod_info表
注意:下述案例1-4的表数据都是使用上述的prod_info表中的数据
案例1: 将prod_info表中class字段中的'日用品'更改为'生活日用品'
UPDATE prod_info SET class='生活日用品';
结果展示:
如果执行UPDATE而不带WHERE子句,则表中每个行都将用新值更新
案例2: 将prod_id为'10001'该行记录中的class字段的'生活日用品'更换为'抽纸类日用品'
UPDATE prod_info
SET class='抽纸类日用品'
WHERE prod_id='10001';
结果展示:
案例3: 对商品打九折(赋的新值可以是计算表达式)
在UPDATE语句中,更新字段时可以使用表达式
UPDATE prod_info
SET sale_price = sale_price * 0.9;
结果展示:
未对商品打九折
对商品打九折
案例4: 将prod_id为'10002'和'10003'该2行记录中的class字段值设置为空
为了删除某个列的值,可设置它为NULL(假如表定义允许NULL值)
原数据
UPDATE prod_info
SET class=NULL
WHERE prod_id='10002' OR prod_id='10003';
结果展示:
其中NULL用来去除class列中的值
补充练习案例:将所有to_date为9999-01-01的全部更新为NULL
导入数据
drop table if exists titles_test;
CREATE TABLE titles_test (
id int(11) not null primary key,
emp_no int(11) NOT NULL,
title varchar(50) NOT NULL,
from_date date NOT NULL,
to_date date DEFAULT NULL);
insert into titles_test values
('1', '10001', 'Senior Engineer', '1986-06-26', '9999-01-01'),
('2', '10002', 'Staff', '1996-08-03', '9999-01-01'),
('3', '10003', 'Senior Engineer', '1995-12-03', '9999-01-01'),
('4', '10004', 'Senior Engineer', '1995-12-03', '9999-01-01'),
('5', '10001', 'Senior Engineer', '1986-06-26', '9999-01-01'),
('6', '10002', 'Staff', '1996-08-03', '9999-01-01'),
('7', '10003', 'Senior Engineer', '1995-12-03', '9999-01-01');
titles_test表
问题:将所有to_date为9999-01-01的全部更新为NULL,且 from_date更新为2001-01-01
UPDATE titles_test SET to_date = NULL, from_date = '2001-01-01'
WHERE to_date = '9999-01-01';
结果展示:
案例5
UPDATE语句中可以使用子查询,使得能用SELECT语句检索出的数据更新列数据
导入数据
DROP TABLE IF EXISTS `Customers`;
CREATE TABLE `Customers` (
`id` int(11) NOT NULL,
`customer` varchar(255) DEFAULT NULL,
`tutor` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `Customers` VALUES ('1', 'Odin', null);
INSERT INTO `Customers` VALUES ('2', 'Jack', 'Rita');
INSERT INTO `Customers` VALUES ('3', 'Harry', null);
INSERT INTO `Customers` VALUES ('4', 'Sun', 'Ted');
INSERT INTO `Customers` VALUES ('5', 'Summer', null);
DROP TABLE IF EXISTS `Tutors`;
CREATE TABLE `Tutors` (
`id` int(11) NOT NULL,
`tutor` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `Tutors` VALUES ('1', 'tutor1');
INSERT INTO `Tutors` VALUES ('2', 'tutor2');
INSERT INTO `Tutors` VALUES ('3', 'tutor3');
INSERT INTO `Tutors` VALUES ('4', 'tutor4');
Customers表(客户信息表)
customer: 客户名 tutor:助教名
在Customers表中,有些客户没有助教(tutor字段取值为Null)
Tutors表(助教信息表)
问题:从Tutors表中随机选取一名助教,为那些没有助教的客户提供帮助
UPDATE Customers
SET tutor = (SELECT tutor
FROM Tutors
ORDER BY RAND()
LIMIT 1)
WHERE tutor IS NULL;
结果展示:Customer表
UPDATE JOIN语句 跨表更新
UPDATE 表1
JOIN 表2
ON 表1.列名 = 表2.列名
SET 列名 = 表达式 [, 列名 = 表达式 ...]
[WHERE 条件];
说明:
1.利用表2中的数据更新表1,要求两个表必须存在关联字段
2.表1和表2关联的条件为表1.列名 = 表2.列名
案例1
导入数据
DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
`emp_id` int(11) NOT NULL,
`emp_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`gender` enum('男','女') CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT '男',
`salary` float(255, 2) DEFAULT NULL,
`phone` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`dept_id` int(255) NOT NULL,
`dept_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
PRIMARY KEY (`emp_id`, `dept_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `emp` VALUES (1, 'Jack', '男', 4725.00, '15937321555', 11, NULL);
INSERT INTO `emp` VALUES (1101, 'Mark', '男', 7455.00, '15903732155', 11, NULL);
INSERT INTO `emp` VALUES (1102, 'Rose', '女', 6510.00, '15937321666', 12, NULL);
INSERT INTO `emp` VALUES (1103, 'Jerry', '男', 3885.00, '15537312999', 12, NULL);
DROP TABLE IF EXISTS `dept`;
CREATE TABLE `dept` (
`dept_id` int(11) NOT NULL,
`dept_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
PRIMARY KEY (`dept_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `dept` VALUES (11, '人事部');
INSERT INTO `dept` VALUES (12, '财务部');
emp表
dept表
UPDATE emp
JOIN dept
ON emp.dept_id = dept.dept_id
SET emp.dept_name = dept.dept_name
WHERE emp.dept_id = 11;
结果展示: emp表
案例2
有两个表 stu和 certificate,使用stu表的s_name 和phone列的数据更新certificate 表
导入数据
DROP TABLE IF EXISTS `stu`;
CREATE TABLE `stu` (
`s_no` int(11) NOT NULL,
`s_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`birth` timestamp(0) DEFAULT NULL,
`phone` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`addr` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
PRIMARY KEY (`s_no`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `stu` VALUES (1, 'Jack', '1999-01-23 00:00:00', '13703735566', 'Beijing');
INSERT INTO `stu` VALUES (2, 'Mark', '1999-10-03 00:00:00', '13783735566', 'Beijing');
INSERT INTO `stu` VALUES (3, 'Rose', '2000-11-21 00:00:00', '13783735522', 'Shanghai');
INSERT INTO `stu` VALUES (4, 'John', '2000-03-04 00:00:00', '18503735214', 'Guangzhou');
INSERT INTO `stu` VALUES (5, 'Jerry', '2001-04-25 00:00:00', '13303735266', 'Guangzhou');
DROP TABLE IF EXISTS `certificate`;
CREATE TABLE `certificate` (
`s_no` int(11) NOT NULL,
`s_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`phone` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`certificate` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
PRIMARY KEY (`s_no`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `certificate` VALUES (1, NULL, NULL, '英语四级');
INSERT INTO `certificate` VALUES (2, NULL, NULL, '英语四级');
INSERT INTO `certificate` VALUES (3, NULL, NULL, '英语六级');
INSERT INTO `certificate` VALUES (4, NULL, NULL, '计算机二级');
INSERT INTO `certificate` VALUES (5, NULL, NULL, '英语四级');
stu表
certificate表
UPDATE certificate AS c
JOIN stu AS s
ON c.s_no = s.s_no
SET c.s_name = s.s_name, c.phone = s.phone;
结果展示: certificate表
更新的指导原则
1.如果执行UPDATE而不带WHERE子句,则表中每个行都将用新值更新,除非确实打算更新每一行,否则绝对不要使用不带WHERE子句的UPDATE语句
2.在执行UPDATE语句时要非常小心,最好先用SELECT语句来测试WHERE条件是否筛选出了期望的记录集,然后再用UPDATE更新 (MySQL没有撤销(undo)按钮,应该非常小心地使用UPDATE,否则你会发现自己更新了错误的数据)