2015-10-16 59 views
0

我有大约100,000行唯一的数据。客户说他想要一个名为id的列是auto_incrementing和主键。现在,它充满了0。我究竟会如何去做这件事?将重复项设置为PRIMARY AUTO_INCREMENT?

当我尝试将其设置为auto_increment MySQL时,表示该列需要为主,并且只能有1个auto_increment列。

当我尝试将它设置为主键时,它表示重复条目,因为该列中的所有行都包含“0”。

+0

首先更新列值。然后更改列属性。 – RubahMalam

+0

当我希望他们从1自动增加时,我会更新它们到什么程度? – User

+0

你可以用变量来做。但是,请分享您的表格结构和样本数据,并让我们看看,如果我们可以用其他列更新它们而不使用变量。 – RubahMalam

回答

1

我建议你滴ID列并重新创建它像这样:

create table test (id int, name varchar(50)); 
insert into test values (0, 'john'), (0, 'matt'), (0, 'tom'); 

alter table test add primary key(id); 
ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY' 

alter table test drop id; 
alter table test add id int primary key auto_increment; 

select * from test; 
+------+----+ 
| name | id | 
+------+----+ 
| john | 1 | 
| matt | 2 | 
| tom | 3 | 
+------+----+ 

让您的数据备份第一,但!