Friday, October 19, 2012

SQL DEFAULT Constraint

Posted by Rahul Kharde at 8:29 AM

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)

AddedDate will be insert the default value (means the Current Date) into the table 

Now with Column value is specified

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






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