如何知道表是否是mysql

时间:2016-11-28 01:16:28

标签: java mysql

我试过了这个查询

SELECT
            CONSTRAINT_CATALOG AS constraintCatalog,
            CONSTRAINT_SCHEMA AS constraintSchema,
            CONSTRAINT_NAME AS constraintName,
            TABLE_CATALOG AS tableCatalog,
            TABLE_SCHEMA AS tableSchema,
            TABLE_NAME AS tableName,
            COLUMN_NAME AS columnName,
            ORDINAL_POSITION AS ordinalPosition,
            POSITION_IN_UNIQUE_CONSTRAINT AS positionUniqueConstraint,
            REFERENCED_TABLE_SCHEMA AS referencedTableSchema,
            REFERENCED_TABLE_NAME AS referencedTableName,
            REFERENCED_COLUMN_NAME AS referencedColumn

        FROM
            INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
        WHERE
            TABLE_NAME = 'table_name'

但问题我只得到了检测外键的结果。 有没有办法检测表的父和子?因为我们需要在用户界面中显示它。谢谢

1 个答案:

答案 0 :(得分:1)

你试过这个:

SELECT
  FK.referenced_table_name parent_table,
  FK.table_name child_table,
  FK.constraint_name as FK_name
FROM
  information_schema.KEY_COLUMN_USAGE FK
WHERE
  FK.referenced_table_name IS NOT NULL

参考:http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html