草稿整理ingmysql两个数据库结构对比
Posted xiaostudy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了草稿整理ingmysql两个数据库结构对比相关的知识,希望对你有一定的参考价值。
1、草稿:
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, database_table_no 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 AND INSTR(database_table_no, CONCAT(database_2, \'_\', table_name)) = 0) 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 SET database_table_no=CONCAT(database_table_no, \';\', database_2, \'_\', table_name, \';\'); 159 END IF; 160 ELSE 161 IF (database_name=database_2 AND INSTR(database_table_no, CONCAT(database_1, \'_\', table_name)) = 0) THEN 162 CALL getTableCount(database_1, table_name, resoult_count); 163 IF (resoult_count <> 0) THEN 164 CALL getColumnCount(database_1, table_name, column_name, resoult_count); 165 IF (resoult_count = 0) THEN 166 IF (this_info IS NULL OR this_info=\'\') THEN 167 SET this_info=CONCAT(database_1, \'的\', table_name, \'表的\', column_name, \'列不存在;\\n\'); 168 ELSE 169 SET this_info=CONCAT(this_info, database_1, \'的\', table_name, \'表的\', column_name, \'列不存在;\\n\'); 170 END IF; 171 END IF; 172 ELSE 173 IF (this_info IS NULL OR this_info=\'\') THEN 174 SET this_info=CONCAT(database_1, \'的\', table_name, \'表不存在;\\n\'); 175 ELSE 176 SET this_info=CONCAT(this_info, database_1, \'的\', table_name, \'表不存在;\\n\'); 177 END IF; 178 SET database_table_no=CONCAT(database_table_no, \';\', database_1, \'_\', table_name, \';\'); 179 END IF; 180 END IF; 181 END IF; 182 -- 24.读取游标中数据,存储到指定变量。(和18一样) 183 FETCH sql_resoult INTO database_name, table_name, column_name, collation_name; 184 END; 185 END WHILE; 186 -- 25.关闭游标 187 CLOSE sql_resoult; 188 ELSE 189 IF (database_count_1 = 0 AND database_count_2 = 0) THEN 190 SET this_info = CONCAT(database_1, \'和\', database_2, \'数据库不存在或为空数据库\'); 191 ELSE 192 IF (database_count_1 = 0) THEN 193 SET this_info = CONCAT(database_1, \'数据库不存在或为空数据库\'); 194 ELSE 195 SET this_info = CONCAT(database_2, \'数据库不存在或为空数据库\'); 196 END IF; 197 END IF; 198 END IF; 199 -- 26.把数据放到传出参数 200 SET info=this_info; 201 END 202 -- 27.定义存储过程结束 203 & 204 -- 28.将mysql分隔符从&设置为; 205 DELIMITER ; 206 -- 29.设置变量 207 SET @database_1=\'my_test\'; 208 SET @database_2=\'my_test2\'; 209 SET @column_info=\'data_type\'; 210 SET @count=\'\'; 211 -- 30.调用存储过程 212 CALL comparison(@database_1, @database_2, @column_info, @count); 213 -- 31.打印 214 SELECT @count; 215 -- 32.如果存在存储过程则删除 216 DROP PROCEDURE IF EXISTS `comparison`;
2、整理:
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, \'\\JSP基础学习