How can I list all foreign keys referencing a given table in SQL Server?

1. EXEC sp_fkeys ‘TableName‘

2. SELECT obj.name AS FK_NAME,
sch.name AS [schema_name],
tab1.name AS [table],
col1.name AS [column],
tab2.name AS [referenced_table],
col2.name AS [referenced_column]
FROM sys.foreign_key_columns fkc
INNER JOIN sys.objects obj
ON obj.object_id = fkc.constraint_object_id
INNER JOIN sys.tables tab1
ON tab1.object_id = fkc.parent_object_id
INNER JOIN sys.schemas sch
ON tab1.schema_id = sch.schema_id
INNER JOIN sys.columns col1
ON col1.column_id = parent_column_id AND col1.object_id = tab1.object_id
INNER JOIN sys.tables tab2
ON tab2.object_id = fkc.referenced_object_id
INNER JOIN sys.columns col2
ON col2.column_id = referenced_column_id AND col2.object_id = tab2.object_id

时间: 2024-10-05 04:19:28

How can I list all foreign keys referencing a given table in SQL Server?的相关文章

[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 something in another table exists before inserting new data? This lesson will teach us about foreign keys and references. CREATE TABLE directors ( id

A MySQL foreign keys drop table, re-create table example

Summary: How to drop MySQL database tables and recreate them when you have foreign keyrelationships between the tables. This is pretty obscure, but I thought I'd post it here so I wouldn't forget how to do this ... if you ever have a situation when u

sql server create foreign key

in table design view(right click table and choose design), right click on a column, and select 'relationship', then create your foreign keys, sql, stop changes that require recreating table: select tools ->options-> designer, then remove checked fro

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

原文:Sql Server 2008R2版本中有关外键Foreign的使用 1. 在数据库设计的过程中往往会想让2张表进行关联而使用到Foreign从而加强2张表之间的约束(如图) 以前有个问题一直没弄明白,那就是外键列的值的情况,正常情况下表中的列的值可以是null或者满足该列所定义的类型的数据即可,但是外键是不是也一样呢? 答案显然不是的,那到底是个什么情况呢? 官方帮助文档是这样说明的"FOREIGN KEY 约束并不仅仅可以与另一表的 PRIMARY KEY 约束相链接,它还可以定义为引

MySQL添加外键Foreign Keys出错,报错[HY000][3780]

今天写开发文档的时候需要做一下数据结构相关的内容,于是就想着一个快捷一点儿的操作,直接在DataGrip里面调用视图,然后将视图打印为PDF,这样就可以直接截图获取到图片了.由于开发的过程中也没有怎么注重外键的建立,因为本来就是一个小小的比赛,所以生成视图的时候外键联系的箭头都没有,真的是自闭了,如果这个直接给评委看,评委估计会气哭...所以我打算花点时间加一些外键上去,心里想着也花不了多少时间,谁知道,这下子还遇到一些哲学问题! 首先看下,本来数据库表视图是这样的,我只是想多增加一些外键约束,

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,REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = '<database>' AND REFERENCED_TABLE_NAME = '<table&g

Get Foreign Keys

import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class Main { public static void main(String[] args) throws Exception { Connection conn = getMySq

如何对Foreign Key 约束的引用列进行 insert 或 update

如果一个Table是其他表的外键,那么对该Table的引用列进行Update 或 Insert 会受到很多限制.Disable Foreign Key Constraints with INSERT and UPDATE Statements. 查看FK的信息,主要使用sys.foreign_keys 和 sys.foreign_key_columns 这两个表,辅助表:sys.objects (type='F'),sys.tables,sys.columns --get FK list sel

hibernate的component使用

hibernate的Component,即组件,表示2个类之间的关系,即其中1个类可以作为另一个类的组件来使用. 1.先来看下annotation中关于component的API 2.2.2.3. 嵌入式对象(又名组件) 在实体中可以定义一个嵌入式组件(embedded component), 甚至覆盖该实体中原有的列映射. 组件类必须在类一级定义@Embeddable注解. 在特定的实体的关联属性上使用@Embedded和@AttributeOverride注解可以覆盖该属性对应的嵌入式对象的