MySQL从视图表中获取外键

时间:2015-11-03 17:33:04

标签: php mysql sql

我有一个由两个表组成的视图表(让我们称之为view_table)(让我们称之为table_1& table_2)。我们假设table_1在引用表fk_field的字段fk_table上有一个外键。

是否有直接查询view_table以获取字段fk_table的参考表fk_field

我是用PHP做的,所以我想做的事情就是:

  1. VIEW_DEFINITION解析INFORMATION_SCHEMA.VIEWS字段,以获取构成view_table的表格列表(例如table_1& table_2
  2. 对于其中每个表格,请使用this method获取fk_table
  3. 是否有更好(即更简单)的方法?

    修改

    在这种情况下的视图定义,字段Container是一个引用名为Containers的表的外键:

    CREATE 
        ALGORITHM = UNDEFINED 
        DEFINER = `root`@`localhost` 
        SQL SECURITY DEFINER
    VIEW `LIMS_dev`.`DerivativeView` AS
        SELECT 
            `a`.`Derivative` AS `Derivative`,
            `a`.`Container` AS `Container`,
            `a`.`DerivativeTypeID` AS `DerivativeTypeID`,
            `a`.`StorageFormatID` AS `StorageFormatID`,
            `a`.`SourceSample` AS `SourceSample`,
            `a`.`Parent` AS `Parent`,
            `a`.`Date` AS `DerivativeDate`,
            `a`.`Active` AS `SampleActive`,
            `a`.`User` AS `User`,
            `a`.`Notes` AS `Notes`,
            `b`.`Shelf` AS `Shelf`,
            `b`.`Rack` AS `Rack`,
            `b`.`FreezerID` AS `FreezerID`,
            `b`.`Active` AS `BoxActive`,
            `b`.`Date` AS `ContainerDate`
        FROM
            (`LIMS_dev`.`Derivatives` `a`
            LEFT JOIN `LIMS_dev`.`Containers` `b` ON ((`a`.`Container` = `b`.`Container`)))
    

0 个答案:

没有答案