如何使用SQL語句修改字段默認值
在SQL數據庫中,如果要對字段默認值進行修改,應該怎么做呢?下面就將為您詳解使用SQL語句修改字段默認值的方法,供您參考。
SQL語句修改字段默認值
alter table 表名 drop constraint 約束名字
說明:刪除表的字段的原有約束
alter table 表名 add constraint 約束名字 DEFAULT 默認值 for 字段名稱
說明:添加一個表的字段的約束并指定默認值
go
例:
alter table T_ping drop constraint DF_T_ping_p_c
alter table T_ping add constraint DF_T_ping_p_c DEFAULT ((2)) for p_c
go
alter table with check T_ping add constraint DF_T_ping_p_c DEFAULT ((2)) for p_c
alter table with nocheck T_ping add constraint DF_T_ping_p_c DEFAULT ((2)) for p_c
兩者的區別是If you do not want to verify new CHECK or FOREIGN KEY constraints against existing data, use WITH NOCHECK. This is not recommended except in rare cases. The new constraint will be evaluated in all future updates.
對于要建立約束的兩個表,如果其中的一個已有數據,把“在創建時檢查現有數據”選項設置為“是”將告訴SQL SERVER:當開始具體創建約束時,要對表中現有的數據進行檢查。如果現有數據符合約束的定義,則約束被成功加入到表中,然而,如果有任何數據不能通過約束驗證,則不會把約束應用到數據庫中。
【編輯推薦】