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使用方法

最新文章

  1. knockoutjs如何动态加载外部的file作为component中的template数据源
  2. mysql更新密码
  3. spring-Formatter(格式化器)-validator(验证器)-错误信息定制
  4. Swift运算符
  5. (转)C#调用默认浏览器打开网页的几种方法
  6. 准备使用 Office 365 中国版--购买
  7. 什么是AIDL(转)
  8. CentOS RPM安装MySQL-5.6
  9. 【转发】查看Linux版本系统信息方法汇总
  10. Excel插件类库的设计思路
  11. HDU1003前导和
  12. Java中static的用法
  13. Codeforces Round #277 (Div. 2) A. Calculating Function 水题
  14. Linux文件保护禁止修改、删除、移动文件等,使用chattr +i保护
  15. C++中随机函数
  16. git本地仓库与github远程仓库链接协议问题
  17. 如何设置让网站禁止被爬虫收录?robots.txt
  18. android view控件的显示和隐藏动画效果
  19. 【RL-TCPnet网络教程】第13章 RL-TCPnet之TCP服务器
  20. SEO需要掌握的基础知识

热门文章

  1. ant-design里为了清空Modal中的值, modal 中值有缓存 ....
  2. 使用 npm 安装 Vue
  3. 使用PermissionsDispatcher轻松解决Android权限问题
  4. Monkey测试运用实例
  5. 2.MySQL 数据类型
  6. LeetCode5 最长回文子串
  7. Handlebars.js中集合(list)通过中括号的方式取值
  8. 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
  9. ZT 获得/修改共享互斥量属性:pthread_mutexattr_t
  10. Ubuntu 12.04中MyEclipse 10.6+下载+安装+破解