更新记录
更新记录通过UPDATE语句实现,其(单表更新)语法为:
UPDATE TABLE SET col_name={expr|DEFAULT} [ ,… ] [ WHERE WHERE_cONditiON ]
说明:
UPDATE语句更新记录,并且返回被更新的行数(记录数);
SET子句指明被更新的列及其列值,列值可以是表达式或者默认值(用DEFAULT表示);
对于声明为NOT NULL的列在尝试更新为NULL时,在严格的SQL模式下将产生错误;
如果省略WHERE子句,则更新全部记录;
CREATE TABLE employee(
id SMALLINT UNSIGNED KEY AUTO_INCREMENT,
employee_name VARCHAR(20) NOT NULL,
bASe_pay DECIMAL(7,2) UNSIGNED NOT NULL DEFAULT 0,
bONus DECIMAL(7,2) UNSIGNED NOT NULL DEFAULT 0,
taxes DECIMAL(7,2) UNSIGNED NOT NULL DEFAULT 0,
deserve DECIMAL(7,2) UNSIGNED NOT NULL DEFAULT 0
);
INSERT employee(employee_name,bASe_pay,bONus,taxes) VALUES('王志强',36200,5000,200.85);
INSERT employee(employee_name,bASe_pay,bONus,taxes) VALUES('张宇宁',32000,4000,160.01);
INSERT employee(employee_name,bASe_pay,bONus,taxes) VALUES('刘昌涛',29000,7800,310.50);
INSERT employee(employee_name,bASe_pay,bONus,taxes) VALUES('纪盈新',41009.35,600,315.36);
INSERT employee(employee_name,bASe_pay,bONus,taxes) VALUES('肖莉娟',50000,5000,795.33);
UPDATE employee SET deserve = bASe_pay + bONus - taxes;