SQL 表可以有多个带主键的列吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1750335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can SQL table have multiple columns with primary key?
提问by Gal
Can SQL table have multiple columns with primary key?
SQL 表可以有多个带主键的列吗?
回答by Tony Andrews
A table can have just one primary key constraint, but that may be comprised of several columns e.g.
一张表只能有一个主键约束,但它可能由几列组成,例如
create table my_table (col1 integer, col2 integer, col3 integer,
primary key (col1, col2, col3)
);
In addition to the primary key, a table may also have one or more UNIQUE constraints, e.g.
除了主键,一张表还可能有一个或多个 UNIQUE 约束,例如
create table my_table2 (col1 integer, col2 integer, col3 integer,
primary key (col1, col2),
unique (col2, col3)
);
回答by Stu
If you mean "can a primary key in SQL have multiple columns", the answer is yes.
如果您的意思是“ SQL 中的主键可以有多列”,那么答案是肯定的。
回答by Joseph
If you're asking if a table can have multiple columns as a primary key, then for MS SQL Server, the answer is yes, and it's called a composite (corrected) key.
如果你问一个表是否可以有多个列作为主键,那么对于 MS SQL Server,答案是肯定的,它被称为复合(更正)键。
回答by Erwin Smout
"Usually a composite key is a poor practice."
“通常复合键是一种糟糕的做法。”
Beware of the false prophets who try to sell you such crap.
当心那些试图向你兜售这些废话的假先知。
A key is a key is a key is a key. A key ISa set of attributes. Nothing more and nothing less. The cardinality of that set can be 1, or it can be >1, and it can EVEN BE ZERO ! And a key corresponds, one-on-one, to some uniqueness constraint.
钥匙是钥匙是钥匙是钥匙。键是一组属性。不多也不少。该集合的基数可以是 1,也可以是 >1,甚至可以为零!一个键一对一地对应于某个唯一性约束。
The relational model HAS NO PRESCRIPTION WHAT SO EVER that a key/uniqueness constraint can only involve a single attribute.
关系模型从来没有规定键/唯一性约束只能涉及单个属性。
Moreover, nor does the relational model have any prosription what so ever against there being more than one key, and it is EVEN a fact that relational theory has ditched the concept (for decades already) of "primary key" (implying that such a "primary" key would in any sense be "more of a key" than are the others), because completely unnecessary and irrelevant. As far as the uniqueness the keys imply is concerned, ALL KEYS ARE EQUAL (and it is NOT the case the one particular key is more equal than the others).
此外,关系模型也没有任何反对存在多个键的限制,甚至事实是关系理论已经抛弃了“主键”的概念(已经有几十年了)(暗示这样一个“主”键在任何意义上都比其他键“更像一个键”),因为完全没有必要和不相关。就键所暗示的唯一性而言,所有键都是平等的(并不是一个特定键比其他键更平等的情况)。
回答by HLGEM
Usually a composite key is a poor practice. It will cause things to be slower when you need to join to it. It is also harder when you need to update one or more of the fields in 27 child tables. A better practice is a surrogate key and a unique index on the fields that would normally make up the composite key. Then you have the speed of the integer join and the the unique attribute is maintained and when the key value changes (as it often does in a composite key), then you only have to change one table instead of all the child tables.
通常复合键是一种糟糕的做法。当你需要加入它时,它会导致事情变慢。当您需要更新 27 个子表中的一个或多个字段时,这也更加困难。更好的做法是在通常构成复合键的字段上使用代理键和唯一索引。然后您拥有整数连接的速度并保持唯一属性,并且当键值更改时(就像在复合键中经常发生的那样),那么您只需更改一个表而不是所有子表。
There is one place where I will use a composite key though and that isa mapping table that is used to create the realtionships for a many-to-many relationship. In this case you typically only have two columns and both are usually integers which normally do not change. Then I will usually use a composite key as this particular case does not have the disadvatages a composite has in a normal table.
有一个地方我将使用复合键,那是一个映射表,用于创建多对多关系的关系。在这种情况下,您通常只有两列,并且通常都是整数,通常不会改变。然后我通常会使用复合键,因为这种特殊情况没有复合键在普通表中的缺点。