简体   繁体   English

表中有多个主键

[英]Multiple primary keys in table

A friend of mine just sent me an image of his new api database design. 我的一个朋友刚给我发了一张他新的api数据库设计的图片。

When I saw it, I noticed that his user table had three primary ids. 当我看到它时,我注意到他的用户表有三个主要ID。

I actually thought this wouldn't be possible. 我实际上认为这是不可能的。

It got me thinking... Is it okay to do this? 它让我思考......这样做可以吗? As long as each column is unique? 只要每列都是唯一的?

I can't seem to find a reason not to do this, except the id is not primary if there are more than one. 我似乎无法找到不这样做的理由,除非id不是主要的,如果有多个。

Is this a bad database design? 这是一个糟糕的数据库设计吗? And why? 为什么?

There should be only one column(s) designated as the PRIMARY KEY per table and most DB's will disallow usage of multiple PRIMARY KEYS. 每个表应该只有一个列被指定为PRIMARY KEY,并且大多数DB将禁止使用多个PRIMARY KEYS。 Note that a PRIMARY KEY can span multiple columns. 请注意,PRIMARY KEY可以跨越多列。 Use UNIQUE for other column(s) that require unique values. 对于需要唯一值的其他列,请使用UNIQUE。 UNIQUE keys can also be used in foreign key relationships. UNIQUE键也可用于外键关系。

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

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