Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the step auto_increment fields increment by

How do I change the amount auto_increment fields in MySQL increment by from the default (1) to n?

like image 930
sansknwoledge Avatar asked Nov 06 '09 09:11

sansknwoledge


People also ask

How do I change auto increment?

In MySQL, the syntax to reset the AUTO_INCREMENT column using the ALTER TABLE statement is: ALTER TABLE table_name AUTO_INCREMENT = value; table_name. The name of the table whose AUTO_INCREMENT column you wish to reset.

How do I set auto increment to 1?

Syntax for AccessBy default, the starting value for AUTOINCREMENT is 1, and it will increment by 1 for each new record. Tip: To specify that the "Personid" column should start at value 10 and increment by 5, change the autoincrement to AUTOINCREMENT(10,5) .

Can we change auto increment value in MySQL?

In MySQL, the syntax to change the starting value for an AUTO_INCREMENT column using the ALTER TABLE statement is: ALTER TABLE table_name AUTO_INCREMENT = start_value; table_name. The name of the table whose AUTO_INCREMENT value you wish to change.

How can I change column auto increment in SQL Server?

ALTER TABLE Inventory MODIFY COLUMN item_number INT AUTO_INCREMENT=50; After running this code, future item IDs will start at an item_number of 50 and increment by 1. To change the starting increment value and increment in SQL Server, set your non-default values during table creation.


2 Answers

If you want to change autoincrement step from 1 to N then there is a solution. It could be done on MySQL server side: look for '--auto-increment-increment' startup option or use following command SET @@auto_increment_increment=2;, but be warned that this is a server wide change (all tables will increment by 2).

Unortodox solutions could that could be considered:

  1. Launch two MySQL servers on same machine, with different ports (one with auto_increment_increment=1 other with auto_increment_increment=2)
  2. Use some serverside magic (PHP, ASP ,???) combined with turning off tables auto_increment to manually calculate (simple peek at last id and +=2 would be ok) and provide id in INSERT query.

Some official MySQL FAQ

like image 170
mth Avatar answered Sep 28 '22 04:09

mth


You can change it using ALTER TABLE:

ALTER TABLE table AUTO_INCREMENT = n;

Or if you want to do set it from start:

CREATE TABLE table (...) AUTO_INCREMENT = n;
like image 40
Lukáš Lalinský Avatar answered Sep 28 '22 03:09

Lukáš Lalinský