adding lec10 for db
This commit is contained in:
parent
1449858ca3
commit
e6df867def
BIN
cst363/lab/triggers-lab.pdf
Normal file
BIN
cst363/lab/triggers-lab.pdf
Normal file
Binary file not shown.
43
cst363/lec/lec10.md
Normal file
43
cst363/lec/lec10.md
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
# 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';
|
||||||
|
```
|
||||||
|
|
Loading…
Reference in New Issue
Block a user