Thursday 3 May 2012

UNIQUE Constraint in Sql server

The UNIQUE constraint uniquely identifies each record in a database table.
UNIQUE must contain unique values.

For example, in the following CREATE TABLE statement,
CREATE TABLE CustomerDetails
(

CusID integer NOT NULL PRIMARY KEY,
FirstName varchar (30) NOT NULL UNIQUE,
LastName varchar(30)
);
column " FirstName " has a unique constraint, and hence we cannot include duplicate values

cusID
FirstName
LastName
1
Vinoth
Kumar
2
Sajil
Poduval
3
Viji
RajaRajan

Executing the following SQL statement,

INSERT INTO Customer values ('4','Sajil', Lijahs);

Will  result in an error because 'sajil' already exists in the FirstName column, thus trying to insert another row with that value UNIQUE constraint.

Please note that a column that is specified as a primary key must also be unique. At the same time, a column that's unique may or may not be a primary key. In addition, multiple UNIQUE constraints can be defined on a table.we can use in mysql too..

No comments:

Post a Comment