MySQL creates a database table on a monthly schedule with stored procedures and events

Source: Internet
Author: User
Tags stmt

Business needs, the app user is written to a log table App_open_log.
On the line for 7 months, there are 740,000 records.

Now consider the sub-database of the table. At the beginning of each month, create a database table prefixed by App_open_log_ with a date and year suffix, for example: app_open_log_201807.

Copy the following two paragraphs to SQL and execute them.

First create the stored procedure:

DELIMITER //CREATE PROCEDURE create_table_app_open_log_month()BEGINDECLARE `@suffix` VARCHAR(15);DECLARE `@sqlstr` VARCHAR(2560);SET `@suffix` = DATE_FORMAT(DATE_ADD(NOW(),INTERVAL 1 MONTH),’_%Y%m’);SET @sqlstr = CONCAT("CREATE TABLE jz_app_open_log,`@suffix`,"(  `id` int(11) NOT NULL AUTO_INCREMENT,  `equipment_type` varchar(45) DEFAULT NULL ,  `equipment_version` varchar(45) DEFAULT NULL ,  `rom` varchar(45) DEFAULT NULL ,  `cpu` varchar(45) DEFAULT NULL ,  `mac` varchar(100) DEFAULT NULL ,  `ip` varchar(50) DEFAULT NULL ,  `version_code` varchar(10) DEFAULT NULL ,  `client` varchar(45) DEFAULT ‘‘ ,  `create_time` int(10) DEFAULT NULL,  `version_name` varchar(45) DEFAULT ‘‘ ,  `v` varchar(10) DEFAULT ‘‘,  PRIMARY KEY (`id`),  UNIQUE KEY `id_UNIQUE` (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1161854 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;");PREPARE stmt FROM @sqlstr;EXECUTE stmt;END;

Then create the event 1st each month to execute the above stored procedure:

DELIMITER $$SET GLOBAL event_scheduler = 1;CREATE EVENT event_create_table_every_monthON SCHEDULE EVERY 1 MONTHSTARTS ‘2018-07-01 00:00:00‘ON  COMPLETION  PRESERVEENABLEDOBEGINCALL create_table_app_open_log_month();END

You can see through navicate

That's OK!

Heavy hands-on practice. If you have any questions, you are welcome to join the PHP technical question and answer group. Thank you for reading!

----------Recruit the future great God-----------------------

If you have altruistic heart, willing to help others, willing to share
If you encounter PHP problem, Baidu and asked the other groups still did not get answers

Welcome to join, PHP technical question and answer group, QQ Group: 292626152

Educational developments Help others, you will also get promotion!

In order to cherish everyone's valuable time, please do not chat!

May we help each other and make progress together!

Add message code, PHP,AJAX,THINKPHP,YII ...

MySQL creates a database table on a monthly schedule with stored procedures and events

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.