Pregunta

Estoy intentando crear una tabla con dos columnas que comprenden la clave principal en MySQL, pero no puedo entender la sintaxis. Entiendo los PK de una sola columna, pero la sintaxis no es la misma para crear una clave principal con dos columnas.

¿Fue útil?

Solución

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

Otros consejos

Prueba:

create table .....


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

Ejemplo:

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

Un ejemplo (de 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)
);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top