SQL 语句之insert语句插入数据:若表中有重复的主键或数据继续插入解决方案
2018-03-23 16:40
507 查看
已知条件:MySQL数据库
存在一张表,表名为teacher,主键为id,表中有4行数据
select * from teacher;
要求:要求使用数据库插入语句往表中插入数据,若需要插入表中的数据(或者数据的主键)如果已经在表中存在,那么要求SQL在执行的时候不能报错。
例如:插入一行id=3,name=丁老师,salary=5000的记录,
insert
into teacher(id,name,salary) values(3,'丁老师',5000);
因为id=3的主键在表中已经存在,所以强行执行SQL插入的话程序会报错。
方法(1):使用 replace 代替 insert
replace
into teacher(id,name,salary) values(3,'丁老师',5000);
1
在MySQL中 replace 的执行效果和 insert 的效果是一样的,不同的是replace 语句会把后来插入表中的记录替换掉已经存在于表中的记录,此法不推荐使用。
因为若此时再插入一条语句
replace into teacher(id,name,salary) values(3,'苏老师',9000);
那么这条语句的内容会把先前插入表中的内容id=3,name=丁老师,salary=5000的记录替换掉
方法(2):结合select的判断式insert 语句
灵感代码:
insert
into tableA(x,y,z) select * from tableB
模板代码:
insert
into teacher(id,name,salary) select ?,?,? from teacher where
not
exists(select * from teacher where id=?) limit 1;
或者
insert
into teacher(id,name,salary) select
distinct ?,?,? from teacher where
not
exists(select * from teacher where id=?);
例子:插入一行id=3,name=丁老师,salary=5000的记录
insert
into teacher(id,name,salary)
select
3,'丁老师',5000
from teacher
where
not
exists(select * from teacher where id=3) limit 1;
或者
insert
into teacher(id,name,salary)
( select
4,'白老师',4000
from teacher
where
not
exists(select * from teacher where id=4) limit 1);
在上面的SQL语句中:执行的原理解析:
若teacher表中不存在id=3的那条记录,则生成要插入表中的数据并插入表;
若teacher表中存在id=3的那条记录,则不生成要插入表中的数据。
其实程序可以分开看:
① select * from teacher where id=3
若查询有值,则表示真,即存在id=3这条记录,若查询没有值则表示假,即不存在id=3这条记录,
②若果不存在id=3这条记录,那么又因为
not
exists
本身表示假,即不存在的意思;假假为真,所以此时程序可以形象的理解为
select
3,'丁老师',5000
from teacher where
not
exists (false) limit 1;
等价于
select
3,'丁老师',5000
from teacher where
true limit 1;
③所以程序就会生成一行为 3,'丁老师',5000的记录
④最后生成的数据就会插入表中
存在一张表,表名为teacher,主键为id,表中有4行数据
select * from teacher;
要求:要求使用数据库插入语句往表中插入数据,若需要插入表中的数据(或者数据的主键)如果已经在表中存在,那么要求SQL在执行的时候不能报错。
例如:插入一行id=3,name=丁老师,salary=5000的记录,
insert
into teacher(id,name,salary) values(3,'丁老师',5000);
因为id=3的主键在表中已经存在,所以强行执行SQL插入的话程序会报错。
方法(1):使用 replace 代替 insert
replace
into teacher(id,name,salary) values(3,'丁老师',5000);
1
在MySQL中 replace 的执行效果和 insert 的效果是一样的,不同的是replace 语句会把后来插入表中的记录替换掉已经存在于表中的记录,此法不推荐使用。
因为若此时再插入一条语句
replace into teacher(id,name,salary) values(3,'苏老师',9000);
那么这条语句的内容会把先前插入表中的内容id=3,name=丁老师,salary=5000的记录替换掉
方法(2):结合select的判断式insert 语句
灵感代码:
insert
into tableA(x,y,z) select * from tableB
模板代码:
insert
into teacher(id,name,salary) select ?,?,? from teacher where
not
exists(select * from teacher where id=?) limit 1;
或者
insert
into teacher(id,name,salary) select
distinct ?,?,? from teacher where
not
exists(select * from teacher where id=?);
例子:插入一行id=3,name=丁老师,salary=5000的记录
insert
into teacher(id,name,salary)
select
3,'丁老师',5000
from teacher
where
not
exists(select * from teacher where id=3) limit 1;
或者
insert
into teacher(id,name,salary)
( select
4,'白老师',4000
from teacher
where
not
exists(select * from teacher where id=4) limit 1);
在上面的SQL语句中:执行的原理解析:
若teacher表中不存在id=3的那条记录,则生成要插入表中的数据并插入表;
若teacher表中存在id=3的那条记录,则不生成要插入表中的数据。
其实程序可以分开看:
① select * from teacher where id=3
若查询有值,则表示真,即存在id=3这条记录,若查询没有值则表示假,即不存在id=3这条记录,
②若果不存在id=3这条记录,那么又因为
not
exists
本身表示假,即不存在的意思;假假为真,所以此时程序可以形象的理解为
select
3,'丁老师',5000
from teacher where
not
exists (false) limit 1;
等价于
select
3,'丁老师',5000
from teacher where
true limit 1;
③所以程序就会生成一行为 3,'丁老师',5000的记录
④最后生成的数据就会插入表中
相关文章推荐
- SQL 语句之insert语句插入数据;若表中有重复的主键或数据插入的时候要求不能报错
- MS-SQL生成表里数据的insert插入语句
- 如何往有自增标识字段的表插入数据时,同时给自增标识字段插入值呢,在Inset Into语句前后加上SQL语句:SET IDENTITY_INSERT TableName ON和SET IDENTITY_INSERT TableName OFF
- 将表数据生成Insert脚本 比较好用的生成插入语句的SQL脚本
- C# 用sql insert into 语句向oracle插入数据数据一直未更新
- 如何优化用SQL语句INSERT INTO … SELECT插入数据时锁全表的问题
- 两个结构完全相同的表a和b,主键为index,使用SQL语句,把a表中存在但在b表中不存在的数据插入的b表中
- JDBC Insert语句插入Oracle数据库返回数据主键
- SQL语句,在往表test中插入“内容”字段时,检查该字段是否有重复数据
- 用SQL语句向表格中插入数据INSERT
- JDBC Insert语句插入Oracle数据库返回数据主键
- 主键id是自增的,插入数据还是提示 SQL 不能插入重复键
- JDBC Insert语句插入Oracle数据库返回数据主键
- sql语句如何在insert一条数据后直接返回刚刚插入的数据的ID
- 在一条SQL语句中向2个表中插入数据解决方案
- t-sql go 重复执行sql 语句 重复插入数据
- sql-insert一条语句执行多条数据插入
- sql语句如何在insert一条数据后直接返回刚刚插入的数据的ID
- SQL多个主键的表,插入数据有重复时,查询数据的重复值?
- 将表数据生成Insert脚本 比较好用的生成插入语句的SQL脚本