Add unique constraint to a table using the alter table command : Alter Column « Table « MySQL Tutorial






mysql>
mysql> CREATE TABLE myTable
    -> (
    ->    OrderID SMALLINT UNSIGNED NOT NULL PRIMARY KEY,
    ->    ModelID SMALLINT UNSIGNED NOT NULL
    -> );
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   |     |         |       |
+---------+----------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql>
mysql> ALTER TABLE myTable
    -> ADD UNIQUE (OrderID, ModelID);
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

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

mysql>








4.10.Alter Column
4.10.1.Changing a Column Name
4.10.2.Changing a Column Type
4.10.3.Add unique constraint to a table using the alter table command