It is always recommended to use int
type for id
fields in SQL tables - I find some similar answers here, here and here.
But in all these questions id
field is unique with many possible values like user_id
in users table of a blog. Should I still use int
for id
field that have limited range of values like user type, that can be 'admin', 'user', 'editor'?
This is not question about users table, it mentioned only for example, in real databases on my work I often find such fields with 3-20 possible values that change very rarely, but used in many queries, views etc so we have a lot of SQL code like.
SOME_FIELD in (1,7, 12)
SOME_FIELD != 2
We can also have a table with long names of such types to show in UI and foreign key to this table. It is not a natural key like USA citizen's social security number. There is usially a lot of data in one row with many fields like varchar(100)
. I think it will be better with
SOME_FIELD in ('Draft', 'Auto-Draft' , 'Trash')
SOME_FIELD != 'Published'
But it directly contradict to all answers from simular questions that I find here. I tried to examine some popular application - Wordpress database has varchar
fields for post_status or post_type
but int for user_status.
Should I still use int type for such fields? Or it is a question of personal taste?
To make question more clear: the biggest problem with varchar key, that was mentioned in answers to other questions is perfomance problems with joins and other operations. Will these problems decrease with limited number of varchar values, in one language, with small limited size like varchar(10) and values, that modified rarely and only by programmers, not by users, so answers from here or here or using IDENTITY/AUTO_INCREMENT are not relevant? Is there any research on that topic or experimental results from experienced developers?
It is not question just about MySQL or Wordpress, I also use MS SQL Server and data about other RDBMS may be interesting.