Home >Database >Mysql Tutorial >How to Configure MySQL Auto-Increment to Start at 0001?

How to Configure MySQL Auto-Increment to Start at 0001?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-03 10:59:02181browse

How to Configure MySQL Auto-Increment to Start at 0001?

Setting Auto-Increment Format to 0001 in MySQL

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:

  1. 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>
  2. 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>
  3. 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn