拷贝数据库结构的存储过程

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:
今天写的一个COPY一个库的表结构到另外一个表的SP。



DELIMITER $$

DROP PROCEDURE IF EXISTS `test_copy`.`sp_copy_db_schema`$$

CREATE PROCEDURE `test_copy`.`sp_copy_db_schema`(IN db_from varchar(64),IN db_to varchar(64))
BEGIN
  declare i int default 0;
  declare cnt int default 0;
  -- Get the columns number
  select count(1) from information_schema.tables where table_schema = db_from into cnt;
  -- Loop table name one time
  while i < cnt do
    -- Danymic query 
    set @stmt1 = concat('select table_name from information_schema.tables where table_schema = ''',db_from,''' limit ',i,
                     ',1 into @tb_name;'); 
    prepare s1 from @stmt1;
    execute s1;
    deallocate prepare s1
;
    set @stmt1 = '';
    -- Begin copy schema to new database
    set @stmt2 = concat('create table if not exists ',db_to,'.',@tb_name,' select * from ',db_from,'.',@tb_name,' where 1 != 1');
    prepare s2 from @stmt2;
    execute s2;
    deallocate prepare s2
;
    set @stmt2 = '';
    -- Ready for the next table
    set i = i + 1;
  end while;
END$$

DELIMITER ;




测试:

原库:TEST
目标库:TEST_COPY
----------------

mysql> show tables from test \G
*************************** 1. row ***************************
Tables_in_test: a
*************************** 2. row ***************************
Tables_in_test: animals
*************************** 3. row ***************************
Tables_in_test: b
*************************** 4. row ***************************
Tables_in_test: cubeexample
*************************** 5. row ***************************
Tables_in_test: employees
*************************** 6. row ***************************
Tables_in_test: eugene
*************************** 7. row ***************************
Tables_in_test: ewp
*************************** 8. row ***************************
Tables_in_test: info
*************************** 9. row ***************************
Tables_in_test: ip
*************************** 10. row ***************************
Tables_in_test: lk
*************************** 11. row ***************************
Tables_in_test: lk2
*************************** 12. row ***************************
Tables_in_test: lk3
*************************** 13. row ***************************
Tables_in_test: members
*************************** 14. row ***************************
Tables_in_test: mocha_document_content
*************************** 15. row ***************************
Tables_in_test: no_part_tab
*************************** 16. row ***************************
Tables_in_test: os_propertyentry
*************************** 17. row ***************************
Tables_in_test: part_tab
*************************** 18. row ***************************
Tables_in_test: person
*************************** 19. row ***************************
Tables_in_test: rooms
*************************** 20. row ***************************
Tables_in_test: score
*************************** 21. row ***************************
Tables_in_test: shirt
*************************** 22. row ***************************
Tables_in_test: song
*************************** 23. row ***************************
Tables_in_test: song2
*************************** 24. row ***************************
Tables_in_test: t1
*************************** 25. row ***************************
Tables_in_test: t11
*************************** 26. row ***************************
Tables_in_test: t2
*************************** 27. row ***************************
Tables_in_test: table1
*************************** 28. row ***************************
Tables_in_test: table2
*************************** 29. row ***************************
Tables_in_test: table_a
*************************** 30. row ***************************
Tables_in_test: temp
*************************** 31. row ***************************
Tables_in_test: temp2
*************************** 32. row ***************************
Tables_in_test: test1
*************************** 33. row ***************************
Tables_in_test: total_user
33 rows in set (0.01 sec)
---------------------

mysql> show tables from test_copy \G
Empty set (0.00 sec)

-----
mysql> call sp_copy_db_schema('test','test_copy');
Query OK, 0 rows affected (13.97 sec)


--------------
mysql> show tables from test_copy \G
*************************** 1. row ***************************
Tables_in_test_copy: a
*************************** 2. row ***************************
Tables_in_test_copy: animals
*************************** 3. row ***************************
Tables_in_test_copy: b
*************************** 4. row ***************************
Tables_in_test_copy: cubeexample
*************************** 5. row ***************************
Tables_in_test_copy: employees
*************************** 6. row ***************************
Tables_in_test_copy: eugene
*************************** 7. row ***************************
Tables_in_test_copy: ewp
*************************** 8. row ***************************
Tables_in_test_copy: info
*************************** 9. row ***************************
Tables_in_test_copy: ip
*************************** 10. row ***************************
Tables_in_test_copy: lk
*************************** 11. row ***************************
Tables_in_test_copy: lk2
*************************** 12. row ***************************
Tables_in_test_copy: lk3
*************************** 13. row ***************************
Tables_in_test_copy: members
*************************** 14. row ***************************
Tables_in_test_copy: mocha_document_content
*************************** 15. row ***************************
Tables_in_test_copy: no_part_tab
*************************** 16. row ***************************
Tables_in_test_copy: os_propertyentry
*************************** 17. row ***************************
Tables_in_test_copy: part_tab
*************************** 18. row ***************************
Tables_in_test_copy: person
*************************** 19. row ***************************
Tables_in_test_copy: rooms
*************************** 20. row ***************************
Tables_in_test_copy: score
*************************** 21. row ***************************
Tables_in_test_copy: shirt
*************************** 22. row ***************************
Tables_in_test_copy: song
*************************** 23. row ***************************
Tables_in_test_copy: song2
*************************** 24. row ***************************
Tables_in_test_copy: t1
*************************** 25. row ***************************
Tables_in_test_copy: t11
*************************** 26. row ***************************
Tables_in_test_copy: t2
*************************** 27. row ***************************
Tables_in_test_copy: table1
*************************** 28. row ***************************
Tables_in_test_copy: table2
*************************** 29. row ***************************
Tables_in_test_copy: table_a
*************************** 30. row ***************************
Tables_in_test_copy: temp
*************************** 31. row ***************************
Tables_in_test_copy: temp2
*************************** 32. row ***************************
Tables_in_test_copy: test1
*************************** 33. row ***************************
Tables_in_test_copy: total_user

33 rows in set (0.00 sec)








本文转自 david_yeung 51CTO博客,原文链接:http://blog.51cto.com/yueliangdao0608/81280,如需转载请自行联系原作者

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
|
存储 SQL 关系型数据库
【MySQL 数据库】9、存储过程
【MySQL 数据库】9、存储过程
205 0
|
2月前
|
存储 数据库
Navicate 如何导出数据库中的存储过程、事件、视图等?
Navicate 如何导出数据库中的存储过程、事件、视图等?
|
2月前
|
数据库
Google Earth Engine(GEE)——全球树木异体测量和树冠结构(Tallo)数据库
Google Earth Engine(GEE)——全球树木异体测量和树冠结构(Tallo)数据库
22 1
|
3月前
|
存储 Java 数据库
JAVAEE框架数据库技术之13_oracle 之PLSQL技术及存储过程和函数(二)
JAVAEE框架数据库技术之13_oracle 之PLSQL技术及存储过程和函数
38 0
|
15天前
|
存储 SQL Oracle
【Oracle】玩转Oracle数据库(二):体系结构、存储结构与各类参数
【Oracle】玩转Oracle数据库(二):体系结构、存储结构与各类参数
36 7
|
1月前
|
存储 SQL 数据挖掘
视图、触发器和存储过程:提升数据库功能
视图、触发器和存储过程:提升数据库功能
19 1
|
2月前
|
存储 数据库
【数据库】分支与循环&函数&存储过程
【数据库】分支与循环&函数&存储过程
22 1
|
2月前
|
存储 Linux 数据库
期末速成数据库极简版【存储过程】(5)
期末速成数据库极简版【存储过程】(5)
18 0
|
2月前
|
监控 关系型数据库 MySQL
MySQL技能完整学习列表12、性能优化——1、性能指标和监控——2、优化查询和数据库结构——3、硬件和配置优化
MySQL技能完整学习列表12、性能优化——1、性能指标和监控——2、优化查询和数据库结构——3、硬件和配置优化
148 0
|
2月前
|
SQL 存储 算法
【数据库SQL server】数据库系统概述与DBS结构
【数据库SQL server】数据库系统概述与DBS结构
70 0
【数据库SQL server】数据库系统概述与DBS结构

热门文章

最新文章