Compare structures of two databases?

前端 未结 17 1564
我在风中等你
我在风中等你 2021-02-01 04:30

I wanted to ask whether it is possible to compare the complete database structure of two huge databases. We have two databases, the one is a development database, the other a p

17条回答
  •  死守一世寂寞
    2021-02-01 04:54

    To answer this kind of question currently, I've made a script that uses information_schema content to compare column, datatype, and table

    SET @database_current = '';
    SET @database_dev = '';
    -- column and datatype comparison
    SELECT a.TABLE_NAME, a.COLUMN_NAME, a.DATA_TYPE, a.CHARACTER_MAXIMUM_LENGTH,
        b.COLUMN_NAME, b.DATA_TYPE, b.CHARACTER_MAXIMUM_LENGTH
    FROM information_schema.COLUMNS a
        LEFT JOIN information_schema.COLUMNS b ON b.COLUMN_NAME = a.COLUMN_NAME
            AND b.TABLE_NAME = a.TABLE_NAME
            AND b.TABLE_SCHEMA = @database_current
    WHERE a.TABLE_SCHEMA = @database_dev
        AND (
            b.COLUMN_NAME IS NULL
            OR b.COLUMN_NAME != a.COLUMN_NAME
            OR b.DATA_TYPE != a.DATA_TYPE
            OR b.CHARACTER_MAXIMUM_LENGTH != a.CHARACTER_MAXIMUM_LENGTH
        );
    -- table comparison    
    SELECT a.TABLE_SCHEMA, a.TABLE_NAME, b.TABLE_NAME
    FROM information_schema.TABLES a
        LEFT JOIN information_schema.TABLES b ON b.TABLE_NAME = a.TABLE_NAME
            AND b.TABLE_SCHEMA = @database_current
    WHERE a.TABLE_SCHEMA = @database_dev
        AND (
            b.TABLE_NAME IS NULL
            OR b.TABLE_NAME != a.TABLE_NAME
        );
    

    Hope this script can also help people that looks for a non-application solution, but the usage of script. Cheers

提交回复
热议问题