Home  >  Article  >  Database  >  What is the use of the ON COMPLETION PRESERVE clause when creating an event?

What is the use of the ON COMPLETION PRESERVE clause when creating an event?

WBOY
WBOYforward
2023-09-12 09:09:071505browse

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

We know that the event will be automatically deleted after it expires, and we cannot see it from the SHOW EVENTS statement. To change this behavior we can use ON COMPLETION PRESERVE when creating the event. It can be understood through the following example -

Example

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)

The following query will create events without using ON COMPLETION PRESERVE, so in the SHOW EVENTS FROM db_name query This event will not be seen in the output.

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)

The following query will create an event using ON COMPLETION PRESERVE so you can see the event in the output of the SHOW EVENTS FROM db_name query.

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)

The above is the detailed content of What is the use of the ON COMPLETION PRESERVE clause when creating an event?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete