Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Other SQL Server 2008 Topics
 Sql 2008 Default values

Author  Topic 

~AsEdu~
Starting Member

3 Posts

Posted - 2012-09-29 : 02:44:15
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
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-09-29 : 07:57:20
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
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2012-09-29 : 08:16:36
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 - 2012-09-29 : 10:40:20
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 - 2012-09-29 : 10:44:06
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
   

- Advertisement -