业务数据量比较大的时候,可能对数据按月建表。随之而来的是每个月都须要建立对应表结构。若是忘记了,恐怕对业务影响比较大吧!这种表结构的特色:表字段所有相同,索引也是一致的,表的名称随着月份自动变化。html
步骤: mysql
一、建立存储过程
git
二、建立定时任务(须要开启全局的event_scheduler)sql
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 ;
备注: 数据库
一、请将“ TABLE_SCHEMA = '****'”中的"****"修改成表所在的数据库的名称oracle
二、该sql 会对数据库下表名称为:201401这种类型的表结构有效。其它类型的结构可参考下面的说明:spa
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 (0th , 1st , 2nd , 3rd , …) |
%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; WEEK() mode 0 |
%u |
Week (00 ..53 ), where Monday is the first day of the week; WEEK() mode 1 |
%V |
Week (01 ..53 ), where Sunday is the first day of the week; WEEK() mode 2; used with %X |
%v |
Week (01 ..53 ), where Monday is the first day of the week; 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 , for any “x ” not listed above |
三、计划任务、存储过程、游标 不熟悉的,请参考官方文档。
code
四、若是怕定时任务执行失败,能够每半个月执行一次。这样能防止第一次执行失败!
orm
五、oracle、sql server 也有对应的管理数据库,可经过相似方法建立表结构
server
六、请确保用户有访问information_schema数据库的权限