Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a recommended size for a Mysql Primary key

Each entry in my 'projects' table has a unique 32 characters Hash identifier stored using a varchar(32).

Would that be considered a bad practice to use this as the primary key ? Is there a recommended size, datatype for Primary keys ?

like image 758
silkAdmin Avatar asked Dec 21 '12 20:12

silkAdmin


People also ask

Should a primary key be short?

The primary key should be short and consist of one column whenever possible. Data type. The data type of a primary key should be a numeric, an integer or a short, fixed-width character.

What is a good 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.

What should my primary key be SQL?

The PRIMARY KEY constraint uniquely identifies each record in a table. 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).

Is it necessary to have primary key in MySQL?

A table can have only one primary key, and a primary key field cannot contain a null value. By default, primary keys are a requirement in all MySQL database tables. This ensures reliable data replication. However, you can turn off a database's primary key requirement by making a configuration request via our API.


1 Answers

I would say yes, it's a bad idea to use such a large column for a primary key. The reason is that every index you create on that table will have that 32 character column in it, which will bloat the size of all the indexes. Bigger indexes mean more disk space, memory and I/O.

Better to use an auto-increment integer key if possible, and simply create a unique index on your hash identifier column.

like image 99
Eric Petroelje Avatar answered Sep 25 '22 20:09

Eric Petroelje