Could somebody tell is it good idea use varchar as PK. I mean is it less efficient or equal to int/uuid? In example: car VIN I want to use it as PK but I'm not sure as good it will be indexed or work as FK or maybe there is some pitfalls.
CodePudding user response:
It depends on which kind of data you are going to store.
In some cases (I would say in most cases) it is better to use integer-based primary keys:
- for instance,
bigint
needs only 8 bytes,varchar
can require more space. For this reason, a varchar comparison is often more costly than a bigint comparison. - while joining tables it would be more efficient to join them using integer-based values rather that strings
- an integer-based key as a unique key is more appropriate for table relations. For instance, if you are going to store this primary key in another tables as a separate column. Again,
varchar
will require more space in other table too (see p.1).
This post on stackexchange compares non-integer types of primary keys on a particular example.