44 lines
975 B
Markdown
44 lines
975 B
Markdown
# lec10
|
|
|
|
This lecture has a corresponding lab excercise who's instructions can be found in `triggers-lab.pdf`.
|
|
|
|
## What is a trigger
|
|
|
|
Something that executes when _some operation_ is performed
|
|
|
|
## Structure
|
|
|
|
```
|
|
create trigger NAME before some_operation
|
|
when(condition)
|
|
begin
|
|
do_something
|
|
end;
|
|
```
|
|
|
|
To explain: First we `create trigger` followed by some trigger name.
|
|
Then we have to denote that this trigger should fire whenever some operation happens.
|
|
This trigger then executes everything in the `begin...end;` section _before_ the new operation happens.
|
|
|
|
> `after`
|
|
|
|
Likewise if we want to fire a trigger _after_ some operation we ccan just replace the before keyword with `after`.
|
|
|
|
> `new.adsf`
|
|
|
|
Refers to _new_ value being added to a table.
|
|
|
|
> `old.adsf`
|
|
|
|
Refers to _old_ vvalue being changed in a table.
|
|
|
|
|
|
## Trigger Metadata
|
|
|
|
If you want to look at what triggers exist you can query the `sql_master` table.
|
|
|
|
```
|
|
select * from sql_master where name='trigger';
|
|
```
|
|
|