本文转自:https://stackoverflow.com/questions/483193/how-can-i-list-all-foreign-keys-referencing-a-given-table-in-sql-server

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?的更多相关文章

  1. 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 ...

  2. [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 ...

  3. ORA-02266: unique/primary keys in table referenced by enabled foreign keys

    在数据库里面使用TRUNCATE命令截断一个表的数据时,遇到如下错误 SQL >TRUNCATE TABLE ESCMOWNER.SUBX_ITEM ORA-02266: unique/prim ...

  4. ORA-02273: this unique/primary key is referenced by some foreign keys

    关于ORA-02273错误,以前还真没有仔细留意过.昨天遇到了这个问题,遂顺便总结一番,以后遇到这类问题就可以直接用下面方案解决.如下所示,我们首先准备一下测试环境. CREATE TABLE TES ...

  5. 外键 Foreign keys

    https://docs.microsoft.com/en-us/sql/relational-databases/tables/create-foreign-key-relationships?vi ...

  6. sql server create foreign key

    in table design view(right click table and choose design), right click on a column, and select 'rela ...

  7. Sql Server 2008R2版本中有关外键Foreign的使用

    原文:Sql Server 2008R2版本中有关外键Foreign的使用 1. 在数据库设计的过程中往往会想让2张表进行关联而使用到Foreign从而加强2张表之间的约束(如图) 以前有个问题一直没 ...

  8. 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, ...

  9. SQL Server 2008 R2——TRUNCATE TABLE 无法截断表 该表正由 FOREIGN KEY 约束引用

    =================================版权声明================================= 版权声明:原创文章 禁止转载  请通过右侧公告中的“联系邮 ...

随机推荐

  1. Spring Boot 2 实践记录之 条件装配

    实验项目是想要使用多种数据库访问方式,比如 JPA 和 MyBatis. 项目的 Service 层业务逻辑相同,只是具体实现代码不同,自然是一组接口,两组实现类的架构比较合理. 不过这种模式却有一个 ...

  2. web窗体ListView配置分页

    一.配置objectDataSource 1.选择业务逻辑层的类,再选择对应的分页方法 2.配置Select对应的方法,必须是一个带两个整型参数的方法,第一个参数表示要查看的第一条记录的前一条,第二个 ...

  3. ASP.NET Core 2 学习笔记(八)URL重写

    路由跟URL 重写的功能性略有不同.路由是将Request 找到对应的服务,而URL 重写是为了推卸责任转送Request.本篇将简单介绍下ASP.NET Core的URL重写(URL Rewrite ...

  4. C#读取excel文件,并生成json

    这次介绍两种方法,第一种是安装AccessDatabaseEngine,第二种是利用Npoi读取excel 一.第一种利用AccessDatabaseEngine进行读取excel文件 1.安装Acc ...

  5. JDK源码分析(10) CopyOnWriteArrayList

    概述 CopyOnWriteArrayList是一个线程安全集合,原理简单说就是:在保证线程安全的前提下,牺牲掉写操作的效率来保证读操作的高效.所谓CopyOnWrite就是通过复制的方式来完成对数据 ...

  6. 【文文殿下】Win7打开无线热点

    下载程序 : https://files.cnblogs.com/files/Syameimaru/wifi.rar 打开config.ini 修改相关信息,然后运行程序. 运行成功后,打开网络和共享 ...

  7. 【11】JMicro微服务-配置管理

    如非授权,禁止用于商业用途,转载请注明出处作者:mynewworldyyl 往下看前,建议完成前面1到10小节 JMicro目前仅支持基于Zookeeper做配置管理,全部配置信息可以在ZK做增删改查 ...

  8. (转)教你手工mysql拆库

    原文:http://www.cnblogs.com/cchust/p/3859967.html 互联网网站应用大多采用mysql作为DB存储,限于mysql单机性能的瓶颈,为了支撑更大容量和更大的访问 ...

  9. Sublime Text 3编辑器的SublimeRPEL快捷键设置

    Sublime Text 3软件由于每次运行程序都要执行:Tools -> SublimeREPL -> Python -> RUN current file 菜单有点麻烦,所以现在 ...

  10. Spring Security构建Rest服务-0400-使用切片拦截rest服务

    Restful API的拦截: 1,过滤器(Filter) 2,拦截器(Interceptor) 3,切片(Aspect) 1,过滤器 和传统javaweb一鸟样,例,记录controller执行时间 ...