mysql 每月自动创建表结构

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


步骤:    

    1、创建存储过程

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

DELIMITER $$


DROP PROCEDURE IF EXISTS `pro_autocre_month_table`$$

CREATE DEFINER=`root`@`%` PROCEDURE `pro_autocre_month_table`()
BEGIN
	DECLARE old_table_name VARCHAR(128);
	DECLARE new_table_name VARCHAR(128);
	DECLARE done INT DEFAULT 0;
	
	DECLARE table_cursor CURSOR FOR SELECT TABLE_NAME FROM `information_schema`.`TABLES` WHERE TABLE_SCHEMA = '****' AND TABLE_NAME REGEXP DATE_FORMAT(CURDATE(), '%Y%m');
	DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1; 
	
	OPEN table_cursor;   
	REPEAT
		
		FETCH table_cursor INTO old_table_name;
		IF NOT done THEN 
			
			SELECT  REPLACE(old_table_name,DATE_FORMAT(CURDATE(), '%Y%m'),DATE_FORMAT(DATE_ADD(CURDATE(),INTERVAL 1 MONTH), '%Y%m')) INTO new_table_name;
			SET @sqlCmd = CONCAT('create table if not exists `',new_table_name,'` like `' , old_table_name,'`');
			PREPARE preStmt FROM @sqlCmd;  
			EXECUTE preStmt;
		END IF ;
	UNTIL done END REPEAT;
	CLOSE table_cursor;
    END$$

DELIMITER ;
-- ---------------------------------------------------------------------
DELIMITER $$

SET GLOBAL event_scheduler = ON$$   

CREATE	/*[DEFINER = { user | CURRENT_USER }]*/	EVENT `test`.`auto_create_table`

ON SCHEDULE
	ON SCHEDULE EVERY 1 WEEK STARTS '2014-01-01 00:00:00' ON COMPLETION NOT PRESERVE ENABLE 
	DO BEGIN
		CALL pro_autocre_month_table();
	END$$

DELIMITER ;

备注:    

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

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

    

SpecifierDescription
%aAbbreviated weekday name (Sun..Sat)
%bAbbreviated month name (Jan..Dec)
%cMonth, numeric (0..12)
%DDay of the month with English suffix (0th1st2nd3rd, …)
%dDay of the month, numeric (00..31)
%eDay of the month, numeric (0..31)
%fMicroseconds (000000..999999)
%HHour (00..23)
%hHour (01..12)
%IHour (01..12)
%iMinutes, numeric (00..59)
%jDay of year (001..366)
%kHour (0..23)
%lHour (1..12)
%MMonth name (January..December)
%mMonth, numeric (00..12)
%pAM or PM
%rTime, 12-hour (hh:mm:ss followed by AM or PM)
%SSeconds (00..59)
%sSeconds (00..59)
%TTime, 24-hour (hh:mm:ss)
%UWeek (00..53), where Sunday is the first day of the week; WEEK() mode 0
%uWeek (00..53), where Monday is the first day of the week; WEEK() mode 1
%VWeek (01..53), where Sunday is the first day of the week; WEEK() mode 2; used with %X
%vWeek (01..53), where Monday is the first day of the week; WEEK() mode 3; used with %x
%WWeekday name (Sunday..Saturday)
%wDay of the week (0=Sunday..6=Saturday)
%XYear for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%xYear for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%YYear, numeric, four digits
%yYear, numeric (two digits)
%%A literal % character
%xx, for any x not listed above

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

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

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

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



转载于:https://my.oschina.net/u/590649/blog/205754

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值