MySQL database view triggers and MySQL modify and delete triggers (DROP TRIGGER)

MySQL view trigger

Viewing triggers refers to viewing the definition, status and syntax information of triggers that already exist in the database. Methods to view triggers in MySQL include the SHOW TRIGGERS statement and querying the triggers data table under the information_schema database. This section details both methods of viewing triggers.

SHOW TRIGGERS statement to view trigger information

In MySQL, you can view the basic information of triggers through the SHOW TRIGGERS statement. The syntax format is as follows:

SHOW TRIGGERS;
Example 1

First create a data table account. There are two fields in the table, namely accnum of INT type and amount of DECIMAL type. The SQL statements and running results are as follows:

mysql> CREATE TABLE account(
    -> accnum INT(4),
    -> amount DECIMAL(10,2));
Query OK, 0 rows affected (0.49 sec)

Create a trigger named trigupdate, and insert a piece of data into the myevent data table every time the account table updates data. The SQL statement and running results to create the data table myevent are as follows:

mysql> CREATE TABLE myevent(
    -> id INT(11) DEFAULT NULL,
    -> evtname CHAR(20) DEFAULT NULL);
Query OK, 0 rows affected (0.26 sec)

The SQL code to create a trigupdate trigger is as follows:

mysql> CREATE TRIGGER trigupdate AFTER UPDATE ON account
    -> FOR EACH ROW INSERT INTO myevent VALUES(1,'after update');
Query OK, 0 rows affected (0.15 sec)

Use the SHOW TRIGGERS statement to view triggers (add it after the SHOW TRIGGERS command \Gso that the displayed information will be more organized). The SQL statement and running results are as follows:

mysql> SHOW TRIGGERS \G
*************************** 1. row ***************************
             Trigger: trigupdate
               Event: UPDATE
               Table: account
           Statement: INSERT INTO myevent VALUES(1,'after update')
              Timing: AFTER
             Created: 2020-02-24 14:07:15.08
            sql_mode: STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
             Definer: root@localhost
character_set_client: gbk
collation_connection: gbk_chinese_ci
  Database Collation: latin1_swedish_ci
1 row in set (0.09 sec)

You can see the basic information of the trigger from the running results. The description of the above displayed information is as follows:

  • Trigger represents the name of the trigger, here the name of the trigger is trigupdate;
  • Event represents the event that activates the trigger. The trigger event here is the update operation UPDATE;
  • Table represents the operation object table of the activated trigger, here it is the account table;
  • Statement represents the operation performed by the trigger, here it is to insert a piece of data into the myevent data table;
  • Timing represents the time when the trigger is fired, here it is after the update operation (AFTER);
  • There is also some other information, such as the creation time of the trigger, SQL mode, trigger definition account and character set, etc., which will not be introduced one by one here.

Mysql database basic skills full practice icon-default.png?t=N7T8https://edu.csdn.net/course/detail/36210
SHOW TRIGGERS statement is used to view information on all triggers currently created. Because this statement cannot query the specified trigger, it is convenient to use this statement when there are few triggers. If you want to view information about a specific trigger or there are many triggers in the database, you can directly search it from the triggers data table in the information_schema database.

View trigger information in the triggers table

In MySQL, all trigger information is stored in the triggers table of the information_schema database and can be viewed through the query command SELECT. The specific syntax is as follows:

SELECT * FROM information_schema.triggers WHERE trigger_name= '触发器名';

Among them, '触发器名'it is used to specify the name of the trigger to be viewed and needs to be enclosed in single quotes. This method can query the specified trigger, which is more convenient and flexible to use.

Example 2

Use the SELECT command below to view the trigupdate trigger. The SQL statement is as follows:

SELECT * FROM information_schema.triggers WHERE TRIGGER_NAME= 'trigupdate'\G

The above command uses WHERE to specify the name of the trigger that needs to be viewed. The running results are as follows:

mysql> SELECT * FROM information_schema.triggers WHERE TRIGGER_NAME= 'trigupdate'\G
*************************** 1. row ***************************
           TRIGGER_CATALOG: def
            TRIGGER_SCHEMA: test
              TRIGGER_NAME: trigupdate
        EVENT_MANIPULATION: UPDATE
      EVENT_OBJECT_CATALOG: def
       EVENT_OBJECT_SCHEMA: test
        EVENT_OBJECT_TABLE: account
              ACTION_ORDER: 1
          ACTION_CONDITION: NULL
          ACTION_STATEMENT: INSERT INTO myevent VALUES(1,'after update')
        ACTION_ORIENTATION: ROW
             ACTION_TIMING: AFTER
ACTION_REFERENCE_OLD_TABLE: NULL
ACTION_REFERENCE_NEW_TABLE: NULL
  ACTION_REFERENCE_OLD_ROW: OLD
  ACTION_REFERENCE_NEW_ROW: NEW
                   CREATED: 2020-02-24 16:07:15.08
                  SQL_MODE: STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
                   DEFINER: root@localhost
      CHARACTER_SET_CLIENT: gbk
      COLLATION_CONNECTION: gbk_chinese_ci
        DATABASE_COLLATION: latin1_swedish_ci
1 row in set (0.22 sec)

The detailed information of the trigger can be seen from the running results. The description of the above displayed information is as follows:

  • TRIGGER_SCHEMA indicates the database where the trigger is located;
  • TRIGGER_NAME represents the name of the trigger;
  • EVENT_OBJECT_TABLE indicates which data table it is triggered on;
  • ACTION_STATEMENT represents the specific operation performed when the trigger is fired;
  • The value of ACTION_ORIENTATION is ROW, which means it is triggered on every record;
  • ACTION_TIMING indicates that the triggering moment is AFTER;
  • There is also some other information, such as the creation time of the trigger, SQL mode, trigger definition account and character set, etc., which will not be introduced one by one here.

 
The above SQL statement can also not specify the trigger name, so that all triggers will be viewed. The SQL statement is as follows:

SELECT * FROM information_schema.triggers \G

This statement will display all trigger information in the triggers data table.

MySQL modify and delete triggers (DROP TRIGGER)

You can modify a trigger by deleting the original trigger and creating a new trigger with the same name.

basic grammar

Like other  MySQL  database objects, triggers can be removed from the database using the DROP statement.

The syntax format is as follows:

DROP TRIGGER [ IF EXISTS ] [数据库名] <触发器名>

The syntax is explained as follows:

1) Trigger name

The name of the trigger to delete.

2) Database name

Optional. Specifies the name of the database where the trigger resides. If not specified, it is the current default database.

3) Permissions

SUPER authority is required to execute the DROP TRIGGER statement.

4) IF EXISTS

Optional. Avoid deleting triggers without triggers.

注意:删除一个表的同时,也会自动删除该表上的触发器。另外,触发器不能更新或覆盖,为了修改一个触发器,必须先删除它,再重新创建。

delete trigger

Use the DROP TRIGGER statement to delete triggers that have been defined in MySQL.

[Example] Delete the double_salary trigger. The input SQL statement and execution process are as follows.

mysql> DROP TRIGGER double_salary;
Query OK, 0 rows affected (0.03 sec)

After deleting the double_salary trigger, when inserting records into the data table tb_emp6 again, the data in the data table tb_emp7 no longer changes, as shown below.

mysql> INSERT INTO tb_emp6
    -> VALUES (3,'C',1,200);
Query OK, 1 row affected (0.09 sec)
mysql> SELECT * FROM tb_emp6;
+----+------+--------+--------+
| id | name | deptId | salary |
+----+------+--------+--------+
|  1 | A    |      1 |   1000 |
|  2 | B    |      1 |    500 |
|  3 | C    |      1 |    200 |
+----+------+--------+--------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM tb_emp7;
+----+------+--------+--------+
| id | name | deptId | salary |
+----+------+--------+--------+
|  1 | A    |      1 |   2000 |
|  2 | B    |      1 |   1000 |
+----+------+--------+--------+
2 rows in set (0.00 sec)

Dachang senior database engineer mysql database practical training icon-default.png?t=N7T8https://edu.csdn.net/course/detail/39021

Guess you like

Origin blog.csdn.net/m0_37449634/article/details/135553763