UNIQUE 唯一限制 (SQL UNIQUE Constraint)
UNIQUE 用來保證欄位在資料表中的唯一性,約束資料表中的欄位不能有重複的資料。
1 | CREATE TABLE customer ( |
替唯一鍵命名與多欄位的唯一限制
C_Id 及 Name 這兩個欄位共同唯一,CONSTRAINT 後面接著的即是此唯一鍵的名稱。
1 | CREATE TABLE `customers` ( |
更改資料表限制 ALTER TABLE
1 | ALTER TABLE `customers` ADD UNIQUE (C_Id); |

1 | ALTER TABLE `customers` ADD CONSTRAINT u_Customer_Id UNIQUE (C_Id, Name); |


移除資料表限制 ALTER TABLE…
刪除C_Id
1 | ALTER TABLE `customers` DROP INDEX C_Id; |
刪除u_Customer_Id;
1 | ALTER TABLE `customers` DROP INDEX u_Customer_Id; |