简体   繁体   English

找到正确的主键

[英]Finding the right primary key

我决定将三个值的组合设置为数据库表的主键(复合键),大多数情况下前两列是唯一的,但在某些情况下,第三列的值在同一时间都相同问题是第三列是描述,它是用户可以输入的任何字符串。我知道不建议用户给定字符串作为主键,但是可以吗?将其作为组合键的一部分。我真的没有其他选择。

If at all possible, you should add a surrogate primary key, meaning a key that is unique and has no business meaning whatsoever. 如果有可能,您应该添加代理主键,这意味着唯一且没有业务意义的键。

Composite primary keys very often turn out to be less 'stable' than anticipated - but if you MUST use one, using one or more component columns over which the user has little or no control (like created_timestamp) would be preferable. 复合主键通常比预期的“稳定”要少-但是,如果您必须使用复合主键,则最好使用一个或多个用户几乎没有控制权的组件列(例如created_timestamp)。

Add a separate column called id and make it auto increment and use this column as a primary key . 添加一个名为id的单独列,并使其auto increment然后将该列用primary key

Hope this will solve your problem. 希望这能解决您的问题。

Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM