查询数据库中所有表名
select table_name from information_schema.tables where table_schema='数据库名' and table_type='base table';
查询指定数据库中指定表的所有字段名column_name
select column_name from information_schema.columns where table_schema='数据库名' and table_name='表名';

#查看分布式系统中不同库所有相同表名的comment的不同的表

select * from
(
select TABLE_NAME, TABLE_COMMENT from information_schema.tables where table_schema='test1_1207_bak' and table_type='base table'
) a
INNER JOIN
(
select TABLE_NAME, TABLE_COMMENT from information_schema.tables where table_schema='test2_1207_bak' and table_type='base table'
) b on a.TABLE_NAME=b.TABLE_NAME
where a.TABLE_COMMENT!=b.TABLE_COMMENT;

#查看分布式库中同一表的同一字段类型,字段长度和comment等不同之处

select a.table_name,b.table_name,a.column_name,b.column_name,a.data_type,b.data_type,a.COLUMN_TYPE,b.COLUMN_TYPE,
a.CHARACTER_MAXIMUM_LENGTH,b.CHARACTER_MAXIMUM_LENGTH,a.NUMERIC_PRECISION,b.NUMERIC_PRECISION,
a.NUMERIC_SCALE,b.NUMERIC_SCALE,a.COLUMN_KEY,b.COLUMN_KEY,a.EXTRA,b.EXTRA,a.my_COLUMN_COMMENT,b.my_COLUMN_COMMENT from
(
select
table_name,column_name,data_type,COLUMN_TYPE,CHARACTER_MAXIMUM_LENGTH,NUMERIC_PRECISION,NUMERIC_SCALE,COLUMN_KEY,EXTRA,
CASE
WHEN COLUMN_COMMENT is null THEN ''
ELSE COLUMN_COMMENT
END as my_COLUMN_COMMENT
from information_schema.columns
where table_schema='test2_1207_bak'
) a
INNER JOIN
(
select
table_name,column_name,data_type,COLUMN_TYPE,CHARACTER_MAXIMUM_LENGTH,NUMERIC_PRECISION,NUMERIC_SCALE,COLUMN_KEY,EXTRA,
CASE
WHEN COLUMN_COMMENT is null THEN ''
ELSE COLUMN_COMMENT
END as my_COLUMN_COMMENT
from information_schema.columns
where table_schema='test1_1207_bak'
) b on a.table_name=b.table_name and a.column_name=b.column_name
where a.data_type!=b.data_type or a.my_COLUMN_COMMENT!=b.my_COLUMN_COMMENT;

#查询缺失表语句:

select table_name from information_schema.tables where table_schema='test1_1207_bak' and table_type='base table' and table_name not in(
select table_name from information_schema.tables where table_schema='test2_1207_bak' and table_type='base table'
);

#改用存储过程实现为

# 对比不同库中表区别存储过程
DROP PROCEDURE IF EXISTS p_find_dif_table;
delimiter $$
create procedure p_find_dif_table(database_name1 VARCHAR(100),database_name2 VARCHAR(100))
begin
-- 比较database_name1库表名与database_name2表名不同的表
select table_name as db1_table_name from information_schema.tables where table_schema=database_name1 and table_type='base table'
and table_name not in(
select table_name from information_schema.tables where table_schema=database_name2 and table_type='base table'
);
-- 比较database_name2库表名与database_name1表名不同的表
select table_name as db2_table_name from information_schema.tables where table_schema=database_name2 and table_type='base table'
and table_name not in(
select table_name from information_schema.tables where table_schema=database_name1 and table_type='base table'
);
end;
$$
delimiter ;
-- 调用存储过程
CALL p_find_dif_table('test1_1207_bak','test2_1207_bak');

#查询具体表缺失字段语句:

select column_name from information_schema.columns where table_schema='test2_1207_bak' and table_name='oi_auto'
and column_name not in (
select column_name from information_schema.columns where table_schema='test1_1207_bak' and table_name='oi_auto'
);

# 改用存储过程和游标实现,对比数据库缺失字段

drop TABLE if EXISTS `release_no_field`;
CREATE TABLE `release_no_field` (
`column_name` varchar(255),
`table_name` varchar(255),
`db_name` varchar(255)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='正式库中缺失字段'; drop TABLE if EXISTS `beta_no_field`;
CREATE TABLE `beta_no_field` (
`column_name` varchar(255),
`table_name` varchar(255),
`db_name` varchar(255)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='测试库中缺失字段'; # 对比不同库中缺失字段存储过程
DROP PROCEDURE IF EXISTS p_find_dif_column;
delimiter $$
create procedure p_find_dif_column(release_db VARCHAR(100),beta_db VARCHAR(100))
begin
DECLARE cur_table VARCHAR(100);
DECLARE no_more_record INT DEFAULT 0;
DECLARE cur_record CURSOR FOR
SELECT TABLE_NAME from information_schema.tables
where table_schema=release_db and table_type='base table' and TABLE_NAME not in('release_no_field','beta_no_field');
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_record = 1;
OPEN cur_record;
FETCH cur_record INTO cur_table; delete from release_no_field;
delete from beta_no_field; WHILE no_more_record != 1 DO
-- 正式库中缺失字段
INSERT INTO release_no_field(column_name,table_name,db_name) select column_name,table_name,release_db from information_schema.columns where table_schema=beta_db and table_name=cur_table
and column_name not in (
select column_name from information_schema.columns where table_schema=release_db and table_name=cur_table
);
-- 测试库中缺失字段
INSERT INTO beta_no_field(column_name,table_name,db_name) select column_name,table_name,beta_db from information_schema.columns where table_schema=release_db and table_name=cur_table
and column_name not in (
select column_name from information_schema.columns where table_schema=beta_db and table_name=cur_table
);
FETCH cur_record INTO cur_table;
END WHILE;
CLOSE cur_record; SELECT * from release_no_field;
SELECT * from beta_no_field;
end;
$$
delimiter ;

对比不同库同一表同一字段类型不一致

SELECT
hosp_release.table_name AS table_name,
hosp_release.column_name AS column_name,
hosp_release.column_type AS hosp_release_column_type,
hosp_beta.column_type AS hosp_beta_column_type
FROM
(
select table_name,column_name,column_type from information_schema.columns where table_schema='hosp2' and table_name in (
select TABLE_NAME from information_schema.tables where table_schema='hosp2' and table_type='base table'
)) hosp_release ,
(
select table_name,column_name,column_type from information_schema.columns where table_schema='hosp_shunde_0226' and table_name in (
select TABLE_NAME from information_schema.tables where table_schema='hosp_shunde_0226' and table_type='base table'
)) hosp_beta
WHERE hosp_release.table_name=hosp_beta.table_name and hosp_release.column_name=hosp_beta.column_name and hosp_release.column_type!=hosp_beta.column_type

MySql 查询数据库中所有表名以及对比分布式库中字段和表的不同的更多相关文章

  1. Mysql查询数据库 整理

    一.       查询数据: 查询所有列:SELECT * FROM student; 查询指定列:SELECT id,NAME,gender FROM student; 格式:select字段名, ...

  2. 在这个应用中,我使用了 MQ 来处理异步流程、Redis 缓存热点数据、MySQL 持久化数据,还有就是在系统中调用另外一个业务系统的接口,对我的应用来说这些都是属于 RPC 调用,而 MQ、MySQL 持久化的数据也会存在于一个分布式文件系统中,他们之间的调用也是需要用 RPC 来完成数据交互的。

    在这个应用中,我使用了 MQ 来处理异步流程.Redis 缓存热点数据.MySQL 持久化数据,还有就是在系统中调用另外一个业务系统的接口,对我的应用来说这些都是属于 RPC 调用,而 MQ.MySQ ...

  3. bpexpdate – 更改映像目录库中备份的截止日期以及介质目录库中介质的截止日期nbu

    1.根据bpdbjobs查找backupidbpdbjobs -jobid xxx -all_columns|grep backupid 2.查看数据保留时间[root@backup]# bpimag ...

  4. MySql 查询数据库中所有表名

    查询数据库中所有表名select table_name from information_schema.tables where table_schema='csdb' and table_type= ...

  5. MySQL 查询 数据库有多少表 表名是哪些

    1.查询sjcenter数据库里开头为sj_demo和sj_onlyinv的所有表的总条数 select sum(table_rows) from (select table_name,table_r ...

  6. mysql查询数据库中包含某字段(列名)的所有表

    SELECT TABLE_NAME '表名',TABLE_SCHEMA '数据库名',ORDINAL_POSITION '顺序',COLUMN_NAME '字段',DATA_TYPE '类型' ,CH ...

  7. mysql 查询数据库或某张表有多大(字节)

    转载:https://www.cnblogs.com/diandiandidi/p/5582309.html 1.要查询表所占的容量,就是把表的数据和索引加起来就可以了 select sum(DATA ...

  8. mysql查询数据库大小和表

    每个mysql都有一个库information_schema,里面有一张表TABLES存储了所有数据库表的信息,因此,可以从这张表中查看数据库大小和表大小 查询数据库大小 ,),'GB') as da ...

  9. MySQL查询数据库中表名或字段名

    查询数据库中所有表名 select table_name from information_schema.tables where table_schema='csdb' and table_type ...

随机推荐

  1. 第八篇:文件共享和使用 dup 函数创建新描述符的区别

    前言 文件共享是指同时打开一个文件 用 dup 函数能对指定文件描述符再创建一个新的描述符,且这个新的描述符和旧的描述符指向的是同一个文件. 这两种行为有什么区别呢?下面给出的两张文件系统的图形象的解 ...

  2. jeesite学习笔记(一) 项目框架

    JeeSite是基于多个优秀的开源项目,高度整合封装而成的高效,高性能,强安全性的开源Java EE快速开发平台. 在github上,对jeesite平台有详细的介绍,这里稍作整理,给出项目的内置功能 ...

  3. Windows查看网络端口被占用情况netstat命令

    在windows命令行窗口下执行: C:\>netstat -aon|findstr "80" TCP     127.0.0.1:80         0.0.0.0:0  ...

  4. SQL Server快捷方式丢了怎么启动

    为了帮助网友解决“SQL Server快捷方式丢了怎么启动”相关的问题,中国学网通过互联网对“SQL Server快捷方式丢了怎么启动”相关的解决方案进行了整理,用户详细问题包括:Microsoft ...

  5. 【BZOJ4552】[Tjoi2016&Heoi2016]排序 二分+线段树

    [BZOJ4552][Tjoi2016&Heoi2016]排序 Description 在2016年,佳媛姐姐喜欢上了数字序列.因而他经常研究关于序列的一些奇奇怪怪的问题,现在他在研究一个难题 ...

  6. 170110、Spring 事物机制总结

    spring两种事物处理机制,一是声明式事物,二是编程式事物 声明式事物 1)Spring的声明式事务管理在底层是建立在AOP的基础之上的.其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加 ...

  7. SPOJ Number of Palindromes(回文树)

    Number of Palindromes Time Limit: 100MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu ...

  8. YOLO v1论文笔记

    You Only Look Once:Unified, Real-Time Object Detection   论文链接:https://arxiv.org/abs/1506.02640 Homep ...

  9. 短URL DH 密钥交换算法

    w  追问:0-短URL 的时效性,(比如微信个人账户的永久二维码和群的约7天时效二维码):1-0中的时效性对于算法选择的影响,比如简单的HAS映射.sha1.md5......   https:// ...

  10. Linux命令之pip

    使用pip进行install,sudo pip install xxx 使用pip进行update,sudo pip install --update xxx 使用pip设置超时时间,sudo pip ...