How to Append a Primary Key to a MySQL Table
When attempting to add a primary key to a MySQL table, you may encounter errors similar to the one described in the question. This error occurs if you use the PRIMARY keyword instead of PRIMARY KEY.
To correctly add a primary key to a table, follow these steps:
Add the Column:
<code class="sql">ALTER TABLE goods ADD COLUMN `id` INT(10) UNSIGNED;</code>
Specify the Primary Key:
<code class="sql">ALTER TABLE goods ADD PRIMARY KEY (id);</code>
As an example, the corrected query that the user was attempting to use would be:
<code class="sql">ALTER TABLE goods ADD COLUMN `id` INT(10) UNSIGNED PRIMARY KEY AUTO_INCREMENT;</code>
The above is the detailed content of How to Fix \'Error 1062 (23000): Duplicate entry\' When Adding a Primary Key in MySQL?. For more information, please follow other related articles on the PHP Chinese website!