SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Other SQL Server 2008 Topics
 Sql 2008 Default values
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

~AsEdu~
Starting Member

3 Posts

Posted - 09/29/2012 :  02:44:15  Show Profile  Reply with Quote
Can someone please tell me , what is the use of DEFAULT constraint in sql?
i know that it is used to insert a default value into a column.
But i have to give a value when inserting values into the columns even though i give a default value when creating that particular columns.


pls help..

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 09/29/2012 :  07:57:20  Show Profile  Reply with Quote
Your understanding of default value is correct; Google for it if you are interested in all the gory details.

Whether you have to give a value or not depends on what your insert statement looks like. For example, if you don't explicitly list the columns in the insert part of the insert statement, you will need to provide values for even columns that have default values - see this example:
CREATE TABLE #tmp (id1 INT, id2 INT DEFAULT 0);

INSERT INTO #tmp (id1) VALUES (1); -- works
INSERT INTO #tmp VALUES (2); -- does not work

DROP TABLE #tmp
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2210 Posts

Posted - 09/29/2012 :  08:16:36  Show Profile  Reply with Quote
Hi,

CREATE TABLE #table (col1 INT, col2 INT DEFAULT 0);

INSERT INTO #table VALUES( 1, DEFAULT);
INSERT INTO #table VALUES( 2, 123 ); 
INSERT INTO #table(col1) VALUES(3); 

SELECT * FROM #table
DROP TABLE #table

OUTPUT:

col1        col2
----------- -----------
1           0
2           123
3           0


quote:
Originally posted by ~AsEdu~

Can someone please tell me , what is the use of DEFAULT constraint in sql?
i know that it is used to insert a default value into a column.
But i have to give a value when inserting values into the columns even though i give a default value when creating that particular columns.


pls help..



--
Chandu
Go to Top of Page

~AsEdu~
Starting Member

3 Posts

Posted - 09/29/2012 :  10:40:20  Show Profile  Reply with Quote
quote:
Originally posted by sunitabeck

Your understanding of default value is correct; Google for it if you are interested in all the gory details.

Whether you have to give a value or not depends on what your insert statement looks like. For example, if you don't explicitly list the columns in the insert part of the insert statement, you will need to provide values for even columns that have default values - see this example:
CREATE TABLE #tmp (id1 INT, id2 INT DEFAULT 0);

INSERT INTO #tmp (id1) VALUES (1); -- works
INSERT INTO #tmp VALUES (2); -- does not work

DROP TABLE #tmp




thank you
Go to Top of Page

~AsEdu~
Starting Member

3 Posts

Posted - 09/29/2012 :  10:44:06  Show Profile  Reply with Quote
quote:
Originally posted by bandi

Hi,

CREATE TABLE #table (col1 INT, col2 INT DEFAULT 0);

INSERT INTO #table VALUES( 1, DEFAULT);
INSERT INTO #table VALUES( 2, 123 ); 
INSERT INTO #table(col1) VALUES(3); 

SELECT * FROM #table
DROP TABLE #table

OUTPUT:

col1        col2
----------- -----------
1           0
2           123
3           0




got it clearly
thank you.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000