Alter column, add default constraint

后端 未结 6 2100
臣服心动
臣服心动 2020-12-07 16:21

I have a table and one of the columns is \"Date\" of type datetime. We decided to add a default constraint to that column

Alter table TableName
alter column         


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-07 16:57

    Try this

    alter table TableName 
     add constraint df_ConstraintNAme 
     default getutcdate() for [Date]
    

    example

    create table bla (id int)
    
    alter table bla add constraint dt_bla default 1 for id
    
    
    
    insert bla default values
    
    select * from bla
    

    also make sure you name the default constraint..it will be a pain in the neck to drop it later because it will have one of those crazy system generated names...see also How To Name Default Constraints And How To Drop Default Constraint Without A Name In SQL Server

提交回复
热议问题