查找架构差异

时间:2011-02-18 05:24:25

标签: mysql

我想与DB Schema进行比较,其中表和列几乎完全相同。

mysql> select a.TABLE_SCHEMA, a.TABLE_NAME FROM information_schema.tables as a left join information_schema.tables as b on a.TABLE_NAME = b.TABLE_NAME WHERE a.TABLE_SCHEMA = 'india' AND b.TABLE_SCHEMA = 'china' AND b.TABLE_NAME IS NULL;

我预计上面的查询将返回那些存在于India DB中的表,但在China DB中不存在。但这似乎并没有起作用。 除了Tables差异之外,我还需要找到2个DB中可能不同的列。

我不想使用像Navicat这样的应用程序。我需要一个查询或UNIX命令。

1 个答案:

答案 0 :(得分:1)

您需要将AND b.TABLE_SCHEMA = 'china'谓词从WHERE移动到JOIN:

select a.TABLE_SCHEMA, a.TABLE_NAME 
FROM information_schema.tables as a left join information_schema.tables as b
  ON b.TABLE_SCHEMA = 'china'
    AND a.TABLE_NAME = b.TABLE_NAME
WHERE a.TABLE_SCHEMA = 'india'  AND b.TABLE_NAME IS NULL

要查找列信息:

SELECT a.TABLE_NAME, a.COLUMN_NAME
FROM information_schema.columns a LEFT JOIN information_schema.columns b
  ON b.TABLE_SCHEMA = 'china'
    AND a.TABLE_NAME = b.TABLE_NAME
    AND a.ORDINAL_POSITION = b.ORDINAL_POSITION
    AND a.COLUMN_DEFAULT   = b.COLUMN_DEFAULT
    AND a.IS_NULLABLE      = b.IS_NULLABLE
    AND a.DATA_TYPE        = b.DATA_TYPE
    AND a.COLUMN_TYPE      = b.COLUMN_TYPE
.... Add other comparisons here depending on what you consider non identical.
WHERE a.TABLE_SCHEMA = 'india' AND b.COLUMN_NAME IS NULL