SQL UNIQUE Key Constraint :
1 . At The Time Of Table Creation :
UNIQUE Key Constraint is used to identify every record uniquely. Multiple Unique Key can be in a sigle table.
SQL – UNIQUE Key Constraint Syntax Example :
Example
CREATE TABLE UserProfile (
ID int(11) NOT NULL,
Name varchar(110) NOT NULL,
Email varchar(100) NOT NULL,
CONSTRAINT USER_EMAIL_UNIQUE_CONSTRAINT UNIQUE (Email)
) ;
ID int(11) NOT NULL,
Name varchar(110) NOT NULL,
Email varchar(100) NOT NULL,
CONSTRAINT USER_EMAIL_UNIQUE_CONSTRAINT UNIQUE (Email)
) ;
Where USER_EMAIL is Constraint’s name you can choose constraint name as per you understanding.
2 .In Existing Table :
Example
ALTER TABLE UserProfile
ADD CONSTRAINT USER_EMAIL_UNIQUE_CONSTRAINT UNIQUE (Email);
ADD CONSTRAINT USER_EMAIL_UNIQUE_CONSTRAINT UNIQUE (Email);
Drop UNIQUE Key Constraint :
Example
ALTER TABLE UserProfile
DROP CONSTRAINT USER_EMAIL_UNIQUE_CONSTRAINT;
DROP CONSTRAINT USER_EMAIL_UNIQUE_CONSTRAINT;
Note : In Mysql You have to Use Following Query to Drop the Constraint :
Example
ALTER TABLE UserProfile
DROP INDEX USER_EMAIL_UNIQUE_CONSTRAINT;
DROP INDEX USER_EMAIL_UNIQUE_CONSTRAINT;