MySQL - DO Statement



The DO statement of MySQL statement is used to execute MySQL expression. This is similar to the SELECT statement and it does not return any values.

Example

The SLEEP() function accepts an integer value and pauses for the specified seconds. After executing the following query, it waits for 15 seconds and gets executed since we used SELECT statement this query produces a result set with 0 as shown below −

mysql> SELECT SLEEP(15);
+-----------+
| SLEEP(15) |
+-----------+
| 0         |
+-----------+

If you use DO statement it will just pause for the specified amount of time −

rettyprint notranslate">
mysql> DO SLEEP(15);

As you observe if you use the DO statement a result set will not be displayed.

Example

Following is another example demonstrating the usage of the DO statement. It creates an event which truncates the table data every month −

mysql> CREATE EVENT event_hourly ON SCHEDULE EVERY 1 MONTH DO TRUNCATE TABLE data;

Example

Assume we have created a table with name data using the CREATE TABLE statement as shown below −

mysql> CREATE TABLE Data (Name VARCHAR(255), age INT);

Following queries creates two events with names example_event1 and example_event2 it inserts a record in the above created table one minute after the execution

mysql> CREATE EVENT example_event ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 Hour DO INSERT INTO new.Data VALUES('Rahman', 25);

mysql> CREATE EVENT event_hourly ON SCHEDULE EVERY 1 MONTH DO TRUNCATE TABLE data;

The SHOW EVENTS statement lists out all the (upcoming) events.

mysql> SHOW EVENTS;

This will generate the following output −

+------+---------------+----------------+-----------+-----------+---------------------+----------------+----------------+---------------------+------+---------+------------+----------------------+----------------------+--------------------+
| Db   | Name          | Definer        | Time zone | Type      | Execute at          | Interval value | Interval field | Starts              | Ends | Status  | Originator | character_set_client | collation_connection | Database Collation |
+------+---------------+----------------+-----------+-----------+---------------------+----------------+----------------+---------------------+------+---------+------------+----------------------+----------------------+--------------------+
| new  | event_hourly  | root@localhost | SYSTEM    | RECURRING | NULL                | 1              | MONTH          | 2021-03-10 20:58:41 | NULL | ENABLED | 1          | cp850                | cp850_general_ci     | utf8mb4_0900_ai_ci |
| new  | example_event | root@localhost | SYSTEM    | ONE TIME  | 2021-03-10 21:58:33 | NULL           | NULL           | NULL                | NULL | ENABLED | 1          | cp850                | cp850_general_ci     | utf8mb4_0900_ai_ci |
+------+---------------+----------------+-----------+-----------+---------------------+----------------+----------------+---------------------+------+---------+------------+----------------------+----------------------+--------------------+
2 rows in set (0.09 sec)
mysql_statements_reference.htm
Advertisements