Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "varchar" as the primary key? bad idea? or ok?

Tags:

Is it really that bad to use "varchar" as the primary key?

(will be storing user documents, and yes it can exceed 2+ billion documents)

like image 833
001 Avatar asked Sep 21 '11 09:09

001


People also ask

Is it okay to have VARCHAR as primary key?

It is perfectly acceptable to use a varchar column as the primary key. This is often the case when one uses a natural key that doesn't happen to be an integer.

What is the disadvantage of using VARCHAR?

In addition to the size and performance considerations of setting the size of a varchar (and possibly more important, as storage and processing get cheaper every second), the disadvantage of using varchar(255) "just because" is reduced data integrity.

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 it bad to use VARCHAR Max?

Use varchar when the sizes of the column data entries vary considerably. Use varchar(max) when the sizes of the column data entries vary considerably, and the size might exceed 8,000 bytes. So varchar(MAX) is inappropriate fore columns like FirstName, where the value will never exceed 8,000 bytes.


2 Answers

It totally depends on the data. There are plenty of perfectly legitimate cases where you might use a VARCHAR primary key, but if there's even the most remote chance that someone might want to update the column in question at some point in the future, don't use it as a key.

like image 159
ninesided Avatar answered Oct 19 '22 03:10

ninesided


If you are going to be joining to other tables, a varchar, particularly a wide varchar, can be slower than an int.

Additionally if you have many child records and the varchar is something subject to change, cascade updates can causes blocking and delays for all users. A varchar like a car VIN number that will rarely if ever change is fine. A varchar like a name that will change can be a nightmare waiting to happen. PKs should be stable if at all possible.

Next many possible varchar Pks are not really unique and sometimes they appear to be unique (like phone numbers) but can be reused (you give up the number, the phone company reassigns it) and then child records could be attached to the wrong place. So be sure you really have a unique unchanging value before using.

If you do decide to use a surrogate key, then make a unique index for the varchar field. This gets you the benefits of the faster joins and fewer records to update if something changes but maintains the uniquess that you want.

Now if you have no child tables and probaly never will, most of this is moot and adding an integer pk is just a waste of time and space.

like image 23
HLGEM Avatar answered Oct 19 '22 02:10

HLGEM