How do I detect if the ON UPDATE event fired with query in MySQL?

MySQLMySQLi Database

You can detect with the help of row_count(). If the row_count() returns 1 that means it is a new record. If it returns 2, that means the ON UPDATE event is fired with query. Following is the syntax −

select row_count();

Let us first create a table −

mysql> create table DemoTable1512
   -> (
   -> Value int ,
   -> UNIQUE(Value)
   -> );
Query OK, 0 rows affected (0.60 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1512 values(90) on duplicate key update Value=Value+10;
Query OK, 1 row affected (0.09 sec)

Now you can check the on update event is fired with the help of above query −

mysql> select row_count();

This will produce the following output −

+-------------+
| row_count() |
+-------------+
|           1 |
+-------------+
1 row in set (0.00 sec)

It returns 1 that means no.

Now you can insert same records again in the table using insert command −

mysql> insert into DemoTable1512 values(90) on duplicate key update Value=Value+10;
Query OK, 2 rows affected (0.12 sec)

Now you can check the on update event is fired with the help of above query −

mysql> select row_count();

This will produce the following output −

+-------------+
| row_count() |
+-------------+
|           2 |
+-------------+
1 row in set (0.00 sec)
raja
Published on 11-Dec-2019 10:42:18
Advertisements