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.
Example:
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 (2,'Anup','Hyderabad','2019-01-05')
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:
- Not Null Constraint
- Check Constraint
- Default Constraint
- Unique Constraint
- Primary Key Constraint
- Foreign Key Constraint
No comments:
Post a Comment