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 @   liwenchao1995  阅读(447)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
点击右上角即可分享
微信分享提示