Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a reason to specify DEFAULT (NULL) on a nullable column?

Tags:

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?

like image 746
Rand.Function Avatar asked Sep 23 '11 18:09

Rand.Function


People also ask

Is default NULL necessary?

No, it isn't necessary because without adding DEFAULT NULL, it gives NULL value. For example, let's say you haven't added DEFAULT NULL and inserted a record with no value, then the result would display the NULL value as the inserted value.

Can a nullable column have a default value?

The Simple AnswerIf the column is nullable then it will create the column with a NULL value instead of the default value, however, if column is not nullable and there is a default value, SQL Server has to apply that value to column to avoid violating not null constraint.

What does default NULL mean?

If a data type specification includes no explicit DEFAULT value, MySQL determines the default value as follows: If the column can take NULL as a value, the column is defined with an explicit DEFAULT NULL clause. If the column cannot take NULL as a value, MySQL defines the column with no explicit DEFAULT clause.

What is the difference between not NULL and default NULL?

By default, a column can hold NULL values. The NOT NULL constraint enforces a column to NOT accept NULL values. This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.


1 Answers

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.

like image 92
Oded Avatar answered Sep 24 '22 16:09

Oded