[英]Is it a good practice to use surrogate primary key even if the table contains only one column and that itself contains the unique values?
I am having a gender
table as: gender_id | gender_label
我的
gender
表为: gender_id | gender_label
gender_id | gender_label
Will it be a bad practice if I remove the gender_id
column, as gender_label
is also unique and can be used as a primary key? 如果我删除了
gender_id
列,这是一个不好的做法,因为gender_label
也是唯一的并且可以用作主键?
I know surrogate keys should be used wherever possible as they are efficient and fast to search. 我知道应该尽可能使用代理键,因为它们高效且快速地搜索。 But will it make any difference for above scenario?
但这对上述情况有什么影响吗? How?
怎么样?
I have written hundreds of tables. 我写了几百张桌子。 2/3 of them have a "natural" key;
其中2/3具有“自然”键; 1/3 have a "surrogate key".
1/3有一个“代理钥匙”。
From that, I deduce that the answer to your question is "it depends". 由此,我推断出您问题的答案是“取决于”。
OK, so what does it depend on? 好的,那取决于什么?
UNIQUE
? UNIQUE
钥匙? If so, probably you should use it. country_code
taken from an international standard is, in my opinion, better than a 4-byte INT
. country_code
优于4字节的INT
。 In your simple case, is gender_id
strings like M/F/etc? 在您的简单情况下,是否像M / F / etc这样的
gender_id
字符串? That's what I would use. 那就是我会用的。 Actually, I might not have the table at all;
实际上,我可能根本没有桌子。 instead have
gender
in other tables as ENUM('unknown', 'male', 'female', 'other')
而是在其他表格中使用
gender
作为ENUM('unknown', 'male', 'female', 'other')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.