如何使用php比较文件中的两个mysql表结构

时间:2015-10-27 06:23:13

标签: php mysql

我有两个mysql转储文件。

dump1.sql

CREATE TABLE `designs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `active` tinyint(1) NOT NULL,
  `template` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

dump2.sql

CREATE TABLE `designs` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `act` tinyint(1) NOT NULL,
      `temp` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

如何在dump1&中找到两个表结构的差异dump2?

2 个答案:

答案 0 :(得分:1)

第二个查询选择table1的所有COLUMN_NAME,并使用NOT IN我们比较两个表的COLUMN: -

SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_SCHEMA = 'your_schema' AND 
      TABLE_NAME = 'table2' AND 
      COLUMN_NAME NOT IN (
         SELECT COLUMN_NAME
         FROM INFORMATION_SCHEMA.COLUMNS 
         WHERE TABLE_SCHEMA = 'your_schema' AND 
               TABLE_NAME = 'table1');

答案 1 :(得分:0)

但是你的表名应该不同来检查一下。 试试这个,

select column_name
      ,max(case when table_name = 'table_1' then 'Yes' end) as in_table_1
      ,max(case when table_name = 'table_2' then 'Yes' end) as in_table_2
  from information_schema.columns
  where table_name in('table_1', 'table_2')
   and table_schema = 'your_database'
  group
    by column_name
 order
    by column_name;

或者,如果您想使用文件,

mysqldump --skip-comments --skip-extended-insert -u root -p dbName1>file1.sql
mysqldump --skip-comments --skip-extended-insert -u root -p dbName2>file2.sql

diff file1.sql file2.sql

有关详细信息,请参阅this link此处提供了所有可能的方法。