SQL UNIQUE Constraint

1.The UNIQUE constraint uniquely identifies each record in a database table.

2.The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns.

3.A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.

Note you can have many UNIQUE constraints per table but only one PRIMARY KEY constraint per table.

CREATE TABLE Persons

(

P_Id int UNIQUE,

LastName text(255) NOT NULL,

FirstName text(50),

Address text(50),

City text(50)

)

To allow naming of a UNIQUE constraint, and for

defining a UNIQUE constraint on multiple columns,

use the following SQL syntax:

CREATE TABLE Persons

(

P_Id int NOT NULL,

LastName text(50) NOT NULL, 

FirstName text(50),

Address text(50),

City text(50),

CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName)

)

Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post