DevonDahon
DevonDahon

Reputation: 8360

Should I better use VARCHAR(3) or BIGINT() for my primary key with PostgreSQL?

I'm seeding a big main table and generating smaller tables from some columns. These smaller tables are linked to the big one with foreign keys.

The small tables have a 2~3 characters unique code for each entry, so using an bigint id to store them seems superfluous to me. Also, I don't need auto-incrementation.

In such case, should I set the code of type varchar(3) as my primary key ? Or is bigint always better for performances ?

Upvotes: 0

Views: 474

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 247625

Performance won't matter much in this case, and a few bytes of storage more or less won't either.

You should choose the primary key that is most practical. If there is a natural three-character unique identifier, and you are sure that the identifier will never change for an existing table row, then there is no problem with using varchar(3) as a primary key for a table.

Note that there are people who think that you should always use a numeric artificial primary key. These people will probably disagree, and I am not willing to start a holy war here. The only convincing argument for this stance that I have heard is that there are ORMs and other abstraction layers that cannot work with anything but a numerical primary key. But if you are not hobbled by such a tool, you need not worry.

Upvotes: 2

Related Questions