Add Unique constaints to cover two columns : Unique « Table « MySQL Tutorial






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

mysql>
mysql> desc myTable;
+-------------+----------------------+------+-----+---------+-------+
| Field       | Type                 | Null | Key | Default | Extra |
+-------------+----------------------+------+-----+---------+-------+
| OrderID     | smallint(5) unsigned | NO   | PRI |         |       |
| ModelID     | smallint(5) unsigned | NO   |     |         |       |
| 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)








4.17.Unique
4.17.1.Unique Keys: A unique index allows only unique values in the column.
4.17.2.You can create more than one unique key.
4.17.3.Add Unique constaints to cover two columns
4.17.4.ALTER TABLE table_name ADD UNIQUE index_name (column_name[,...]);