EXEC sp_fkeys 'TableName'
SELECT
'ALTER TABLE ['+sch.name+'].['+referencingTable.Name+'] DROP CONSTRAINT ['+foreignKey.name+']' '[DropCommand]'
FROM sys.foreign_key_columns fk
JOIN sys.tables referencingTable ON fk.parent_object_id = referencingTable.object_id
JOIN sys.schemas sch ON referencingTable.schema_id = sch.schema_id
JOIN sys.objects foreignKey ON foreignKey.object_id = fk.constraint_object_id
JOIN sys.tables referencedTable ON fk.referenced_object_id = referencedTable.object_id
;
SELECT
f.name AS 'Name of Foreign Key',
OBJECT_NAME(f.parent_object_id) AS 'Table name',
COL_NAME(fc.parent_object_id,fc.parent_column_id) AS 'Fieldname',
OBJECT_NAME(t.object_id) AS 'References Table name',
COL_NAME(t.object_id,fc.referenced_column_id) AS 'References fieldname',
'ALTER TABLE [' + OBJECT_NAME(f.parent_object_id) + '] DROP CONSTRAINT [' + f.name + ']' AS 'Delete foreign key',
'ALTER TABLE [' + OBJECT_NAME(f.parent_object_id) + '] WITH NOCHECK ADD CONSTRAINT [' +
f.name + '] FOREIGN KEY([' + COL_NAME(fc.parent_object_id,fc.parent_column_id) + ']) REFERENCES ' +
'[' + OBJECT_NAME(t.object_id) + '] ([' +
COL_NAME(t.object_id,fc.referenced_column_id) + '])' AS 'Create foreign key'
-- , delete_referential_action_desc AS 'UsesCascadeDelete'
FROM sys.foreign_keys AS f,
sys.foreign_key_columns AS fc,
sys.tables t
WHERE f.OBJECT_ID = fc.constraint_object_id
AND t.OBJECT_ID = fc.referenced_object_id
AND OBJECT_NAME(t.object_id) = 'Employees' -- Just show the FKs which reference a particular table
ORDER BY 2
[转]How can I list all foreign keys referencing a given table in SQL Server?的更多相关文章
-
How can I list all foreign keys referencing a given table in SQL Server?
How can I list all foreign keys referencing a given table in SQL Server? how to check if columns in ...
-
[PostgreSQL] Use Foreign Keys to Ensure Data Integrity in Postgres
Every movie needs a director and every rented movie needs to exist in the store. How do we make sure ...
-
ORA-02266: unique/primary keys in table referenced by enabled foreign keys
在数据库里面使用TRUNCATE命令截断一个表的数据时,遇到如下错误 SQL >TRUNCATE TABLE ESCMOWNER.SUBX_ITEM ORA-02266: unique/prim ...
-
ORA-02273: this unique/primary key is referenced by some foreign keys
关于ORA-02273错误,以前还真没有仔细留意过.昨天遇到了这个问题,遂顺便总结一番,以后遇到这类问题就可以直接用下面方案解决.如下所示,我们首先准备一下测试环境. CREATE TABLE TES ...
-
外键 Foreign keys
https://docs.microsoft.com/en-us/sql/relational-databases/tables/create-foreign-key-relationships?vi ...
-
sql server create foreign key
in table design view(right click table and choose design), right click on a column, and select 'rela ...
-
Sql Server 2008R2版本中有关外键Foreign的使用
原文:Sql Server 2008R2版本中有关外键Foreign的使用 1. 在数据库设计的过程中往往会想让2张表进行关联而使用到Foreign从而加强2张表之间的约束(如图) 以前有个问题一直没 ...
-
How do I see all foreign keys to a table or column?
down voteaccepted For a Table: SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME, ...
-
SQL Server 2008 R2——TRUNCATE TABLE 无法截断表 该表正由 FOREIGN KEY 约束引用
=================================版权声明================================= 版权声明:原创文章 禁止转载 请通过右侧公告中的“联系邮 ...
随机推荐
-
ZendStudio 解决svn导出项目乱码问题
从svn导出项目往往会出现乱码,可以右击项目,点击properties(或者选中项目alt+enter键进入)直接修改项目编码为utf-8,但是html文件还是乱码. 下面的方法可以解决: windo ...
-
ramnit病毒
今天打开Hbuilder,发现每个html文档都有如下代码: <SCRIPT Language=VBScript><!--DropFileName = "svchost.e ...
-
PC-老鸟装机
老鸟装机 一.硬件安装莫疏忽 1>安装硬盘有讲究 1.单硬盘+单光驱 IDE1----硬盘(Mastet接口) IDE2----光驱(Mastet接口) ...
-
dedecms由子目录访问修改为根目录访问
现在我需要将原来位于xampp/htdocs/zm下的网站修改到D:/lyh/webhome目录下访问,原来的访问路径为http://localhost/zm,现在为http://www.yihui. ...
-
Python - 命令式编程与符号编程
原文链接:https://zh.d2l.ai/chapter_computational-performance/hybridize.html本文是对原文内容的摘取和扩展. 命令式编程(imperat ...
-
js-notebook
注意DOM和BOM的区别,ECMAScript只针对js的语法核心,实际大部分浏览器里的js = ECMAScript + DOM + BOM, 而nodejs里就只包括core js 隐性的toSt ...
-
JAVA面向对象和类
一.构造方法 1.构造方法是一个与类同名的方法,用来对类进行实例化(创建对象) 2.特点 1).构造方法没有返回值: 2).构造方法的名称要与本类的名称相同. 例 public class Test{ ...
-
java中源代码和lib库中有包名和类名都相同的类(转)
https://blog.csdn.net/itachiwwwg/article/details/9003261 当java的源代码中出现了和系统的lib库中的包名与类名完全一样的类时,系统应当怎么加 ...
-
Unity3d官方测试插件学习-单元测试,集成测试
2016/11/27更新:官方的测试工具有许多问题,我修改了一个版本 https://git.oschina.net/Hont/UnitTest_Modifyed 支持切场景,异常不失败等 其实Uni ...
-
无法启动DISTRIBUTED TRANSACTION COORDINATOR解决方法
有时候我们需要进行COM应用程序的权限设置,控制面板-->管理工具-->组件服务-->然后依此展开:组件服务-->计算机-->我的电脑-->DCOM 配置,接下来找 ...