Top Menu

Search This Blog

SQL Constraints(UNIQUE)

The UNIQUE constraint uniquely identifies each record in a database table. The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns.

A PRIMARY KEY constriant automatically has a UNIQUE constriant defined on it.

You can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.

CREATE TABLE CANDIDATE
 (
   CandidateId int IDENTITY(1,1) PRIMARY KEY,
   CandidateNumber int NOT NULL UNIQUE,
   LastName varchar(50) NOT NULL,
   FirstName varchar(50) NOT NULL,
   PostCode int NULL,
   Address varchar(50) NULL,
   Phone varchar(50) NULL,
)

We see that the "CandidateNumber" is set to UNIQUE, meaning each customer must have a unique CandidateNumber









Set Unique in the MS-SQL Server Tools:

If you want to use the MS-SQL Server designer, right‐click on the column that you want to be UNIQUE and select “Indexes/Keys…”



















Then click “Add” and then set the “Is Unique” property to “Yes”





Post a Comment

Designed by OddThemes | Distributed By Gooyaabi Templates