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


MySQL TUTORIALS » Table » Alter Column »

 

Add unique constraint to a table using the alter table command


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

mysql>
mysql> desc myTable;
+---------+----------------------+------+-----+---------+-------+
| Field   | Type                 | Null | Key | Default | Extra |
+---------+----------------------+------+-----+---------+-------+
| OrderID | smallint(5unsigned | NO   | PRI |         |       |
| ModelID | smallint(5unsigned | NO   |     |         |       |
+---------+----------------------+------+-----+---------+-------+
rows in set (0.00 sec)

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

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

mysql>



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .

Follow Navioo On Twitter

MySQL TUTORIALS

 Navioo Table
» Alter Column