Home > Database > Mysql Tutorial > How Can I Find All Foreign Key Constraints Referencing a MySQL Table or Column?

How Can I Find All Foreign Key Constraints Referencing a MySQL Table or Column?

Patricia Arquette
Release: 2024-12-15 10:22:16
Original
987 people have browsed it

How Can I Find All Foreign Key Constraints Referencing a MySQL Table or Column?

How Do I Determine All Foreign Key Constraints Linked to a Table or Column in MySQL?

This question has been raised previously for Oracle, and here is the solution for MySQL:

To list foreign key constraints pointing to a specific table, use the following query:

SELECT 
  TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
FROM
  INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
  REFERENCED_TABLE_SCHEMA = (SELECT DATABASE()) AND
  REFERENCED_TABLE_NAME = '<table_name>' \G
Copy after login

To list foreign key constraints pointing to a specific column, alter the query like this:

SELECT 
  TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
FROM
  INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
  REFERENCED_TABLE_SCHEMA = (SELECT DATABASE()) AND
  REFERENCED_TABLE_NAME = '<table_name>' AND
  REFERENCED_COLUMN_NAME = '<column_name>' \G
Copy after login

Just substitute '' and '' with the target table and column names, respectively.

The above is the detailed content of How Can I Find All Foreign Key Constraints Referencing a MySQL Table or Column?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template