Sunday, 24 July 2016

Unique key and default constraint

We use UNIQUE constraint to enforce uniqueness of a column.

Column shouldn't allow any duplicate values.

 

Create table

 

create table Employee
(
EmployeeId int,
Username varchar(100)
)

 

Alter table to add unique key

 

Alter Table Employee
Add Constraint uk_uname Unique(Username)

 

Alter table to drop unique key

 

Alter Table Employee

Drop COnstraint uk_uname


The DEFAULT constraint is used to insert a default value into a column.

The default value will be added to all new records, if no other value is specified.

create table Employee
(
EmployeeId int,
Name varchar(50),
age int,
salary money
)

ALTER TABLE Employee
ADD CONSTRAINT df_emp_sal default 0.0


ALTER TABLE Employee
drop CONSTRAINT df_emp_sal



No comments:

Post a Comment