Monday, October 01, 2012

Default Constraint in SQL-Server with example

Posted by Rahul Kharde at 11:57 PM

The DEFAULT constraint is used to insert a default value into a column. The default value will be added to all new records, if no other value is specified.

Example
The following SQL query creates a DEFAULT constraint on the "AddedDate" column when the "Emp" table is created

CREATE TABLE Emp
(
            EID INT PRIMARY KEY IDENTITY,
            ENAME VARCHAR(100),
            EAGE INT,
            AddedDate DateTime Default GetDate()
)

Insert Record into the Emp table , here we are not specified any value into the “AddedDate

INSERT INTO Emp(ENAME,EAGE)
VALUES('abc',24)

With Column value is specified

INSERT INTO Emp(ENAME,EAGE,AddedDate)
VALUES('abc',24,getdate())

In first Insert query Default current date are inserted and in second column manually we insert the current date.




If you enjoyed this post and wish to be informed whenever a new post is published, then make sure you subscribe to my regular Email Updates. Subscribe Now!


Kindly Bookmark and Share it:

YOUR ADSENSE CODE GOES HERE

0 comments:

Have any question? Feel Free To Post Below:

 

Popular Posts

Recent Comments

© 2011. All Rights Reserved | Help to understand .Net | Template by Blogger Widgets

Home | About | Top