mysql添加重复列,MySQL-尝试添加新列时出现重复输入错误

I have a MySQL database with a table that has 2 million rows using innodb engine. I want to add another column, but I keep getting the following error:

Error 1062: Duplicate entry '' for key 'PRIMARY' SQL Statement: ALTER TABLE `mydb`.`table` ADD COLUMN `country` VARCHAR(35) NULL DEFAULT NULL AFTER `email`

How can I add the column without getting this error?

EDIT: Table definition

id int(11) NOT NULL AUTO_INCREMENT,

user_id varchar(45) NOT NULL,

first_name varchar(150) DEFAULT NULL,

last_name varchar(150) DEFAULT NULL,

gender varchar(10) DEFAULT NULL,

email varchar(100) DEFAULT NULL,

created_at bigint(20) DEFAULT NULL,

updated_at bigint(20) DEFAULT NULL,

PRIMARY KEY (`id`,`user_id`),

UNIQUE KEY `user_id_UNIQUE` (`user_id`),

KEY `first_name` (`first_name`),

KEY `last_name` (`last_name`)

EDIT #2: SHOW INDEXES output

Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Index_type

table 0 PRIMARY 1 id A 3516446 BTREE

table 0 PRIMARY 2 user_id A 3516446 BTREE

table 0 user_id_UNIQUE 1 user_id A 3516446 BTREE

table 1 first_name 1 first_name A 390716 BTREE

table 1 last_name 1 last_name A 439555 BTREE

解决方案

it solution will lock table on write, but often suitable for solving the problem if table is not very big

LOCK TABLES my_table WRITE;

ALTER TABLE my_table

ADD COLUMN `ts` DATETIME NULL AFTER `id`;

UNLOCK TABLES;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值