Why does SQL Server keep creating a DF constraint?

前端 未结 3 1751
离开以前
离开以前 2021-01-31 14:31

I\'m trying to create upgrade and backout scripts in SQL. The upgrade script adds a column like so:

IF NOT EXISTS (SELECT * FROM sys.columns WHERE Name = N\'Col         


        
3条回答
  •  面向向阳花
    2021-01-31 14:57

    This is the default constraint that is added because of the DEFAULT(0) in your newly added column.

    You can name this yourself so it has a known fixed name rather than relying on the auto name generation.

    ALTER TABLE TableName
        ADD ColumnName bit NOT NULL CONSTRAINT DF_Some_Fixed_Name DEFAULT(0) 
    

    Then to remove the column and constraint together

    ALTER TABLE dbo.TableName
    DROP CONSTRAINT DF_Some_Fixed_Name, COLUMN ColumnName
    

提交回复
热议问题