Default Constraint

Constraint are rules enforced on column.

1) We can set default value to column of the table.

2) while inserting record into the table column value left blank, then such value accept default value

if Default constraint is applied.

Default Constraint

No need to provide value for column CreatedDate (in below example) because it will take value from function getdate() that is current date and time.

if you are providing this value then it also fine.

1. while creating table.

Create Table Emp

(

EmpId Int Identity(1,1),

Name Varchar(50) Not Null,

Age Int,

CreatedDate DateTime Default getdate()

)


2. After table Creation -New Column 

Alter Table Emp 

Add City varchar(50) Default 'Delhi'


3. Add after table Creation -Existing Column 

Alter Table Emp 

Add Default 25 for Age

OR

Alter Table Emp 

Add Constraint DF_Age Default 25 for Age;


Drop Constraint

Alter Table Emp

Drop Constraint DF_Age


Comments