mysql trigger trigger

Trigger: Monitor a situation and trigger an action.
Four elements of trigger creation syntax: 1. Monitoring location (table) 2. Monitoring event (insert/update/delete) 3. Trigger time (after/before) 4. Trigger event (insert/update/delete)
syntax:
    create trigger triggerName
     after/before   insert/update/delete on table name
    for each row #This sentence is a fixed
    begin
         sql statement in mysql;

    end;


How do we refer to the value of the row in the trigger, that is, we want to get the value of gid or much in our newly inserted order record.
For insert, the newly inserted row is represented by new, and the value of each column in the row is represented by new.column name.
begin
update g set num=num-new.much where id=new.gid;

end


For delete: there was originally a row, but it was deleted later. If you want to refer to the deleted row, it is represented by old, and the old. column name can refer to the value of the deleted row.

    begin
        update g set num = num + old.much where id = old.gid;

    end


For update: the modified row, the data before modification, is represented by old, the old.column name refers to the value in the row before the modification;
the modified data is represented by new, and the new.column name is referred to by The value in the row after modification.

begin
update g set num = num+old.much-new.much where id = old.gid/new.gid;
end
first restore the old number and then subtract the new number to get the modified number.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324840998&siteId=291194637
Recommended