Home >Database >Mysql Tutorial >How to Set a Custom Starting Value for Auto-Increment in MySQL?

How to Set a Custom Starting Value for Auto-Increment in MySQL?

Linda Hamilton
Linda HamiltonOriginal
2024-12-24 21:30:26243browse

How to Set a Custom Starting Value for Auto-Increment in MySQL?

Setting Custom Auto-Increment Start Value in MySQL

When working with MySQL databases, it may be necessary to set a custom starting value for auto-increment columns. By default, MySQL auto-increments from the number 1, but it is possible to change this to any desired value.

Solution:

To alter the starting number for auto-increment columns, you can use the following MySQL query:

ALTER TABLE table_name AUTO_INCREMENT = starting_value;

For example, if you have a table named "tbl" and you want to set the auto-increment value to 5, you would execute the following query:

ALTER TABLE tbl AUTO_INCREMENT = 5;

This query will change the auto-increment starting value to 5 for the specified table. Subsequent insertions into this table will start from that value.

Additional Notes:

  • You can also set the auto-increment starting value during table creation using the AUTO_INCREMENT clause.
  • The ALTER TABLE query is not case-sensitive, so you can also use AUTO_INCREMENT = 5 or auto_increment = 5.
  • Refer to the MySQL documentation for more detailed information and additional options.

The above is the detailed content of How to Set a Custom Starting Value for Auto-Increment in MySQL?. 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