如何使用SQL語句修改字段默認值
在SQL數(shù)據(jù)庫中,如果要對字段默認值進行修改,應(yīng)該怎么做呢?下面就將為您詳解使用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
兩者的區(qū)別是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.
對于要建立約束的兩個表,如果其中的一個已有數(shù)據(jù),把“在創(chuàng)建時檢查現(xiàn)有數(shù)據(jù)”選項設(shè)置為“是”將告訴SQL SERVER:當(dāng)開始具體創(chuàng)建約束時,要對表中現(xiàn)有的數(shù)據(jù)進行檢查。如果現(xiàn)有數(shù)據(jù)符合約束的定義,則約束被成功加入到表中,然而,如果有任何數(shù)據(jù)不能通過約束驗證,則不會把約束應(yīng)用到數(shù)據(jù)庫中。
【編輯推薦】