创建事件时 ON COMPLETION PRESERVE 子句有什么用?

创建事件时 ON COMPLETION PRESERVE 子句有什么用?

我们知道,事件过期后会自动删除,并且我们无法从 SHOW EVENTS 语句中看到它。要改变这种行为,我们可以在创建事件时使用 ON COMPLETION PRESERVE。可以通过以下示例来理解 –

示例

mysql> Create table event_messages(ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT, MESSAGE VARCHAR(255) NOT NULL, Generated_at DATETIME NOT NULL);
Query OK, 0 rows affected (0.61 sec)

以下查询将在不使用 ON COMPLETION PRESERVE 的情况下创建事件,因此在 SHOW EVENTS FROM db_name 查询的输出中不会看到该事件。

mysql> CREATE EVENT testing_event_without_Preserves ON SCHEDULE AT CURRENT_TIMESTAMP DO INSERT INTO event_messages(message,generated_at) Values('Without Preserve',NOW());
Query OK, 0 rows affected (0.00 sec)
mysql> Select * from event_messages;
+----+------------------+---------------------+
| ID | MESSAGE          | Generated_at        |
+----+------------------+---------------------+
| 1  | Without Preserve | 2017-11-22 20:32:13 |
+----+------------------+---------------------+
1 row in set (0.00 sec)
mysql> SHOW EVENTS FROM query\G
*************************** 1. row ***************************
Db: query
Name: testing_event5
Definer: root@localhost
Time zone: SYSTEM
Type: ONE TIME
Execute at: 2017-11-22 17:09:11
Interval value: NULL
Interval field: NULL
Starts: NULL
Ends: NULL
Status: DISABLED
Originator: 0
character_set_client: cp850
collation_connection: cp850_general_ci
Database Collation: latin1_swedish_ci
1 row in set (0.00 sec)

以下查询将使用 ON COMPLETION PRESERVE 创建一个事件,因此可以在 SHOW EVENTS FROM db_name 查询的输出中看到该事件。

mysql> CREATE EVENT testing_event_with_Preserves ON SCHEDULE AT CURRENT_TIMESTAMP ON COMPLETION PRESERVE DO INSERT INTO event_messages(message,generated_at) Values('With Preserve',NOW());
Query OK, 0 rows affected (0.00 sec)
mysql> Select * from event_messages;
+----+------------------+---------------------+
| ID | MESSAGE          | Generated_at        |
+----+------------------+---------------------+
| 1  | Without Preserve | 2017-11-22 20:32:13 |
| 2  | With Preserve    | 2017-11-22 20:35:12 |
+----+------------------+---------------------+
2 rows in set (0.00 sec)
mysql> SHOW EVENTS FROM query\G
*************************** 1. row ***************************
Db: query
Name: testing_event5
Definer: root@localhost
Time zone: SYSTEM
Type: ONE TIME
Execute at: 2017-11-22 17:09:11
Interval value: NULL
Interval field: NULL
Starts: NULL
Ends: NULL
Status: DISABLED
Originator: 0
character_set_client: cp850
collation_connection: cp850_general_ci
Database Collation: latin1_swedish_ci
*************************** 2. row ***************************
Db: query
Name: testing_event_with_Preserves
Definer: root@localhost
Time zone: SYSTEM
Type: ONE TIME
Execute at: 2017-11-22 20:35:12
Interval value: NULL
Interval field: NULL
Starts: NULL
Ends: NULL
Status: DISABLED
Originator: 0
character_set_client: cp850
collation_connection: cp850_general_ci
Database Collation: latin1_swedish_ci
2 rows in set (0.00 sec)
原文来自:www.php.cn
© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容