
Check It Out!
/*Creating a sample table with Default constraint on it*/
Create Table Tbl_DefaultSample
(
ID Int Identity(1,1) Primary Key,
Name Varchar(100),
Gender Varchar(10) Default 'Male',
Age Int
)
Go
/*Using default value on "Gender" column*/
Insert Tbl_DefaultSample(Name,Age) Values('Aequea',25)
Go
/*
ID Name Gender Age
1 Aequea Male 25
*/
/*Explicitly inserting value on "Gender" column*/
Insert Tbl_DefaultSample(Name,Gender,Age) Values('Salino','Female',20)
Go
/*
ID Name Gender Age
1 Aequea Male 25
2 Salino Female 20
*//*DEFAULT constraint can not be changed. It has to be DROPPED and Recreated*/
SELECT Name FROM SYS.DEFAULT_CONSTRAINTS
WHERE parent_object_id = OBJECT_ID('Tbl_DefaultSample')
GO
/*Drop the Default constraint*/
ALTER TABLE Tbl_DefaultSample DROP CONSTRAINT DF__Tbl_Defau__Gende__14E61A24
GO
/*Creating Default constraint again with different value*/
ALTER TABLE Tbl_DefaultSample ADD CONSTRAINT DF__Tbl_Defau__Gende__14E61A24 DEFAULT 'Female' FOR Gender
GO
/*Using default value on "Gender" column*/
Insert Tbl_DefaultSample(Name,Age) Values('Setaceo',19)
Go
/*
ID Name Gender Age
1 Aequea Male 25
2 Salino Female 20
3 Setaceo Female 19
*/You got any idea ?
Cheers
www.SQLServerbuddy.blogspot.com
iLink Multitech Solutions