Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when autoincrement clashes with existing data in MySQL?

Tags:

mysql

I have a MySQL table with an autoincremented id column. The id started from 1 and is now in the 4000s.

However, I also need to port some legacy data into this table from an old version of the application. The ids of this data start from 5000 and must be preserved for auditing purposes.

What happens if I insert an entry after my autoincrement counter is up to 4999? Is autoincrement smart enough to look for the next available id, or will it crash because it tries to insert id 5000, which already exists?

While advice on how to work around this problem is very helpful, I'd also like to understand what MySQL would do in this situation and if I need to intervene at all.

like image 599
ctford Avatar asked Sep 30 '09 09:09

ctford


People also ask

What does Autoincrement do in MySQL?

Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.

What happens when auto increment reaches limit?

When the AUTO_INCREMENT column reaches the upper limit of data type then the subsequent effort to generate the sequence number fails. That is why it is advised to use a large enough integer data type for the AUTO_INCREMENT column to hold the maximum sequence value required by us.

Can we have 2 auto increment in MySQL?

You can't have two auto-increment columns.

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 do I use auto increment in MySQL?

MySQL AUTO_INCREMENT Keyword MySQL uses the AUTO_INCREMENT keyword to perform an auto-increment feature. By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record. The following SQL statement defines the "Personid" column to be an auto-increment primary key field in the "Persons" table:

Why are auto increment values missing in SQL Server?

Missing auto increment values are not necessarily an indication that a query failed to insert data — it could just be that the data already existed in the table. That is technically still a failure, but it is an expected failure.

Why is my auto_increment not working?

When auto_increment is skipping values it is often because of a failed insert query. These gaps between values can also occur if someone has been deleting rows from the MySQL table.

What happens when a MySQL database is full?

When that is full, you have probably set the record of having the most data in a MySQL database ever. Assuming you don't mess around with the auto_increment column and just let it do it's work. What I mean to say is, that you don't have to worry about that. Have you looked at how huge the highest possible bigint value is?


2 Answers

Autoincrement will use the next available id for both InnoDB and MyISAM tables but you can manually change it's next position in the following way:

After your insert, you could set the auto increment to a value above the now-highest id:

ALTER TABLE tbl AUTO_INCREMENT = 9000;
like image 112
Andrew Duffy Avatar answered Nov 06 '22 03:11

Andrew Duffy


Autoincrement will use the next available id for both InnoDB and MyISAM tables.

I have tested this for MySQL 4.1.22 running on Windows Vista. I created two simple tables, one using InnoDB and one using MyISAM. Each had an autoincrementing primary key called 'id' and a varchar column called 'description'.

I ran the following commands (with no errors):

INSERT INTO MyIsamTest (description)     VALUES ('autoincrement id insert'); 
INSERT INTO MyIsamTest (id, description) VALUES (100, 'manual id insert');
INSERT INTO MyIsamTest (description)     VALUES ('autoincrement id insert');

SELECT * FROM MyIsamTest;

I got the following result, which shows that the 'id' column was correctly autoincremented:

+=====+=========================+
| id  | description             |
+=====+=========================+
|   1 | autoincrement id insert |
+-----+-------------------------+
| 100 | manual id insert        |
+-----+-------------------------+
| 101 | autoincrement id insert |
+-----+-------------------------+

I repeated the experiment on my InnoDbTest table with the same outcome.

like image 25
ctford Avatar answered Nov 06 '22 02:11

ctford