1、连表更新和删除
可以使用其他表(或一个查询视图)更新、删除本表,单要注意表连接更新和表连接删除时使用方式的差距
- CREATE TABLE `test1` (
- `id` int(11) NOT NULL,
- `name` varchar(16) DEFAULT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- create table test2 like test1;
- -- 根据test2更新test1的name列,条件是id相同
- -- 表连接
- update test1 a inner JOIN test2 b on a.id=b.id set a.name=b.name ;
- update test1 a , test2 b set a.name=b.name where a.id=b.id ;
- -- 删除test1表中id和test2表中id重复的
- -- 表连接
- delete a from test1 a inner JOIN test2 b on a.id=b.id ;
- -- 子查询
- delete from test1 where exists (select 1 from test2 where test1.id=test2.id);
- delete a from test1 a where exists (select 1 from test2 where test1.id=test2.id);
貌似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