开发者

how to add primary key to table having duplicate values?

开发者 https://www.devze.com 2023-03-24 23:03 出处:网络
I have a table with structure Table-a Id was primary开发者_Go百科 key ID | Name 1| Gourav 2| Amit 3| Rahul

I have a table with structure

Table-a Id was primary开发者_Go百科 key

ID | Name
1  | Gourav
2  | Amit
3  | Rahul

But After that primary key automatically removed by restoring database to other database..

Now Records in Table are

ID | Name
1  | Gourav
2  | Amit
3  | Rahul
0  | AAA
0  | BBB
0  | CCC

How can i add primary key to id column again and can handle the existing data as well in MYSQL.

Thanks in advance..


Add PK as AUTO_INCREMENT, it will change all 0 values automatically -

ALTER TABLE table_a
  CHANGE COLUMN id id INT(11) NOT NULL AUTO_INCREMENT,
  ADD PRIMARY KEY (id);

After, AUTO_INCREMENT property can be removed -

ALTER TABLE table_a
  CHANGE COLUMN id id INT(11) NOT NULL;
0

精彩评论

暂无评论...
验证码 换一张
取 消