sql server add column with default value

alter table AdventureWorks2019.sales.SalesOrderDetail
add   IsValid bit not null
constraint IsValid_Default_Constraint Default 1
with values;

This will make a sense when you want to delete logically instead of delete physically.

For example physically deletion.

delete from tableName where id=conditionValue;

While the logically deletion will be more elegant and easy to roll back or restore/undo.

update tableName set IsValid=0 where id=conditionValue

and when you retrieve valid result just add where condition to filter as below

select * from tableName where IsValid=1;

原文地址:https://www.cnblogs.com/Fred1987/p/14083297.html