当前位置:网站首页>MySQL (17 trigger)

MySQL (17 trigger)

2022-06-10 19:10:00 Fate friend I

Make progress every day , Make progress !! Thank myself

 Insert picture description here

 Insert picture description here

One Trigger Overview

  • MySQL from 5.0.2 Trigger is supported in version .MySQL The trigger is the same as the stored procedure , Are embedded in MySQL A program of the server .
  • The trigger is made by Events to trigger Some operation , These events include INSERT 、 UPDATE 、 DELETE event . The so-called event refers to the user's action or triggering a certain behavior . If the trigger program is defined , When the database executes these statements , It's equivalent to an event , will Automatically The trigger performs the corresponding action .
  • When inserting data in the data table 、 Update and delete operations , When some database logic needs to be executed automatically , Triggers can be used to implement .

Two Trigger creation

2.1 grammar -‘ Only child ’

  • Grammatical structure
CREATE TRIGGER  Trigger Name 
{BEFORE|AFTER} {
   INSERT|UPDATE|DELETE} ON  Table name 
FOR EACH ROW
 The block of statements executed by the trigger ;
  • explain
    • Table name : Represents the object monitored by the trigger
    • BEFORE|AFTER: Indicates the trigger time ( Relative to event ).
    • INSERT|UPDATE|DELETE: Indicates the departure event
      • INSERT —— Triggered when a record is inserted ;
      • UPDATE —— Triggered when the record is updated .
      • DELETE —— Triggered when a record is deleted .
    • The block of statements executed by the trigger : It can be a single sql sentence , Or by BEGIN……END Structure .

2.2 Case understanding -‘ triplets ’

🪬🪬2.2.1 Case a

  • Create trigger : Create name as before_insert The trigger of , towards test_trigger Data table before inserting data , towards
    test_trigger_log Insert... Into the data table before_insert Log information .
DELIMITER //
CREATE TRIGGER before_insert
BEFORE INSERT ON test_trigger
FOR EACH ROW
BEGIN
INSERT INTO test_trigger_log (t_log)
VALUES('before_insert');
END //
DELIMITER ;

2.2.2 Case 2

  • Create name as after_insert The trigger of , towards test_trigger After inserting data into the data table , towards test_trigger_log Insert in data table
    Enter into after_insert Log information
DELIMITER //
CREATE TRIGGER after_insert
AFTER INSERT ON test_trigger
FOR EACH ROW
BEGIN
INSERT INTO test_trigger_log (t_log)
VALUES('after_insert');
END //
DELIMITER ;

2.2.3 Case three

DELIMITER //
CREATE TRIGGER salary_check_trigger
BEFORE INSERT ON employees FOR EACH ROW
BEGIN
DECLARE mgrsalary DOUBLE; 
SELECT salary INTO mgrsalary FROM employees WHERE employee_id = NEW.manager_id;
IF NEW.salary > mgrsalary THEN #NEW Keyword represents INSERT Add a new record of the statement 
SIGNAL SQLSTATE 'HY000' SET MESSAGE_TEXT = ' The salary is higher than the leader's salary ';
END IF;
END //
DELIMITER ;

3、 ... and see 、 Delete trigger

3.1 View by -‘ Three brothers ’

  • The eldest brother – View the definitions of all triggers in the current database
SHOW TRIGGERS\G
  • The second – View the definition of a trigger in the current database
SHOE CREATE TRIGGER  Trigger Name 
  • Youngest – From the system library information_schema Of TRIGGERS Query in table “salary_check_trigger” Trigger information .
SELECT * FROM information_schema.TRIGGERS;

3.2 Delete trigger

  • Triggers are also database objects , Delete triggers also use DROP sentence , The syntax is as follows :
DROP TRIGGER IF EXISTS  Trigger Name ;

Four Success and failure of triggers

4.1 become

  1. Triggers ensure data integrity
  2. Triggers can help us log operations .
  3. Triggers can also be used before manipulating data , Check the validity of the data

🧩🧩4.2 Defeat

  1. One of the biggest problems with triggers is poor readability .
  2. Change of relevant data , May cause trigger errors
原网站

版权声明
本文为[Fate friend I]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101815406391.html