博客
关于我
MySQL数据库干货分享!mysql每月自动创建表结构
阅读量:423 次
发布时间:2019-03-06

本文共 3443 字,大约阅读时间需要 11 分钟。

 

如果你刚好在学MySQL,博主推荐一套很详细的MySQL教程

主要详细讲解了MySQL的相关知识,包括MySQL概述,MySQL应用环境,MySQL系统特性,MySQL初学基础,MySQL管理工具,如何安装MySQL及MySQL新特性,学习完就可掌握MySQL全套知识。

https://www.bilibili.com/video/BV1fx411X7BD

 

业务数据量比较大的时候,可能对数据按月建表。随之而来的是每月都需要创建对应表结构。如果忘记了,恐怕对业务影响比较大吧!这种表结构的特点:表字段全部相同,索引也是一致的,表的名称随着月份自动变化。

步骤:    

    1、创建存储过程

    2、创建定时任务(需要开启全局的event_scheduler)

1 DELIMITER $$  2  3  4 DROP PROCEDURE IF EXISTS `pro_autocre_month_table`$$ 5  6 CREATE DEFINER=`root`@`%` PROCEDURE `pro_autocre_month_table`() 7 BEGIN 8     DECLARE old_table_name VARCHAR(128); 9     DECLARE new_table_name VARCHAR(128);10     DECLARE done INT DEFAULT 0;11     12     DECLARE table_cursor CURSOR FOR SELECT TABLE_NAME FROM `information_schema`.`TABLES` WHERE TABLE_SCHEMA = '****' AND TABLE_NAME REGEXP DATE_FORMAT(CURDATE(), '%Y%m');13     DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1; 14     15     OPEN table_cursor;   16     REPEAT17         18         FETCH table_cursor INTO old_table_name;19         IF NOT done THEN 20             21             SELECT  REPLACE(old_table_name,DATE_FORMAT(CURDATE(), '%Y%m'),DATE_FORMAT(DATE_ADD(CURDATE(),INTERVAL 1 MONTH), '%Y%m')) INTO new_table_name;22             SET @sqlCmd = CONCAT('create table if not exists `',new_table_name,'` like `' , old_table_name,'`');23             PREPARE preStmt FROM @sqlCmd;  24             EXECUTE preStmt;25         END IF ;26     UNTIL done END REPEAT;27     CLOSE table_cursor;28     END$$29 30 DELIMITER ;31 -- ---------------------------------------------------------------------32 DELIMITER $$33 34 SET GLOBAL event_scheduler = ON$$   35 36 CREATE    /*[DEFINER = { user | CURRENT_USER }]*/    EVENT `test`.`auto_create_table`37 38 ON SCHEDULE39     ON SCHEDULE EVERY 1 WEEK STARTS '2014-01-01 00:00:00' ON COMPLETION NOT PRESERVE ENABLE 40     DO BEGIN41         CALL pro_autocre_month_table();42     END$$43 44 DELIMITER ;

 

备注:    

    1、请将“ TABLE_SCHEMA = '****'”中的"****"修改为表所在的数据库的名称

    2、该sql 会对数据库下表名称为:201401这种类型的表结构有效。其它类型的结构可参考下面的说明:

    

Specifier Description
%a Abbreviated weekday name (Sun..Sat)
%b Abbreviated month name (Jan..Dec)
%c Month, numeric (0..12)
%D Day of the month with English suffix (0th1st2nd3rd, …)
%d Day of the month, numeric (00..31)
%e Day of the month, numeric (0..31)
%f Microseconds (000000..999999)
%H Hour (00..23)
%h Hour (01..12)
%I Hour (01..12)
%i Minutes, numeric (00..59)
%j Day of year (001..366)
%k Hour (0..23)
%l Hour (1..12)
%M Month name (January..December)
%m Month, numeric (00..12)
%p AM or PM
%r Time, 12-hour (hh:mm:ss followed by AM or PM)
%S Seconds (00..59)
%s Seconds (00..59)
%T Time, 24-hour (hh:mm:ss)
%U Week (00..53), where Sunday is the first day of the week;  mode 0
%u Week (00..53), where Monday is the first day of the week;  mode 1
%V Week (01..53), where Sunday is the first day of the week;  mode 2; used with %X
%v Week (01..53), where Monday is the first day of the week;  mode 3; used with %x
%W Weekday name (Sunday..Saturday)
%w Day of the week (0=Sunday..6=Saturday)
%X Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%Y Year, numeric, four digits
%y Year, numeric (two digits)
%% A literal “%” character
%x x, for any “x” not listed above

    3、计划任务、存储过程、游标 不熟悉的,请参考官方文档。

    4、如果怕定时任务执行失败,可以每半个月执行一次。这样能防止第一次执行失败!

    5、oracle、sql server 也有对应的管理数据库,可通过类似方法创建表结构

    6、请确保用户有访问information_schema数据库的权限

转载地址:http://ifakz.baihongyu.com/

你可能感兴趣的文章
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>
MySQL Connector/Net 句柄泄露
查看>>
multiprocessor(中)
查看>>
mysql CPU使用率过高的一次处理经历
查看>>
Multisim中555定时器使用技巧
查看>>
MySQL CRUD 数据表基础操作实战
查看>>