如何使用SQL語句修改字段默認(rèn)值
在SQL數(shù)據(jù)庫中,如果要對(duì)字段默認(rèn)值進(jìn)行修改,應(yīng)該怎么做呢?下面就將為您詳解使用SQL語句修改字段默認(rèn)值的方法,供您參考。
SQL語句修改字段默認(rèn)值
alter table 表名 drop constraint 約束名字
說明:刪除表的字段的原有約束
alter table 表名 add constraint 約束名字 DEFAULT 默認(rèn)值 for 字段名稱
說明:添加一個(gè)表的字段的約束并指定默認(rèn)值
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.
對(duì)于要建立約束的兩個(gè)表,如果其中的一個(gè)已有數(shù)據(jù),把“在創(chuàng)建時(shí)檢查現(xiàn)有數(shù)據(jù)”選項(xiàng)設(shè)置為“是”將告訴SQL SERVER:當(dāng)開始具體創(chuàng)建約束時(shí),要對(duì)表中現(xiàn)有的數(shù)據(jù)進(jìn)行檢查。如果現(xiàn)有數(shù)據(jù)符合約束的定義,則約束被成功加入到表中,然而,如果有任何數(shù)據(jù)不能通過約束驗(yàn)證,則不會(huì)把約束應(yīng)用到數(shù)據(jù)庫中。
【編輯推薦】