[英]Does MySQL allow two primary keys on one table?
CREATE TABLE Orders
-> (
-> ID SMALLINT UNSIGNED NOT NULL,
-> ModelID SMALLINT UNSIGNED NOT NULL,
-> Descrip VARCHAR(40),
-> PRIMARY KEY (ID, ModelID)
-> );
Basically, this appears to me to be creating two primary key on one table. 基本上,对我来说,这似乎是在一个表上创建两个主键。 Is that correct? 那是对的吗?
I thought that we could create a number of unique keys in one table, but only one primary key. 我以为我们可以在一个表中创建多个唯一键,但是只能创建一个主键。
How is it that my system is allowing the creation of multiple primary keys? 我的系统如何允许创建多个主键?
Please advise: what are the rules governing this? 请告知:关于此的规则是什么?
您的系统不允许使用多个主键-它基于2列(ID, ModelID)
创建密钥
You are making 1 primary key. 您正在制作1个主键。 But that key is a combination of 2 values. 但是那个关键是两个值的组合。
Which is not a wrong thing to do. 这不是错的事。 But in your case it does look wrong. 但是在您的情况下,它看起来确实不对。
You seem to have a primary key named ID
and a foreign key named ModelID
. 您似乎有一个名为ID
的主键和一个名为ModelID
的外键。 You should probable have an index on the ModelID
, and a primary key constraint on the ID
您应该在ModelID
上有一个索引,并在ID
上有一个主键约束
Think of it like it suggest, a 'KEY'. 像它建议的那样思考它,一个“关键”。 So the key would be all of the columns specified. 因此,键将是所有指定的列。 In your case you can have multiple rows with the same 'ID' and multiple rows with the same 'ModelID' but there shall never be two rows that have the same 'ID' AND 'ModelID'. 在您的情况下,可以有多个具有相同“ ID”的行和多个具有相同“ ModelID”的行,但决不能有两行具有相同的“ ID”和“ ModelID”。
So in this case it is not saying that the column 'ID' must be unique nor is it saying that 'ModelID' must be unique, but only the combination. 因此,在这种情况下,并不是说“ ID”列必须是唯一的,也不是说“ ModelID”必须是唯一的,而不仅仅是组合。
You can have one primary key (thats why it is called the primary key) 您可以有一个主键(这就是为什么它被称为主键)
You can have multiple UNIQUE
keys if you like. 如果愿意,可以有多个UNIQUE
键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.