Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

primary key should always be unsigned?

Tags:

database

mysql

since a primary key (identifier) wont be under 0, i guess it should always be unsigned?

like image 704
never_had_a_name Avatar asked Apr 21 '10 21:04

never_had_a_name


People also ask

What is the best choice for a primary key?

Integer (number) data types are the best choice for primary key, followed by fixed-length character data types. SQL Server processes number data type values faster than character data type values because it converts characters to ASCII equivalent values before processing, which is an extra step.

Is primary key obligatory?

Every table can have (but does not have to have) a primary key. The column or columns defined as the primary key ensure uniqueness in the table; no two rows can have the same key. The primary key of one table may also help to identify records in other tables, and be part of the second table's primary key.

Why primary key is not mandatory?

No, it is not required for every table to have a primary key. Whether or not a table should have a primary key is based on requirements of your database. Even though this is allowed it is bad practice because it allows for one to add duplicate rows further preventing the unique identification of rows.

Is primary key optional?

A table cannot have more than one primary key, but it can have multiple unique keys. Primary keys are optional, and can be defined when a table is created or altered. They are also beneficial, because they order the data when data is exported or reorganized.


2 Answers

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 SERIAL data type (presumably for compatibility with PostgreSQL, since SERIAL is not standard ANSI SQL). This data type is just shorthand that creates a BIGINT UNSIGNED.

Go ahead try it:

CREATE TABLE test.foo (foo_id SERIAL PRIMARY KEY);  SHOW CREATE TABLE test.foo;  CREATE TABLE `test`.`foo` (   `foo_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,   PRIMARY KEY (`foo_id`),   UNIQUE KEY `foo_id` (`foo_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1  

You get the same number of distinct values whether you declare an integer signed or unsigned: 232 for an INT and 264 for a BIGINT. 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/2 to max/2-1. Either way, you get the same absolute number of distinct values.

But since AUTO_INCREMENT starts 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 BIGINT for 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.

like image 121
Bill Karwin Avatar answered Sep 21 '22 08:09

Bill Karwin


Why exactly are you presuming that a primary key won't be under 0? That is not a given. I think you are confusing it with an identity column.

In any case it should make no appreciable difference either way, map the data type to the type of data you expect in the column regardless of whether it is a primary key or not.

like image 31
JohnFx Avatar answered Sep 18 '22 08:09

JohnFx