Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL primary/foreign key size?

I seem to see a lot of people arbitrarily assigning large sizes to primary/foreign key fields in their MySQL schemas, such as INT(11) and even BIGINT(20) as WordPress uses.

Now correct me if I'm wrong, but even an INT(4) would support (unsigned) values up to over 4 billion. Change it to INT(5) and you allow for values up to a quadrillion, which is more than you would ever need, unless possibly you're storing geodata at NASA/Google, which I'm sure most of us aren't.

Is there a reason people use such large sizes for their primary keys? Seems like a waste to me...

like image 558
David Avatar asked May 15 '10 17:05

David


People also ask

How to create primary and foreign keys in MySQL?

The Foreign key is a field that contains the primary key of some other table to establish a connection between each other. Let’s have a look at the syntax and different examples to create primary and foreign keys in MySQL. We can make a primary key on a segment of the table by utilizing the ALTER TABLE.

How many primary keys can a mySQL table have?

Primary keys must contain UNIQUE values, and cannot contain NULL values. A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).

What is the use of foreign key constraint in MySQL?

MySQL FOREIGN KEY Constraint. The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables. A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another table. The table with the foreign key is called the child table, and the table with the primary key is called the ...

What is the difference between primary key and foreign key?

The primary key can be any field or column of a table, which should be a unique and non-null value for each record or a row. The Foreign key is a field that contains the primary key of some other table to establish a connection between each other.


1 Answers

The size is neither bits nor bytes. It's just the display width, that is used when the field has ZEROFILL specified.

and

INT[(M)] [UNSIGNED] [ZEROFILL] A normal-size integer. The signed range is -2147483648 to 2147483647. The unsigned range is 0 to 4294967295.

See this explanation.

like image 88
Eric J. Avatar answered Sep 22 '22 03:09

Eric J.