1.创建模板表:如test
2.创建存储过程
- DELIMITER //
- CREATE PROCEDURE create_table()
- BEGIN
- DECLARE `@suffix` VARCHAR(15);
- DECLARE `@sql_str` VARCHAR(2560);
- SET `@suffix` = DATE_FORMAT(DATE_ADD(NOW(),INTERVAL 1 MONTH),'_%Y%m');
- SET @sql_str = CONCAT("CREATE TABLE IF NOT EXISTS test",`@suffix`," LIKE test;");
- PREPARE stmt FROM @sql_str;
- EXECUTE stmt;
- END;
3.创建触发器
- DELIMITER $$
- CREATE EVENT event_create_table
- ON SCHEDULE
- EVERY 1 MONTH
- STARTS DATE_ADD(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL DAY(CURDATE())+1 DAY), INTERVAL 0 MONTH),INTERVAL 0 HOUR)
- ON COMPLETION PRESERVE
- ENABLE
- DO
- BEGIN
- CALL create_table();
- END$$
- DELIMITER ;
4.查看定时器开关有没有开启
- SHOW VARIABLES LIKE 'event%';
如果是off,则设置开启
SET GLOBAL event_scheduler = ON;
一条评论
这个我会,在学习一下