mysql详解3:插入 删除 更新
插入
insert into 表名(字段名1,字段名2,字段名3,...) values(字段值1,字段值2,字段值3,...)
不指定字段名 需要按顺序指定所有字段值
auto_increment 递增列直接赋值为DEFAULT
插入多行
INSERT into shippers(name) values ("shippers1"),("shippers2"),(shippers3)
插入分层行
LAST_INSERT_ID() 最新插入的id
创建表复制
CREATE TABLE order_archived as
select * from orders
会忽略主键
将一张表的一些记录复制到另一张表
INSERT into order_archived
select * from orders
where order_date <'2019-01-01'
更新
update invoices set payment_total =10 where id =3
更新多行
update invoices set payment_total =invoice_total *0.5,payment_date=due_date
where client_id in (3,4)
在update 中使用子查询
UPDATE customers
SET points = points + 50
WHERE client_id = (
SELECT client_id
FROM clients
WHERE NAME = "Myworks")
删除行
delete from student where id =2;