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.
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_TABLE_NAME = '<table>' AND
REFERENCED_COLUMN_NAME = '<column>';
Basically, we changed REFERENCED_TABLE_NAME with REFERENCED_COLUMN_NAME in the where clause.
where REFERENCED_TABLE_SCHEMA = 'mydatabase' and REFERENCED_TABLE_NAME = 'mytable'
TABLE_SCHEMA
to the list of columns (SELECT TABLE_SCHEMA, TABLE_NAME, …
) which is useful in case of there are multiple databases that have tables relating to our table/column of interest.EDIT: As pointed out in the comments, this is not the correct answer to the OPs question, but it is useful to know this command. This question showed up in Google for what I was looking for, and figured I'd leave this answer for the others to find.
I found this answer here: MySQL : show constraints on tables command
I needed this way because I wanted to see how the FK functioned, rather than just see if it existed or not.
<yourtable>
, not all the constraints that point to<yourtable>
.display foreign keys mysql
in google, may be that is why ;)If you use InnoDB and defined FK's you could query the information_schema database e.g.:
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: USE '';
SELECT i.TABLE_NAME, i.CONSTRAINT_TYPE, i.CONSTRAINT_NAME, k.REFERENCED_TABLE_NAME, k.REFERENCED_COLUMN_NAME
FROM information_schema.TABLE_CONSTRAINTS i
LEFT JOIN information_schema.KEY_COLUMN_USAGE k ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME
WHERE i.CONSTRAINT_TYPE = 'FOREIGN KEY'
AND i.TABLE_SCHEMA = DATABASE()
AND i.TABLE_NAME = '';
To see all the tables and FKs in your schema: USE '';
SELECT i.TABLE_NAME, i.CONSTRAINT_TYPE, i.CONSTRAINT_NAME, k.REFERENCED_TABLE_NAME, k.REFERENCED_COLUMN_NAME
FROM information_schema.TABLE_CONSTRAINTS i
LEFT JOIN information_schema.KEY_COLUMN_USAGE k ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME
WHERE i.CONSTRAINT_TYPE = 'FOREIGN KEY'
AND i.TABLE_SCHEMA = DATABASE();
To see all the FKs in your database: SELECT i.TABLE_SCHEMA, i.TABLE_NAME, i.CONSTRAINT_TYPE, i.CONSTRAINT_NAME, k.REFERENCED_TABLE_NAME, k.REFERENCED_COLUMN_NAME
FROM information_schema.TABLE_CONSTRAINTS i
LEFT JOIN information_schema.KEY_COLUMN_USAGE k ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME
WHERE i.CONSTRAINT_TYPE = 'FOREIGN KEY';
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:
As an alternative to Node’s answer, if you use InnoDB and defined FK’s you could query the information_schema database e.g.:
for foreign keys from
for foreign keys to
You can also get the UPDATE_RULE and DELETE_RULE if you want them.
Constraints in SQL are the rules defined for the data in a table. Constraints also limit the types of data that go into the table. If new data does not abide by these rules the action is aborted.
You can view all constraints by using
select * from information_schema.table_constraints;
(This will produce a lot of table data).
You can also use this for MySQL:
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 schema name:
Likewise, for a specific column name, add
and table_name = 'table_name
at the end of the query.
Inspired by this post here
Using REFERENCED_TABLE_NAME does not always work and can be a NULL value. The following query can work instead:
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.
I'm reluctant to add yet another answer, but I've had to beg, borrow and steal from the others to get what I want, which is a complete list of all the FK relationships on tables in a given schema, INCLUDING FKs to tables in other schemas. The two crucial recordsets are information_schema.KEY_COLUMN_USAGE and information_schema.referential_constraints. If an attribute you want is missing, just uncomment the KCU., RC. to see what's available
The solution I came up with is fragile; it relies on django's naming convention for foreign keys.
Then, in the shell,
To find all tables containing a particular foreign key such as
employee_id
If you also want to get the name of the foreign key column:
It's often helpful to know the update and delete behaviour, which the other answers don't provide. So here goes.
I needed a bird's-eye-view on the relationships among the tables (to use in an ORM). Using the suggestions from this page, and after experimenting, I've put together the following query:
It returns just what I need, and in the order that I want.
I also do little processing on the result (turn it into a some kind of dictionary), so that it's ready to be used for creating an aggregate.
I had a "myprodb" MySql database and for checking all foreign keys in this data base I used the following simple command.
I hope it help.
Follow WeChat
Success story sharing
Want to stay one step ahead of the latest teleworks?
Subscribe Now相似问题
HuntsBot,a one-stop outsourcing task, remote job, product ideas sharing and subscription platform, which supports DingTalk, Lark, WeCom, Email and Telegram robot subscription. The platform will push outsourcing task requirements, remote work opportunities, product ideas to every subscribed user with timely, stable and reliable.
Platform
Support
Links
Contact US
Any questions or suggestions during use, you can contact us in the following ways:
Email: huntsbot@xinbeitime.com
微信公众号
Tips