Sql Tutorial Class


Default Constraint in sql server

Default Constraint in sql server


DEFAULT Constraint is used to provide the default value on column.


SQL DEFAULT Constraint on CREATE RABLE




Create table Orders
(
OrderId varchar(10)NOT NULL,
ItemName varchar(20)NOT NULL,
Description Varchar(500)NOT NULL,
OrderDate  Date NOT NULL Constraint dfDate DEFAULT GETDATE(),
City varchar(30)NOT NULL Constraint dfCity DEFAULT 'NY'
)
Insert into Orders Values('001','Computer','Apple Computer',default,default)
Insert into Orders Values('002','Computer','Apple Computer','2020-03-06','CF')



ALTER / DROP DEFAULT Constraint in SQL

ALTER TABLE Orders DROP CONSTRAINT dfDate
ALTER TABLE Orders DROP CONSTRAINT dfCity


ALTER/ADD DEFAULT Constraint in SQL
ALTER TABLE Orders ADD CONSTRAINT dfDate DEFAULT GETDATE() FOR OrderDate
ALTER TABLE Orders ADD CONSTRAINT dfCity DEFAULT 'CF' FOR City





SQL DEFAULT Constraint example in video








SQL DEFAULT Constraint example in image
default constraint in sql




No comments:

Post a Comment