Command for adding a default constraint

前端 未结 1 931
独厮守ぢ
独厮守ぢ 2020-12-15 02:28

There appears to be at least two ways to add a default constraint using straight T-SQL. Am I correct that the only difference between the two below is that the second method

相关标签:
1条回答
  • 2020-12-15 02:44

    Pretty much, yes for an ALTER TABLE

    You can add a columnn with default in one step for CREATE or ALTER too.

    ALTER TABLE foo ADD bar varchar(100) CONSTRAINT DF_Foo_Bar DEFAULT ('bicycle')
    ALTER TABLE foo ADD bar varchar(100) DEFAULT ('bicycle')
    

    As you noted, the system generates a name if one is not supplied. CONSTRAINT constraint_name is optional says MSDN. The same applies to any column or table CONSTRAINT

    Edit If the column was already created, and you only want to add the constraint use:

    ALTER TABLE TableName ADD CONSTRAINT DF_Foo_Bar DEFAULT 'bicycle' FOR FieldName;
    
    0 讨论(0)
提交回复
热议问题