如果我有:
create table order_items
(
orderid int unsigned not null references orders(orderid),
isbn char(13) not null,
quantity tinyint unsigned,
primary key (orderid, isbn)
);
如何查看是否存在orderid int unsigned not null references orders(orderid)
外键?
答案 0 :(得分:3)
您可以像这样使用INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
:
SELECT *
FROM information_schema.REFERENTIAL_CONSTRAINTS
WHERE CONSTRAINT_SCHEMA = '<schema>'
AND REFERENCED_TABLE_NAME = 'order_items'
AND CONSTRAINT_NAME = 'orderid'
答案 1 :(得分:0)
您可以使用它来检查整个数据库中的外键约束
SELECT TABLE_NAME ,
COLUMN_NAME,
CONSTRAINT_NAME,
REFERENCED_TABLE_NAME,
REFERENCED_COLUMN_NAME
FROM
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
需要检查特定表意味着请在上面的查询中的条件中添加此
WHERE
REFERENCED_TABLE_SCHEMA = 'student' AND REFERENCED_TABLE_NAME = 'student_details'