本文转自: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. TransactionScope事务使用

    using (System.Transactions.TransactionScope T_Scope = new System.Transactions.TransactionScope()) { ...

  2. 深入理解Aspnet Core之Identity(1)

    最近学习asp.netcore 打算写出来和大家分享,我计划先写Identity部分,会从开始asp.netocre identity的简单实用开始,然后再去讲解主要的类和自定义这些类. 主题:asp ...

  3. vs2017 修改项目名称

    由于经常接到各种项目需求, 又不想重新搭建框架. 于是便想到直接修改项目名. 话不多说, 上图~ 1 重命名 解决方案 和  项目名称 2 重命名项目的应用程序名和命名空间 3 Ctrl+F 将旧的工 ...

  4. MaxScript调用DotNet时命名空间的问题

    Fn GetSpecialFolder argEnumName = (DotNetClass "System.Environment").GetFolderPath (Execut ...

  5. Hbuilder系列索引

    『原创』手把手教你搭建一个实用的油耗App(一)

  6. Docker容器的原理与实践 (下)

    欢迎访问网易云社区,了解更多网易技术产品运营经验. Docker原理分析 Docker架构 镜像原理 镜像是一个只读的容器模板,含有启动docker容器所需的文件系统结构及内容Docker以镜像和在镜 ...

  7. ko内核模块文件以及载入模块命令modprobe insmod

    原文链接:https://blog.csdn.net/evenness/article/details/7655921?utm_source=blogxgwz5 modprobe: Load modu ...

  8. C++ STL的容器类型

    1.顺序容器 2.关联容器 3.vector的使用 vector<数据类型> a; a.push_back(10)  -------->把数据从末末尾段插入vector里面 a.po ...

  9. Design-341. Flatten Nested List Iterator

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  10. python stdout 重定向

    import sys class Buffer(object): def __init__(self): self.buffer = [] def write(self, *args, **kwarg ...