mysql查询表外键_如何获取mysql中的表的外键

bd96500e110b49cbb3cd949968f18be7.png

I am creating a class which, takes a table from a database, and displays it to a web-page, with as much functionality as possible. One of the things I would like to support, would be having the class detect which columns in the table have a foreign key constraint on them, so that it can then go to those tables, get all of their values and use them in a select-box which is called when you edit those fields, to avoid someone violating foreign key constraints,

The main problem is discovering which fields have a foreign key constraint on them, and which tables they are pointing to. Does anyone know how to do this???

Thanks,

Lemiant

解决方案

Simple way to get foreign keys for given table:

SELECT

`column_name`,

`referenced_table_schema` AS foreign_db,

`referenced_table_name` AS foreign_table,

`referenced_column_name` AS foreign_column

FROM

`information_schema`.`KEY_COLUMN_USAGE`

WHERE

`constraint_schema` = SCHEMA()

AND

`table_name` = 'your-table-name-here'

AND

`referenced_column_name` IS NOT NULL

ORDER BY

`column_name`;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值