当前位置:网站首页>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 边栏推荐
猜你喜欢

迷你洗衣机触摸芯片-DLT8MA12TS-杰力科创

The combination of text and words perfectly explains the implementation process of MySQL logical backup

2021.7.18 notes MySQL data type

mysql视图基本操作

「MySQL那些事」一文详解索引原理

虚拟偶像的歌声原来是这样生成的!

The song of the virtual idol was originally generated in this way!

What does the number of network request interface layers (2/3 layers) mean

uniapp运行到手机(真机调试)

10 SQL optimization schemes summarized by Alibaba P8 (very practical)
随机推荐
知识图谱 — jieba、pyhanlp、smoothnlp工具实现中文分词(词性表)
The combination of text and words perfectly explains the implementation process of MySQL logical backup
PyGame aircraft war game background implementation
js中的函数与DOM获取元素和事件属性的使用
百度地图技术概述,及基本API与WebApi的应用开发
2021.7.22 note constraints
nacos显示服务注册地址错误
Error launching IDEA
Idea packaging war package and war package location
LED带风扇护眼学习台灯触摸芯片-DLT8S12A
飞机大战敌机出场
Wechat applet wxacode.getunlimited generates applet code
Uniapp has no effect on the page selector on the app side
地图找房的实例
Login page tablelayout
Idea 2020.1 Community Edition download experience
The second parameter of fragmenttransaction.replace reports an error
org.gradle.api. UncheckedIOException: Could not load properties for module ‘gradle-kotlin-dsl‘ from C
JPA connection database password field blob
Wechat applet obtains mobile number