mysql管理主键的表_mysql处理库中大量缺失主键表(添加主键)方案

背景:

从单节点mysql库将数据迁移至mysql集群库中,因为原单节点mysql数据库大量表缺失主键,而导致导入mysql集群(msyql集群要求每张表必须有主键)报错。

----查询无主键的表

select

table_schema,

table_name

from information_schema.tables

where (table_schema,table_name) not in(

select distinct table_schema,table_name from information_schema.columns where COLUMN_KEY='PRI'

)

and table_schema in (

'目标数据库'

);

----添加主键

通过excel拼接sql,再批量执行。

----删除重复数据保留一条

(针对重复数据完全一致的情况,其他具体情况具体分析)

1、查询重复uuid的数据

select uuid from 目标表名 GROUP BY uuid having COUNT(uuid)>1

2、添加临时主键

alter table 目标表名 add column id int(11) PRIMARY KEY AUTO_INCREMENT;

3、删除重复数据保留一条

DELETE from 目标表名where id in (select id from (select max(id) as id,uuid from interface_record GROUP BY uuid having COUNT(uuid)>1 ORDER BY id ) as tmp )

4、删掉临时主键

ALTER table 目标表名 DROP column id;

5、添加主键

alter table `表名` add primary key(主键列名);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值