【问题标题】:Get MySQL primary key and foreign keys constraints from PHP?从 PHP 获取 MySQL 主键和外键约束?
【发布时间】:2012-10-10 06:23:38
【问题描述】:

我正在使用 PDO/PHP/MySQL 构建一个与 Smarty 集成的数据网格应用程序。

有没有快速便捷的方式(使用db schema或者一些PDO命令)在PHP中获取:

  • 表的主键
  • 表的外键约束列表?

(我使用 InnoDB 作为架构)。

【问题讨论】:

    标签: php mysql pdo


    【解决方案1】:

    可以通过查询information_schema表获取关键信息:

    SELECT * 
    FROM information_schema.KEY_COLUMN_USAGE 
    WHERE REFERENCED_TABLE_NAME = 'my_table_name'
    

    MySQL: How to I find all tables that have foreign keys that reference particular table.column AND have values for those foreign keys?

    可以通过SHOW TABLE获取主键:

    SHOW INDEX FROM my_table_name 
    WHERE key_name = 'PRIMARY'
    

    MySQL: Determine Table's Primary Key Dynamically

    在具有大量数据库/表的服务器上查询 information_schema 表可能会很慢。

    您可以使用它来加快速度:

    SET GLOBAL innodb_stats_on_metadata = 0
    

    http://www.mysqlperformanceblog.com/2011/12/23/solving-information_schema-slowness/

    【讨论】:

    • 非常感谢,特别是 information_schema 性能提示,这正是我需要的信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    相关资源
    最近更新 更多