since a primary key (identifier) wont be under 0, i guess it should always be unsigned?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
TL/DR: Yes, but it almost doesn’t matter.
Auto-increment always increases, so it will never use negative values. You might as well make it unsigned, and you get twice the range of values.
On the other hand, if your table uses 231 values, it will probably also use 232 values in a short time, so having twice the range of values isn’t a big difference. You will have to upgrade to BIGINT anyway.
MySQL supports an optional
SERIALdata type (presumably for compatibility with PostgreSQL, sinceSERIALis not standard ANSI SQL). This data type is just shorthand that creates aBIGINT UNSIGNED.Go ahead try it:
You get the same number of distinct values whether you declare an integer signed or unsigned: 232 for an
INTand 264 for aBIGINT. If the number is unsigned, you get values from 0 to that max value minus one. If the number is signed, you get values from-max/2tomax/2-1. Either way, you get the same absolute number of distinct values.But since
AUTO_INCREMENTstarts at zero by default and increments in the positive direction, it’s more convenient to utilize the positive values than the negative values.But it hardly matters that you get 2X as many positive values. Any table that would exceed the maximum signed integer value 231-1 is likely to continue to grow, so you should just use a
BIGINTfor these tables.You’re really, really, really unlikely to allocate more than 263-1 primary key values, even if you delete all your rows and re-load them many times a day.