📺 Develpreneur YouTube Episode

Video + transcript

SQL Tutorial - Day 13

2022-05-17 •Youtube

Detailed Notes

1. Temp tables

delimiter // create or replace procedure tutorial.temp_table() BEGIN CREATE TEMPORARY TABLE t_mystuff( id integer, name VARCHAR(100) NOT NULL, city VARCHAR(50) NOT NULL, state varchar(2), zip integer, type_name varchar(100) ); create index temp_idx on t_mystuff(id);

insert into t_mystuff select t_one_id,a.address_name,a.city,a.state,a.zip,t.name from address a inner join lkp_type t on t.type_id = a.type_id;

select * from t_mystuff;

END; // delimiter ;

select t_one_id,a.address_name,a.city,a.state,a.zip,t.name from address a inner join lkp_type t on t.type_id = a.type_id;

Transcript Text
[Music]
well hello and welcome back we are
continuing looking at our working our
way through sql
uh tutorials and examples and today
we are going to continue looking at
stored procedures but we're going to
look at something that is
not stored procedures particularly but
just
i think used rather often or at least i
know i have
and that is the idea of temporary tables
now a temporary table as we can see here
the command is very much like creating
tables in generals as we you know the
general kind of things we've done to
this point and it's essentially supports
all of the same things
the difference
is instead of create table you put this
temporary
and what that does is it says this table
is only going to exist for this session
for example
in a sword procedure that you're going
to go ahead run it throw it out there
create it just use it while it's there
and then you're done you don't have to
worry about it anymore this is something
that's
basically for
i saw like a helper kind of table so i
can gather some data and that's
where we usually use it in a stored
procedure so that's what i'm gonna focus
with today
and i'm gonna use a simple example as
always but
i think this will give you something to
you know sort of
work on and look for
as we move forward and see where this is
just one of the tools we're going to
have to do some of the complex data
manipulation stuff that's out there
so
let's go back to
let's start with i want to start with
the query just to show
and that is
this query
uh well let me go ahead and do this let
me drop that table
uh t my stuff and i'll show you working
around with that table here in just a
minute
now let's take this query
and what i'm going to do
is
pull data from that address table and
i'm going to pull data from the lookup
type
and if you remember that lookup type
gives you a address type
a name for it and it has an id and
having this query if i was going to run
this query a bunch
or if i was going to have to do it over
maybe a large set of data or something
like that there are
there are definitely performance
issues when you start putting a lot of
table joins together
and particularly now this one's very
simple but let's say i had
i don't know like you know 10 or 15
different look up type values sitting in
my table
and had to join all of those
then
doing so bringing all that in and then
working with it may be something that's
not
that doesn't make sense performance wise
it's just a little too slow
instead what we can do is we basically
we're effectively going to denormalize
this data
instead of having our id we're going to
have the name so when we run this query
we're going to see here
that we have our we're selecting the id
city state zip the address name so this
would be like maybe a street address or
something like that but now we also have
this name which is the type so each of
these were business addresses
and this
doesn't have that that lookup type id
and so now this idea of the temp table
is well what if we wanted to keep this
data around for a little bit
we don't necessarily this isn't um
like a source table this is something
where we it's more or less like almost
like a reporting type table we want to
take this and we're going to do
something with it and so let's go ahead
and get rid of the normalization and
then just do stuff with this
with this data already
combined out or resolved out
and what we want to do then
is we're going to go in and we're going
to create
something called temp table
and
let's see if i can get all of that right
see if i copy paste it correctly
and so what we have here
is we've created this new procedure
called temp table
and
so you can see we don't have our
our t underscore my stuff doesn't exist
and what this procedure does is it's
going to come in it creates a table
it's going to insert data into that
table and that data is the it's
inserting into tmi stuff and what i'm
doing
here i've got that id name city state
zip and type name
and i'm just going to
take that same query i just showed that
gives me this data
and i'm going to throw it into this and
now here even like notice that i'm going
to rename it i'm not going to call it
the t1 id i'm just going to call it id
and then because this is a simple stored
procedure
i'm just going to do select star from
this temp table
we could do this with a regular table
but this is something where we and we're
saying hey we don't want this thing to
to sit around
so if i do call
where is that temp table
then it comes through
does that suck star and i see that this
is in that query basically that i just
showed you however you know now it's
with the these are the field names in
that temp table
and again if this was for reporting or
something like that that may be
the best way for you to go about it is
that you can actually take whatever the
names are
in the source database and essentially i
guess hide them or relabel them in a
temp table and then kick this output out
now you can do that with aliases and
stuff like that but
just yet another little
uh
bit of work or you know bonus that you
can add while you're doing this
now with this let's go back into our
show tables
and we're gonna see
that although
i created tmi stuff
it does not exist it's not there because
it was only there in this case the scope
was only during
the um the
execution of the stored procedure
now if i go back and do where was that
create
let me just do it this way
but now if i take the same thing and i
do create temporary table my stuff
if i do it inside just a normal
session
oh it says already exist so it's not
even showing it in the
um
it is not showing it everywhere i wonder
if
because it's out there it didn't get
cleaned up
but it also doesn't show
when i do show tables
yeah so it doesn't even show up for here
because it was owned by oh because it's
owned by the
um the store procedure
if i do drop
table
t underscore my stuff
then okay now he's gone but if i take
the same thing and now i'm to create
them
i quit out let's see if he's still there
because it sometimes changes stuff so if
i do use it
shoot
tables uh let's see if i just select
star from
t underscore my stuff
yeah it says it doesn't exist
if i try to create it
it's not there because i had it's a
temporary table and so once i quit out
of this session it's gone
and so with that stored procedure
typically your session is going to be
opened and then closed and then you're
done now you
so it's not the stored procedure itself
i misstated that it's the let me jump
back in here
it is the session you're in so if you
connect
and run a couple of store procedures and
then disconnect the temp table will
disappear but you could have multiple
stored procedures working on that temp
table
but now in this case i'm not seeing it
because i didn't i did not set up
everything to see it
this is going to show you only your
regular tables
but again it's one of those things you
don't really have to keep track of it
because once you close the session out
then your data is gone but of course
that's the challenge is you don't want
to
be in a situation where you do a lot of
work building out a temp table where you
really want to keep that where you want
to keep that information and then as
soon as you lose a session then you know
it's basically it's all all those
queries and everything have to be rerun
but with the temp table
you can even
um i'm pretty sure let's go do this um
so let's do
create index
temp idx on
t
my stuff
uh let's just do name
let's do the id
so now let's go back and see how that
works for us
and so when we do it everything's there
and if we go back and do the call
how far back is that
okay that's back a little bit
there it is
oh
i needed to use tutor
sorry
tutorial
tutorial
there we go
and so it runs through and so this time
it actually created an index in there so
we which again show you can
create multiple tables you can do
foreign keys you can create indexes
there's a lot that you can do
and in particular
again when you're
in a stored procedure these are very
useful because you're probably
depending on what you're doing you may
have some subset of data that you're
trying to build out
and you're wanting to work with that
you're wanting to manipulate that
and it may not be the same use that that
data is in the source database
particularly if you're like a
let's say you've got a transactional
kind of database where you're storing
i don't know
very transactional let's say that you
have a database that's storing credit
card transactions for sales for your
company
and you've got all this data in here and
all it's doing
typically is inserting new transaction
records and so you don't want to have a
lot of indexes on it you don't want to
have a lot of overhead because you want
that insert to just go quickly and be
done you don't want to have to worry
about stuff reindexing and slowing down
particularly as the t table gets large
but
if you want to do a bunch of queries on
that
then of course one thing you can do is
just go create a whole other database
pull that stuff out put your indexes on
things like that but you can also do it
i will say sort of on the fly by
creating a temp table and create that
table pull the data over
set up your indexes
and then go work on the data that way so
work on it from that temp table as
opposed to
your actual table
and you can there are some other things
we're going to see that are
similar approaches to doing this kind of
stuff but a temp table is one of those
that
it's just such an easy way to do
that sort of that sort of work on a
regular basis
particularly within the database
if you're going from a programming
language what you would probably do is
you would do a query you pull the result
set back into your your language into
your application
and then on that you know on that
application side you're going to walk
through it you're going to do your
manipulations or calculations or
whatever it is that you need to do
well what this gives you is an easy way
to build out you know a result set that
you can work with
and now we're going to work look at some
other things we can do as we get further
into
our stored procedures but this one i
really just want to make sure that i
covered temp tables and talked about
those for a bit
because i do think they're going to be
you know useful for us moving forward
and it's just one of those that i
figured also would be a pretty good you
know like one day topic kind of thing
so
you know bottom line don't be afraid to
play around with
commands we've already
learned
you know we've already looked at and
even some of the others within your
stored procedures you can do a lot of
stuff now like i said i could create a
whole table here
there's
it becomes complicated if you're going
to start doing dynamic sql and things
like that but yeah i could have stored
procedures that take parameters and
based on those they go out and build
tables and they go out and do
data loads and all kinds of stuff
particularly if you wanted to somehow
have a way to generate multiple in my
sequel databases that are you know like
make one per customer
stuff like that
if you want to do some sort of
vertical
you know
siloing and stuff like that so you have
one database that you hit and that's
where you get your data you don't have
to worry about
data crossing over from one you know
that's
one group can access that can also
be accessed by somebody else as well
but we get into that's getting into a
lot of i guess some of the more advanced
kinds of things that we're going to talk
about later
most likely if not in this session or
this series than in some of the others
because that does get a little bit more
into
security over just being in a database
that being said
i digress a bit
so i just wanted to point this out
temporary tables they just remember they
last for the session so if you do some
stuff and you're like on that command
line if you're sitting here on a command
line and you create a temporary table
and you quit and come back
it ain't going to be there
and you're gonna have to rebuild it
which again goes back to the whole idea
of you know let's
store this stuff out start a file
somewhere and execute that file or you
know maybe it's a stored procedure so
you can export execute that
uh all within that stored procedure and
then all is good
that being said it's time to wrap it up
for today
keep this i guess shortish
and give you a little bit of time back
as always 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.599

continuing looking at our working our

32

way through sql

33.44

uh tutorials and examples and today

36.88

we are going to continue looking at

38.399

stored procedures but we're going to

39.76

look at something that is

41.84

not stored procedures particularly but

43.68

just

44.399

i think used rather often or at least i

46.399

know i have

47.6

and that is the idea of temporary tables

50.64

now a temporary table as we can see here

53.76

the command is very much like creating

57.039

tables in generals as we you know the

59.199

general kind of things we've done to

61.039

this point and it's essentially supports

63.68

all of the same things

66.08

the difference

67.28

is instead of create table you put this

69.76

temporary

71.28

and what that does is it says this table

73.28

is only going to exist for this session

76.64

for example

77.92

in a sword procedure that you're going

79.68

to go ahead run it throw it out there

82

create it just use it while it's there

83.68

and then you're done you don't have to

85.28

worry about it anymore this is something

87.119

that's

88.88

basically for

90.64

i saw like a helper kind of table so i

93.759

can gather some data and that's

96.079

where we usually use it in a stored

97.759

procedure so that's what i'm gonna focus

100.4

with today

102.479

and i'm gonna use a simple example as

104.32

always but

106.32

i think this will give you something to

107.68

you know sort of

108.799

work on and look for

110.64

as we move forward and see where this is

113.119

just one of the tools we're going to

114.64

have to do some of the complex data

117.119

manipulation stuff that's out there

119.68

so

120.96

let's go back to

123.52

let's start with i want to start with

124.88

the query just to show

127.36

and that is

129.44

this query

131.84

uh well let me go ahead and do this let

133.44

me drop that table

137.12

uh t my stuff and i'll show you working

140.16

around with that table here in just a

141.36

minute

142.64

now let's take this query

144.8

and what i'm going to do

146.239

is

147.36

pull data from that address table and

149.84

i'm going to pull data from the lookup

151.92

type

153.519

and if you remember that lookup type

155.599

gives you a address type

158.239

a name for it and it has an id and

161.84

having this query if i was going to run

163.28

this query a bunch

165.68

or if i was going to have to do it over

168

maybe a large set of data or something

169.599

like that there are

171.12

there are definitely performance

173.2

issues when you start putting a lot of

175.04

table joins together

177.2

and particularly now this one's very

178.959

simple but let's say i had

181.84

i don't know like you know 10 or 15

183.599

different look up type values sitting in

185.519

my table

186.879

and had to join all of those

189.2

then

190.879

doing so bringing all that in and then

193.12

working with it may be something that's

195.12

not

197.12

that doesn't make sense performance wise

199.12

it's just a little too slow

200.959

instead what we can do is we basically

204

we're effectively going to denormalize

205.599

this data

207.519

instead of having our id we're going to

209.519

have the name so when we run this query

214.159

we're going to see here

216

that we have our we're selecting the id

219.76

city state zip the address name so this

222.159

would be like maybe a street address or

224.08

something like that but now we also have

225.68

this name which is the type so each of

227.76

these were business addresses

230.72

and this

232.08

doesn't have that that lookup type id

234.799

and so now this idea of the temp table

237.12

is well what if we wanted to keep this

239.04

data around for a little bit

241.439

we don't necessarily this isn't um

243.76

like a source table this is something

245.76

where we it's more or less like almost

247.2

like a reporting type table we want to

249.04

take this and we're going to do

250

something with it and so let's go ahead

251.76

and get rid of the normalization and

253.28

then just do stuff with this

255.28

with this data already

257.6

combined out or resolved out

260.479

and what we want to do then

262.479

is we're going to go in and we're going

263.6

to create

266.08

something called temp table

269.04

and

270.16

let's see if i can get all of that right

272.32

see if i copy paste it correctly

277.759

and so what we have here

279.759

is we've created this new procedure

282.639

called temp table

284.56

and

289.36

so you can see we don't have our

292.479

our t underscore my stuff doesn't exist

295.199

and what this procedure does is it's

296.88

going to come in it creates a table

299.44

it's going to insert data into that

301.68

table and that data is the it's

303.759

inserting into tmi stuff and what i'm

305.84

doing

307.039

here i've got that id name city state

308.96

zip and type name

311.039

and i'm just going to

312.56

take that same query i just showed that

314.8

gives me this data

317.6

and i'm going to throw it into this and

319.12

now here even like notice that i'm going

320.96

to rename it i'm not going to call it

322.639

the t1 id i'm just going to call it id

325.28

and then because this is a simple stored

326.96

procedure

328.08

i'm just going to do select star from

329.84

this temp table

331.44

we could do this with a regular table

333.52

but this is something where we and we're

335.039

saying hey we don't want this thing to

336.479

to sit around

337.759

so if i do call

340.24

where is that temp table

346.4

then it comes through

348.88

does that suck star and i see that this

350.72

is in that query basically that i just

352.72

showed you however you know now it's

354.96

with the these are the field names in

357.28

that temp table

358.96

and again if this was for reporting or

360.56

something like that that may be

362.56

the best way for you to go about it is

364.8

that you can actually take whatever the

366.88

names are

368.24

in the source database and essentially i

371.039

guess hide them or relabel them in a

372.8

temp table and then kick this output out

375.199

now you can do that with aliases and

376.479

stuff like that but

378.639

just yet another little

380.56

uh

381.28

bit of work or you know bonus that you

383.44

can add while you're doing this

385.84

now with this let's go back into our

387.28

show tables

389.36

and we're gonna see

390.96

that although

392.319

i created tmi stuff

395.84

it does not exist it's not there because

398.479

it was only there in this case the scope

401.039

was only during

403.199

the um the

405.199

execution of the stored procedure

407.759

now if i go back and do where was that

410.479

create

413.12

let me just do it this way

416.639

but now if i take the same thing and i

418.72

do create temporary table my stuff

423.12

if i do it inside just a normal

425.52

session

426.56

oh it says already exist so it's not

428

even showing it in the

431.36

um

432.479

it is not showing it everywhere i wonder

435.12

if

437.36

because it's out there it didn't get

438.56

cleaned up

440.56

but it also doesn't show

442.4

when i do show tables

445.44

yeah so it doesn't even show up for here

447.199

because it was owned by oh because it's

448.96

owned by the

450.479

um the store procedure

452.479

if i do drop

454

table

455.199

t underscore my stuff

459.84

then okay now he's gone but if i take

461.84

the same thing and now i'm to create

463.36

them

465.039

i quit out let's see if he's still there

469.28

because it sometimes changes stuff so if

471.28

i do use it

476

shoot

476.84

tables uh let's see if i just select

479.199

star from

482.96

t underscore my stuff

486.24

yeah it says it doesn't exist

488.08

if i try to create it

490.4

it's not there because i had it's a

492.24

temporary table and so once i quit out

494.24

of this session it's gone

497.039

and so with that stored procedure

498.479

typically your session is going to be

499.84

opened and then closed and then you're

501.039

done now you

502.8

so it's not the stored procedure itself

504.639

i misstated that it's the let me jump

507.599

back in here

510.08

it is the session you're in so if you

511.759

connect

512.88

and run a couple of store procedures and

514.719

then disconnect the temp table will

516.88

disappear but you could have multiple

519.599

stored procedures working on that temp

521.839

table

522.959

but now in this case i'm not seeing it

524.32

because i didn't i did not set up

526.88

everything to see it

529.04

this is going to show you only your

530.08

regular tables

532.08

but again it's one of those things you

533.519

don't really have to keep track of it

534.64

because once you close the session out

536.72

then your data is gone but of course

538.959

that's the challenge is you don't want

540.88

to

541.68

be in a situation where you do a lot of

543.04

work building out a temp table where you

544.959

really want to keep that where you want

546.48

to keep that information and then as

549.04

soon as you lose a session then you know

550.8

it's basically it's all all those

552.24

queries and everything have to be rerun

555.44

but with the temp table

557.04

you can even

558.8

um i'm pretty sure let's go do this um

562

so let's do

565.12

create index

566.8

temp idx on

570.08

t

570.8

my stuff

573.519

uh let's just do name

576.959

let's do the id

581.279

so now let's go back and see how that

583.04

works for us

588.08

and so when we do it everything's there

590.16

and if we go back and do the call

594.08

how far back is that

598

okay that's back a little bit

600.959

there it is

603.04

oh

604.959

i needed to use tutor

610.72

sorry

611.48

tutorial

613.279

tutorial

615.44

there we go

618.88

and so it runs through and so this time

621.04

it actually created an index in there so

622.88

we which again show you can

625.36

create multiple tables you can do

626.72

foreign keys you can create indexes

628.32

there's a lot that you can do

630.48

and in particular

633.04

again when you're

634.959

in a stored procedure these are very

636.72

useful because you're probably

639.36

depending on what you're doing you may

640.88

have some subset of data that you're

643.44

trying to build out

645.519

and you're wanting to work with that

647.279

you're wanting to manipulate that

649.279

and it may not be the same use that that

652.72

data is in the source database

655.279

particularly if you're like a

657.44

let's say you've got a transactional

658.72

kind of database where you're storing

661.2

i don't know

662.32

very transactional let's say that you

664.48

have a database that's storing credit

666.32

card transactions for sales for your

668.88

company

669.92

and you've got all this data in here and

671.279

all it's doing

672.56

typically is inserting new transaction

675.279

records and so you don't want to have a

677.04

lot of indexes on it you don't want to

678.56

have a lot of overhead because you want

680.399

that insert to just go quickly and be

682.16

done you don't want to have to worry

684.16

about stuff reindexing and slowing down

686.56

particularly as the t table gets large

689.519

but

690.72

if you want to do a bunch of queries on

692.959

that

693.839

then of course one thing you can do is

695.36

just go create a whole other database

696.88

pull that stuff out put your indexes on

698.88

things like that but you can also do it

701.04

i will say sort of on the fly by

703.36

creating a temp table and create that

705.279

table pull the data over

708.24

set up your indexes

710

and then go work on the data that way so

712.32

work on it from that temp table as

714

opposed to

715.36

your actual table

716.959

and you can there are some other things

718.639

we're going to see that are

720.8

similar approaches to doing this kind of

723.04

stuff but a temp table is one of those

725.279

that

727.2

it's just such an easy way to do

732.079

that sort of that sort of work on a

734.24

regular basis

735.6

particularly within the database

737.44

if you're going from a programming

738.959

language what you would probably do is

740.88

you would do a query you pull the result

742.72

set back into your your language into

744.8

your application

746.16

and then on that you know on that

748

application side you're going to walk

749.279

through it you're going to do your

750.88

manipulations or calculations or

752.48

whatever it is that you need to do

755.2

well what this gives you is an easy way

757.6

to build out you know a result set that

759.839

you can work with

761.44

and now we're going to work look at some

762.88

other things we can do as we get further

764.32

into

765.44

our stored procedures but this one i

768

really just want to make sure that i

769.279

covered temp tables and talked about

771.6

those for a bit

773.2

because i do think they're going to be

774.88

you know useful for us moving forward

777.519

and it's just one of those that i

778.639

figured also would be a pretty good you

780.32

know like one day topic kind of thing

783.839

so

784.639

you know bottom line don't be afraid to

787.92

play around with

789.839

commands we've already

792.079

learned

793.279

you know we've already looked at and

794.8

even some of the others within your

796.48

stored procedures you can do a lot of

798.8

stuff now like i said i could create a

801.04

whole table here

803.36

there's

804.639

it becomes complicated if you're going

805.92

to start doing dynamic sql and things

807.68

like that but yeah i could have stored

810.32

procedures that take parameters and

812.32

based on those they go out and build

813.68

tables and they go out and do

815.839

data loads and all kinds of stuff

818

particularly if you wanted to somehow

819.44

have a way to generate multiple in my

822.24

sequel databases that are you know like

824.88

make one per customer

826.8

stuff like that

828.48

if you want to do some sort of

830.72

vertical

831.839

you know

832.72

siloing and stuff like that so you have

834.639

one database that you hit and that's

837.44

where you get your data you don't have

838.48

to worry about

839.76

data crossing over from one you know

842.24

that's

843.04

one group can access that can also

846.079

be accessed by somebody else as well

849.68

but we get into that's getting into a

851.36

lot of i guess some of the more advanced

853.279

kinds of things that we're going to talk

854.399

about later

855.6

most likely if not in this session or

857.839

this series than in some of the others

859.36

because that does get a little bit more

860.48

into

861.44

security over just being in a database

864.639

that being said

866.24

i digress a bit

868.72

so i just wanted to point this out

869.92

temporary tables they just remember they

872

last for the session so if you do some

874

stuff and you're like on that command

876

line if you're sitting here on a command

877.12

line and you create a temporary table

878.8

and you quit and come back

880.8

it ain't going to be there

882.24

and you're gonna have to rebuild it

884.079

which again goes back to the whole idea

885.68

of you know let's

887.36

store this stuff out start a file

888.88

somewhere and execute that file or you

891.68

know maybe it's a stored procedure so

893.12

you can export execute that

895.36

uh all within that stored procedure and

897.279

then all is good

899.92

that being said it's time to wrap it up

902

for today

903.12

keep this i guess shortish

905.44

and give you a little bit of time back

907.6

as always go out there and have yourself

909.12

a great day a great week and we will

911.36

talk to you

912.8

next time

929.68

you