Add a primary key column in to a no primay key old table

北城余情 提交于 2019-11-30 16:47:15

问题


If a table, data might be duplicated amount rows, and there is not primary key for every row,

can i add an column to be a primary key?


回答1:


Yes. Add a new column and set it as the primary key with AUTO_INCREMENT. Doing so will create a new column and automatically add a unique id for each row.

ALTER TABLE old_table ADD pk_column INT AUTO_INCREMENT PRIMARY KEY;



回答2:


This is possible with ALTER TABLE (Assuming you have a column that you want to use as a PK)

ALTER TABLE table 
ADD PRIMARY KEY(column)

Alternativly:

ALTER TABLE table 
ADD your_pk_column INT(11) AUTO_INCREMENT PRIMARY KEY


来源:https://stackoverflow.com/questions/4574866/add-a-primary-key-column-in-to-a-no-primay-key-old-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!