Alternate Keys In SQL Server 2008

In this article we will learn about Alternate Key in SQL Server.
  • 11572

Alternate keys is like primary key that can be a column or group of column of a table whose values are unique at all time. There can be multiple alternate keys in a table and can contain null value unless it is explicitly defined with a NOT NULL integrity rule.

UNIQUE keyword is used to define alternate key. It indicates that value must remail unique.

Create alternate key

create table DepartmentId(

DeptNo integer not null,

Emp_No integer not null unique,

Dep_Name varchar(10) not null,

primary key (DeptNo)

)

Output:

table.jpg

This code can also be written as follows.

create table DepartmentId(

DeptNo integer not null,

Emp_No integer not null,

Dep_Name varchar(10) not null,

primary key (DeptNo),

unique(Emp_No)

)

© 2020 DotNetHeaven. All rights reserved.