首頁 >資料庫 >mysql教程 >建立事件時 ON COMPLETION PRESERVE 子句有什麼用?

建立事件時 ON COMPLETION PRESERVE 子句有什麼用?

WBOY
WBOY轉載
2023-09-12 09:09:071530瀏覽

创建事件时 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)

以上是建立事件時 ON COMPLETION PRESERVE 子句有什麼用?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除