📺 Develpreneur YouTube Episode

Video + transcript

SQL Tutorial - Stored Functions

2022-06-07 •Youtube

Detailed Notes

1. Functions 2. Removing Functions 3. Listing Functions

DELIMITER //

CREATE FUNCTION multiply3( param1 decimal(10,2), param2 decimal(10,2), param3 decimal(10,2)

) RETURNS float DETERMINISTIC BEGIN return param1 * param2 * param3; END //

DELIMITER ;

DELIMITER // CREATE FUNCTION SumStats( param1 integer ) RETURNS integer DETERMINISTIC BEGIN select sum(value2) freom stats limit param1; END //

DELIMITER ;

SHOW FUNCTION STATUS;

DELIMITER //

CREATE FUNCTION add3( param1 decimal(10,2) ) RETURNS float DETERMINISTIC BEGIN return param1 + 3; END //

DELIMITER ;

Transcript Text
[Music]
well hello and welcome back we are
continuing our series of sql stuff uh
focused uh we're using mysql and mariadb
for our
uh our examples
and today we're gonna talk about
functions
now we have
looked at function
things like functions
with stored procedures so what we're
going to see is something that's just a
little bit different
when you get into the world of functions
but it's a couple of key
changes so let's just start with this
oops
let me do this
spoiler alert you'll be able to see this
okay so
we have uh with a function we're going
to do start with create function
much like
create procedure would be and we can do
create or replace function much as we
did with stored procedures
we can give it a name same thing we did
we give it some parameters with types
this looks should look
very much like a stored procedure
and then in this case we get uh we've
got a couple little different things one
we have this returns
and returns is a data type so it could
be float integer varchar whatever it
happens to be it could be decimals
whatever you want it to be
and that's telling it what this function
is going to return
and then you have this command
it's either deterministic or not space
deterministic
and that says whether or not
the same parameters will return the same
value
now this becomes pretty important when
you talk about
performance and caching and things like
that
this being a good example if i'm gonna
call multiply three
a thousand times
and
i have the same you know and it's the
parameters are one two three let's just
say those are the values
and i know that the answer is the same
every time then instead of going into
this code
i can just store that when i call it
with those values what the you know what
the
solution is what the result is
and that's going to save a lot of
processing time particularly if you're
doing this you know a thousand times so
deterministic versus non-deterministic
is
uh important thing to have now by
default it's not deterministic
so
you usually don't have to worry about it
except in the cases where you know that
the parameters are the same parameters
are always going to return the same
value
and this can be very important
particularly as you get into more
complicated functions that are doing
lookups or things of that nature because
you can really
allow the database to
cache and tune
itself so that it you know
it effectively runs as fast as possible
now then from there
it's going to be just as we do with a
stored procedure we do a begin and an
end
we're going to have a bunch of commands
now the difference with a function
is that you're going to have this return
and it could be a value uh it could be
like a variable it could be as i did in
this case or i just return
since just multiply 3 i'm just going to
return multiplying those three numbers
together
and so
the
the functions are going to look
very much like a stored procedure the
difference between the two is stored
procedures
don't necessarily
return values and you do your selects or
something like that whereas a function
the whole point is to call that function
return a value
therefore
if this is a stored procedure you would
say call multiply three
um and since we're decimals let's just
do like 35.8
i don't know 82.4
and four okay
that would be if it was a stored
procedure we'd call that
but it's not
because the call is
sort of like along the lines of a
selector something like that instead
we just do it like this there's no call
in front of it and so i can do a select
so i can do a select and then i have
this function and we've seen functions
these are custom ones but we've seen
them built in when we do like a
a min and a max or concat or things like
that so those functions are there we're
just
adding our own so now if i do select
then as we normally see i get my you
know and i could do uh
mult
multiply
three numbers
i can even do it this way
so multiply three numbers
and i get that
and notice i know it's you know this
one's a very simple one but notice that
initially this was in 0.001 seconds this
one's actually faster because after you
call it the first time so let's uh let's
change this up
so i think we'll see that it's such a
fast one in general
oh this is a big number
uh that's going to come in same thing
it's going to hit super fast but what's
happening is it can store that so it you
will typically see uh the first call
will be a little slower and then after
that it will have a cash value
in some cases and it'll go faster
in particular for like i said if we're
getting into something that it takes
a few seconds to build that out
so if we wanted to do um
let's see what did we call that well
let's do it this way
so if we do what did we have so if we do
like some value
stats
so let's do
we're gonna play around with this a
little bit
so now we're gonna do
we're going to do some stats
sum
so we'll even do it this way so we're
going to do some stats
and it's going to be parameter one
that's just going to be an integer
and it's going to return an integer
let's do it that way
this will be deterministic
and from what we want to do here
let's do select some
value two
from
stats
limit
param let's see if this will work
because normally we haven't talked about
a limit yet we'll come back to that
later but that's normally going to be so
let's do that first
so if i do select star from stats
i can get my eight rows back but i can
also do limit
five
and only get five rows back
so now let's take this guy
i need to set my delimiter somewhere up
here
oh and that's in
put this in my notes
and now we can get rid of that
and let's see if that works
so do source current
uh
yeah so it needs a hard number
i wonder if it is it let's see if we'll
do that
nope
so i can't do
that
let's do
uh what was stats where
we'll do a little different
id
less than that
well uh
wait
that may be a typo so maybe i can do
that
i'm not allowed to return a result set
from a function
oh
my about
into
uh student clear
at results
integer
into
that results
i need to return
that result let's see if that works
so i'm running into all right now let's
go look back here where i was playing
with variables before
uh let's make oh listen make him so
result
there
there we go okay
and then i can do
uh select what was that uh some stats
and let's do it seven let's see if it
takes a minute
okay so it took point zero one let's do
the same thing now it took zero so again
oh well we're not seeing it
unfortunately this is not taking long
enough i need a lot more rows however
uh it gives you the idea now that we can
so we can do this we can
you know we can change it up
oh this one's given the same value no
matter what
interesting
uh
we have a bug that we're
id is less than that and let's do create
or replace
five
okay so it didn't yeah it didn't like
that it needed a hard number instead of
a value variable
oh and now we're going to see
if we called the first time
oh no
because it was deterministic and i
already called it uh
yeah it's too close so i can't i'm not
and i was looking at i apologize i'm
looking at the how fast it ran
and
it does annoy it's it's not a big enough
one if you've got something that runs
like a second or two then i think when
you start adding to terministic then you
can do it you just got to figure out
something that takes a while in this our
database we're just not going to have it
so
you've already seen
let me skip ahead so the
next thing you can do
is you can list functions
and
let's show stats functions
let's see
so we have here actually it's a show
function
status
and so we can see this a little bit
complicated but we can see here that
in this database tutorial
we have two functions we have multiply 3
and some stats
the type is a function
and
basically you can see who created it
that's my user name and you know some
other stats if you wanted when it was
created when it was modified
um not a whole lot of interesting stuff
there
but we can do drop function
multiply three
and now we'll see that doesn't exist
anymore then we can do drop function
some stats
and
now we have no functions anymore
so we can do these just like for proceed
stored procedures that again the key
here is that now we can do this um
whereas
you're going to do a call with a stored
procedure so you can't do like select uh
let's see i don't know if i have a store
proceed but if i do
store procedure real quick um
so
i don't know if i have
that cursor example
so let's do that real quick let's see if
i can cut and paste not done like that
it's too long okay
so if i put that in here
and then i do call
what did i call that cursor example
i get mine that but i can't do
select cursor example
so when i'm doing so if i do select star
from
stats i can't do like selects
uh stat id comma
cursor example
comma
value one
from
stats
it's gonna blow up but i can do
let's do multiply through let's just
create multiply uh let's just we're
going to create one real simple
but i can do things like
that's a very simple one add three that
just has one parameter
turns a float deterministic
return that
plus three
very simple
and if we go in here
and now if we do source
current
dot sql
we can come in here and we can do
so we can do value one
comma add three value one
and we can put that there bam and so we
can see here we're with each number
you know one plus three is four two plus
three is five
and we can build that into our select so
now instead of where stored procedure is
gonna do some work a function is going
to do usually a modification to some
work based on data so we can use those
within our data as opposed to a stored
procedure that is sort of encapsulating
tasks and within data
so that gets us to
gets us through functions and
hopefully now between stored procedures
and functions we've got a good list of
things that we can work on
work with to do some pretty complicated
logic within our database and within our
queries
and that'll wrap this one up we're going
to go ahead and call it a lesson on this
one but we'll come back we've got plenty
more to discuss
and as always go out there and have
yourself a great day a great week and we
will talk to you
next time
[Music]
you
Transcript Segments
0.43

[Music]

27.039

well hello and welcome back we are

29.279

continuing our series of sql stuff uh

31.92

focused uh we're using mysql and mariadb

34.559

for our

35.76

uh our examples

37.6

and today we're gonna talk about

39.52

functions

40.879

now we have

42.559

looked at function

44

things like functions

45.92

with stored procedures so what we're

47.84

going to see is something that's just a

49.76

little bit different

51.52

when you get into the world of functions

54.64

but it's a couple of key

56.879

changes so let's just start with this

60.96

oops

62.8

let me do this

65.76

spoiler alert you'll be able to see this

71.76

okay so

74.56

we have uh with a function we're going

77.04

to do start with create function

80.72

much like

81.92

create procedure would be and we can do

85.119

create or replace function much as we

87.28

did with stored procedures

89.2

we can give it a name same thing we did

91.36

we give it some parameters with types

93.84

this looks should look

95.68

very much like a stored procedure

99.04

and then in this case we get uh we've

101.68

got a couple little different things one

103.439

we have this returns

105.759

and returns is a data type so it could

108.799

be float integer varchar whatever it

111.36

happens to be it could be decimals

113.36

whatever you want it to be

115.439

and that's telling it what this function

117.68

is going to return

120.24

and then you have this command

122.159

it's either deterministic or not space

125.439

deterministic

127.04

and that says whether or not

129.52

the same parameters will return the same

132.8

value

133.76

now this becomes pretty important when

135.12

you talk about

136.4

performance and caching and things like

138.48

that

140

this being a good example if i'm gonna

142.56

call multiply three

144.72

a thousand times

147.04

and

148.08

i have the same you know and it's the

149.68

parameters are one two three let's just

151.36

say those are the values

153.04

and i know that the answer is the same

154.72

every time then instead of going into

156.959

this code

158.319

i can just store that when i call it

159.68

with those values what the you know what

161.92

the

162.8

solution is what the result is

164.8

and that's going to save a lot of

166.959

processing time particularly if you're

169.04

doing this you know a thousand times so

172.8

deterministic versus non-deterministic

175.36

is

176.08

uh important thing to have now by

178.72

default it's not deterministic

181.36

so

182.319

you usually don't have to worry about it

184.8

except in the cases where you know that

187.04

the parameters are the same parameters

189.44

are always going to return the same

190.8

value

192

and this can be very important

193.04

particularly as you get into more

194.319

complicated functions that are doing

196.64

lookups or things of that nature because

198.8

you can really

200.159

allow the database to

202.239

cache and tune

204.4

itself so that it you know

207.76

it effectively runs as fast as possible

212.08

now then from there

214.239

it's going to be just as we do with a

216.159

stored procedure we do a begin and an

217.92

end

218.64

we're going to have a bunch of commands

220.959

now the difference with a function

224.239

is that you're going to have this return

226.799

and it could be a value uh it could be

228.799

like a variable it could be as i did in

230.959

this case or i just return

232.879

since just multiply 3 i'm just going to

234.48

return multiplying those three numbers

236.959

together

240.159

and so

241.12

the

242.239

the functions are going to look

244.239

very much like a stored procedure the

246.64

difference between the two is stored

248

procedures

249.2

don't necessarily

250.959

return values and you do your selects or

253.2

something like that whereas a function

255.84

the whole point is to call that function

257.6

return a value

259.04

therefore

260.079

if this is a stored procedure you would

262.16

say call multiply three

264.96

um and since we're decimals let's just

267.04

do like 35.8

269.44

i don't know 82.4

272.96

and four okay

275.6

that would be if it was a stored

276.639

procedure we'd call that

278.8

but it's not

280.639

because the call is

283.84

sort of like along the lines of a

285.04

selector something like that instead

288.56

we just do it like this there's no call

290

in front of it and so i can do a select

291.36

so i can do a select and then i have

293.28

this function and we've seen functions

295.199

these are custom ones but we've seen

296.639

them built in when we do like a

298.96

a min and a max or concat or things like

302.24

that so those functions are there we're

303.919

just

304.639

adding our own so now if i do select

309.36

then as we normally see i get my you

312.16

know and i could do uh

315.12

mult

316.56

multiply

320.16

three numbers

322.56

i can even do it this way

327.759

so multiply three numbers

329.759

and i get that

332.24

and notice i know it's you know this

334.96

one's a very simple one but notice that

337.12

initially this was in 0.001 seconds this

339.84

one's actually faster because after you

341.84

call it the first time so let's uh let's

344.24

change this up

346.08

so i think we'll see that it's such a

348.16

fast one in general

351.36

oh this is a big number

353.68

uh that's going to come in same thing

355.28

it's going to hit super fast but what's

356.96

happening is it can store that so it you

359.12

will typically see uh the first call

361.68

will be a little slower and then after

363.36

that it will have a cash value

365.84

in some cases and it'll go faster

368.4

in particular for like i said if we're

369.84

getting into something that it takes

372.16

a few seconds to build that out

374.56

so if we wanted to do um

377.36

let's see what did we call that well

379.52

let's do it this way

381.919

so if we do what did we have so if we do

384.639

like some value

386.56

stats

393.6

so let's do

400.639

we're gonna play around with this a

401.68

little bit

403.52

so now we're gonna do

407.6

we're going to do some stats

410.639

sum

412.88

so we'll even do it this way so we're

414.319

going to do some stats

416.16

and it's going to be parameter one

417.52

that's just going to be an integer

423.36

and it's going to return an integer

428.08

let's do it that way

431.039

this will be deterministic

432.8

and from what we want to do here

437.84

let's do select some

442.16

value two

450.56

from

451.599

stats

453.919

limit

455.12

param let's see if this will work

458.24

because normally we haven't talked about

459.919

a limit yet we'll come back to that

461.759

later but that's normally going to be so

464.56

let's do that first

466.319

so if i do select star from stats

469.199

i can get my eight rows back but i can

470.96

also do limit

472.56

five

473.84

and only get five rows back

477.039

so now let's take this guy

482.479

i need to set my delimiter somewhere up

484

here

489.599

oh and that's in

495.039

put this in my notes

498.8

and now we can get rid of that

503.52

and let's see if that works

506.479

so do source current

511.759

uh

512.88

yeah so it needs a hard number

518.64

i wonder if it is it let's see if we'll

520.56

do that

523.12

nope

526.64

so i can't do

528.56

that

529.6

let's do

534.64

uh what was stats where

537.2

we'll do a little different

540.08

id

541.12

less than that

544

well uh

547.12

wait

552.56

that may be a typo so maybe i can do

554.56

that

558.48

i'm not allowed to return a result set

561.12

from a function

564.56

oh

567.68

my about

569.04

into

574.16

uh student clear

576.8

at results

579.68

integer

582.64

into

584.48

that results

586.16

i need to return

591.519

that result let's see if that works

615.519

so i'm running into all right now let's

617.839

go look back here where i was playing

619.839

with variables before

623.36

uh let's make oh listen make him so

626.64

result

627.92

there

630.32

there we go okay

632.88

and then i can do

634.72

uh select what was that uh some stats

640.32

and let's do it seven let's see if it

642.56

takes a minute

644.32

okay so it took point zero one let's do

646.32

the same thing now it took zero so again

648.399

oh well we're not seeing it

650

unfortunately this is not taking long

651.36

enough i need a lot more rows however

656.079

uh it gives you the idea now that we can

657.76

so we can do this we can

660.48

you know we can change it up

663.279

oh this one's given the same value no

666.24

matter what

670.399

interesting

675.279

uh

684.72

we have a bug that we're

687.839

id is less than that and let's do create

690.64

or replace

702.16

five

704.399

okay so it didn't yeah it didn't like

706.48

that it needed a hard number instead of

708.399

a value variable

713.04

oh and now we're going to see

714.8

if we called the first time

716.639

oh no

717.92

because it was deterministic and i

719.36

already called it uh

725.12

yeah it's too close so i can't i'm not

727.68

and i was looking at i apologize i'm

729.279

looking at the how fast it ran

733.12

and

734.88

it does annoy it's it's not a big enough

736.72

one if you've got something that runs

737.839

like a second or two then i think when

739.68

you start adding to terministic then you

741.76

can do it you just got to figure out

743.04

something that takes a while in this our

745.2

database we're just not going to have it

747.2

so

749.6

you've already seen

752.24

let me skip ahead so the

755.04

next thing you can do

756.8

is you can list functions

759.839

and

761.36

let's show stats functions

766.639

let's see

768

so we have here actually it's a show

771.519

function

774.639

status

776.72

and so we can see this a little bit

778.16

complicated but we can see here that

780.639

in this database tutorial

782.8

we have two functions we have multiply 3

784.959

and some stats

786.56

the type is a function

788.88

and

789.839

basically you can see who created it

791.6

that's my user name and you know some

793.839

other stats if you wanted when it was

795.279

created when it was modified

797.76

um not a whole lot of interesting stuff

800.399

there

802.16

but we can do drop function

805.839

multiply three

808.88

and now we'll see that doesn't exist

810.56

anymore then we can do drop function

815.6

some stats

818.959

and

820.399

now we have no functions anymore

822.88

so we can do these just like for proceed

824.88

stored procedures that again the key

827.76

here is that now we can do this um

830.32

whereas

831.6

you're going to do a call with a stored

833.04

procedure so you can't do like select uh

835.519

let's see i don't know if i have a store

836.72

proceed but if i do

839.04

store procedure real quick um

844.88

so

845.68

i don't know if i have

847.12

that cursor example

849.199

so let's do that real quick let's see if

850.639

i can cut and paste not done like that

852.56

it's too long okay

854.639

so if i put that in here

861.92

and then i do call

863.6

what did i call that cursor example

870

i get mine that but i can't do

876.24

select cursor example

877.839

so when i'm doing so if i do select star

880.8

from

882.839

stats i can't do like selects

887.279

uh stat id comma

891.839

cursor example

894.399

comma

897.04

value one

900.24

from

901.92

stats

904.959

it's gonna blow up but i can do

909.839

let's do multiply through let's just

911.44

create multiply uh let's just we're

913.839

going to create one real simple

916.88

but i can do things like

922.8

that's a very simple one add three that

925.6

just has one parameter

929.199

turns a float deterministic

933.6

return that

936.48

plus three

940.8

very simple

943.12

and if we go in here

948.8

and now if we do source

951.12

current

953.12

dot sql

954.959

we can come in here and we can do

961.04

so we can do value one

962.8

comma add three value one

966.48

and we can put that there bam and so we

969.199

can see here we're with each number

971.279

you know one plus three is four two plus

973.279

three is five

975.199

and we can build that into our select so

978.24

now instead of where stored procedure is

980.079

gonna do some work a function is going

983.759

to do usually a modification to some

985.759

work based on data so we can use those

989.839

within our data as opposed to a stored

992.399

procedure that is sort of encapsulating

995.199

tasks and within data

997.839

so that gets us to

1000.24

gets us through functions and

1002.72

hopefully now between stored procedures

1004.48

and functions we've got a good list of

1006.16

things that we can work on

1008.16

work with to do some pretty complicated

1011.36

logic within our database and within our

1013.6

queries

1014.959

and that'll wrap this one up we're going

1016.72

to go ahead and call it a lesson on this

1018.8

one but we'll come back we've got plenty

1021.12

more to discuss

1022.88

and as always go out there and have

1025.28

yourself a great day a great week and we

1027.76

will talk to you

1029.52

next time

1030.859

[Music]

1046.4

you