Getting Started with MySQL Triggers

Trigger concept:

A trigger is a special stored procedure that can trigger execution when inserting, deleting or modifying data in a table. It has more sophisticated and complex data logic control capabilities than the standard functions of the database itself.


Triggers mainly have the following functions:

1. Security . A user can be given certain rights to operate the database based on the values ​​in the database.
  # You can limit user operations based on time, such as not allowing database data to be modified after get off work and holidays.
  # The user's actions can be restricted based on the data in the database, for example, the price of a stock is not allowed to rise more than 10% at a time.

2. Audit . User operations on the database can be tracked.  
  # Audit the statements of users operating the database.
  # Write user updates to the database to the audit table.

3. Implement complex data integrity rules
  # Implement non-standard data integrity checks and constraints. Triggers can create more complex constraints than rules. Unlike rules, triggers can reference columns or database objects. For example, a trigger can roll back any futures that attempt to eat more than its own margin.
  # Provide a variable default value.

4. Implement complex non-standard database-related integrity rules. Triggers can serially update related tables in the database. For example, a delete trigger on the author_code column of the auths table may result in the corresponding deletion of matching rows in other tables.
  # Cascade modification or deletion of matching rows in other tables when modifying or deleting.
  # Set matching rows in other tables to NULL when modifying or deleting.
  # Set matching row cascades in other tables to the default value when modifying or deleting.
  # Triggers can reject or roll back changes that violate the integrity of the correlation, canceling transactions attempting to update data. This trigger works when a foreign key is inserted that does not match its primary key. For example, you can generate an insert trigger on the books.author_code column, and if the new value does not match a value in the auths.author_code column, the insert is rolled back.

5. Synchronously replicate the data in the table in real time.

6. Automatically calculate the data value. If the data value meets certain requirements, specific processing will be performed. For example, if the funds on the company's account are less than 50,000 yuan, it will immediately send warning data to the financial staff.


Simple case:
DELIMITER //
DROP TABLE IF EXISTS tab1 //
CREATE TABLE tab1(
    tab1_id varchar(11)
 )//

DROP TABLE IF EXISTS tab2//
CREATE TABLE tab2(
    tab2_id varchar(11)
)//

DROP TRIGGER IF EXISTS t_afterinsert_on_tab1//
CREATE TRIGGER t_afterinsert_on_tab1
AFTER INSERT ON tab1
FOR EACH ROW
BEGIN
     insert into tab2(tab2_id) values(new.tab1_id);
END//

INSERT INTO tab1(tab1_id) values('0001')//
SELECT * FROM tab1//
SELECT * FROM tab2//

DROP TRIGGER IF EXISTS t_afterdelete_on_tab1//
CREATE TRIGGER t_afterdelete_on_tab1
AFTER DELETE ON tab1
FOR EACH ROW
BEGIN
     delete from tab2 where tab2_id=old.tab1_id;
END//

DELETE FROM tab1 WHERE tab1_id='0001'//

SELECT * FROM tab1//
SELECT * FROM tab2//

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326833311&siteId=291194637