博客
关于我
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/

你可能感兴趣的文章
Mysql5.6主从复制-基于binlog
查看>>
MySQL5.6忘记root密码(win平台)
查看>>
MySQL5.6的Linux安装shell脚本之二进制安装(一)
查看>>
MySQL5.6的zip包安装教程
查看>>
mysql5.7 for windows_MySQL 5.7 for Windows 解压缩版配置安装
查看>>
Webpack 基本环境搭建
查看>>
mysql5.7 安装版 表不能输入汉字解决方案
查看>>
MySQL5.7.18主从复制搭建(一主一从)
查看>>
MySQL5.7.19-win64安装启动
查看>>
mysql5.7.19安装图解_mysql5.7.19 winx64解压缩版安装配置教程
查看>>
MySQL5.7.37windows解压版的安装使用
查看>>
mysql5.7免费下载地址
查看>>
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>