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;
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;
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.
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;
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
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.