What is DEFAULT key constraint in SQL Server ?

DEFAULT constraint help to insert default value in table when user not provide any value at the time of data insertion.


1) Add DEFAULT key constraint on table by Query.
In below example we are creating new table of tblStudentDetails and create IsActive column as default key constraint.
We are inserting BIT value (1, 0) in IsActive column, 1 indicate active and 0 indicate deactivate student.

Example

CREATE TABLE tblStudentDetails
(
Id INT IDENTITY (1,1),
StudentName VARCHAR (50),
City VARCHAR (50),
IsActive BIT NOT NULL DEFAULT (1)
)

Note : In above example we insert not null and default key constraint on IsActive column.


2) Add DEFAULT key constraint on table using table design.

Step 1 :


default key constraint in sql server

Step 2 : Save table.


Let's try to insert some record in student details.

Example

INSERT INTO tblStudentDetails (StudentName, City)
VALUES     ('Vijay', 'Pune'),
('Ramesh', 'Goa')

Output

The INSERT statement conflicted with the CHECK constraint


Note : If you observe above insert statement then you can see that we not provide the value of IsActive column.


List the table after above insert statements.

default key constraint in sql server