SQLServer禁用、启用外键约束
2024-10-15 05:52:05
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
---启用or禁用指定表所有外键约束 alter table PUB_STRU NOCHECK constraint all ; alter table PUB_STRU CHECK constraint all ; ---生成启用or禁用指定表外键约束的sql select 'ALTER TABLE ' + b. name + ' NOCHECK CONSTRAINT ' + a. name + ';' from sysobjects a ,sysobjects b where a.xtype = 'f' and a.parent_obj = b.id and b. name = '表名' ; select 'ALTER TABLE ' + b. name + ' CHECK CONSTRAINT ' + a. name + ';' from sysobjects a ,sysobjects b where a.xtype = 'f' and a.parent_obj = b.id and b. name = '表名' ; --生成的sql如下 ALTER TABLE PUB_STRU NOCHECK CONSTRAINT PUBSTRU_FK1; ALTER TABLE PUB_STRU NOCHECK CONSTRAINT PUBSTRU_FK2; ALTER TABLE PUB_STRU CHECK CONSTRAINT PUBSTRU_FK1; ALTER TABLE PUB_STRU CHECK CONSTRAINT PUBSTRU_FK2; --查看约束状态(查询字典表 sys.foreign_keys,该字典表开始出现于sqlserver2005及以上版本): select name , is_disabled from sys.foreign_keys order by name ; --其中:name : 外键约束名称 is_disabled : 是否已禁用 |
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
--删除外键 alter table AdItem drop constraint AdOrder_AdItem_FK1 --增加外键 alter table AdItem add constraint AdOrder_AdItem_FK1 foreign key (AI_nOrderNo) references AdOrder(AO_nOrderNo) --单个表的一个外键 alter table Student nocheck constraint FK__Student__SchoolN__4222D4EF alter table Student check constraint FK__Student__SchoolN__4222D4EF --单个表的所有外键 alter table Student nocheck constraint all alter table Student check constraint all --某个数据库的所有表 EXEC sp_MSforeachtable @command1= 'alter table ? NOCHECK constraint all; EXEC sp_MSforeachtable @command1=' alter table ? CHECK constraint all ; |
参考:
Enable/Disable Constraint in SQLServer sp_MSforeachtable使用方法
--启用or禁用指定表所有外键约束 alter table PUB_STRU NOCHECK constraint all ; alter table PUB_STRU CHECK constraint all ; ---生成启用or禁用指定表外键约束的sql select 'ALTER TABLE ' + b. name + ' NOCHECK CONSTRAINT ' + a. name + ';' from sysobjects a ,sysobjects b where a.xtype = 'f' and a.parent_obj = b.id and b. name = '表名' ; select 'ALTER TABLE ' + b. name + ' CHECK CONSTRAINT ' + a. name + ';' from sysobjects a ,sysobjects b where a.xtype = 'f' and a.parent_obj = b.id and b. name = '表名' ; --生成的sql如下 ALTER TABLE PUB_STRU NOCHECK CONSTRAINT PUBSTRU_FK1; ALTER TABLE PUB_STRU NOCHECK CONSTRAINT PUBSTRU_FK2; ALTER TABLE PUB_STRU CHECK CONSTRAINT PUBSTRU_FK1; ALTER TABLE PUB_STRU CHECK CONSTRAINT PUBSTRU_FK2; --查看约束状态(查询字典表 sys.foreign_keys,该字典表开始出现于sqlserver2005及以上版本): select name , is_disabled from sys.foreign_keys order by name ; --其中:name : 外键约束名称 is_disabled : 是否已禁用 |
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
--删除外键 alter table AdItem drop constraint AdOrder_AdItem_FK1 --增加外键 alter table AdItem add constraint AdOrder_AdItem_FK1 foreign key (AI_nOrderNo) references AdOrder(AO_nOrderNo) --单个表的一个外键 alter table Student nocheck constraint FK__Student__SchoolN__4222D4EF alter table Student check constraint FK__Student__SchoolN__4222D4EF --单个表的所有外键 alter table Student nocheck constraint all alter table Student check constraint all --某个数据库的所有表 EXEC sp_MSforeachtable @command1= 'alter table ? NOCHECK constraint all; EXEC sp_MSforeachtable @command1=' alter table ? CHECK constraint all ; |
参考:
Enable/Disable Constraint in SQLServer sp_MSforeachtable使用方法
最新文章
- knockoutjs如何动态加载外部的file作为component中的template数据源
- mysql更新密码
- spring-Formatter(格式化器)-validator(验证器)-错误信息定制
- Swift运算符
- (转)C#调用默认浏览器打开网页的几种方法
- 准备使用 Office 365 中国版--购买
- 什么是AIDL(转)
- CentOS RPM安装MySQL-5.6
- 【转发】查看Linux版本系统信息方法汇总
- Excel插件类库的设计思路
- HDU1003前导和
- Java中static的用法
- Codeforces Round #277 (Div. 2) A. Calculating Function 水题
- Linux文件保护禁止修改、删除、移动文件等,使用chattr +i保护
- C++中随机函数
- git本地仓库与github远程仓库链接协议问题
- 如何设置让网站禁止被爬虫收录?robots.txt
- android view控件的显示和隐藏动画效果
- 【RL-TCPnet网络教程】第13章 RL-TCPnet之TCP服务器
- SEO需要掌握的基础知识
热门文章
- ant-design里为了清空Modal中的值, modal 中值有缓存 ....
- 使用 npm 安装 Vue
- 使用PermissionsDispatcher轻松解决Android权限问题
- Monkey测试运用实例
- 2.MySQL 数据类型
- LeetCode5 最长回文子串
- Handlebars.js中集合(list)通过中括号的方式取值
- java.lang.UnsatisfiedLinkError: /usr/openv/java/jre/lib/amd64/libawt_xawt.so: libXtst.so.6: cannot open shared object file: No such file or directory
- ZT 获得/修改共享互斥量属性:pthread_mutexattr_t
- Ubuntu 12.04中MyEclipse 10.6+下载+安装+破解