📺 Develpreneur YouTube Episode

Video + transcript

SQL Tutorial - Triggers

2022-08-09 •Youtube

Detailed Notes

1. Triggers

create trigger [name] [BEFORE | AFTER] [INSERT | UPDATE | DELETE ] on [table] FOR EACH ROW [do something]

alter table lkp_type add column updated datetime;

create or replace trigger myFirstTrigger BEFORE UPDATE on lkp_type FOR EACH ROW set NEW.updated = now();

CREATE TABLE address_audit ( id integer, name VARCHAR(100) NOT NULL, city VARCHAR(50) NOT NULL, state varchar(2), zip integer, type_id integer, updated timestamp );

create or replace trigger myAuditTrigger BEFORE UPDATE on address FOR EACH ROW INSERT into address_audit (id,name,city,state,zip,type_id) VALUES (OLD.t_one_id,OLD.address_name,OLD.city,OLD.state,OLD.zip,OLD.type_id);

Transcript Text
[Music]
well hello and welcome back we are
continuing our tutorial on mariadb mysql
all that kind of good stuff
and today we're going to talk about
triggers
something we haven't really covered yet
we've sort of seen them but it's worth
taking a closer look at those and talk
about
how we would build those out now first
let's talk about what is a trigger
a trigger is
something that fires off when either an
insert an update or delete is made on a
table
so for example what we've seen before is
we've seen some default values those
would in a sense be a trigger
because although we're going to see
something more specific but they work in
a similar way where it checks to see if
a value is empty and if it is then it
uses the default value
and then does the insert or the update
or i guess it's actually it's the insert
in that case there are some cases where
we have seen
actually i'm not sure we've looked at it
but there's some cases where you can
have a delete
that does not allow a record to delete
if
it's
being referenced somewhere else now some
of those things are built into the
system or at least a part of the
sort of default configuration you can
make as a of a table
but there are other things that we can
do so we can create our own
triggers now triggers in general let me
go look
are pretty easy to create
the way you do a create trigger is you
use the command create trigger you give
the trigger a name
and you're going to say either before or
after because it's going to become
before or after we either insert update
or delete
and then it just is on a certain table
for each row and then you're going to do
some code so let's
play around first let's see let's go
look at
our app user
and so here
we have
a password email username we've got a
couple different things here
let's go ahead
and say
uh we already have one on that let's go
look at a different table
we already have something there so let's
go look at stats
so stats doesn't have anything terribly
useful in it so let's say after the fact
we want to come in we're going to do an
alter table
stats add column
uh updated
timestamp
just make sure i copy that because we
don't want to do that
okay so now if we just grab stats
and we're going to see now we have that
it's a time stamp so oops i don't want
to do this in timestamp
my mistakes so now if i do
from
yeah let's see start from stats
so now i'm going to get that update yeah
update is going to be there so now if i
do
update stats i want to do a date time
and set apologies
um
let's do
set
value one equal to five
where
stat id
equals
nine
and then if we look
we're going to see where that was
updated so let's not use a time stamp
let's use a date time
so let's look at a different table
i forgot that automatically
timestamp will give us a timestamp let's
do a lookup type
so we're gonna look here look up
type
there we go so now we're gonna do
and we're gonna do something a little
more complicated this is actually pretty
easy to do
as far as putting a time stamp in it but
let's see so alter table look at type
and column
updated date time
now if we do select star from
look up type oops let me get that alter
in there
so i can pull that up here
and so those are going to be nulls
and if i do an update it's and do
anything but what i can do is now i'm
going to do a create let's put this up
here first
so i'm going to create trigger
i'm going to call it my
first trigger
and it's going to be
before
update
and then i'm going to do
let's see on
lookup type
lookup type for each row
and then i'm going to do my little thing
so what i'm going to do
and there is a
essentially it's a reserved word so i
can do which is called old and new
so i can say
new
dot
updated
equals now
so if i take
that oop
yeah we'll do it and we'll do it before
the update so we're actually going to do
uh
update
okay this is really such a bad thing to
do but we're doing it just for showing
purposes don't do anything this way
there's better ways to do it
update look up row set
updated equal now
okay so now if i do where's my select
star
there
so now if i do update
uh lookup type
set
name
equal to
undefined
where
type id equals four
oop
let's not look up row let's look up type
so my mistake
i can't update it's always being used
and so okay so i can't do that update
so this was really it's really not going
to be a good one so let's do
okay lee let's go back to because i
can't do that i need to do so for each
one i think it's actually going to be um
what is that set
updated
equals now let's try that
i think that's what i want
oh it's because i don't have the
there we go my mistake okay so now if i
do where's that update statement uh
oh darn it uh
okay let's go back so
okay let's go look at select star that's
what happens when you're not paying
attention okay so if i'm gonna suck star
so you've all got this update but now if
i go back and do
there we go
now if i do that
and then do a select
now we're gonna see that it was a
slightly different uh
was it
yeah so it's a slightly different time
it's a few seconds later
so
uh this goes back to i'm sorry it's a
set it's not just a new so i have to set
a value so i could also do something
like um
i could do
something more complicated
so let's do something here i'm going to
do this as my second trigger
and in this case before and update i
want to say if
new
dot
code equals
empty
new dot
code
equals
so let's try that
okay so now i've got that so now let's
create ah that's not an update darn it
darn it darn it donut i want to do that
on
insert my mistake
so this is also going to give me sort of
a
oh
damn that
so what i want to do is get a little
more complicated
and we're going to create this new table
called address audits
uh hopefully this works
okay
and what i'm doing here
is
uh it's the same as the address table
except it's got a timestamp added to it
and it doesn't do an auto
increment
and so what i'm going to do here is i'm
going to do something a little more
complicated i'm going to say
create a replace trigger
this is going to be my
my audit trigger
this is something that we will see
probably more often than not so before
update on
uh address
let's make sure that's right
yeah for update for each row and in this
case i guess we can do here we can just
do a straight although we can do a
beginning and we're gonna go insert into
address
audit
and it's going to be uh id comma
name comma city comma
state comma zip
up comma type id
and then the values are going to be the
same things except from
the address table and what we're going
to do is we're going to say before we
change something we're going to copy it
or we're going to say what it is that
we're changing so let's do
um
and let's do
uh we'll keep it old
so what we're going to do
old dot
so what we're going to do now is
so this works
uh id in old
uh id oh let's see
oh
down here
did i have a different name for that i
did oh it's t1id
so it's gonna be old
uh
t1 id so i'm going to take
the old id basically the old values
and copy them into
oh come here
the audit table
uh name
ah
i have a couple there that okay name
city state zip okay so now it should
work
there we go so now if i do
uh this is before an update so let's
just select star from address
and we'll look at and wants to do uh
address audit we'll see if there's
nothing in there
nothing and nothing under my ad nothing
up my sleeve
and so now let's go do update
address set
uh
address name equal
my street
where
t1 id
equals two
and so we're just going to change it
from name three to address or to my
street
so that works
and
uh if we select from
address we're gonna see that now that's
my street but we're also gonna see if we
go from address audit
we're gonna see that it took that data
from what we just changed
and assigned that over to the audit
table and gave us a date so now we can
actually go backwards
and
recreate data changes which is uh
definitely a much more typical business
use for
a um
for a trigger is it will use things like
audits of some sort to either save a
value that we're changing
or
you know sometimes it's for checks and
things like that but typically what you
want to do is something very simple
because triggers are done
uh like while we're doing an update
so if you do an update without a trigger
it's gonna be a lot faster particularly
if you do mass updates that trigger's
gonna run each time so if i go in and
say
um
update
let me go here where did that update go
from
update address
in every case
and i'm going to set now
type id equals two
if i do that
now if i go look at audit see i'm going
to see it for each of those so i did
this mass update so it took a little bit
and took a
four one thousandths of a second but
it would be much faster
if it was not
kicking off that extra insert each time
so you also want to be careful what
what is it that i'm doing within my
trigger if you do something really
complicated
um
so
you know if you wanted to do like a
i don't know like a select star against
a table or something like that or do
some sort of complicated search
and you could end up really slowing down
your uh your performance
i think that'll wrap us up for now um i
think that's probably a good stopping
point as we're talking about our
triggers so
we will uh come back and we'll just
continue working through various my
sequel things but
until then go out there and have
yourself a great day a great week
and we will talk to you
next time
you
Transcript Segments
0.43

[Music]

27.439

well hello and welcome back we are

29.359

continuing our tutorial on mariadb mysql

32.8

all that kind of good stuff

34.48

and today we're going to talk about

36.96

triggers

38.48

something we haven't really covered yet

40.399

we've sort of seen them but it's worth

43.6

taking a closer look at those and talk

45.36

about

46.32

how we would build those out now first

49.36

let's talk about what is a trigger

52.32

a trigger is

54.16

something that fires off when either an

56.64

insert an update or delete is made on a

59.359

table

60.48

so for example what we've seen before is

62.96

we've seen some default values those

65.92

would in a sense be a trigger

68.88

because although we're going to see

70.479

something more specific but they work in

72.72

a similar way where it checks to see if

75.28

a value is empty and if it is then it

77.68

uses the default value

79.759

and then does the insert or the update

82.88

or i guess it's actually it's the insert

84.32

in that case there are some cases where

87.36

we have seen

90.4

actually i'm not sure we've looked at it

91.6

but there's some cases where you can

92.799

have a delete

94.159

that does not allow a record to delete

96.4

if

97.28

it's

98.159

being referenced somewhere else now some

100.479

of those things are built into the

101.84

system or at least a part of the

104.56

sort of default configuration you can

106.64

make as a of a table

108.479

but there are other things that we can

110.64

do so we can create our own

112.84

triggers now triggers in general let me

115.6

go look

116.96

are pretty easy to create

118.799

the way you do a create trigger is you

120.799

use the command create trigger you give

123.6

the trigger a name

125.119

and you're going to say either before or

127.119

after because it's going to become

128.56

before or after we either insert update

130.959

or delete

132.56

and then it just is on a certain table

134.8

for each row and then you're going to do

136.239

some code so let's

138.319

play around first let's see let's go

140.239

look at

143.12

our app user

148.4

and so here

150.16

we have

151.599

a password email username we've got a

153.599

couple different things here

155.12

let's go ahead

156.959

and say

159.68

uh we already have one on that let's go

161.12

look at a different table

163.36

we already have something there so let's

164.8

go look at stats

169.519

so stats doesn't have anything terribly

171.76

useful in it so let's say after the fact

173.599

we want to come in we're going to do an

174.8

alter table

176.72

stats add column

179.76

uh updated

182.4

timestamp

185.36

just make sure i copy that because we

187.12

don't want to do that

190.08

okay so now if we just grab stats

195.519

and we're going to see now we have that

197.599

it's a time stamp so oops i don't want

199.599

to do this in timestamp

201.12

my mistakes so now if i do

205.28

from

209.68

yeah let's see start from stats

213.92

so now i'm going to get that update yeah

215.76

update is going to be there so now if i

217.28

do

218.159

update stats i want to do a date time

220.4

and set apologies

222

um

224.159

let's do

227.599

set

229.36

value one equal to five

232.799

where

234.239

stat id

236.48

equals

238.159

nine

241.439

and then if we look

245.519

we're going to see where that was

246.319

updated so let's not use a time stamp

247.599

let's use a date time

249.439

so let's look at a different table

251.68

i forgot that automatically

254.879

timestamp will give us a timestamp let's

256.4

do a lookup type

258.4

so we're gonna look here look up

262.24

type

265.52

there we go so now we're gonna do

274.08

and we're gonna do something a little

275.04

more complicated this is actually pretty

276.72

easy to do

278.8

as far as putting a time stamp in it but

282.479

let's see so alter table look at type

284.639

and column

287.28

updated date time

292.08

now if we do select star from

296.08

look up type oops let me get that alter

298.96

in there

300.639

so i can pull that up here

306.56

and so those are going to be nulls

308.479

and if i do an update it's and do

309.84

anything but what i can do is now i'm

311.44

going to do a create let's put this up

313.6

here first

317.36

so i'm going to create trigger

319.6

i'm going to call it my

321.28

first trigger

325.28

and it's going to be

328

before

330.8

update

335.759

and then i'm going to do

337.68

let's see on

340

lookup type

344.88

lookup type for each row

347.84

and then i'm going to do my little thing

349.44

so what i'm going to do

351.759

and there is a

354.32

essentially it's a reserved word so i

356.479

can do which is called old and new

360.479

so i can say

361.919

new

363.84

dot

366.88

updated

369.44

equals now

372.56

so if i take

378.84

that oop

380.88

yeah we'll do it and we'll do it before

382.479

the update so we're actually going to do

384.08

uh

385.12

update

387.28

okay this is really such a bad thing to

389.52

do but we're doing it just for showing

391.36

purposes don't do anything this way

393.039

there's better ways to do it

395.199

update look up row set

397.84

updated equal now

405.28

okay so now if i do where's my select

409.039

star

410.8

there

412.56

so now if i do update

416.56

uh lookup type

418.72

set

420.08

name

422.08

equal to

423.36

undefined

426.639

where

427.599

type id equals four

435.12

oop

436.08

let's not look up row let's look up type

442

so my mistake

453.039

i can't update it's always being used

454.8

and so okay so i can't do that update

460.8

so this was really it's really not going

462.72

to be a good one so let's do

465.84

okay lee let's go back to because i

467.759

can't do that i need to do so for each

469.759

one i think it's actually going to be um

472.479

what is that set

474.24

updated

476.56

equals now let's try that

481.12

i think that's what i want

485.68

oh it's because i don't have the

489.919

there we go my mistake okay so now if i

493.199

do where's that update statement uh

499.199

oh darn it uh

501.68

okay let's go back so

503.599

okay let's go look at select star that's

506.479

what happens when you're not paying

507.68

attention okay so if i'm gonna suck star

509.599

so you've all got this update but now if

511.52

i go back and do

518

there we go

519.039

now if i do that

520.719

and then do a select

522.8

now we're gonna see that it was a

524.159

slightly different uh

526.24

was it

527.279

yeah so it's a slightly different time

529.36

it's a few seconds later

531.36

so

532.24

uh this goes back to i'm sorry it's a

533.92

set it's not just a new so i have to set

535.68

a value so i could also do something

537.76

like um

543.76

i could do

545.12

something more complicated

547.279

so let's do something here i'm going to

548.8

do this as my second trigger

553.839

and in this case before and update i

557.279

want to say if

561.519

new

562.399

dot

563.68

code equals

567.2

empty

569.68

new dot

571.519

code

572.959

equals

582.959

so let's try that

589.6

okay so now i've got that so now let's

591.519

create ah that's not an update darn it

593.36

darn it darn it donut i want to do that

594.72

on

597.76

insert my mistake

600.64

so this is also going to give me sort of

602.24

a

613.44

oh

615.279

damn that

620.72

so what i want to do is get a little

622.959

more complicated

624.64

and we're going to create this new table

626.88

called address audits

629.92

uh hopefully this works

632.959

okay

633.839

and what i'm doing here

635.44

is

636.56

uh it's the same as the address table

640.399

except it's got a timestamp added to it

642.64

and it doesn't do an auto

644.32

increment

645.36

and so what i'm going to do here is i'm

646.64

going to do something a little more

647.519

complicated i'm going to say

649.36

create a replace trigger

653.839

this is going to be my

655.519

my audit trigger

657.04

this is something that we will see

658.56

probably more often than not so before

660.8

update on

664.32

uh address

669.68

let's make sure that's right

671.519

yeah for update for each row and in this

674.56

case i guess we can do here we can just

676.48

do a straight although we can do a

677.76

beginning and we're gonna go insert into

682.32

address

684

audit

688.079

and it's going to be uh id comma

692

name comma city comma

695.2

state comma zip

698.16

up comma type id

703.2

and then the values are going to be the

705.36

same things except from

707.92

the address table and what we're going

709.2

to do is we're going to say before we

710.72

change something we're going to copy it

713.2

or we're going to say what it is that

714.8

we're changing so let's do

717.2

um

719.2

and let's do

723.2

uh we'll keep it old

725.36

so what we're going to do

726.639

old dot

734.32

so what we're going to do now is

737.36

so this works

742.48

uh id in old

746.399

uh id oh let's see

751.12

oh

754.639

down here

756.8

did i have a different name for that i

758.56

did oh it's t1id

760.32

so it's gonna be old

761.92

uh

763.16

t1 id so i'm going to take

765.839

the old id basically the old values

768.959

and copy them into

771.519

oh come here

772.8

the audit table

776.72

uh name

779.04

ah

782.8

i have a couple there that okay name

786

city state zip okay so now it should

788.24

work

793.68

there we go so now if i do

796.399

uh this is before an update so let's

798.8

just select star from address

802.72

and we'll look at and wants to do uh

804.48

address audit we'll see if there's

805.92

nothing in there

807.519

nothing and nothing under my ad nothing

809.6

up my sleeve

810.88

and so now let's go do update

814.32

address set

818.16

uh

819

address name equal

823.92

my street

830

where

831.24

t1 id

834.32

equals two

837.92

and so we're just going to change it

839.199

from name three to address or to my

841.519

street

842.88

so that works

844.8

and

846.079

uh if we select from

849.12

address we're gonna see that now that's

850.88

my street but we're also gonna see if we

852.56

go from address audit

854.24

we're gonna see that it took that data

858.079

from what we just changed

860.72

and assigned that over to the audit

862.48

table and gave us a date so now we can

864.16

actually go backwards

866

and

867.68

recreate data changes which is uh

871.519

definitely a much more typical business

873.92

use for

875.44

a um

876.639

for a trigger is it will use things like

878.639

audits of some sort to either save a

880.959

value that we're changing

883.199

or

884.56

you know sometimes it's for checks and

886.16

things like that but typically what you

887.68

want to do is something very simple

889.92

because triggers are done

892.16

uh like while we're doing an update

895.279

so if you do an update without a trigger

897.199

it's gonna be a lot faster particularly

899.44

if you do mass updates that trigger's

901.6

gonna run each time so if i go in and

904.639

say

906.079

um

909.44

update

910.88

let me go here where did that update go

912.56

from

913.6

update address

916.8

in every case

920.48

and i'm going to set now

924.639

type id equals two

933.759

if i do that

937.6

now if i go look at audit see i'm going

940.56

to see it for each of those so i did

942.079

this mass update so it took a little bit

943.92

and took a

945.68

four one thousandths of a second but

948.72

it would be much faster

950.399

if it was not

952

kicking off that extra insert each time

954.079

so you also want to be careful what

956.399

what is it that i'm doing within my

958.32

trigger if you do something really

959.839

complicated

961.44

um

962.639

so

963.759

you know if you wanted to do like a

966.32

i don't know like a select star against

968

a table or something like that or do

969.519

some sort of complicated search

972.24

and you could end up really slowing down

974.56

your uh your performance

977.6

i think that'll wrap us up for now um i

981.279

think that's probably a good stopping

982.639

point as we're talking about our

983.759

triggers so

985.12

we will uh come back and we'll just

987.199

continue working through various my

988.8

sequel things but

990.399

until then go out there and have

991.759

yourself a great day a great week

994.48

and we will talk to you

996.48

next time

1014

you