当前位置:网站首页>MySQL create event execution task
MySQL create event execution task
2022-07-27 18:49:00 【Code slave was born to know only to move forward】
This article has participated in 「 New people's creation ceremony 」 Activities , Start the road of nuggets creation together
1、CREATE EVENT Create event Syntax
stay MySQL in , Can pass CREATE EVENT Statement to create an event , The syntax is as follows :
CREATE EVENT [IF NOT EXISTS] event_name
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE | DISABLE ON SLAVE]
[COMMENT 'comment']
DO event_body;
Copy code As can be seen from the above grammar ,CRATE EVENT A sentence consists of multiple clauses , The detailed description of each clause is shown in the following table .
| Clause | explain | ||
|---|---|---|---|
| DEFINER | Optional Used to define the user who checks permissions when an event is executed | ||
| IF NOT EXISTS | Optional Used to determine whether the event to be created exists | ||
| EVENT event_name | Mandatory Used to specify the event name ,event_name The maximum length of is 64 Characters If not specified event_name, Default to current MySQL user name ( Case insensitive ) | ||
| ON SCHEDULE schedule | Mandatory Used to define the time and interval of execution schedule Indicates the trigger point | ||
| ON COMPLETION [NOT] PRESERVE | Optional Used to define whether an event is executed in a loop , That is, once or forever , The default is to execute once , namely NOT PRESERVE | ||
| ENABLE | DISABLE | DISABLE ON SLAVE | Optional , A property used to specify an event . among , keyword ENABLE Indicates that the event is active , That is, the scheduler checks whether the event must call ; keyword DISABLE Indicates that the event is closed , That is, the declaration of the event is stored in the directory , But it should not call the scheduler to check if ; keyword DISABLE ON SLAVE Indicates that the event is closed in the slave . If not specified above 3 Any one of the options , The default is ENABLE |
| COMMENT 'comment' | Optional , Comments used to define events | ||
| DO event_body | Mandatory Used to specify the code to be executed when the event starts , It can be anything that works SQL sentence 、 A stored procedure or an event scheduled for execution . If it contains more than one statement , You can use BEGIN..END Composite structure |
2、 See if the event is on
show VARIABLES like 'event%'; -- if OFF, Then execute the following instructions
-- Turn on
SET GLOBAL event_scheduler = 1;
SET GLOBAL event_scheduler = ON;
-- close
SET GLOBAL event_scheduler = 0;
SET GLOBAL event_scheduler = OFF;
-- View all events
SHOW EVENTS;
-- Check the implementation of specific events
SELECT * FROM information_schema.events WHERE EVENT_SCHEMA='test' ; -- Query event details
Copy code 3、 Create events and view execution data
3.1 Create a table
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for one_data
-- ----------------------------
DROP TABLE IF EXISTS `one_data`;
CREATE TABLE `one_data` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT ' Primary key ID',
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT ' name ',
`sort_number` int(11) NULL DEFAULT NULL COMMENT ' Serial number ',
`create_time` datetime(0) NULL DEFAULT NULL COMMENT ' Creation time ',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci COMMENT = ' Create events and add one piece of data per second ' ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
Copy code 3.2 Create an event
-- If there are stored procedures e_test, Delete
DROP PROCEDURE IF EXISTS e_test;
-- Create an event
CREATE EVENT IF NOT EXISTS e_test
ON SCHEDULE EVERY '1' SECOND STARTS '2022-04-14 10:01:00'
ON COMPLETION PRESERVE COMMENT ' every other 1 Add a piece of data in seconds '
DO INSERT INTO one_data(name, sort_number, create_time) VALUES (' name ', 2, NOW());
-- Create an event
CREATE EVENT IF NOT EXISTS event_delete_de_records_7days ON SCHEDULE EVERY 1 DAY STARTS '2018-01-01 02:00:00'
ON COMPLETION NOT PRESERVE COMMENT ' Every morning 2 Click to execute once '
DO INSERT INTO one_data(name, sort_number, create_time) VALUES (' name ', 2, NOW());
Copy code 3.3 Event parameter description
-- Parameter name explain
-- EVENT_CATALOG Event storage directory , In general , The value is def, Modification is not recommended
-- EVENT_SCHEMA The database where the event is located
-- EVENT_NAME Event name
-- DEFINER The definer of the event
-- TIME_ZONE The time zone used by the event , The default is SYSTEM, Modification is not recommended
-- EVENT_BODY In general , The value is SQL, Modification is not recommended
-- EVENT_DEFINITION The content of the event , It can be specific INSERT etc. SQL, It can also be a calling stored procedure
-- EVENT_TYPE Event type , This parameter is more important , Specify... When defining
-- There are two values :RECURRING and ONE TIME
-- RECURRING It means that the execution will be repeated as long as the conditions are met ,RECURRING Types of events are generally NULL, Indicates the estimated execution time of the event
-- ONE TIME It will only call EXECUTE_AT, in the light of one-time Events of type are valid
-- INTERVAL_VALUE in the light of RECURRING Events of type are valid , Indicates the execution interval length
-- INTERVAL_FIELD in the light of RECURRING Events of type are valid , Unit indicating execution interval , It's usually SECOND,DAY equivalence , Refer to the creation syntax
-- SQL_MODE Current event SQL_MODE
-- STARTS in the light of RECURRING Events of type are valid , Indicates the point in time at which an event starts executing , and one-time Of EXECUTE_AT The function is similar to .
-- by NULL When, it means that the execution starts as soon as the conditions are met
-- ENDS in the light of RECURRING Events of type are valid , Indicates the time point at which an event will not be executed , If NULL Just never stop
-- STATUS There are generally three values ,ENABLED、DISABLED and SLAVESIDE_DISABLED
-- ON_COMPLETION There are only two values ,PRESERVE and NOT PRESERVE
-- CREATED Event creation time
-- LAST_ALTERED The time when the event was last modified
-- LAST_EXECUTED The time when the event was last executed , If NULL Indicates that... Has never been executed
-- EVENT_COMMENT Comment information of the event
-- ORIGINATOR When the current event is created server-id, Used for master-slave processing , such as SLAVESIDE_DISABLED
-- CHARACTER_SET_CLIENT The client character set at the time of event creation
-- COLLATION_CONNECTION Connection character verification rules when creating events
-- DATABASE_COLLATION Database character set verification rules at the time of event creation
Copy code 3.4 Verify view data
SELECT * FROM `one_data`;
select lpad((select max(sort_number)+1 from one_data),3,'0')
Copy code 4、 Create a second event
4.1 Create table
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for interface_call
-- ----------------------------
DROP TABLE IF EXISTS `interface_call`;
CREATE TABLE `interface_call` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT ' Primary key ',
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT ' The name of the interface ',
`code` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT ' Unique code ',
`all_value` int(10) NULL DEFAULT NULL COMMENT ' Total calls ',
`today_value` int(10) NULL DEFAULT NULL COMMENT ' Number of calls today ',
`create_time` datetime(0) NULL DEFAULT NULL COMMENT ' Creation time ',
`update_time` datetime(0) NULL DEFAULT NULL COMMENT ' Update time ',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 22 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = ' Health Report - Interface call data ' ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
Copy code 4.2 Once every morning
CREATE DEFINER = `root`@`%` EVENT `interface_call`
ON SCHEDULE
EVERY '1' DAY STARTS '2021-10-12 00:00:00'
COMMENT ' The update is executed every morning, and the call volume today is zero '
DO UPDATE `interface_call` SET `today_value` = 0, `update_time` = NOW();
Copy code 4.3 View event execution details
-- Check the implementation of specific events
SELECT * FROM information_schema.events WHERE EVENT_SCHEMA='test' and EVENT_NAME='e_test';
-- test: Is the database name
-- e_test: Specific event name
Copy code 边栏推荐
- 兆骑科创海内外引进高层次人才,创新创业项目对接
- Use mobaxtermto establish a two-tier springboard connection
- Alibaba architects spent 280 hours sorting out 1015 pages of distributed full stack pamphlets to easily start the distributed system
- Navicat 导出表生成PDM文件
- Openstack login dashboard prompt authentication error
- 商品评论信息与评论信息分类
- Generate PDM file from Navicat export table
- MySQL learning day3 multi table query / transaction / DCL
- Set the arc button to be displayed in the middle
- 2021.7.12 internal and external connection of notes
猜你喜欢
随机推荐
RuntimeError: output with shape [1, 256, 256] doesn‘t match the broadcast shape [3, 256, 256]【报错】
C basic concepts list description suggestions collection
2021.8.7 note Servlet
Solve the problem of JSP cascading
MySQL 主从复制数据不一致,怎么办?
was not registered for synchronization because synchronization is not active[已解决]
A case to understand MySQL view
Knowledge map - Jieba, pyhanlp, smoothnlp tools to realize Chinese word segmentation (part of speech)
低噪负离子风扇触摸IC
idea 2020.1社区版下载体验
EN 1155 building hardware swing door opener - CE certification
JS中的数组与对象
Commonly used built-in methods of mybtis plus
2021.7.12 internal and external connection of notes
浅谈JVM(面试常考)
商品评论信息与评论信息分类
Uniapp H5 cross domain problem
Canvas draws graphics according to coordinate points
org.gradle.api. UncheckedIOException: Could not load properties for module ‘gradle-kotlin-dsl‘ from C
TS study notes class









