How do I see all foreign keys to a table or column?

Posted 功夫 熊猫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;

以上是关于How do I see all foreign keys to a table or column?的主要内容,如果未能解决你的问题,请参考以下文章

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

csharp 来自https://stackoverflow.com/questions/874380/wpf-how-do-i-loop-through-the-all-controls-in-a-

sh 列出Git远程分支(来自http://stackoverflow.com/questions/3471827/how-do-i-list-all-remote-branches-in-git-1

Mirror, Mirror,What do I see一切都是投射

How do I install uudecode?

How do I code well?