Alex Rivera | Logout

What happens when autoincrement clashes with existing data in MySQL?

Asked 2009-09-30T09:02:01.447
14

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.

Edit
Report

1 Answer

0

I believe MySQL checks if you insert on an auto-incremented column and will update AUTO_INCREMENT so that AUTO_INCREMENT > MAX(id), but I need to look through the docs. You should still follow Andrew Duffy's suggestion, to be safe.

answered 2009-09-30T09:54:34.643

Your Answer