How to add a primary key to a MySQL table?

Mysql

Mysql Problem Overview


This is what I tried but it fails:

alter table goods add column `id` int(10) unsigned primary AUTO_INCREMENT;

Does anyone have a tip?

Mysql Solutions


Solution 1 - Mysql

After adding the column, you can always add the primary key:

ALTER TABLE goods ADD PRIMARY KEY(id)

As to why your script wasn't working, you need to specify PRIMARY KEY, not just the word PRIMARY:

alter table goods add column `id` int(10) unsigned primary KEY AUTO_INCREMENT;

Solution 2 - Mysql

Existing Column

If you want to add a primary key constraint to an existing column all of the previously listed syntax will fail.

To add a primary key constraint to an existing column use the form:

ALTER TABLE `goods`
MODIFY COLUMN `id` INT(10) UNSIGNED PRIMARY KEY AUTO_INCREMENT;

Solution 3 - Mysql

If your table is quite big better not use the statement:

alter table goods add column `id` int(10) unsigned primary KEY AUTO_INCREMENT;

because it makes a copy of all data in a temporary table, alter table and then copies it back. Better do it manually. Rename your table:

rename table goods to goods_old;

create new table with primary key and all necessary indexes:

create table goods (
	id	int(10) unsigned not null AUTO_INCREMENT
	... other columns ...
	primary key (id)
);

move all data from the old table into new, disabling keys and indexes to speed up copying:

-- USE THIS FOR MyISAM TABLES:

SET UNIQUE_CHECKS=0;
    ALTER TABLE goods DISABLE KEYS;
        INSERT INTO goods (... your column names ...) SELECT ... your column names FROM goods_old; 
    ALTER TABLE goods ENABLE KEYS;
SET UNIQUE_CHECKS=1;
OR

-- USE THIS FOR InnoDB TABLES:

SET AUTOCOMMIT = 0; SET UNIQUE_CHECKS=0; SET FOREIGN_KEY_CHECKS=0;
    INSERT INTO goods (... your column names ...) SELECT ... your column names FROM goods_old; 
SET FOREIGN_KEY_CHECKS=1; SET UNIQUE_CHECKS=1; COMMIT; SET AUTOCOMMIT = 1;

It takes 2 000 seconds to add PK to a table with ~200 mln rows.

Solution 4 - Mysql

Not sure if this matters to anyone else, but I prefer the id for the table to be the first column in the database. The syntax for that is:

ALTER TABLE your_db.your_table ADD COLUMN `id` int(10) UNSIGNED PRIMARY KEY AUTO_INCREMENT FIRST;

Which is just a slight improvement over the first answer. If you wanted it to be in a different position, then

ALTER TABLE unique_address ADD COLUMN `id` int(10) UNSIGNED PRIMARY KEY AUTO_INCREMENT AFTER some_other_column;

HTH, -ft

Solution 5 - Mysql

Use this query,

alter table `table_name` add primary key(`column_name`);

Solution 6 - Mysql

This code work in my mysql db:

ALTER TABLE `goods`
ADD COLUMN `id` INT NOT NULL AUTO_INCREMENT,
ADD PRIMARY KEY (`id`);

Solution 7 - Mysql

For me, none of suggestions worked giving me the errors of syntax, so I just gave a try using phpmyadmin(version 4.9.2), (10.4.10-MariaDB) and added id column with auto-increment primary key. Id column was nicely added from the first element.

Query output was:

ALTER TABLE table_name ADD id INT NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY (id);

Solution 8 - Mysql

ALTER TABLE GOODS MODIFY ID INT(10) NOT NULL PRIMARY KEY;

Solution 9 - Mysql

Try this,

alter table goods add column `id` int(10) unsigned primary key auto_increment

Solution 10 - Mysql

ALTER TABLE `goods`
MODIFY COLUMN `id` INT(10) UNSIGNED PRIMARY KEY AUTO_INCREMENT;

Solution 11 - Mysql

Remove quotes to work properly...

alter table goods add column id int(10) unsigned primary KEY AUTO_INCREMENT;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmysql_goView Question on Stackoverflow
Solution 1 - MysqlThe Scrum MeisterView Answer on Stackoverflow
Solution 2 - MysqlMERView Answer on Stackoverflow
Solution 3 - MysqlDenis MakarskiyView Answer on Stackoverflow
Solution 4 - MysqlftrotterView Answer on Stackoverflow
Solution 5 - MysqlRATAN KUMARView Answer on Stackoverflow
Solution 6 - MysqlMichał MyszorView Answer on Stackoverflow
Solution 7 - Mysqlav3000View Answer on Stackoverflow
Solution 8 - MysqlVimit PatelView Answer on Stackoverflow
Solution 9 - MysqlphponwebsitesView Answer on Stackoverflow
Solution 10 - MysqlYassine GribView Answer on Stackoverflow
Solution 11 - MysqlRaja MuthukuttyView Answer on Stackoverflow