業務數據量比較大的時候,可能對數據按月建表。隨之而來的是每月都需要創建對應表結構。如果忘記了,恐怕對業務影響比較大吧!這種表結構的特點:表字段全部相同,索引也是一致的,表的名稱隨着月份自動變化。
步驟:
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這種類型的表結構有效。其它類型的結構可參考下面的說明:
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 |
3、計劃任務、存儲過程、遊標 不熟悉的,請參考官方文檔。
4、如果怕定時任務執行失敗,可以每半個月執行一次。這樣能防止第一次執行失敗!
5、oracle、sql server 也有對應的管理數據庫,可通過類似方法創建表結構
6、請確保用戶有訪問information_schema數據庫的權限