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

 
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 





原文地址:https://www.cnblogs.com/georgelei/p/74660033366930d787aecca9cd59aa8b.html