mysql可以有两个主键么,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?

解决方案

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'.

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.