SQLServer禁用、启用外键约束
|
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 AdItemadd 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 AdItemadd 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使用方法
SQLServer禁用、启用外键约束的更多相关文章
- [转] SQL Server 批量 停用/启用 外键约束
本文转自:http://hi.baidu.com/wangzhiqing999/item/ca699308de4f1ff9a1103429 今天百度知道上面,看到这样一个要求: 现在有一个库,有很多张 ...
- mysql关闭/启用外键约束
1.有时为了导入数据方便,需要临时关闭外键约束mysql>SET FOREIGN_KEY_CHECKS=0; 2.打开外键约束mysql>SET FOREIGN_KEY_CHECKS=1; ...
- mssql禁用启用主键约束
EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL' --禁用 EXEC sp_msforeachtable 'ALTER TA ...
- SQL Server 临时禁用和启用所有外键约束(高版本向低版本迁移数据)
--获得禁用所有外键约束的语句 select 'ALTER TABLE [' + b.name + '] NOCHECK CONSTRAINT ' + a.name +';' as 禁用约束 from ...
- oracle 删除外键约束 禁用约束 启用约束
oracle 删除外键约束 禁用约束 启用约束 执行以下sql生成的语句即可 删除所有外键约束 Sql代码 select 'alter table '||table_name||' drop con ...
- MySQL删除所有表的外键约束、禁用外键约束
转: MySQL删除所有表的外键约束.禁用外键约束 2017年10月27日 00:11:34 李阿飞 阅读数:4512 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blo ...
- oracle 禁用外键约束
1.ORACLE数据库中的外键约束名都在表user_constraints中可以查到.其中constraint_type='R'表示是外键约束.2.启用外键约束的命令为:alter table tab ...
- (转载)MySQL删除所有表的外键约束、禁用外键约束
其实如果想删除所有表可以直接如下操作: 在navicat中直接选中所有表,然后右键删除表即可,会有提示,一路确定,就会先删掉没有外键的表和字表,只要一路确定,删几批就把表都删完了,并不算太麻烦. 转: ...
- Constraint6:更新外键约束(Foreign Key Constraint)的引用列
在SQL Server中,表之间存在引用关系,引用关系通过创建外键约束(Foreign Key Constraint)实现.如果一个Table中的column被其他Table引用,那么该表是参考表,或 ...
随机推荐
- 在PHP中使用加密技术
Gpg4win 是一款基于 GPG 的非对称加密软件.非对称加密方式,简单理解就是用公钥加密文件,用私钥解密文件.如果你需要发送加密信息,首先获取接收者的公钥,然后利用该公钥加密后传递,对方利用对应的 ...
- c# copy类中值到另外一个对象中
贴图: 调用:
- LeetCode算法题5----Longest Palindromic Substring
#5. Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You ...
- ArcGIS最权威、最专业的技术分享网站:积思园(www.iarcgis.com)
你对iArcGIS.com说点什么 为什么会有该网站的产生 在这个所谓的“大数据”的时代,每个人都深陷于海量信息无法自拔,因为过多碎片化的数据只会让自己的思维更加迷离,快餐式的阅读只会让自己变得虚胖. ...
- VC++中如何将字符串转换成整型数字
原文:http://blog.csdn.net/yongf2014/article/details/47071663 注意: atoi函数是c的函数,它的输入参数是char *类型. 你声明了stri ...
- Maximum Product Subarray 最大连续乘积子集
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- Chromebook 阿里云ECS 配置 jupyter Notebook
前言 新购入一台Chromebook Pixel ,为方便机器学习编程,用学生版的阿里云ECS搭建了jupyter Notebook 为减少下次踩坑,参考链接记录如下: 参考链接 阿里云使用笔记(一) ...
- SQL Server Mobile/Compact Edition 简单介绍
除了SQL Server Express,SQL Server还有个更轻量级的版本:SQL Server Compact Edition,容易让人想起Windows Compact Edition ( ...
- In-app Billing 概述
这个文档讲解了In-app Billing的基础构成和功能,为了能够把In-app Billing功能添加到你的程序中,你需要很好的理解这些内容. 注意: 保证你的应用遵从发布地国家的相关法律. 举例 ...
- Linq使用技巧及查询示例(一)
Linq的使用大体分为两种:语句表达式 和 方法 首先,我们要在控制器中定义好context private ApplicationDbContext db = new ApplicationD ...