In MySQL, how do I get a list of all foreign key constraints pointing to a particular table? a particular column? This is the same thing as this Oracle question, but for MySQL.
相关问题
- sqlyog export query result as csv
- NOT DISTINCT query in mySQL
- MySQL: conduct a basic search
- Why sometimes there is one of more gap(s) in the v
- mySQL alter table on update, current timestamp
This solution will not only display all relations but also the constraint name, which is required in some cases (e.g. drop contraint):
If you want to check tables in a specific database, at the end of the query add the table name:
Likewise, for a specific column name, add
at the end of the query.
Inspired by this post here
A quick way to list your FKs (Foreign Key references) using the
This query does assume that the constraints and all referenced and referencing tables are in the same schema.
Add your own comment.
Source: the official mysql manual.
Posting on an old answer to add some useful information.
I had a similar problem, but I also wanted to see the CONSTRAINT_TYPE along with the REFERENCED table and column names. So,
To see all FKs in your table:
To see all the tables and FKs in your schema:
To see all the FKs in your database:
Remember!
This is using the InnoDB storage engine. If you can't seem to get any foreign keys to show up after adding them it's probably because your tables are using MyISAM.
To check:
To fix, use this:
To find all tables containing a particular foreign key such as
employee_id
If you use InnoDB and defined FK's you could query the information_schema database e.g.:
Using REFERENCED_TABLE_NAME does not always work and can be a NULL value. The following query can work instead: