MySQL/MariaDB数据库的触发器
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.触发器概述
1>.什么是触发器
触发器的执行不是由程序调用,也不是由手工启动,而是由事件来触发、激活从而实现执行。=
2>.创建触发器帮助信息
MariaDB [yinzhengjie]> HELP CREATE TRIGGER Name: 'CREATE TRIGGER' Description: Syntax: CREATE [DEFINER = { user | CURRENT_USER }] TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_body This statement creates a new trigger. A trigger is a named database object that is associated with a table, and that activates when a particular event occurs for the table. The trigger becomes associated with the table named tbl_name, which must refer to a permanent table. You cannot associate a trigger with a TEMPORARY table or a view. CREATE TRIGGER requires the TRIGGER privilege for the table associated with the trigger. The statement might also require the SUPER privilege, depending on the DEFINER value, as described later in this section. If binary logging is enabled, CREATE TRIGGER might require the SUPER privilege, as described in https://mariadb.com/kb/en/binary-logging-of-stored-routines/. The DEFINER clause determines the security context to be used when checking access privileges at trigger activation time. See later in this section for more information. trigger_time is the trigger action time. It can be BEFORE or AFTER to indicate that the trigger activates before or after each row to be modified. trigger_event indicates the kind of statement that activates the trigger. The trigger_event can be one of the following: o INSERT: The trigger is activated whenever a new row is inserted into the table; for example, through INSERT, LOAD DATA, and REPLACE statements. o UPDATE: The trigger is activated whenever a row is modified; for example, through UPDATE statements. o DELETE: The trigger is activated whenever a row is deleted from the table; for example, through DELETE and REPLACE statements. However, DROP TABLE and TRUNCATE TABLE statements on the table do not activate this trigger, because they do not use DELETE. Dropping a partition does not activate DELETE triggers, either. See [HELP TRUNCATE TABLE]. URL: https://mariadb.com/kb/en/create-trigger/ MariaDB [yinzhengjie]> MariaDB [yinzhengjie]>