SQL Server 2019 NULL default constraint for nullable column

Johannes Maly 101 Reputation points
2024-08-20T05:37:29.4566667+00:00

I have to take care of a SQL Server 2019 database. What I found is that a lot of the exisiting tables have nullable columns and even though an additional NULL default constraint.

Here an example:

CREATE TABLE [dbo].[Customer](

[ID] [INT] IDENTITY(1,1) NOT NULL,

...

**[Birthday] [DATETIME] NULL**,

...

)

GO

ALTER TABLE [dbo].[Customer] ADD DEFAULT (null) FOR [Birthday]

GO

For me this default constraint doesn't make sense. But what I want to know is if it is harming performance - means does SQL Server check it even tough it is some sort of obsolete?

Thanks for your help in advance

Best Regards

Johannes

SQL Server | Other
0 comments No comments
{count} votes

Accepted answer
  1. LiHongMSFT-4306 31,566 Reputation points
    2024-08-20T06:16:44.16+00:00

    Hi @Johannes Maly

    For me this default constraint doesn't make sense.

    Yes, it makes no difference. If a column is nullable and you do not provide a value for it, it will default to NULL.

    But what I want to know is if it is harming performance - means does SQL Server check it even tough it is some sort of obsolete?

    The default constraint itself will not harm performance. But the NULL values can affect performance.

    For example, SQL queries must handle NULL values with special logic when doing comparisons.

    Best regards,

    Cosmog


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".


1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2024-08-20T22:20:01.6133333+00:00

    the column default value defines what value to use on an insert if the column is not specified in the insert statement. if not specified, the default value is null, so your schema is just formally defining the default value as null, instead of using the inferred value.

    it is similar to specifying the default value for a variable to be null:

    declare @v1 int
    declare @v2 int = null
    
    select @v1,@v2
    > null, null
    

    this should have no impact on performance.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.