Home > Database > Mysql Tutorial > How to Change the Auto-Increment Starting Number in MySQL?

How to Change the Auto-Increment Starting Number in MySQL?

DDD
Release: 2024-12-09 19:37:14
Original
287 people have browsed it

How to Change the Auto-Increment Starting Number in MySQL?

Modifying Auto-Increment Starting Number in MySQL

Question:

In MySQL, how can you change the starting value for the auto-increment column from the default 1 to a specific number, such as 5?

Answer:

To modify the auto-increment starting value in MySQL, you can use the ALTER TABLE statement with the AUTO_INCREMENT keyword. The syntax is as follows:

ALTER TABLE tbl_name AUTO_INCREMENT = starting_value;
Copy after login

where:

  • tbl_name is the name of the table containing the auto-increment column.
  • starting_value is the desired starting value for the auto-increment sequence.

For example, to set the auto-increment value to 5 for the id column in the tbl table, you would use the following query:

ALTER TABLE tbl AUTO_INCREMENT = 5;
Copy after login

This will change the starting value for the auto-increment sequence to 5, so the next inserted record will have an id of 5.

Additional Information:

  • The AUTO_INCREMENT value must be an integer greater than or equal to 0.
  • If the AUTO_INCREMENT value is changed to a number that is less than the current maximum value of the column, the auto-increment sequence will continue from the current maximum value.
  • The ALTER TABLE statement can be used to modify other table attributes as well, such as data types, column constraints, and indexes.

The above is the detailed content of How to Change the Auto-Increment Starting Number in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template