MySQL是否在一个表上允许两个主键? [英] Does MySQL allow two primary keys on one table?

查看:987
本文介绍了MySQL是否在一个表上允许两个主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

推荐答案

像它建议的那样,它是一个'KEY'.因此,键将是所有指定的列.在您的情况下,您可以有多行具有相同的"ID",多行具有相同的"ModelID",但是永远不会有两行具有相同的"ID"和"ModelID".

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"必须是唯一的,而是结合在一起.

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.

这篇关于MySQL是否在一个表上允许两个主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆