Created
January 16, 2021 06:28
-
-
Save zaifastafa/1185d94cc8ae2a160a8f51ce18c0edcb to your computer and use it in GitHub Desktop.
Compare database structure with SQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SET @firstDatabaseName = '[first database name]'; | |
SET @secondDatabaseName = '[second database name]'; | |
SELECT * FROM | |
(SELECT | |
CONCAT(cl.TABLE_NAME, ' [', cl.COLUMN_NAME, ', ', cl.COLUMN_TYPE, ']') tableRowType | |
FROM information_schema.columns cl, information_schema.TABLES ss | |
WHERE | |
cl.TABLE_NAME = ss.TABLE_NAME AND | |
cl.TABLE_SCHEMA = @firstDatabaseName AND | |
ss.TABLE_TYPE IN('BASE TABLE', 'VIEW') | |
ORDER BY | |
cl.table_name ) AS t1 | |
LEFT JOIN | |
(SELECT | |
CONCAT(cl.TABLE_NAME, ' [', cl.COLUMN_NAME, ', ', cl.COLUMN_TYPE, ']') tableRowType | |
FROM information_schema.columns cl, information_schema.TABLES ss | |
WHERE | |
cl.TABLE_NAME = ss.TABLE_NAME AND | |
cl.TABLE_SCHEMA = @secondDatabaseName AND | |
ss.TABLE_TYPE IN('BASE TABLE', 'VIEW') | |
ORDER BY | |
cl.table_name ) AS t2 ON t1.tableRowType = t2.tableRowType | |
WHERE | |
t2.tableRowType IS NULL | |
UNION | |
SELECT * FROM | |
(SELECT | |
CONCAT(cl.TABLE_NAME, ' [', cl.COLUMN_NAME, ', ', cl.COLUMN_TYPE, ']') tableRowType | |
FROM information_schema.columns cl, information_schema.TABLES ss | |
WHERE | |
cl.TABLE_NAME = ss.TABLE_NAME AND | |
cl.TABLE_SCHEMA = @firstDatabaseName AND | |
ss.TABLE_TYPE IN('BASE TABLE', 'VIEW') | |
ORDER BY | |
cl.table_name ) AS t1 | |
RIGHT JOIN | |
(SELECT | |
CONCAT(cl.TABLE_NAME, ' [', cl.COLUMN_NAME, ', ', cl.COLUMN_TYPE, ']') tableRowType | |
FROM information_schema.columns cl, information_schema.TABLES ss | |
WHERE | |
cl.TABLE_NAME = ss.TABLE_NAME AND | |
cl.TABLE_SCHEMA = @secondDatabaseName AND | |
ss.TABLE_TYPE IN('BASE TABLE', 'VIEW') | |
ORDER BY | |
cl.table_name ) AS t2 ON t1.tableRowType = t2.tableRowType | |
WHERE | |
t1.tableRowType IS NULL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment