Z
Z
ZmeuSnake2016-03-27 09:53:48
SQL
ZmeuSnake, 2016-03-27 09:53:48

Is using a string primary key justified?

For example, there is a table that stores a catalog of cars. Is it justified to make the state number the primary key, i.e. , let's say an nchar(6) field? And if not, in what cases is it justified? Here, after all, we get rid of the int field with auto-increment, and there is no need to do additional checks for the uniqueness of the number.
How will using a string pk affect performance?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
Stanislav Makarov, 2016-03-27
@ZmeuSnake

Is it justified to make the state number the primary key, i.e. , let's say an nchar(6) field?

Justified if a) a fixed-length field - it doesn't matter if it's a string or a number; b) you are really sure that the values ​​will be unique, including in time. There are subject areas where this is true, but not everywhere.
If the string is of variable length, it is better not to make it the primary key, but you can do a full-text search on it if you need it.
For a DBMS, a small fixed-length string (eg 12 bytes) is the same as a large number, there is not much difference in general. But variable-length strings are usually stored differently, in a separate area, and a pointer is placed in the record itself. All this, of course, increases the cost of requests.

A
Armenian Radio, 2016-03-27
@gbg

1. No good. The key must be a number, this time. This affects performance.
2. The assumption about the uniqueness of car numbers is gloom. It is quite possible that two cars with the same number will appear, just one of them was deregistered 100,500 years ago.

T
trevoga_su, 2016-03-27
@trevoga_su

wrong. car number is not unique.
in 2013 I went to the traffic police, paid 2000 rubles and put my old state number on a new car

W
wadeg, 2016-03-27
@wadeg

If the string key is not too wide, then it is much better than using a "fake" PK. It does NOT affect the performance of inserts / joins within the real error: you don’t need to listen to those who once read this bike, it’s enough to put up a simple experiment yourself with a real average record width. The only limitation here is the possible reuse of the key, but if this is not allowed, as in this problem, then it should be done.

K
Konstantin Tsvetkov, 2016-05-04
@tsklab

For example, there is a table that stores a catalog of cars. Is it justified to make the state number the primary key
Justified, but Mr. the number does not define the entity (car). Possibly determines the VIN, but this must be determined by the subject area. Using a surrogate key (usually an auto-incrementing number, although it can be anything, for example, uniqueidentifier 0E984725-C51C-4BF4-9960-E1C80E27ABA0) eliminates the need to look up (and understand its uniqueness) a natural key.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question