stay MySQL How to get a list of all foreign key constraints to a specific table? Specific columns? This and The Oracle problem Same, but for MySQL.
#1 building
Post old answers to add some useful information.
I have a similar problem, but I also want to look at constraint'u type and referred table and column names. So,
-
To view all FK S in a table:
USE '<yourschema>'; 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 = '<yourtable>';
-
To view all tables and FK S in the schema:
USE '<yourschema>'; 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 view all FK S in the 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 in use InnoDB Storage engine. If you don't see any foreign keys after you add them, it may be because your table is using MyISAM.
To check:
SELECT * TABLE_NAME, ENGINE FROM information_schema.TABLES WHERE TABLE_SCHEMA = '<yourschema>';
To solve this problem, use the following command:
ALTER TABLE `<yourtable>` ENGINE=InnoDB;
#2 building
Editor: as pointed out in the comments, this is not the correct answer to the OPs question, but it's useful to understand this command. This question appears on Google. It's something I've been looking for, and I think I'll leave the answer to someone else.
SHOW CREATE TABLE `<yourtable>`;
Here I find the answer: MySQL: display constraints on table commands
I need this way because I want to understand how FK works, not just to see if it exists.
#3 building
Use the following methods to list your FK (foreign key reference) quick methods
KEY_COLUMN_USAGE view: SELECT CONCAT( table_name, '.', column_name, ' -> ', referenced_table_name, '.', referenced_column_name ) AS list_of_fks FROM information_schema.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = (your schema name here) AND REFERENCED_TABLE_NAME is not null ORDER BY TABLE_NAME, COLUMN_NAME;
The query does assume that the constraint and all reference and reference tables are in the same schema.
Add your own comments.
Source: Official MySQL manual.
#4 building
The solution will display not only all relationships, but also constraint names, which are required in some cases (for example, placing constraints):
select concat(table_name, '.', column_name) as 'foreign key', concat(referenced_table_name, '.', referenced_column_name) as 'references', constraint_name as 'constraint name' from information_schema.key_column_usage where referenced_table_name is not null;
If you want to check the tables in a specific database, add the schema name at the end of the query:
select concat(table_name, '.', column_name) as 'foreign key', concat(referenced_table_name, '.', referenced_column_name) as 'references', constraint_name as 'constraint name' from information_schema.key_column_usage where referenced_table_name is not null and table_schema = 'database_name';
Also, for a specific column name, add
And table name ='table name
At the end of the query.
Ad locum Inspired by this article
#5 building
If you use InnoDB and define FK, you can query the information ﹣ u schema database, for example:
SELECT * FROM information_schema.TABLE_CONSTRAINTS WHERE information_schema.TABLE_CONSTRAINTS.CONSTRAINT_TYPE = 'FOREIGN KEY' AND information_schema.TABLE_CONSTRAINTS.TABLE_SCHEMA = 'myschema' AND information_schema.TABLE_CONSTRAINTS.TABLE_NAME = 'mytable';
#6 building
The solution I came up with is fragile; it relies on Django's foreign key naming convention.
USE information_schema; tee mysql_output SELECT * FROM TABLE_CONSTRAINTS WHERE CONSTRAINT_TYPE = 'FOREIGN KEY' AND TABLE_SCHEMA = 'database_name'; notee
Then, in the shell
grep 'refs_tablename_id' mysql_output
#7 building
For tables:
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 columns:
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 change the referenced table name to referenced column name in the where clause.
#8 building
As an alternative to Node's answer, if you use InnoDB and define FK, you can query the information u schema database, for example:
SELECT CONSTRAINT_NAME, TABLE_NAME, REFERENCED_TABLE_NAME FROM information_schema.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = '<schema>' AND TABLE_NAME = '<table>'
For foreign keys in < Table >, or
SELECT CONSTRAINT_NAME, TABLE_NAME, REFERENCED_TABLE_NAME FROM information_schema.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = '<schema>' AND REFERENCED_TABLE_NAME = '<table>'
Foreign key for < Table >
If necessary, you can also get update "rule" and delete "rule".
#9 building
Find all tables that contain a specific foreign key (for example, employee? ID
SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME IN ('employee_id') AND TABLE_SCHEMA='table_name';
#10 building
Using referenced table name is not always valid and can be a NULL value. The following queries can be substituted:
select * from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where TABLE_NAME = '<table>';
#11 building
If you also want to get the name of the foreign key column:
SELECT i.TABLE_SCHEMA, i.TABLE_NAME, i.CONSTRAINT_TYPE, i.CONSTRAINT_NAME, k.COLUMN_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.TABLE_SCHEMA = '<TABLE_NAME>' AND i.CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY i.TABLE_NAME;