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

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介:
今天写的一个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,如需转载请自行联系原作者

相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
27天前
|
数据管理 关系型数据库 MySQL
数据管理服务DMS支持MySQL数据库的无锁结构变更
本文介绍了使用Sysbench准备2000万数据并进行全表字段更新的操作。通过DMS的无锁变更功能,可在不锁定表的情况下完成结构修改,避免了传统方法中可能产生的锁等待问题。具体步骤包括:准备数据、提交审批、执行变更及检查表结构,确保变更过程高效且不影响业务运行。
55 2
|
5月前
|
存储 SQL 关系型数据库
MySql数据库---存储过程
MySql数据库---存储过程
79 5
|
7月前
|
存储 SQL 安全
【数据库高手的秘密武器:深度解析SQL视图与存储过程的魅力——封装复杂逻辑,实现代码高复用性的终极指南】
【8月更文挑战第31天】本文通过具体代码示例介绍 SQL 视图与存储过程的创建及应用优势。视图作为虚拟表,可简化复杂查询并提升代码可维护性;存储过程则预编译 SQL 语句,支持复杂逻辑与事务处理,增强代码复用性和安全性。通过创建视图 `high_earners` 和存储过程 `get_employee_details` 及 `update_salary` 的实例,展示了二者在实际项目中的强大功能。
73 1
|
7月前
|
存储 SQL JSON
【Azure Logic App】微软云逻辑应用连接到数据库,执行存储过程并转换执行结果为JSON数据
【Azure Logic App】微软云逻辑应用连接到数据库,执行存储过程并转换执行结果为JSON数据
【Azure Logic App】微软云逻辑应用连接到数据库,执行存储过程并转换执行结果为JSON数据
|
8月前
|
Oracle 关系型数据库 数据库连接
|
7月前
|
JSON 数据格式 Java
化繁为简的魔法:Struts 2 与 JSON 联手打造超流畅数据交换体验,让应用飞起来!
【8月更文挑战第31天】在现代 Web 开发中,JSON 成为数据交换的主流格式,以其轻量、易读和易解析的特点受到青睐。Struts 2 内置对 JSON 的支持,结合 Jackson 库可便捷实现数据传输。本文通过具体示例展示了如何在 Struts 2 中进行 JSON 数据的序列化与反序列化,并结合 AJAX 技术提升 Web 应用的响应速度和用户体验。
189 0
|
9月前
|
存储 关系型数据库 MySQL
|
9月前
|
存储 SQL 关系型数据库
MySQL数据库进阶第四篇(视图/存储过程/触发器)
MySQL数据库进阶第四篇(视图/存储过程/触发器)
|
9月前
|
SQL 运维 数据管理
数据管理DMS产品使用合集之如何修改在结构设计中的基准数据库
阿里云数据管理DMS提供了全面的数据管理、数据库运维、数据安全、数据迁移与同步等功能,助力企业高效、安全地进行数据库管理和运维工作。以下是DMS产品使用合集的详细介绍。
160 4
|
9月前
|
存储 缓存 监控
如何设计数据库结构?
如何设计数据库结构?【6月更文挑战第9天】
83 3