SQL DEFAULT Constraint

Default Constraint

SQL DEFAULT constraint is used to specify and inserts the default value in a column when there is no value specified to insert for a new record.

Below query creates a table named ‘Students’ where ‘Country’ column is set to default constraint.

So at the time of inserting records if the country is not mentioned then default country as ‘INDIA’ will be inserted to that column.

CREATE TABLE Students
(
 RollNumber int NOT NULL,
 Title varchar(8),
 FirstName varchar(50) NOT NULL,
 MiddleName varchar(50),
 LastName varchar(50),
 Country varchar(50) DEFAULT 'INDIA'
)

So, now we will try to insert record into Students table without mentioning Country column.

INSERT INTO Students (RollNumber, Title, FirstName, MiddleName, LastName)
VALUES ( 1000, 'Dr.', 'Mike', 'F', 'Ching')

DefaultConstraint

You can see in the results, Country column is filled with default name as INDIA. In this way during development of your applications there may be business requirements where if user is not filling particular values then you can use this constraint to insert default values automatically.

Reference: Manzoor Siddiqui [www.SQLServerLog.com]

CLICK HERE to watch live practical.

You may also like...

Leave a Reply

Your email address will not be published.