第七节:DML操作之插入、更新、删除

一. 插入

1. 全字段插入

(1). 标准格式

insert into tableName (c1,c2,c3 ...) values (x1,x2,x3,...)

可以省略:

insert into tableName values (x1,x2,x3,...)

(2). 插入多条数据 

insert into LoginRecords values('0003','1','中桥','xx.xx.xx.x','2020-03-26'),
('0004','1','中桥','xx.xx.xx.x','2020-03-26'),
('0005','1','中桥','xx.xx.xx.x','2020-03-26'),
('0006','1','中桥','xx.xx.xx.x','2020-03-26');

PS:全字段插入的时候可以省略字段名称。

2. 部分字段插入

 (1). 标准格式

insert into tableName (c2,c3 ...) values (x2,x3,...)

PS:不能省略字段名称。

(2). 插入多条数据

insert into LoginRecords (id,userId) values('0007','1'),
('0008','1'),
('0009','1'),
('00010','1')

3. 插入查询结果

insert into table1 (x1,x2)
select c1,c2 from table2 where condition

注:没有values关键字哦

 

二. 删除

1. delete关键字

delete from tableName where condition

2. truncate关键字

truncate tableName

PS:truncate是删除全表,和delete删除全表而言,如果是自增主键,truncate 后从1开始,delete后还要接着原先的自增。

 

 

 

 

 

三. 更新

1. 单表更新

update tableName set x1=xx [, x2=xx] where condition

2. 关联表更新

(1). SQLServer

update table1
set table1.x1='abc'
from table1
join table2 on table1.id=table2.uId  where condition

(2). MySQL

update table1,table2
set table1.x1='abc'
where table1.id=table2.uId

eg: 可以同时更新多张关联表的数据

update T_SysUser u,T_SysLoginLog l
set u.userPhone ='111111',l.userName='ypf'
where u.id=l.userId and u.id='1'

3. 案例

 

 

 

 

 

!

  • 作       者 : Yaopengfei(姚鹏飞)
  • 博客地址 : http://www.cnblogs.com/yaopengfei/
  • 声     明1 : 如有错误,欢迎讨论,请勿谩骂^_^。
  • 声     明2 : 原创博客请在转载时保留原文链接或在文章开头加上本人博客地址,否则保留追究法律责任的权利。
 
posted @ 2017-07-16 23:41  Yaopengfei  阅读(364)  评论(0编辑  收藏  举报