Adding AUTO_INCREMENT to an Existing MySQL Column
Database administrators sometimes need to add auto-increment functionality to an existing primary key column. A common mistake is using incorrect SQL syntax. For instance, the following command will result in a syntax error:
<code class="language-sql">ALTER TABLE document ALTER COLUMN document_id AUTO_INCREMENT;</code>
The correct syntax to modify the column and enable auto-increment is:
<code class="language-sql">ALTER TABLE document MODIFY COLUMN document_id INT auto_increment;</code>
This updated command explicitly specifies the data type (INT
) for the document_id
column, ensuring the auto_increment
attribute is applied correctly. This avoids syntax errors and properly configures the column for automatic incrementing values.
The above is the detailed content of How to Correctly Add AUTO_INCREMENT to an Existing MySQL Column?. For more information, please follow other related articles on the PHP Chinese website!