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>‘;

For a Column:

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_COLUMN_NAME = ‘<column>‘;

Basically, we changed REFERENCED_TABLE_NAME with REFERENCED_COLUMN_NAME in the where clause.

use INFORMATION_SCHEMA;
select TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME,
REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME from KEY_COLUMN_USAGE
where TABLE_SCHEMA = "<your_database_name>" and TABLE_NAME = "<your_table_name>"
and referenced_column_name is not NULL;
时间: 2024-10-12 22:41:55

How do I see all foreign keys to a table or column?的相关文章

[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

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

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

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 fkcINNER JOIN sys.object

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

Could not drop object &#39;student&#39; because it is referenced by a FOREIGN KEY constraint

1. Find foreign keys SELECT * FROM sys.foreign_keys WHERE referenced_object_id = object_id('Student') 2. Delete foreign keys SELECT     'ALTER TABLE ' +  OBJECT_SCHEMA_NAME(parent_object_id) +    '.[' + OBJECT_NAME(parent_object_id) +     '] DROP CON

MSSQL-Scripter,一个新的生成T-SQL脚本的SQL Server命令行工具

这里向大家介绍一个新的生成T-SQL脚本的SQL Server命令行工具:mssql-scripter.它支持在SQL Server.Azure SQL DB以及Azure SQL DW中为数据库生成CREATE和INSERT T-SQL脚本. Mssql-scripter是一个跨平台的命令行工具,功能等同于SQL Server Management Studio中的Generate and Publish Scripts Wizard. 咱们能够在Linux.macOS和Windows上使用它

类handler

/** The handler class is the interface for dynamically loadable storage engines. Do not add ifdefs and take care when adding or changing virtual functions to avoid vtable confusion */ class handler :public Sql_alloc { public: typedef ulonglong Table_

【oracle11g,18】存储结构:临时表,手工条带化,表/索引迁移表空间,删除表,外部表

一. 临时表 临时表放在临时表空间,不生成redo,只有undo. 在临时表中可以创建索引.视图及触发器,还可以使用"Export and Import(导出和导入)"或"Data Pump(数据泵)"导出和导入临时表的定义.但是,即使使用了ROWS 选项,也不会导出数据. 有基于事务(默认)和基于session两种,其他的会话不能访问到. 在临时表中,DML锁永远不需要. 1.创建默认的(基于事务的)临时表:(on commit delete rows:提交时清空