39

I have a table created like this originally:

CREATE TABLE dbo.XX (
YY int DEFAULT(NULL)
)

If you do this, and then script the table later on, you will get the following code, plus a randomly named default constraint:

CREATE TABLE [dbo].XX([YY] [int] NULL) 
GO
ALTER TABLE [dbo].XX ADD  DEFAULT (NULL) FOR [YY]
GO

Now is there any real point in specifying DEFAULT (NULL) on a column instead of just leaving it as [YY] [int] NULL in the first place?

4
  • 1
    No point in any database I'm familiar with. Commented Sep 23, 2011 at 18:58
  • 23
    On second thought, although it doesn't change the way the data will be added, I guess it could be considered an expression of design intent ("I intend for this column to be added as NULL, I didn't forget to set a default") which makes the design of the database clearer to the next person to come along. Commented Sep 23, 2011 at 19:02
  • Next (related) question: is there any pint in specifying NULL on a column instead of just leaving it as [yy] [INTEGER] in the first place? Commented Sep 26, 2011 at 13:58
  • 3
    Yes, there is a point in specifying NULL after the data type for nullable columns. The server or database may have different defaults set re: NULL than what you've assumed. Also it just makes it more clear what your intention was. Commented Oct 25, 2011 at 22:28

2 Answers 2

35

There is no need to add a DEFAULT(NULL) to nullable columns.

If data is not supplied to such columns, they will have a NULL.

The only benefit I see is the one Larry Lustig has posted in his comment to the question - it documents the fact that you have not forgotten to add a default to the nullable column.

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

1 Comment

Default constraint needs to be dropped first before the column is dropped
8

None that I can think of.

From a programming point of view NULL is the implicit default anyway without needing to (implicitly) create an actual default constraint in sys.objects.

Both of the following will end up inserting NULL without the default constraint.

INSERT INTO [dbo].XX([YY])  VALUES (DEFAULT)

INSERT INTO [dbo].XX([YY])  DEFAULT VALUES 

I don't subscribe to the view in the comments that it documents you didn't "forget" to add a default constraint. Nullable columns rarely have default constraints in my experience so this would just add a load of unneeded clutter.

The more important thing to include is the column nullability - as below - so it is clear to future readers that the column is nullable and so it doesn't depend on ANSI_NULL_DFLT session options what you actually end up with.

CREATE TABLE dbo.XX (
YY int NULL
)

Comments

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.