Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when auto increment primary key in MySQL comes to maximum

I have auto increment, primary key in MySQL called ID and it is INT. As my app grows and I'm getting more users they are making more entries. What happens when my INT comes to its maximum value 2 147 483 647? Can I just switch to BIGINT?

like image 694
FosAvance Avatar asked Jul 04 '13 22:07

FosAvance


2 Answers

The MySQL official documentation states that :

When the column reaches the upper limit of the data type, the next attempt to generate a sequence number fails. Use the UNSIGNED attribute if possible to allow a greater range.

And yes, you can switch the types of the auto_increment value. On this point, the documentation advises to use :

[...] the smallest integer data type for the AUTO_INCREMENT column that is large enough to hold the maximum sequence value you will need.

like image 122
Halim Qarroum Avatar answered Oct 02 '22 12:10

Halim Qarroum


Yes, you can alter the table and change from INT to BIGINT without problems.

Also, you may need to change the datatype wherever it is as an FK

like image 44
Cristian Meneses Avatar answered Oct 02 '22 12:10

Cristian Meneses