-- 使用test库 `test_project_management` `oel_project_management`
USE test;
-- 旧表
DROP TABLE old_column_info;
CREATE TABLE `old_column_info` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`table_schema` VARCHAR(64) NOT NULL COMMENT '数据库',
`table_name` VARCHAR(64) NOT NULL COMMENT '表名',
`table_comment` VARCHAR(128) DEFAULT NULL COMMENT '表备注',
`column_name` VARCHAR(64) NOT NULL COMMENT '列名',
`column_type` VARCHAR(32) NOT NULL COMMENT '列类型',
`is_nullable` VARCHAR(8) NOT NULL COMMENT '是否可为空',
`column_default` VARCHAR(32) DEFAULT NULL COMMENT '默认值',
`column_comment` VARCHAR(128) DEFAULT NULL COMMENT '列备注',
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
-- 旧表
DROP TABLE new_column_info;
CREATE TABLE `new_column_info` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`table_schema` VARCHAR(64) NOT NULL COMMENT '数据库',
`table_name` VARCHAR(64) NOT NULL COMMENT '表名',
`table_comment` VARCHAR(128) DEFAULT NULL COMMENT '表备注',
`column_name` VARCHAR(64) NOT NULL COMMENT '列名',
`column_type` VARCHAR(32) NOT NULL COMMENT '列类型',
`is_nullable` VARCHAR(8) NOT NULL COMMENT '是否可为空',
`column_default` VARCHAR(32) DEFAULT NULL COMMENT '默认值',
`column_comment` VARCHAR(128) DEFAULT NULL COMMENT '列备注',
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
-- 插入旧表结构
INSERT INTO `old_column_info` (
`table_schema`,
`table_name`,
`table_comment`,
`column_name`,
`column_type`,
`is_nullable`,
`column_default`,
`column_comment`
)
SELECT 'project_management', tab.table_name, tab.table_comment, col.`COLUMN_NAME`, col.`COLUMN_TYPE`, col.`IS_NULLABLE`, col.`COLUMN_DEFAULT`, col.`COLUMN_COMMENT`
FROM information_schema.`TABLES` tab, information_schema.columns col
WHERE col.table_schema = tab.`TABLE_SCHEMA`
AND col.table_name = tab.`TABLE_NAME`
AND col.table_schema = 'test_project_management';
-- 插入旧表结构
INSERT INTO `new_column_info` (
`table_schema`,
`table_name`,
`table_comment`,
`column_name`,
`column_type`,
`is_nullable`,
`column_default`,
`column_comment`
)
SELECT 'project_management', tab.table_name, tab.table_comment, col.`COLUMN_NAME`, col.`COLUMN_TYPE`, col.`IS_NULLABLE`, col.`COLUMN_DEFAULT`, col.`COLUMN_COMMENT`
FROM information_schema.`TABLES` tab, information_schema.columns col
WHERE col.table_schema = tab.`TABLE_SCHEMA`
AND col.table_name = tab.`TABLE_NAME`
AND col.table_schema = 'oel_project_management';
-- 查看字段名称未变单数据结构变了的
SELECT o.table_schema, o.table_name, o.table_comment,
o.`COLUMN_NAME` AS old_column_name, o.`COLUMN_TYPE` AS old_column_type,
n.`COLUMN_NAME` AS new_column_name, n.`COLUMN_TYPE` AS new_column_type
FROM old_column_info o, new_column_info n
WHERE o.`table_schema` = n.table_schema
AND o.`table_name` = n.table_name
AND o.`column_name` = n.column_name
AND (o.column_type != n.column_type
OR o.`is_nullable` != n.`is_nullable`);
-- 查看字段名称未变单数据结构变了的
SELECT o.table_schema, o.table_name, o.table_comment,
o.`COLUMN_NAME` AS old_column_name, o.`COLUMN_TYPE` AS old_column_type,
n.`COLUMN_NAME` AS new_column_name, n.`COLUMN_TYPE` AS new_column_type
FROM old_column_info o, new_column_info n
WHERE o.`table_schema` = n.table_schema
AND o.`table_name` = n.table_name
AND o.`column_name` = n.column_name
AND o.column_type != n.column_type;
-- 查询老库里存在但新库里不存在的数据
SELECT
o.table_schema, o.table_name, o.table_comment,
o.`COLUMN_NAME` AS old_column_name, o.`COLUMN_TYPE` AS old_column_type
FROM old_column_info o
WHERE NOT EXISTS(
SELECT 1 FROM new_column_info n
WHERE o.`table_schema` = n.table_schema
AND o.`table_name` = n.table_name
AND o.`column_name` = n.column_name
);
-- 查询新库里存在但老库里不存在的数据
SELECT
n.table_schema, n.table_name, n.table_comment,
n.`COLUMN_NAME` AS new_column_name, n.`COLUMN_TYPE` AS new_column_type
FROM new_column_info n
WHERE NOT EXISTS(
SELECT 1 FROM old_column_info o
WHERE o.`table_schema` = n.table_schema
AND o.`table_name` = n.table_name
AND o.`column_name` = n.column_name
);