📺 Develpreneur YouTube Episode

Video + transcript

SQL Tutorial - Day 11

2022-05-10 •Youtube

Detailed Notes

1. Date and time functions select adddate(type_datetime, INTERVAL 1 DAY), adddate(type_date, INTERVAL 1 DAY), adddate(type_year, INTERVAL 1 DAY) from all_dates; select adddate(type_datetime, INTERVAL 1 MONTH), adddate(type_date, INTERVAL 1 MONTH), adddate(type_year, INTERVAL 1 MONTH) from all_dates;

select adddate(type_datetime, INTERVAL -2 MONTH), adddate(type_date, INTERVAL -2 MONTH), adddate(type_year, INTERVAL -2 MONTH) from all_dates;

select adddate(type_datetime, INTERVAL 1 YEAR), adddate(type_date, INTERVAL 1 YEAR), adddate(type_year, INTERVAL 1 YEAR) from all_dates;

select year(adddate(type_datetime, INTERVAL 1 YEAR)) dtm, year(adddate(type_date, INTERVAL 1 YEAR)) dt, year(adddate(type_year, INTERVAL 1 YEAR)) yr from all_dates;

select dayname(type_datetime), dayname(type_date) from all_dates;

select adddate(type_datetime, INTERVAL 5 SECOND), adddate(type_date, INTERVAL 5 SECOND) from all_dates;

select datediff(type_datetime,now(quit)), type_datetime, now() from all_dates; select datediff(type_datetime,now()) / 365, type_datetime, now() from all_dates;

Transcript Text
[Music]
well hello and welcome back we are
continuing looking at sql development uh
particularly in the mysql mariadb world
but a lot of this is more or less uh
going to be applicable regardless of
whatever database you're in
this is an area where it will be a
little different syntactically
in some of the other databases because
we're going to start talking about
stored procedures today
and this is
actually something that wasn't even
supported by my sequel until
you know a version which now it's been
for a while so but there's there was a
point where it didn't even support them
and i think it probably helps to start
with what is a stored procedure
a stored procedure is roughly
uh i mean it's a screw it's a procedure
it's a function essentially although
functions are a little different and
we'll talk about those
but a stored procedure is essentially a
script it is instead of us doing you
know we could do like select star from
address
and then we could do select
star from
uh what was that from app user where
was that t1 id
equals let's just say
one
so we can do this like you know
combination of when we can sit there and
start typing multiple
uh commands essentially
and we can also do this outside of the
database we could have some sort of a
odbc jdbc you know type of connection or
native connection into
the database and have
whatever language we use you know c
sharp java python
whatever
actually send commands
and
put together some
a series of commands and some related
logic based on that
but
that's not always
uh one it's not always the best approach
it's not always option uh an option
and um you know the third thing is it's
actually faster often to do stuff within
the database than to do this back and
forth calls you know if you have to
like pull a result set out of the
database into your application wherever
it's at
do some work
make a couple other calls into the
database take the results do some more
work
it's easier you know you've got all that
transact that
transfer
from database application and it's nice
to not have that
so that's why we have stored procedures
actually it's it's probably more simple
than that it was just so that we can do
a little more let me put some more logic
into
our database and not have to worry about
another language
now the first thing i need to explain
because of the way it works
with stored procedures is there's a
thing called delimiter
now currently
you can see if i do select star from app
user or equals one
this semicolon is my delimiter
that says that ham at the end of it a
line if i use the delimiter command
and say i want to use this slash slash
now
when i do that select star
notice it's i've got this little arrow
it says hey i'm looking for more like
you didn't finish the line i can't
execute it yet
so now if i do that
now when i do that slash it'll give me
you know allow me to do multiple calls
and it means as soon as i put a
semicolon it's not going to end
so i could even do here i'm going to
select star from
uh address
and then do a semicolon
and now what i see here and this allows
me to actually
put together multiple things and you can
see here where first
it grabbed from app user
and then
it did the call select from star address
so it's actually saying that hey each of
these this is a command
because i've got my
my line terminator of a semicolon but
the delimiter is going to tell me when
i'm actually going to execute it so it
allows me to actually
batch together a couple things which is
needed when we get into stored
procedures
now the first thing
that we want to do in a store procedure
is
let's just keep it very simple
so what does one look like well once
we've set our delimiter and you got to
make sure you do that otherwise you'll
have some issues once you start throwing
semicolons in here it's pretty easy so
i'm going to do create procedure
and
i'm just going to call this um
i'm going to call my tables
and i can give it parameters but i'm not
at this point
and enter now i'm going to do a begin
and then i'm just going to say show
tables
i put my semicolon like i normally would
i do an end
and so now this is my procedure my
tables begin show tables end
and i can come back i can do my little
completion
and it doesn't like it in the end so i
need to do probably a semicolon here
uh no what did i miss create procedure
my tables
so let's put you there
oh shoot that's not a it's because
that's not that okay so now i've got
that my mistake
i forgot how to do show tables
and so now i've created this procedure
now let me change my delimiter back to
semicolon
and now i've created this procedure
which uh let me
do it like this
just so that you have it so delimiter is
the dot
and
let's do this
clean this up a little bit
because we can probably come back to
this okay
so now we have a procedure
and if we wanted to look there's a
couple of different ways we can do this
and the one is we can do show
procedures status i believe it is oops i
gotta type it right
and i can see that here i have this
table
in the database and it's a little hard
to read here but rough
let's see if we open up a little bit
database is tutorial the name is my
tables it's a procedure
the definer so that's who who's the user
that created it was it modified created
security type it's got some additional
stuff that we don't really care as much
about so we have this table now this
procedure now
called my tables
and in order to do it
to run it we can do call my tables
or semicolon
and
so now it goes in and it does a show
tables
now if we were to go in and try to
replace uh create it again
so if we go do
this
now when it comes through it says my
tables already exist so what we can do
is uh let's see so we're still in that
delimiter good so we can also try we can
do create or replace
procedure my table
and it's going to be
let's see
let's say use tutorial
and so we can do that so now it's going
to say i'm going to jump in tutorial and
do tables so now
i take the same thing
uh let's leave it at that for now oops i
don't need to change my delimiter
so i can do that let's make sure that it
pastes in it does
and i do
use
is not allowed in store procedures and
so here we're seeing where yes you can't
use everything there's like and the
problem here is that my
um
stored procedure lives in a specific
database
and so i can't just jump in but what i
can do
and so let's just look at the same thing
if i say
what are my databases i don't know what
i have as an option here
oh
uh so information screen so let's say i
use
and i use
information
schema
okay so now i'm there if i do show
tables
i notice i haven't set my delimiter back
so now if i do call
let's go back to where i did that call
um oh let's just do this
whoops
delimiter is now back to here
and where we do our call that worked
here we go call my tables
now
we're going to find that
access denied for user to database
information schema well i'm not able to
see it but i'm it's also not calling it
properly i have to actually say
tell it what database it was in
and then i can call it
so there's a couple of things we you
know we have some limits to and it's but
it's mostly
uh within a database in the mysql
mariadb world
we're going to make so we're going to do
some work with it uh work on tables
there if we want to refer to something
in another database then we have to
explicitly
give it a uh give it a name which in
this case like you know information
schema dot or tutorial dot to tell us
which database we're in and we could do
that all around we could always be
we could always like we could do create
a replace procedure
uh tutorial dot
my tables
and we can do it there
so let's see show tables let's say um
oh let's do
i wonder if we do uh
what was that show procedure
i wonder if it allow us to do that one
it may not we'll find out
so let's take that
okay so it allowed us to do that so now
if we do call
where's our call here we go
uh and so it does work so show procedure
status so we came in and we show list of
tables
and then we get
our procedure status
so we are able to
you know
essentially i don't batch up a couple of
commands
now there are other things we can do
that um
are not necessarily gonna like we can do
this let's do this
let's go back to this one so let's go
back to our oh let me do this
um
so that's my tables
now let's do um
my data
and let's do select
star from
address
select star from
uh
app user
what are our tables
and let's say look at type
look up
type
oh not my date my data
let's create that guy
or gal
uh get our delimiter back and now we're
gonna do a call
my data
and we can see that each of those
selects comes through
now we can
send it something so now we can do
um
let's call it
ident
and we can do select
uh let's do it with an underscore just
to make it easier
and i think we're gonna and i'm not i'm
just gonna go simple right now
um we're gonna get more complicated on
our
parameters next time around but let's
see where
um
what is our address id
t1id okay
t1 id equals
oops
id equals
underscore id
oh let's call this
let's get address
so now
now if i call git address and if i don't
give it a parameter
it's going to say that hey oh because
i'm not the right player let me go ahead
and
use tutorial
first so i can i don't have to specify
who i am and here it tells me incorrect
number of arguments
expected one got zero so it even tells
you how many expected so i'm gonna do
get address two
and now i can see that it
now i've passed a parameter and so we
now i'm giving it
something a little more useful
as far as my procedures go
i think i just want to stop today
because we're going to start getting
into a little bit more fun stuff with
this this is it does go beyond this how
we can actually
um
start working with our data a little bit
more and get a little bit more into how
we deal with
parameters and things like that
there is a lot to learn about these so
we will definitely be spending a little
bit of time on stored procedures and
we'll see a lot of the same things as we
get into functions
and then we'll even do a little bit of
trigger related stuff as we're
other things as we're moving along but
hey
uh each day we're just getting a little
bit better learning a few more things
and uh slogging our way through this so
hopefully you learned a couple things
today and you come back next time and we
just continue learning our way through
this until we're all
masters of some sort that being said 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.119

well hello and welcome back we are

29.359

continuing looking at sql development uh

32

particularly in the mysql mariadb world

35.28

but a lot of this is more or less uh

38.239

going to be applicable regardless of

39.52

whatever database you're in

42.16

this is an area where it will be a

44.239

little different syntactically

46.96

in some of the other databases because

48.879

we're going to start talking about

50.32

stored procedures today

52.8

and this is

55.84

actually something that wasn't even

57.12

supported by my sequel until

59.52

you know a version which now it's been

61.44

for a while so but there's there was a

63.44

point where it didn't even support them

65.6

and i think it probably helps to start

67.68

with what is a stored procedure

70.96

a stored procedure is roughly

73.439

uh i mean it's a screw it's a procedure

75.68

it's a function essentially although

77.28

functions are a little different and

78.32

we'll talk about those

80.159

but a stored procedure is essentially a

81.84

script it is instead of us doing you

85.04

know we could do like select star from

88.799

address

93.04

and then we could do select

94.88

star from

97.36

uh what was that from app user where

104

was that t1 id

108

equals let's just say

110.56

one

113.52

so we can do this like you know

115.04

combination of when we can sit there and

117.68

start typing multiple

120

uh commands essentially

122.479

and we can also do this outside of the

124.719

database we could have some sort of a

127.84

odbc jdbc you know type of connection or

131.039

native connection into

133.12

the database and have

135.04

whatever language we use you know c

136.879

sharp java python

138.879

whatever

140.72

actually send commands

143.12

and

144

put together some

145.68

a series of commands and some related

147.36

logic based on that

150.64

but

152.48

that's not always

154.08

uh one it's not always the best approach

156.08

it's not always option uh an option

158.64

and um you know the third thing is it's

161.599

actually faster often to do stuff within

164.4

the database than to do this back and

166.8

forth calls you know if you have to

169.12

like pull a result set out of the

170.64

database into your application wherever

173.2

it's at

174.319

do some work

175.92

make a couple other calls into the

177.28

database take the results do some more

179.28

work

180.159

it's easier you know you've got all that

181.44

transact that

183.44

transfer

184.56

from database application and it's nice

187.28

to not have that

189.44

so that's why we have stored procedures

191.76

actually it's it's probably more simple

193.28

than that it was just so that we can do

196.48

a little more let me put some more logic

198.159

into

199.36

our database and not have to worry about

201.28

another language

203.44

now the first thing i need to explain

205.599

because of the way it works

207.519

with stored procedures is there's a

209.44

thing called delimiter

211.44

now currently

212.799

you can see if i do select star from app

214.879

user or equals one

217.519

this semicolon is my delimiter

220.319

that says that ham at the end of it a

222.159

line if i use the delimiter command

225.519

and say i want to use this slash slash

228.48

now

229.76

when i do that select star

232.48

notice it's i've got this little arrow

234.159

it says hey i'm looking for more like

235.68

you didn't finish the line i can't

237.36

execute it yet

238.72

so now if i do that

241.439

now when i do that slash it'll give me

243.519

you know allow me to do multiple calls

245.12

and it means as soon as i put a

246.239

semicolon it's not going to end

248.4

so i could even do here i'm going to

249.599

select star from

252.48

uh address

255.12

and then do a semicolon

258.799

and now what i see here and this allows

260.959

me to actually

262.56

put together multiple things and you can

265.04

see here where first

266.96

it grabbed from app user

271.6

and then

272.72

it did the call select from star address

274.8

so it's actually saying that hey each of

276.88

these this is a command

279.36

because i've got my

280.88

my line terminator of a semicolon but

283.759

the delimiter is going to tell me when

286.56

i'm actually going to execute it so it

289.04

allows me to actually

290.639

batch together a couple things which is

292.24

needed when we get into stored

294.72

procedures

297.52

now the first thing

299.44

that we want to do in a store procedure

302.96

is

303.919

let's just keep it very simple

306.639

so what does one look like well once

308.56

we've set our delimiter and you got to

310.24

make sure you do that otherwise you'll

311.6

have some issues once you start throwing

313.199

semicolons in here it's pretty easy so

315.28

i'm going to do create procedure

317.36

and

318.32

i'm just going to call this um

322.32

i'm going to call my tables

326.56

and i can give it parameters but i'm not

328.88

at this point

330.96

and enter now i'm going to do a begin

334.32

and then i'm just going to say show

335.6

tables

338.479

i put my semicolon like i normally would

340.72

i do an end

342.639

and so now this is my procedure my

345.28

tables begin show tables end

348.96

and i can come back i can do my little

351.6

completion

353.759

and it doesn't like it in the end so i

356.16

need to do probably a semicolon here

361.12

uh no what did i miss create procedure

364

my tables

373.6

so let's put you there

378.24

oh shoot that's not a it's because

380.16

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

382.08

that my mistake

383.84

i forgot how to do show tables

386.08

and so now i've created this procedure

387.919

now let me change my delimiter back to

390.56

semicolon

393.44

and now i've created this procedure

395.199

which uh let me

400.16

do it like this

401.759

just so that you have it so delimiter is

404

the dot

405.919

and

412.479

let's do this

417.039

clean this up a little bit

419.84

because we can probably come back to

421.199

this okay

423.12

so now we have a procedure

425.759

and if we wanted to look there's a

428.479

couple of different ways we can do this

432.4

and the one is we can do show

434.84

procedures status i believe it is oops i

438.24

gotta type it right

442.4

and i can see that here i have this

444.319

table

445.36

in the database and it's a little hard

447.199

to read here but rough

448.72

let's see if we open up a little bit

449.759

database is tutorial the name is my

452

tables it's a procedure

454.319

the definer so that's who who's the user

456.4

that created it was it modified created

458.96

security type it's got some additional

460.639

stuff that we don't really care as much

462.24

about so we have this table now this

464.319

procedure now

466.24

called my tables

468

and in order to do it

469.759

to run it we can do call my tables

472.8

or semicolon

474.319

and

475.84

so now it goes in and it does a show

479.12

tables

481.919

now if we were to go in and try to

483.84

replace uh create it again

486.08

so if we go do

488.96

this

493.28

now when it comes through it says my

494.639

tables already exist so what we can do

497.84

is uh let's see so we're still in that

499.84

delimiter good so we can also try we can

502.16

do create or replace

504.879

procedure my table

506.639

and it's going to be

508.72

let's see

512.959

let's say use tutorial

519.279

and so we can do that so now it's going

520.88

to say i'm going to jump in tutorial and

522.32

do tables so now

525.76

i take the same thing

528.32

uh let's leave it at that for now oops i

531.12

don't need to change my delimiter

534.8

so i can do that let's make sure that it

536.72

pastes in it does

539.68

and i do

541.92

use

543.279

is not allowed in store procedures and

545.279

so here we're seeing where yes you can't

548

use everything there's like and the

549.76

problem here is that my

552.24

um

553.92

stored procedure lives in a specific

556.32

database

557.519

and so i can't just jump in but what i

560.16

can do

561.36

and so let's just look at the same thing

562.8

if i say

564.8

what are my databases i don't know what

566.959

i have as an option here

569.2

oh

571.36

uh so information screen so let's say i

574.08

use

575.12

and i use

578.16

information

580

schema

584.64

okay so now i'm there if i do show

586.399

tables

588.88

i notice i haven't set my delimiter back

590.8

so now if i do call

593.92

let's go back to where i did that call

595.839

um oh let's just do this

598.399

whoops

601.2

delimiter is now back to here

605.6

and where we do our call that worked

608.48

here we go call my tables

610.24

now

611.68

we're going to find that

614.72

access denied for user to database

616.8

information schema well i'm not able to

618.72

see it but i'm it's also not calling it

621.36

properly i have to actually say

624.399

tell it what database it was in

626.88

and then i can call it

629.12

so there's a couple of things we you

630.32

know we have some limits to and it's but

632.48

it's mostly

634.399

uh within a database in the mysql

637.12

mariadb world

639.279

we're going to make so we're going to do

640.56

some work with it uh work on tables

642.959

there if we want to refer to something

644.8

in another database then we have to

646.48

explicitly

648.32

give it a uh give it a name which in

650.72

this case like you know information

652.16

schema dot or tutorial dot to tell us

655.519

which database we're in and we could do

657.76

that all around we could always be

660.64

we could always like we could do create

662.399

a replace procedure

664.72

uh tutorial dot

666.88

my tables

668.8

and we can do it there

670.72

so let's see show tables let's say um

676.16

oh let's do

677.92

i wonder if we do uh

681.36

what was that show procedure

685.6

i wonder if it allow us to do that one

687.12

it may not we'll find out

691.6

so let's take that

697.12

okay so it allowed us to do that so now

698.8

if we do call

701.519

where's our call here we go

705.279

uh and so it does work so show procedure

707.279

status so we came in and we show list of

709.12

tables

710.8

and then we get

712.16

our procedure status

714.88

so we are able to

717.36

you know

718.32

essentially i don't batch up a couple of

720.399

commands

721.68

now there are other things we can do

724.639

that um

727.04

are not necessarily gonna like we can do

729.04

this let's do this

731.76

let's go back to this one so let's go

733.2

back to our oh let me do this

736.16

um

740.32

so that's my tables

742.079

now let's do um

746.399

my data

750.8

and let's do select

752.48

star from

754.16

address

757.2

select star from

762.399

uh

763.44

app user

765.839

what are our tables

767.6

and let's say look at type

775.44

look up

777.04

type

780.16

oh not my date my data

784.32

let's create that guy

787.2

or gal

789.12

uh get our delimiter back and now we're

790.959

gonna do a call

794.88

my data

800.16

and we can see that each of those

801.44

selects comes through

803.839

now we can

805.519

send it something so now we can do

809.279

um

810.8

let's call it

812.24

ident

814.639

and we can do select

820.959

uh let's do it with an underscore just

822.8

to make it easier

825.519

and i think we're gonna and i'm not i'm

827.68

just gonna go simple right now

829.839

um we're gonna get more complicated on

831.92

our

832.8

parameters next time around but let's

834.399

see where

836.56

um

838.639

what is our address id

841.839

t1id okay

844.079

t1 id equals

847.76

oops

849.199

id equals

851.279

underscore id

856.32

oh let's call this

858

let's get address

864.56

so now

868.56

now if i call git address and if i don't

871.6

give it a parameter

873.76

it's going to say that hey oh because

875.68

i'm not the right player let me go ahead

877.04

and

878.72

use tutorial

880.959

first so i can i don't have to specify

882.959

who i am and here it tells me incorrect

885.44

number of arguments

887.36

expected one got zero so it even tells

889.839

you how many expected so i'm gonna do

891.519

get address two

894.399

and now i can see that it

896.48

now i've passed a parameter and so we

898.24

now i'm giving it

900.56

something a little more useful

902.639

as far as my procedures go

904.8

i think i just want to stop today

906

because we're going to start getting

907.44

into a little bit more fun stuff with

910.56

this this is it does go beyond this how

912.959

we can actually

914.48

um

915.36

start working with our data a little bit

916.72

more and get a little bit more into how

918.56

we deal with

920.56

parameters and things like that

922.959

there is a lot to learn about these so

925.04

we will definitely be spending a little

926.56

bit of time on stored procedures and

928.88

we'll see a lot of the same things as we

930.399

get into functions

932.56

and then we'll even do a little bit of

934.32

trigger related stuff as we're

936.32

other things as we're moving along but

938.48

hey

939.519

uh each day we're just getting a little

940.959

bit better learning a few more things

942.639

and uh slogging our way through this so

944.56

hopefully you learned a couple things

946.24

today and you come back next time and we

948.48

just continue learning our way through

950.32

this until we're all

952.079

masters of some sort that being said go

954.72

out there and have yourself a great day

956.16

a great week and we will talk to you

958.959

next time

975.92

you