0

I created the following table

create table interiors
(id integer, name varchar(20), type varchar(20) default 'baby cot', price integer)

When I don't insert any value of 'type', by the definition of table, it should take the default value. But when I fetch all rows by 'select' query, it doesn't show default value.

Can anyone tell me that what is the problem?

Thanks & regards,
Pooja.

3
  • Actually I think I might have misread the question Can you confirm whether the issue is that you were expecting the default to apply to pre-existing rows or that it doesn't apply to newly inserted rows? If the latter can you show us the insert statement you are using? Are you explicitly inserting NULL for example? Commented Mar 1, 2011 at 12:25
  • see, when I m not inserting any value into 'type' column, then by the 'default' constraint, it should show default value. when i fetch all rows, 'type' shows no value. for the next, when i insert NULL(without quotation) in 'type' column then select query shows that 'NULL' word. i want the output that, when i m not inserting anything into 'type' column, it should show default type as 'baby cot'. Commented Mar 1, 2011 at 16:49
  • Why are you inserting NULL if you want it to have the default? You need to either do insert into interiors(id,name,price) values (1,'foo',100) OR insert into interiors(id,name,type,price) values (1,'foo',DEFAULT,100) inserting NULL isn't treated any differently then inserting any other explicit value. Commented Mar 1, 2011 at 17:01

2 Answers 2

1

Defaults apply to data that is inserted when the default is active not to select statements against the table.

Sign up to request clarification or add additional context in comments.

1 Comment

I am not understanding what you want to say exactly. Will you please explain more?
1

Not sure what the problem is for you but here is some hints for you what is going on.

This T-SQL

declare @T table (ID int, Name varchar(10) default 'DEF')

insert into @T (ID, Name) values (1, 'VAL')
insert into @T (ID, Name) values (2, null)
insert into @T (ID) values (3)
insert into @T (ID, Name) values (4, default)

select ID, Name
from @T

Have this result

ID          Name
----------- ----------
1           VAL
2           NULL
3           DEF
4           DEF

Default constraint is not used when you specify a value for a column as in row 1 and 2 but it is used when you omit the column from the insert or you specify default. It is also not used when you do a select.

If you want a default value instead of null on select you can use coalesce.

select ID, coalesce(Name, 'DEF') as Name
from @T

Result

ID          Name
----------- ----------
1           VAL
2           DEF
3           DEF
4           DEF

1 Comment

+1. You could also show insert into @T (ID, Name) values (4,default)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.