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

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

WBOY
發布: 2023-09-12 09:09:07
轉載
1523 人瀏覽過

创建事件时 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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板