views:

423

answers:

4

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.

+1  A: 

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`)
 )
Galwegian
There's only one primary key in this example.
Christian Lescuyer
+6  A: 
CREATE TABLE table_name 
(
    c1 INT NOT NULL,
    c2 INT NOT NULL,
    PRIMARY KEY (c1, c2)
)
Neil Williams
+3  A: 

Try:

create table .....


primary key (`id1`, `id2`)
)
itsmatt
+1  A: 

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)
);
Christian Lescuyer