mysql insert出现主键冲突错误的解决方法

mysql insert出现主键冲突错误的解决方法

insert 时防止出现主键冲突错误的方法
在mysql中插入数据的时候常常因为主键存在而冲突报错,下面有两个解决方法:

1 在insert 语句中添加ignore 关键字

insert ignore into table (id,name) values ('1','username');

这是如果id主键已经存在的时候,就会忽略现在数据而保留原来的数据不变;

2、插入的时候用REPLACE INTO语句

REPLACE INTO table(id,name) values ('1','username');

这个方法是如果id主键存在的时候就覆盖掉原来的数据。

查询哪个表没有主键

select table_schema,table_name from information_schema.tables where table_schema not in ('information_schema','mysql','performance_schema') and table_type like '%TABLE%' and engine='innodb' and (table_s
chema,table_name) not in (select distinct table_schema,table_name from information_schema.table_constraints where constraint_type in ('PRIMARY KEY'));
posted @ 2023-08-23 22:43  liwenchao1995  阅读(326)  评论(0编辑  收藏  举报