Friday, February 24, 2012
AllowNulls overrides default?
a default of getdate(), will this column value always be NULL if nothing is
inserted into it upon record insertion of other columns?
It seems to be. Must I delete that column if I want to not allow NULLs?
Thanks,
BrettAllowing null will not override default. See below:
CREATE TABLE #t (c1 int, c2 datetime null default getdate())
INSERT INTO #t(c1, c2) values(1, null)
INSERT INTO #t(c1) values(1)
INSERT INTO #t(c1, c2) values(1, DEFAULT)
SELECT * FROM #t
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"Brett" <no@.spam.net> wrote in message news:%23fgmZWIKFHA.2880@.TK2MSFTNGP09.phx.gbl...[colo
r=darkred]
> If I have a table column type smalldate checked to allow NULLs and also ha
ve a default of
> getdate(), will this column value always be NULL if nothing is inserted in
to it upon record
> insertion of other columns?
> It seems to be. Must I delete that column if I want to not allow NULLs?
> Thanks,
> Brett
>[/color]|||Well, I always get NULLs in that column rather than the default.
Brett
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:e6Bl3cIKFHA.3500@.TK2MSFTNGP14.phx.gbl...
> Allowing null will not override default. See below:
> CREATE TABLE #t (c1 int, c2 datetime null default getdate())
> INSERT INTO #t(c1, c2) values(1, null)
> INSERT INTO #t(c1) values(1)
> INSERT INTO #t(c1, c2) values(1, DEFAULT)
> SELECT * FROM #t
>
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> http://www.sqlug.se/
>
> "Brett" <no@.spam.net> wrote in message
> news:%23fgmZWIKFHA.2880@.TK2MSFTNGP09.phx.gbl...
>|||Did you run my script?
What application are you using?
Did you Profiler trace the application to see the INSERT statement?
My guess is that the applications specifies NULL for the column in the INSER
T statement.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"Brett" <no@.spam.net> wrote in message news:uo1DmpIKFHA.3788@.tk2msftngp13.phx.gbl...darkred">
> Well, I always get NULLs in that column rather than the default.
> Brett
> "Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote i
n message
> news:e6Bl3cIKFHA.3500@.TK2MSFTNGP14.phx.gbl...
>|||Brett wrote:
> Well, I always get NULLs in that column rather than the default.
> Brett
>
Please post your complete insert statement.
David Gugick
Imceda Software
www.imceda.com|||"David Gugick" <davidg-nospam@.imceda.com> wrote in message
news:u5elsKKKFHA.2784@.TK2MSFTNGP09.phx.gbl...
> Brett wrote:
> Please post your complete insert statement.
> --
> David Gugick
> Imceda Software
> www.imceda.com
I think this is occuring because I added this column after the table was
created. I had to allow NULLs in order to add the column. Let me research
a little more and I will post my results.
Thanks,
Brett|||Brett wrote:
> "David Gugick" <davidg-nospam@.imceda.com> wrote in message
> news:u5elsKKKFHA.2784@.TK2MSFTNGP09.phx.gbl...
> I think this is occuring because I added this column after the table
> was created. I had to allow NULLs in order to add the column. Let
> me research a little more and I will post my results.
> Thanks,
> Brett
I just tested with an added column and I get the default value when
inserted. We'll await your post.
David Gugick
Imceda Software
www.imceda.com|||"David Gugick" <davidg-nospam@.imceda.com> wrote in message
news:ONOXbmKKFHA.3356@.TK2MSFTNGP12.phx.gbl...
> Brett wrote:
> I just tested with an added column and I get the default value when
> inserted. We'll await your post.
>
But if you create a table, add four records then add the new column with a
default and allow nulls, what values does it have? I tried this and the
results are as follows:
Adding new records to the table I mention in this post does get the default.
Those in the table before I added this new column have NULLs. I couldn't
add the new column without allowing NULLs because of the previous records.
Thanks,
Brett|||Brett wrote:
> But if you create a table, add four records then add the new column
> with a default and allow nulls, what values does it have? I tried
> this and the results are as follows:
> Adding new records to the table I mention in this post does get the
> default. Those in the table before I added this new column have
> NULLs. I couldn't add the new column without allowing NULLs because
> of the previous records.
Ok, I think what you are saying is that the existing rows that were in
the table before you added the new column do not pick up the default.
That's expected behavior. Defaults are only applied on inserts when the
column is left out of the insert. There is no way for SQL Server to know
whether those NULLs in the existing columns were intentionally put
there. You'll need to run an update on the table and change the values
you need changed to the default.
David Gugick
Imceda Software
www.imceda.com
Thursday, February 16, 2012
Allow NULL or Define DEFAULT Value
a value on each row that is inserted.
What issues determine whether to allow a NULL value to be inserted for
that column or define a default value to be used?
I want to think through the repercussions of this decision before I get
into production.binder wrote:
Quote:
Originally Posted by
I am designing a new table with a few columns that may or may not have
a value on each row that is inserted.
>
What issues determine whether to allow a NULL value to be inserted for
that column or define a default value to be used?
>
I want to think through the repercussions of this decision before I get
into production.
Quote:
Originally Posted by
>From a programmatic standpoint, if I have a column that may or may not
have a value, is it better to insert a default value that indicates no
value was entered, such as 0 for a userid, or insert a NULL value?|||binder (rgondzur@.gmail.com) writes:
Quote:
Originally Posted by
binder wrote:
Quote:
Originally Posted by
>I am designing a new table with a few columns that may or may not have
>a value on each row that is inserted.
>>
>What issues determine whether to allow a NULL value to be inserted for
>that column or define a default value to be used?
>>
>I want to think through the repercussions of this decision before I get
>into production.
>
From a programmatic standpoint, if I have a column that may or may not
have a value, is it better to insert a default value that indicates no
value was entered, such as 0 for a userid, or insert a NULL value?
Programmatic? That's the wrong standpoint to look at it. You should look
at what it means.
Say that you have a column called whotoblameusrid, and no explicit value
is inserted. If you let it be NULL, means that in this case there is
no one to blame. (After all, anyone who is acquainted with Elvis Costello's
early material knows that Accidents can Happen.) If you use a default
value of 0 and 0 is Cain's user id, this mean that we Blame it on
Cain when no one else is at fault. (Costello fans know what I'm talking
about.)
But must 0 be a certain user? Yes, because good database design says
that a userid should be a foreign key to a table that defines users,
so there must be a user with id 0.
This also applies to non-key columns. Say a column that represents
an amount, for instance the cost for something. NULL would indicate
that the price is unknown (and we probably should not sell it). 0
means that the goods is for free.
That is not to say that default values should not be used. For instance
if you open a new account, it makes perfect sense to have default of
0 for the holdingsamt column, because you start with 0 and you may
not make a deposit immediately.
Simply, having NULL or a default value depends on what not entering a
value means. And by the way, a column could permit NULLs, but still have
a default value, because it's only exceptional that the value is not
known. For instance, a column "citizenof" could very well have the
default value of SE for a Swedish system, but the column must permit
NULL to account for stateless persons.
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Erland Sommarskog wrote:
Quote:
Originally Posted by
>
Programmatic? That's the wrong standpoint to look at it. You should look
at what it means.
>
Say that you have a column called whotoblameusrid, and no explicit value
is inserted. If you let it be NULL, means that in this case there is
no one to blame. (After all, anyone who is acquainted with Elvis Costello's
early material knows that Accidents can Happen.) If you use a default
value of 0 and 0 is Cain's user id, this mean that we Blame it on
Cain when no one else is at fault. (Costello fans know what I'm talking
about.)
>
But must 0 be a certain user? Yes, because good database design says
that a userid should be a foreign key to a table that defines users,
so there must be a user with id 0.
>
This also applies to non-key columns. Say a column that represents
an amount, for instance the cost for something. NULL would indicate
that the price is unknown (and we probably should not sell it). 0
means that the goods is for free.
>
That is not to say that default values should not be used. For instance
if you open a new account, it makes perfect sense to have default of
0 for the holdingsamt column, because you start with 0 and you may
not make a deposit immediately.
>
Simply, having NULL or a default value depends on what not entering a
value means. And by the way, a column could permit NULLs, but still have
a default value, because it's only exceptional that the value is not
known. For instance, a column "citizenof" could very well have the
default value of SE for a Swedish system, but the column must permit
NULL to account for stateless persons.
>
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
>
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx
Don't forget the third option: decompose the optional attribute(s) into
another table. To extend Erland's example, the WhoToBlameUsrID can go
in a table along with any other columns that relate only to Blame.
CREATE TABLE Who (WhoID INT NOT NULL PRIMARY KEY /* ... The required
attributes for the Who table ... */);
CREATE TABLE Blame (WhoID INT NOT NULL PRIMARY KEY REFERENCES Who
(WhoID), WhoToBlameUsrID INT NOT NULL /* ... The optional "Blame"
attributes ... */);
The principle at work here is that an entity is determined by its
unique set of attributes. If you analyse the functional dependencies
you find you have more entities than you currently have tables for -
that's what tells you to decompose.
In SQL Server this approach has one special advantage. SQL Server's
UNIQUE constraint treats nulls like values. The constraint doesn't
permit nulls to be duplicated, which means that unique constraints are
of limited use for optional attributes. So if an optional column may
need to be part of a unique constraint you should certainly consider
the decomposition approach.
--
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/...US,SQL.90).aspx
--