如何在MySQL中声明多列PK

时间:2008-10-03 15:52:44

标签: mysql sql database ddl

我正在尝试创建一个包含MySQL列中主键的两列的表,但我无法弄清楚语法。我理解单列PK,但是创建具有两列的主键的语法不同。

4 个答案:

答案 0 :(得分:7)

CREATE TABLE table_name 
(
    c1 INT NOT NULL,
    c2 INT NOT NULL,
    PRIMARY KEY (c1, c2)
)

答案 1 :(得分:3)

尝试:

create table .....


primary key (`id1`, `id2`)
)

答案 2 :(得分:1)

示例:

 CREATE TABLE `synthesis`.`INV_MasterItemList` (
   `MasterItemList_ID` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
   `Customer_ID` INTEGER UNSIGNED NOT NULL,
   `Model_ID` INTEGER UNSIGNED NOT NULL,
   `Serial` VARCHAR(45) NOT NULL,
   PRIMARY KEY (`MasterItemList_ID`),
   UNIQUE INDEX `INDEX_UNIQUE`(`Customer_ID`, `Model_ID`, `Serial`)
 )

答案 3 :(得分:1)

一个例子(来自osCommerce):

CREATE TABLE categories_description (
 categories_id int DEFAULT '0' NOT NULL,
 language_id int DEFAULT '1' NOT NULL,
 categories_name varchar(32) NOT NULL,
 PRIMARY KEY (categories_id, language_id),
 KEY idx_categories_name (categories_name)
);