mysql优化之-增删改具体分析(图)
插入
将多行查询结果插入到表中
语法
insert into table_name1(column_list1) select (column_list2) from table_name2 where (condition)
table_name1指定待插入数据的表;column_list1指定待插入表中要插入数据的哪些列;table_name2指定插入数据是从
哪个表中查询出来的;column_list2指定数据来源表的查询列,该列表必须和column_list1列表中的字段个数相同,数据类型相同;
condition指定select语句的查询条件
从person_old表中查询所有的记录,并将其插入到person表
create table person (
id int unsigned not null auto_increment,
name char(40) not null default '',
age int not null default 0,
info char(50) null,
primary key (id)
)
create table person_old (
id int unsigned not null auto_increment,
name char(40) not null default '',
age int not null default 0,
info char(50) null,
primary key (id)
)
insert into person_old
values (11,'harry',20,'student'),(12,'beckham',31,'police')
select * from person_old
可以看到,插入记录成功,person_old表现在有两条记录。接下来将person_oldperson_old表中的所有记录插入到person表
insert into person(id,name,age,info)
select id,name,age,info from person_old;
select * from person
可以看到数据转移成功,这里的id字段为自增的主键,在插入时要保证该字段值的唯一性,如果不能确定,可以插入的时候忽略该字段,
只插入其他字段的值
如果再执行一次就会出错
mysql和sqlserver的区别:
区别一
当要导入的数据中有重复值的时候,mysql会有三种方案
方案一:使用 ignore 关键字
方案二:使用 replace into
方案三:on duplicate key update
第二和第三种方案这里不作介绍,因为比较复杂,而且不符合要求,这里只讲第一种方案
truncate table person
truncate table persona_old
insert into person_old
values (11,'harry',20,'student'),(12,'beckham',31,'police')
##注意下面这条insert语句是没有ignore关键字的
insert into person(id,name,age,info)
select id,name,age,info from person_old;
insert into person_old
values (13,'kay',26,'student')
##注意下面这条insert语句是有ignore关键字的
insert ignore into person(id,name,age,info)
select id,name,age,info from person_old;
可以看到插入成功
sqlserver
在sqlserver这边,如果要忽略重复键,需要在建表的时候指定 with (ignore_dup_key=on) on [primary]
这样在插入重复值的时候,sqlserver第一次会保留值,第二次发现有重复值的时候,sqlserver就会忽略掉
区别二
插入自增列时的区别
sqlserver需要使用 setidentity_insert 表名on 才能把自增字段的值插入到表中,如果不加 set identity_insert 表名 on
则在插入数据到表中时,不能指定自增字段的值,则id字段不能指定值,sqlserver会自动帮你自动增加一
insertinto person(name,age,info) values ('feicy',33,'student')
而mysql则不需要,而且自由度非常大
你可以将id字段的值指定为null,mysql会自动帮你增一
insertinto person(id,name,age,info) values (null,'feicy',33,'student')
也可以指定值
insert ignore into person(id,name,age,info) values (16,'tom',88,'student')
也可以不写id的值,mysql会自动帮你增一
insert ignore into person(name,age,info) values ('amy',12,'bb')
你可以指定id字段的值也可以不指定,指定的时候只要当前id字段列没有你正在插入的那个值就可以,即没有重复值就可以
自由度非常大,而且无须指定 set identity_insert 表名 on 选项
区别三
唯一索引的null值重复问题
mysql
在mysql中unique 索引将会对null字段失效
insert into test(a) values(null)
insert into test(a) values(null)
上面的插入语句是可以重复插入的(联合唯一索引也一样)
sqlserver
sqlserver则不行
create table person (
id int not null identity(1,1),
name char(40) null default '',
age int not null default 0,
info char(50) null,
primary key (id)
)
create unique index ix_person_unique on [dbo].[person](name)
insert into [dbo].[person]
( [name], [age], [info] )
values ( null, -- name - char(40)
1, -- age - int
'aa' -- info - char(50)
),
( null, -- name - char(40)
2, -- age - int
'bb' -- info - char(50)
)
消息 2601,级别 14,状态 1,第 1 行
不能在具有唯一索引“ix_person_unique”的对象“dbo.person”中插入重复键的行。重复键值为 (<null>)。
语句已终止。
更新
更新比较简单,就不多说了
update person set info ='police' where id between 14 and 17
select * from person
删除
删除person表中一定范围的数据
delete from person where id between 14 and 17
select * from person
如果要删除表的所有记录可以使用下面的两种方法
##方法一
delete from person
##方法二
truncate table person
跟sqlserver一样,truncate table会比delete from table 快
myisam引擎下的测试结果,30行记录
跟sqlserver一样,执行完truncate table后,自增字段重新从一开始。
################################
insert ignore into person(id,name,age,info)
select id,name,age,info from person_old;
select * from person
truncate table person
insert ignore into person(name,age,info) values ('amy',12,'bb')
select * from person
当你刚刚truncate了表之后执行下面语句就会看到重新从一开始
show table status like 'person'
以上就是mysql优化之-增删改具体分析(图)的详细内容。