[英]Get Foreign keys in the table
I created one script to display the foreign keys in the particular table. 我创建了一个脚本来显示特定表中的外键。
SELECT i.CONSTRAINT_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_NAME = 'sample';
When I execute this command in my host, it displays the list of foreign key constraints found in all the databases with table
name "sample"
. 当我在主机中执行此命令时,它将显示在所有
table
名为"sample"
的数据库中找到的外键约束列表。
But i need particular database, sample table foreign keys
. 但是我需要特定的数据库,样本表
foreign keys
。
just add at the end 只需在末尾添加
AND i.table_schema = '<the name of your database which is a schema>'
EDIT : 编辑:
Change the left join to 将左联接更改为
LEFT JOIN information_schema.KEY_COLUMN_USAGE
ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME AND
i.TABLE_SCHEMA = k.TABLE_SCHEMA
because you could find constraint with same name in different schemas. 因为您可以在不同的架构中找到具有相同名称的约束。
Need to select the database before run this query. 在运行此查询之前需要选择数据库。 Following may be helpful.
以下内容可能会有所帮助。
USE YourDatabaseName
SELECT i.CONSTRAINT_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_NAME = 'sample'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.