SQLServer之修改CHECK约束
使用SSMS數(shù)據(jù)庫(kù)管理工具修改CHECK約束
1、打開(kāi)數(shù)據(jù)庫(kù),選擇數(shù)據(jù)表-》右鍵點(diǎn)擊-》選擇設(shè)計(jì)(或者展開(kāi)約束,選擇約束,右鍵點(diǎn)擊,選擇修改,后面步驟相同)。
2、選擇要修改的數(shù)據(jù)列-》右鍵點(diǎn)擊-》選擇CHECK約束。
3、在CHECK約束彈出框中-》選擇要修改的約束-》輸入約束表達(dá)式-》輸入約束名和約束描述-》選擇表設(shè)計(jì)器規(guī)則-》點(diǎn)擊關(guān)閉。
4、點(diǎn)擊保存按鈕(或者ctrl+s)-》刷新表查看修改結(jié)果。
使用T-SQL腳本修改CHECK約束
CHECK約束修改規(guī)則必須首先刪除現(xiàn)有的CHECK約束,然后使用新定義重新創(chuàng)建,才能使用Transact-SQL修改CHECK約束。
語(yǔ)法:
--修改check約束
use 數(shù)據(jù)庫(kù)名
go
--如果約束存在則先刪除
if exists(select * from sysobjects where name=約束名)
alter table 表名 drop constraint 約束名;
go
--添加約束
alter table 表名
--with check --該約束是否應(yīng)用于現(xiàn)有數(shù)據(jù),with check表示應(yīng)用于現(xiàn)有數(shù)據(jù),with nocheck表示不應(yīng)用于現(xiàn)有數(shù)據(jù)
add constraint 約束名
check
not for replication --當(dāng)復(fù)制代理在表中插入或更新數(shù)據(jù)時(shí),禁用該約束。
(約束表達(dá)式);
go
--向表中添加新數(shù)據(jù)或更新表中現(xiàn)有數(shù)據(jù)時(shí)是否禁用該約束。check表示校驗(yàn),nocheck表示不校驗(yàn)
--alter table 表名
--check
--constraint 表名;
--go
--添加check約束描述
execute sp_addextendedproperty N'MS_Description', N'約束描述', N'SCHEMA', N'dbo', N'TABLE', N'表名', N'CONSTRAINT', N'約束名';
go
示例:
--修改check約束
use testss
go
--如果約束存在則先刪除
if exists(select * from sysobjects where name='u_check2')
alter table test1 drop constraint u_check2;
go
--添加約束
alter table test1
--with check --該約束是否應(yīng)用于現(xiàn)有數(shù)據(jù),with check表示應(yīng)用于現(xiàn)有數(shù)據(jù),with nocheck表示不應(yīng)用于現(xiàn)有數(shù)據(jù)
add constraint u_check2
check
not for replication --當(dāng)復(fù)制代理在表中插入或更新數(shù)據(jù)時(shí),禁用該約束。
(height>=100 and height <=200);
go
--向表中添加新數(shù)據(jù)或更新表中現(xiàn)有數(shù)據(jù)時(shí)是否禁用該約束。check表示校驗(yàn),nocheck表示不校驗(yàn)
--alter table test1
--check
--constraint u_check2;
--go
--添加check約束描述
execute sp_addextendedproperty N'MS_Description', N'修改約束', N'SCHEMA', N'dbo', N'TABLE', N'test1', N'CONSTRAINT', N'u_check2';
go
CHECK約束修改優(yōu)缺點(diǎn)
優(yōu)點(diǎn):
1、修改數(shù)據(jù)庫(kù)CHECK約束可以保證數(shù)據(jù)的規(guī)范性和完整性。
缺點(diǎn):
1:修改約束的表設(shè)計(jì)器使用規(guī)則時(shí),可能會(huì)引起原有數(shù)據(jù)與約束的沖突。
總結(jié)
以上是生活随笔為你收集整理的SQLServer之修改CHECK约束的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Delphi 常用属性说明
- 下一篇: Unity3D普通开发人员,U3D主程分