What is DEFAULT Constraint in SQL Server?

Introduction:

In this article i will explain what is DEFAULT Constraint in SQL Server.

Description:

In previous articles i explained what is Constraint, how many Constraints available in SQL Server, and what is NOT NULLCHECKUNIQUEPRIMARY KEY, and FOREIGN KEY Constraint in SQL Server. Now i will explain what is DEFAULT Constraint in SQL Server.

DEFAULT Constraint:

Defaults specify what values are used in a column if you do not specify a value for the column when you insert a row. Defaults can be anything that evaluates to a constant, such as a constant, built-in function, or mathematical expression. To apply defaults, create a default definition by using the DEFAULT keyword in CREATE TABLE.

Example:

Now i'm creating Emp table.

Create table Emp
(
    EmpNo int primary key identity(1,1),
    EmpName varchar(50),
    EmpSalary decimal,
    CreatedDate datetime default getdate(),
    Active bit default 1
)

Here you will see DEFAULT constraint applied on CreatedDate and Active columns. When you insert data into Emp table no need to insert data into CreatedDate and Active columns. By default getdate() datetime will be inserted into CreatedDate column and 1 means True will be inserted into Active column.

insert into Emp(EmpName, EmpSalary) values('AAA', 1000)
insert into Emp(EmpName, EmpSalary) values('BBB', 2000)
insert into Emp(EmpName, EmpSalary) values('CCC', 3000)

select * from Emp

Output:

Note: EmpNo is IDENTITY. So, no need to insert data into EmpNo column.