views:

200

answers:

3

Is there any good practice for this?

I wish I could solve the problem when primary key hit the limit and not to avoid it. Because this is what will happen in my specific problem.

If it's unavoidable... What can i do?

This is mysql question, is there Sybase sql anywhere same problem?

A: 

Well, depending on the autoincrement column's datatype.
Unsign int goes up to 4294967295.

If you want to prevent the error, you can check the value last autoincrement value: LAST_INSERT_ID()

If it's approaching the datatype's max, either do not allow insertion or handle it in other ways.

Other than that, I can only suggest you use bigint so you can almost not hit the max for most scenario.

Can't give you a foolproof answer though :)

o.k.w
A: 

You should pick the correct type for the primary key, if you know you will have lots of rows you could use bigint instead of the commonly used int.

In mysql you can easily adjust the primary key collumn with the alter table statement to adjust the range.
you should also use the unsigned property on that collumn because an auto increment primary key is always positive.

when the limit is reached you could maybe create some algorithm to put inside the ON DUPLICATE KEY UPDATE statement of an INSERT

Nicky De Maeyer
+1  A: 

Why would you hit the limit on that field? If you defined it with a datatype large enough it should be able to hold all your records.

If you used an unsigned bigint you can have up to 18,446,744,073,709,551,615 records!

Andre Miller