How to add a primary key to a MySQL table?
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;
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;