今天做了一个小Model,就是把另一数据库中的表复制到目标数据库中,但是复制到目标数据库中的表中的记录为空
思路:
1 读取建表的SQL语句
2 通过jdbc执行
3 对新建表进行重新命名
精要:
1 读取建表的SQL语句
show create table table_name;
测试:
mysql> use test; Database changed mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | stu1 | | stu2 | | stu3 | | stu4 | | stu5 | +----------------+ 5 rows in set (0.01 sec) mysql> show create table stu1; +-------+------------------------------------------------------------------------------------------ ------------------------------------------------------------+ | Table | Create Table | +-------+------------------------------------------------------------------------------------------ ------------------------------------------------------------+ | stu1 | CREATE TABLE `stu1` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'a', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='备注1' | +-------+------------------------------------------------------------------------------------------ ------------------------------------------------------------+ 1 row in set (0.00 sec) mysql>
第二次测试
mysql> show create table stu1 \G; *************************** 1. row *************************** Table: stu1 Create Table: CREATE TABLE `stu1` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'a', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='备注1' 1 row in set (0.00 sec) ERROR: No query specified mysql>
2 对数据表进行重新命名
alter table table_name rename to new_table_name;
测试:
mysql> alter table stu5 rename to stu6; Query OK, 0 rows affected (0.05 sec) mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | stu1 | | stu2 | | stu3 | | stu4 | | stu6 | +----------------+ 5 rows in set (0.01 sec) mysql>