您的位置:首页 > 其它

4.表数据的操作-insert、delete

2015-09-09 21:25 459 查看
1.插入数据
insert...values:插入一行数据



insert...set:可以指定插入行中某些列的值,注意非空字段必须赋值



insert...select:插入其他表数据,注意主键不能重复



replace:针对primary key或者unique key有重复的情况,insert无法插入,3中语法同insert
包含:replace...values/replace...set/replace...select

注意:在插入前将表中冲突的记录删除,再插入新记录



2.删除数据
delete:一行行删除

delete table1, table2 from table1,table2 where table1.id=table2.id
或者:delete from table1, table2 using table1,table2 where table1.id=table2.id
示例:删除表1中在表2里不存在的记录:
DELETE t1 FROM table1 t1 LEFT JOIN table2 t2 ON t1.id=t2,id WHERE t2.id IS NULL
truncate:速度比delete快,先删除表再建立新表

truncate tablename

3.修改数据 --update
修改单个表:
update tablename set tname='' where ...

修改多个表:

update table1,table2 set t1='...',t2='...' where table1.id=table2.id

来自为知笔记(Wiz)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: