Question

I'm trying to create a table with two columns comprising the primary key in MySQL, but I can't figure out the syntax. I understand single-column PKs, but the syntax isn't the same to create a primary key with two columns.

Was it helpful?

Solution

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

OTHER TIPS

Try:

create table .....


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

Example:

 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`)
 )

An example (from 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)
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top