MySQL连表更新和删除的差别

1、连表更新和删除

可以使用其他表(或一个查询视图)更新、删除本表,单要注意表连接更新和表连接删除时使用方式的差距

[sql]  view plain copy
  1. CREATE TABLE `test1` (  
  2.   `id` int(11) NOT NULL,  
  3.   `namevarchar(16) DEFAULT NULL,  
  4.   PRIMARY KEY (`id`)  
  5. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  
  6.   
  7. create table test2 like test1;  
  8.   
  9.   
  10. -- 根据test2更新test1的name列,条件是id相同  
  11. -- 表连接  
  12. update test1 a inner JOIN test2 b on a.id=b.id set a.name=b.name ;  
  13.   
  14. update test1 a , test2 b set a.name=b.name where a.id=b.id ;  
  15.   
  16.   
  17. --  删除test1表中id和test2表中id重复的  
  18. --  表连接  
  19. delete a from test1 a inner JOIN test2 b on a.id=b.id ;  
  20. --  子查询  
  21. delete from test1 where exists (select 1 from test2 where test1.id=test2.id);  
  22. delete a from test1 a where exists (select 1 from test2 where test1.id=test2.id); 
注意子查询删除时,可以用别名或者不用别名;但是join删除时,必须用别名。

貌似mysql需要这样,oracle不需要这样。


sqlserver的update和delete都要用别名    

update a  set a.name=b.name from tb1 a inner join tb2 b on a.id=b.id 。。。;



2、like提速

cola like 'mysql%'  为了提高速度,可以改成   cola>='mysql'  and cola<mysqm

修改表的自增字段值   alter table tb auto_increment=200;


3、null

mysql中   ''  is not null   注意这种空字符串和null的区别。

假如使用文本直接导入数据库的时候需要注意下边:

insert中的空值用null  文本中的空值用\N

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值