Home >Database >Mysql Tutorial >How to Configure MySQL Auto-Increment to Start at 0001?
Question: How can MySQL be configured to auto-increment a primary key in a four-digit format (e.g., 0001 instead of 1)?
Answer:
To achieve a four-digit auto-increment format in MySQL, follow these steps:
Create a Table with an AUTO_INCREMENT Column:
<code class="sql">CREATE TABLE my_table ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (id) ) ENGINE=InnoDB;</code>
Modify the Column to Use ZEROFILL Attribute:
By default, MySQL auto-increments values without padding. To enable zero-padding, add the ZEROFILL attribute to the column definition:
<code class="sql">ALTER TABLE my_table MODIFY COLUMN id INT NOT NULL AUTO_INCREMENT ZEROFILL;</code>
Restart MySQL Server or Issue FLUSH:
To apply the changes, you need to either restart the MySQL server or issue a FLUSH TABLES command:
<code class="sql">FLUSH TABLES;</code>
After following these steps, the id column in the my_table will auto-increment in the 0001 format.
The above is the detailed content of How to Configure MySQL Auto-Increment to Start at 0001?. For more information, please follow other related articles on the PHP Chinese website!