Sunday, August 16, 2020

Default Constraint in SQL Server

 The Default constraint is used to provide default value for a column. Default value will be added to all new records if not values are provided.

You can get information about default constraints from sys.default_constraints catalog view.


Create table Order_Details


ID int Not NULL,

First_Name varchar(50) Not NULL,

City varchar(30),

OrderDate date Default GETDATE()


 insert into Order_Details values (1,'Bilash','Bangalore',Default)

insert into Order_Details values (2,'Anup','Hyderabad','2019-01-05')

 select * from Order_Details

First record is inserted the default date value.

Add and Drop the Default Constraint

Alter table Order_Details add constraint Order_Details_Default Default 'Delhi' for City

insert into Order_Details values (3,'Indy',Default,Default)

Alter table Order_Details drop constraint Order_Details_Default

Different types of SQL Server Constraints:

No comments:

Post a Comment