码迷,mamicode.com
首页 > 数据库 > 详细

【草稿整理ing】【mysql】两个数据库结构对比

时间:2020-03-05 01:31:54      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:拼接   http   continue   hand   set   into   data   lte   rom   

 

 

  1 -- 1.将mysql分隔符从;设置为&
  2 DELIMITER &
  3 
  4 -- 2.如果存在存储过程getdatabaseCount则删除
  5 DROP PROCEDURE IF EXISTS `getdatabaseCount` &
  6 -- 3.定义存储过程,获取特定数据库的数量
  7 -- (传入参数database_name字符串类型,为数据库名;传出参数count_date整数类型,为数量)
  8 CREATE DEFINER=`root`@`localhost` PROCEDURE getdatabaseCount(IN database_name CHAR(20), OUT count_date INT) 
  9     BEGIN
 10     -- 4.声明变量
 11     DECLARE $sqltext VARCHAR(1000);
 12     -- 5.动态sql,把sql返回值放到@count_date中
 13     SET $sqltext = CONCAT(SELECT COUNT(*) into @count_date FROM information_schema.COLUMNS t where t.`TABLE_SCHEMA` = \‘‘, database_name, \;);
 14     SET @sqlcounts := $sqltext;
 15     -- 6.预编释,stmt预编释变量的名称
 16     PREPARE stmt FROM @sqlcounts;
 17     -- 7.执行SQL语句
 18     EXECUTE stmt; 
 19     -- 8.释放资源
 20     DEALLOCATE PREPARE stmt;
 21     -- 9.获取动态SQL语句返回值
 22     SET count_date = @count_date;
 23     END
 24 -- 10.定义存储过程结束
 25 &
 26 
 27 -- 2.如果存在存储过程getCount则删除
 28 DROP PROCEDURE IF EXISTS `getTableCount` &
 29 -- 3.定义存储过程,获取特定数据库表的数量
 30 -- (传入参数database_name字符串类型,为数据库名;传入参数table_name字符串类型,为表名;传出参数count_date整数类型,为数量)
 31 CREATE DEFINER=`root`@`localhost` PROCEDURE getTableCount(IN database_name CHAR(20), IN table_name CHAR(200), OUT count_date INT) 
 32     BEGIN
 33     -- 4.声明变量
 34     DECLARE $sqltext VARCHAR(1000);
 35     -- 5.动态sql,把sql返回值放到@count_date中
 36     SET $sqltext = CONCAT(SELECT COUNT(*) into @count_date FROM information_schema.COLUMNS t where t.`TABLE_SCHEMA` = \‘‘, database_name, \ and t.`TABLE_NAME` = \‘‘, table_name, \;);
 37     SET @sqlcounts := $sqltext;
 38     -- 6.预编释,stmt预编释变量的名称
 39     PREPARE stmt FROM @sqlcounts;
 40     -- 7.执行SQL语句
 41     EXECUTE stmt; 
 42     -- 8.释放资源
 43     DEALLOCATE PREPARE stmt;
 44     -- 9.获取动态SQL语句返回值
 45     SET count_date = @count_date;
 46     END
 47 -- 10.定义存储过程结束
 48 &
 49 
 50 
 51 
 52 
 53 -- 2.如果存在存储过程getColumnCount则删除
 54 DROP PROCEDURE IF EXISTS `getColumnCount` &
 55 -- 3.定义存储过程,获取特定数据库表列的数量
 56 -- (传入参数database_name字符串类型,为数据库名;传入参数table_name字符串类型,为表名;传入参数column_name字符串类型,为列名;传出参数count_date整数类型,为数量)
 57 CREATE DEFINER=`root`@`localhost` PROCEDURE getColumnCount(IN database_name CHAR(20), IN table_name CHAR(200), IN column_name CHAR(200), OUT count_date INT) 
 58     BEGIN
 59     -- 4.声明变量
 60     DECLARE $sqltext VARCHAR(1000);
 61     -- 5.动态sql,把sql返回值放到@count_date中
 62     SET $sqltext = CONCAT(SELECT COUNT(*) into @count_date FROM information_schema.COLUMNS t where t.`TABLE_SCHEMA` = \‘‘, database_name, \ and t.`TABLE_NAME` = \‘‘, table_name, \ and t.`COLUMN_NAME` = \‘‘, column_name, \;);
 63     SET @sqlcounts := $sqltext;
 64     -- 6.预编释,stmt预编释变量的名称
 65     PREPARE stmt FROM @sqlcounts;
 66     -- 7.执行SQL语句
 67     EXECUTE stmt; 
 68     -- 8.释放资源
 69     DEALLOCATE PREPARE stmt;
 70     -- 9.获取动态SQL语句返回值
 71     SET count_date = @count_date;
 72     END
 73 -- 10.定义存储过程结束
 74 &
 75 
 76 
 77 -- 2.如果存在存储过程getColumnInfo则删除
 78 DROP PROCEDURE IF EXISTS `getColumnInfo` &
 79 -- 3.定义存储过程,获取特定数据库表列的信息
 80 -- (传入参数database_name字符串类型,为数据库名;传入参数table_name字符串类型,为表名;传入参数column_name字符串类型,为列名;传入参数column_info字符串类型,列信息;传出参数result_data字符串类型,信息)
 81 CREATE DEFINER=`root`@`localhost` PROCEDURE getColumnInfo(IN database_name CHAR(20), IN table_name CHAR(200), IN column_name CHAR(200), IN column_info CHAR(50), OUT result_data CHAR(20)) 
 82     BEGIN
 83     -- 4.声明变量
 84     DECLARE $sqltext VARCHAR(1000);
 85     -- 5.动态sql,把sql返回值放到@count_date中
 86     SET $sqltext = CONCAT(SELECT t., column_info, into @column_info FROM information_schema.COLUMNS t where t.`TABLE_SCHEMA` = \‘‘, database_name, \ and t.`TABLE_NAME` = \‘‘, table_name, \ and t.`COLUMN_NAME` = \‘‘, column_name, \;);
 87     SET @sqlcounts := $sqltext;
 88     -- 6.预编释,stmt预编释变量的名称
 89     PREPARE stmt FROM @sqlcounts;
 90     -- 7.执行SQL语句
 91     EXECUTE stmt; 
 92     -- 8.释放资源
 93     DEALLOCATE PREPARE stmt;
 94     -- 9.获取动态SQL语句返回值
 95     SET result_data = @column_info;
 96     END
 97 -- 10.定义存储过程结束
 98 &
 99 
100 -- 11.如果存在存储过程comparison则删除
101 DROP PROCEDURE IF EXISTS `comparison` &
102 -- 12.定义存储过程,获取指定数据库关键词的表列名
103 -- (传入参数database_n字符串类型,数据库名;传入参数collation_n字符串类型,具体编码类型;传入参数key_name字符串类型,为关键字;传出参数tableColumnNames字符串类型,表列名)
104 CREATE DEFINER=`root`@`localhost` PROCEDURE comparison(IN database_1 CHAR(20), IN database_2 CHAR(20), IN column_info CHAR(50), OUT info TEXT) 
105     BEGIN
106     -- 13.声明变量。database_name查询出来的数据库,table_name查询出来的表名,column_name查询出来的列名,collation_name查询出来的具体编码类型
107     DECLARE database_name, table_name, column_name, collation_name, result_data_1, result_data_2 CHAR(200);
108     DECLARE this_info TEXT DEFAULT ‘‘;
109     DECLARE database_count_1, database_count_2, resoult_count INT DEFAULT 0;
110         -- 14.定义游标结束标识,默认为0
111     DECLARE stopflag INT DEFAULT 0;
112     -- 15.定义游标,其实就是临时存储sql返回的集合
113     DECLARE sql_resoult CURSOR FOR SELECT t.TABLE_SCHEMA, t.TABLE_NAME, t.COLUMN_NAME, t.COLLATION_NAME FROM information_schema.COLUMNS t;
114     -- 16.游标结束就设置为1
115     DECLARE CONTINUE HANDLER FOR NOT FOUND SET stopflag=1;
116     CALL getdatabaseCount(database_1, database_count_1);
117     CALL getdatabaseCount(database_2, database_count_2);
118     IF (database_count_1 <> 0 AND database_count_2 <> 0) THEN
119         -- 17.打开游标
120         OPEN sql_resoult;
121             -- 18.读取游标中数据,存储到指定变量
122             FETCH sql_resoult INTO database_name, table_name, column_name, collation_name;
123             -- 19.没有结束继续往下走
124             WHILE (stopflag=0) DO
125                 BEGIN
126                 -- 20.判断数据库是否为输入的数据库名称,和,指定具体编码类型,和,不含.
127                 IF (database_name=database_1) THEN
128                     -- 21.调用存储过程,获取特定表列关键词的数量
129                     CALL getTableCount(database_2, table_name, resoult_count);
130                     -- 22.如果数量不等于0,那么记录表列名
131                     IF (resoult_count <> 0) THEN
132                         CALL getColumnCount(database_2, table_name, column_name, resoult_count);
133                         -- 23.拼接字符串,不可直接用传出变量设值
134                         IF (resoult_count <> 0) THEN
135                             CALL getColumnInfo(database_1, table_name, column_name, column_info, result_data_1);
136                             CALL getColumnInfo(database_2, table_name, column_name, column_info, result_data_2);
137                             -- 23.拼接字符串,不可直接用传出变量设值
138                             IF (result_data_1 <> result_data_2) THEN
139                                 IF (this_info IS NULL OR this_info=‘‘) THEN
140                                     SET this_info=CONCAT(table_name, 表的, column_name, 列的, column_info, 不一样;\n);
141                                 ELSE
142                                     SET this_info=CONCAT(this_info, table_name, 表的, column_name, 列的, column_info, 不一样;\n);
143                                 END IF;
144                             END IF;
145                         ELSE
146                             IF (this_info IS NULL OR this_info=‘‘) THEN
147                                 SET this_info=CONCAT(database_2, , table_name, 表的, column_name, 列不存在;\n);
148                             ELSE
149                                 SET this_info=CONCAT(this_info, database_2, , table_name, 表的, column_name, 列不存在;\n);
150                             END IF;
151                         END IF;
152                     ELSE
153                         IF (this_info IS NULL OR this_info=‘‘) THEN
154                             SET this_info=CONCAT(database_2, , table_name, 表不存在;\n);
155                         ELSE
156                             SET this_info=CONCAT(this_info, database_2, , table_name, 表不存在;\n);
157                         END IF;
158                     END IF;
159                 ELSE
160                     IF (database_name=database_2) THEN
161                         CALL getTableCount(database_1, table_name, resoult_count);
162                         IF (resoult_count <> 0) THEN
163                             CALL getColumnCount(database_1, table_name, column_name, resoult_count);
164                             IF (resoult_count = 0) THEN
165                                 IF (this_info IS NULL OR this_info=‘‘) THEN
166                                     SET this_info=CONCAT(database_1, , table_name, 表的, column_name, 列不存在;\n);
167                                 ELSE
168                                     SET this_info=CONCAT(this_info, database_1, , table_name, 表的, column_name, 列不存在;\n);
169                                 END IF;
170                             END IF;
171                         ELSE
172                             IF (this_info IS NULL OR this_info=‘‘) THEN
173                                 SET this_info=CONCAT(database_1, , table_name, 表不存在;\n);
174                             ELSE
175                                 SET this_info=CONCAT(this_info, database_1, , table_name, 表不存在;\n);
176                             END IF;
177                         END IF;
178                     END IF;
179                 END IF;
180                 -- 24.读取游标中数据,存储到指定变量。(和18一样)
181                 FETCH sql_resoult INTO database_name, table_name, column_name, collation_name;
182                 END;
183             END WHILE;
184         -- 25.关闭游标
185         CLOSE sql_resoult;
186     ELSE
187         IF (database_count_1 = 0 AND database_count_2 = 0) THEN
188             SET this_info = CONCAT(database_1, , database_2, 数据库不存在或为空数据库);
189         ELSE
190             IF (database_count_1 = 0) THEN
191                 SET this_info = CONCAT(database_1, 数据库不存在或为空数据库);
192             ELSE
193                 SET this_info = CONCAT(database_2, 数据库不存在或为空数据库);
194             END IF;
195         END IF;
196     END IF;
197     -- 26.把数据放到传出参数
198     SET info=this_info;
199     END
200 -- 27.定义存储过程结束
201 &
202 -- 28.将mysql分隔符从&设置为;
203 DELIMITER ;
204 -- 29.设置变量
205 SET @database_1=my_test;
206 SET @database_2=my_test2;
207 SET @column_info=data_type;
208 SET @count=‘‘;
209 -- 30.调用存储过程
210 CALL comparison(@database_1, @database_2, @column_info, @count);
211 -- 31.打印
212 SELECT @count;
213 -- 32.如果存在存储过程则删除
214 DROP PROCEDURE IF EXISTS `comparison`;

 

 

技术图片

 

【草稿整理ing】【mysql】两个数据库结构对比

标签:拼接   http   continue   hand   set   into   data   lte   rom   

原文地址:https://www.cnblogs.com/xiaostudy/p/12417151.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!