Use two columns as the combined primary key : PRIMARY KEY « Table « MySQL Tutorial






mysql>
mysql> CREATE TABLE myTable
    -> (
    ->    OrderID     SMALLINT UNSIGNED NOT NULL,
    ->    ModelID     SMALLINT UNSIGNED NOT NULL,
    ->    Description VARCHAR(40),
    ->    PRIMARY KEY (OrderID, ModelID)
    -> );
Query OK, 0 rows affected (0.03 sec)

mysql>
mysql> desc myTable;
+-------------+----------------------+------+-----+---------+-------+
| Field       | Type                 | Null | Key | Default | Extra |
+-------------+----------------------+------+-----+---------+-------+
| OrderID     | smallint(5) unsigned | NO   | PRI |         |       |
| ModelID     | smallint(5) unsigned | NO   | PRI |         |       |
| Description | varchar(40)          | YES  |     | NULL    |       |
+-------------+----------------------+------+-----+---------+-------+
3 rows in set (0.02 sec)

mysql>
mysql> drop table myTable;
Query OK, 0 rows affected (0.00 sec)

mysql>








4.15.PRIMARY KEY
4.15.1.Primary Keys
4.15.2.Create primary key in column definition
4.15.3.Create primary key after column definition
4.15.4.Multiple-Column Primary Keys
4.15.5.Auto_INCREMENT primary key
4.15.6.Use two columns as the combined primary key
4.15.7.Use partial indexing as the primary key
4.15.8.ALTER TABLE table_name ADD PRIMARY KEY index_name (column_name[,...]);
4.15.9.To drop a PRIMARY KEY
4.15.10.To remove a primary key
4.15.11.Alter table to drop primary key and foreign key
4.15.12.Add Index and primary key to a table in table creation command