对比数据库字段不同的sql (mysql版)

Posted 飞羽127

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对比数据库字段不同的sql (mysql版)相关的知识,希望对你有一定的参考价值。

-- 使用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
);


以上是关于对比数据库字段不同的sql (mysql版)的主要内容,如果未能解决你的问题,请参考以下文章

数据库MYSQL中,如果有个字段是自增型,如何写插入语句?

数据库mongodb和mysql的对比

MySQL--如何快速对比数据

如何对比两个相同数据库表的不同

mysql 查询时 按照某个字段计算后的值 排序?

在mysql中将两个表字段中相同的值进行对比!