📺 Develpreneur YouTube Episode

Video + transcript

SQL Tutorial - Day 4

2022-04-14 •Youtube

Detailed Notes

1. Create a second table 2. Foreign keys 3. alter table ALTER TABLE table_name ADD new_column_name column_definition [ FIRST | AFTER column_name ]; 4. Date types: DATE, TIME, DATETIME, TIMESTAMP, YEAR

Transcript Text
[Music]
well hello and welcome back we are
continuing talking about our mysql
mazria database
mariadb type tutorials and we are into
day four
and today we are going to continue
talking about tables basically
and the creation of but we talked about
last time we talked about
creating just a table and putting some
data into it this time we're going to
talk about
a
we're going to talk about creating a
parent table a child parent print child
relationship
so
if i can remember my password and my
user id and type that right
i'll be able to get into
database and
if we see our tables we've got our just
we've got our one table if you remember
tutorial one is our table
and we had just a sort of like an
address kind of table so this time
we're going to create a sort of like a
user we're going to call him tutorial
parent so this is what we're looking at
making next
and we're going to see a lot of
similarities that we've seen before so
we've got a parent id is the
the key the primary key that we talked
about before
before it was whoops
i have to make sure i hit the right keys
when i switch
before we had t1 id for tutorial one
this one is going to be
uh t parent id so tutorial parent id and
again not null auto increment so we're
gonna be able to count those up now here
we're gonna actually
because this is the parent we have to
we're going to give it a child record
so we are going to point to it
by giving it t1 id so wherever we create
a record here
and let's make sure i still have some of
those
oh
i got too many
type it to me letters okay so there's
nothing in there so let's go see if i
can do an insert real quick got one
[Music]
let's go back
uh so we'll do four
and we'll create another one
three
and a two
and we'll just go with three records for
now
so if we go back there so we've got
essentially call these our address type
records so we've got those so we're
gonna do is we're going to be able to
grab it we're going to refer to it based
on that id
so that id we're going to store here and
then for this record this we'll call it
a user we're going to have a name let's
call it username i like that a little
better
and it's again we're just going to have
a varchar 100
not null we're going to use the default
we're going to talk about defaults a
little bit more here i'll come back to
that and then email login
i'll call it user password just because
it's better i know pwd will work but
we're going to call it user login
so we'll
and these we've seen with the varchars
now
we have a primary key which we've talked
about before
he doesn't need to be i don't know i had
that type twice and then um now the way
we're gonna link
is we have to create a constraint
and we're gonna call and we give it a
constraint name so we're gonna call it
simple fk for simple foreign key
and we're gonna tell the type of
constraint so foreign key is the type of
constraint and the way we do it with
this one
is we're going to say what column
in this
this table so t1 id we're going to link
that
to by the references command
by that keyword to the table name
tutorial1
and then t1id in there so we could call
this something else i could call this
like if i want to call this
address id
and call this
address id then it would link those two
that way even though i'm changing the
names but for consistency
oh
accidentally hit the wrong way i don't
want to save right now
consistently let's keep the names the
same so it's a little easier to deal
with our inserts and things of that
nature i need to stop accidentally
saving stuff
now let's talk about that default we've
we have not used
the default keyword before
but we have mentioned the idea of there
being default values that can be
assigned and if you look at the
description
you can see where there's a default so
here default's always null we haven't
really messed with that before
and that is the default if you don't
give it one the default is going to be
null but now when we create this table
and let's hope that i entered everything
properly so that it
so that it does
create it does
now if i look at
tutorial parent
then we'll see that the default is not
assigned
so let's actually do a record into that
i'm going to insert into
tutorial parent
and let's give it
uh t1id because we're gonna we're gonna
go ahead and tie it into an address
uh username
email
user login
and password okay
and value now let's do this what values
are going to be so we need to use a
valid well we do need to use a valid t1
id so we've got one two and three are
valid so let's just tie it to well let's
type to three we'll start we'll be a
little different
type to three
the username is going to be
user one
the email
let's call user one at test.com
uh let's see each login will be user one
and a password i'll just call it test
user one
let's see if that works it does
so now if we do that select star from
tutorial parent
if i enter that letter right then we'll
see that we've got our record in there
now
let's take that same record so i said
that it has to be valid it has to exist
so now we know that we have values 1 2
and 3 in our
that tutorial 1 table let's change it to
five
and see what happens
now we get this it's going to tell us we
can't do that
because
we cannot update or add or update a
child row
the foreign key constraint fails and so
remember we created this foreign key
and named it simple fk
well it tells us exactly that
in tutorial parent
the constraint simple fk foreign key
that one that we created it failed so
it's telling us right there that foreign
key tells us that doesn't exist which
says
okay that t1 id that we sent does not
exist in tutorial 1 as a t1 id value
so we're cool
now we can
however
because this is a this has created a
mini to one relationship so we can reuse
that address that we used before
it's not like a primary key it doesn't
have to be unique
and we can create user two
and we're gonna come in
and now if we look at all our tables
we see that it's got that even though
two and both of them refer to
uh t1 id
and we can see here where it tried to
increment actually that tried because of
this insert that failed insert it did
try to increment the t parent id
but failed so it just said well i'm
going to go ahead and jump to the next
available id or the next id and my
counting up in my auto increment
so we have our values there
now if we want to
get further if we want to actually
utilize this
as part of a report or something like
that
we can do
select star we use that before from and
this is where we're going to use what's
called an inner join so we're going to
select everything from tutorial parent
so we want each of these records here
but we also want to instead of that id
we also want to see what was their
address information
so in that case and we're going to
do an alias so we're going to call it as
tp so that'll be so
tutorial parent is instead going to be
tp as we refer to it elsewhere
and then we do this thing called an
inner join which means that we are going
to only grab records where this next
thing
occurs so we're going to interjoin
uh tutorial
1
and we'll call it t o uh no we're not
because that's it doesn't like that
we'll call it t1
on so it says i'm going to take
data from this other table and i'm going
to join it where
uh it's on instead of where clause or tp
dot
t1 id
equals the record in t1 t1
t1 dot
t1 id
and so now
we can see that it pulls this very long
thing here but it's going to pull so we
can see the parent record here
uh a parent one id
username all the way through the
password and then
here's the matching record from the
tutorial1 table and so you can see down
here where it comes into address name
now you can get a little more
you know you can clean that up a little
bit so you don't see the duplicates
so i can instead and i'm going to use
these aliases that i showed
so i can do like tp dot
username
comma tp.email
comma
tp dot
user login
comma i'm not going to show the password
so i'm going to tp dot
address name
comma
oh that's not tp now that's t1
because it's in the t1 table
t1.city
and t1.state
oh and t1.zip
so now i can clean that up a little bit
and i have the username email and user
login from the parent and then from the
address i have the name city state zip
so that gives me a join
that i can do now another thing i wanted
to
touch on in this episode
we've talked about is being able to
alter a table
so once i've created a table we've seen
that like here
once i've created a table before we
would drop it and then recreate it
but instead i can do an alter
and let me see if i've got one that's
like
that'll probably work
no don't want that
there we go
and in that what you do is so i can take
let's say i want to
add a column
we can use this for modify as well but
for right now let's go ahead and do
alter
table
uh let's see what i want to add let's go
ahead and add
to the user table let's
uh we'll add a login date so because we
haven't seen dates before so we're going
to alter table
tutorial parent
add column
and we're going to give it a name so it
will be
let's uh creation
whoops
creation date
it's going to be a date time
and it can be null
actually i don't think it's co i don't
think it's column let's go see i bet it
is
way back i think it is
it's just a col
as we are scrolling back through a lot
of
commands to see if i can find where i
last use it which a long time ago
apparently
wow i got way back on that
okay it may be
okay i'm just gonna do it this way
it's better to go if i can do the pace
there okay i got right
all right so now
if we look
uh describe tutorial parent
we can see that now i it's this date
this creation date date time is in there
and if i look
from there
oops i will see
oops i need to do an f
if i look here and see that it's null
anywhere now i can do an update i'm just
going to make it very simple right now
just to show you
uh update
tutorial
parent
and i'm going to set the
creation date
to
now
so now i can see where now i can see
dates there
now you have uh there are variations and
dates
you can do a date time which is what we
did
where you've got a time you have a date
22 4 1 and then you have a time you can
also do just a date or just a time or a
time stamp or a year now time stamp
i do want to do that
is if i've got that here here we go so
let's add
so that was
created and let's get the column
updation update date
time stamp
and i will get into some of the
uh update there we go
and he's going to be default by null so
we should be good
so if we come in here
and now if we do the same thing we do
that update
in the timestamp instead we do
update date and show you what a
timestamp looks like and we will get
more into dates and times and everything
else at a later date and
later discussions
but here because it's going to format it
even though it's a time stamp it's going
to give me that full date time
but depending on what you pull out of it
you'll get a
long integer which is basically a system
time kind of thing
this one's nice enough to format stuff
if i wanted to do
oops if i wanted to add let's
say creation
let's see
that's called start year
and make it a year
then we can look at it here
let's see they're all known but if we
update it
then we will find out
oh it doesn't like to do that so start
year it doesn't like to do it with the
now so i have to format that out so
instead let's do 2022
and we can see that there you go got all
our data there
so i think that'll probably cover for
this time
we're in a good spot
so we have added a child apparent child
relationship in our data
we have talked about dates and we're
going to continue digging into those as
we go into future episodes
so for now we'll wrap this one up uh
we'll have show notes for a lot of these
calls as well as always
and uh as always go out there have
yourself a great day a great week and we
will talk to you next time
you
Transcript Segments
0.48

[Music]

26.16

well hello and welcome back we are

27.92

continuing talking about our mysql

30.32

mazria database

31.84

mariadb type tutorials and we are into

36.16

day four

38.719

and today we are going to continue

40.48

talking about tables basically

42.8

and the creation of but we talked about

45.68

last time we talked about

47.68

creating just a table and putting some

49.52

data into it this time we're going to

51.12

talk about

52.64

a

54.559

we're going to talk about creating a

55.6

parent table a child parent print child

58.48

relationship

60.079

so

61.12

if i can remember my password and my

63.199

user id and type that right

67.439

i'll be able to get into

70.84

database and

73.84

if we see our tables we've got our just

76.08

we've got our one table if you remember

77.759

tutorial one is our table

84.08

and we had just a sort of like an

86.08

address kind of table so this time

88.96

we're going to create a sort of like a

90.96

user we're going to call him tutorial

92.88

parent so this is what we're looking at

95.759

making next

97.52

and we're going to see a lot of

98.479

similarities that we've seen before so

100

we've got a parent id is the

102.72

the key the primary key that we talked

104.56

about before

106.079

before it was whoops

109.04

i have to make sure i hit the right keys

110.399

when i switch

112.159

before we had t1 id for tutorial one

115.04

this one is going to be

118.88

uh t parent id so tutorial parent id and

122.079

again not null auto increment so we're

124.479

gonna be able to count those up now here

126.479

we're gonna actually

127.84

because this is the parent we have to

129.679

we're going to give it a child record

132.319

so we are going to point to it

134.56

by giving it t1 id so wherever we create

138.319

a record here

140.8

and let's make sure i still have some of

142.4

those

154.879

oh

156.64

i got too many

158.16

type it to me letters okay so there's

159.519

nothing in there so let's go see if i

160.72

can do an insert real quick got one

162.49

[Music]

166.8

let's go back

168.8

uh so we'll do four

174.319

and we'll create another one

180.159

three

181.36

and a two

183.44

and we'll just go with three records for

184.8

now

189.76

so if we go back there so we've got

192.319

essentially call these our address type

194.879

records so we've got those so we're

196.72

gonna do is we're going to be able to

197.92

grab it we're going to refer to it based

199.519

on that id

201.12

so that id we're going to store here and

202.959

then for this record this we'll call it

204.879

a user we're going to have a name let's

206.56

call it username i like that a little

207.84

better

208.959

and it's again we're just going to have

209.92

a varchar 100

211.84

not null we're going to use the default

213.84

we're going to talk about defaults a

215.28

little bit more here i'll come back to

216.879

that and then email login

219.68

i'll call it user password just because

222.56

it's better i know pwd will work but

224.48

we're going to call it user login

228.72

so we'll

229.92

and these we've seen with the varchars

232.159

now

232.959

we have a primary key which we've talked

234.64

about before

236.56

he doesn't need to be i don't know i had

238.4

that type twice and then um now the way

241.599

we're gonna link

243.519

is we have to create a constraint

246.08

and we're gonna call and we give it a

247.28

constraint name so we're gonna call it

249.2

simple fk for simple foreign key

252.159

and we're gonna tell the type of

253.68

constraint so foreign key is the type of

255.92

constraint and the way we do it with

257.359

this one

258.479

is we're going to say what column

261.359

in this

262.4

this table so t1 id we're going to link

264.8

that

265.68

to by the references command

268.96

by that keyword to the table name

270.88

tutorial1

272.56

and then t1id in there so we could call

275.68

this something else i could call this

277.12

like if i want to call this

279.84

address id

282.479

and call this

286.08

address id then it would link those two

288.56

that way even though i'm changing the

290.24

names but for consistency

292.88

oh

294

accidentally hit the wrong way i don't

295.28

want to save right now

297.12

consistently let's keep the names the

298.8

same so it's a little easier to deal

300.56

with our inserts and things of that

302.4

nature i need to stop accidentally

303.919

saving stuff

305.68

now let's talk about that default we've

307.44

we have not used

309.759

the default keyword before

312.88

but we have mentioned the idea of there

315.28

being default values that can be

317.44

assigned and if you look at the

319.759

description

321.84

you can see where there's a default so

323.36

here default's always null we haven't

325.919

really messed with that before

328.96

and that is the default if you don't

330.16

give it one the default is going to be

331.759

null but now when we create this table

335.44

and let's hope that i entered everything

337.28

properly so that it

339.28

so that it does

340.88

create it does

342.32

now if i look at

345.28

tutorial parent

349.919

then we'll see that the default is not

352.24

assigned

354

so let's actually do a record into that

356.8

i'm going to insert into

359.36

tutorial parent

362.8

and let's give it

364.84

uh t1id because we're gonna we're gonna

367.759

go ahead and tie it into an address

370.639

uh username

373.759

email

375.6

user login

378.72

and password okay

381.36

and value now let's do this what values

385.36

are going to be so we need to use a

386.96

valid well we do need to use a valid t1

389.6

id so we've got one two and three are

391.039

valid so let's just tie it to well let's

392.56

type to three we'll start we'll be a

393.759

little different

394.8

type to three

396.319

the username is going to be

399.12

user one

401.68

the email

403.68

let's call user one at test.com

410.479

uh let's see each login will be user one

416.4

and a password i'll just call it test

418.8

user one

420.8

let's see if that works it does

423.84

so now if we do that select star from

429.039

tutorial parent

433.599

if i enter that letter right then we'll

436.639

see that we've got our record in there

438.24

now

439.52

let's take that same record so i said

441.52

that it has to be valid it has to exist

443.68

so now we know that we have values 1 2

445.36

and 3 in our

446.96

that tutorial 1 table let's change it to

449.36

five

450.56

and see what happens

452.4

now we get this it's going to tell us we

454.8

can't do that

456.24

because

457.28

we cannot update or add or update a

459.599

child row

462.88

the foreign key constraint fails and so

466.16

remember we created this foreign key

468.8

and named it simple fk

471.199

well it tells us exactly that

473.919

in tutorial parent

475.68

the constraint simple fk foreign key

477.919

that one that we created it failed so

480.08

it's telling us right there that foreign

481.84

key tells us that doesn't exist which

483.52

says

484.56

okay that t1 id that we sent does not

487.52

exist in tutorial 1 as a t1 id value

491.199

so we're cool

493.039

now we can

494.8

however

496.319

because this is a this has created a

497.84

mini to one relationship so we can reuse

499.84

that address that we used before

502.08

it's not like a primary key it doesn't

503.36

have to be unique

504.639

and we can create user two

509.199

and we're gonna come in

514.08

and now if we look at all our tables

517.919

we see that it's got that even though

519.279

two and both of them refer to

522.479

uh t1 id

524.159

and we can see here where it tried to

525.6

increment actually that tried because of

527.68

this insert that failed insert it did

529.44

try to increment the t parent id

531.92

but failed so it just said well i'm

533.839

going to go ahead and jump to the next

535.2

available id or the next id and my

537.76

counting up in my auto increment

540.16

so we have our values there

542.16

now if we want to

545.6

get further if we want to actually

546.8

utilize this

548.48

as part of a report or something like

551.12

that

556.56

we can do

558.16

select star we use that before from and

561.12

this is where we're going to use what's

562.16

called an inner join so we're going to

563.44

select everything from tutorial parent

567.6

so we want each of these records here

571.519

but we also want to instead of that id

573.839

we also want to see what was their

575.36

address information

577.519

so in that case and we're going to

579.76

do an alias so we're going to call it as

581.519

tp so that'll be so

583.6

tutorial parent is instead going to be

586.8

tp as we refer to it elsewhere

589.36

and then we do this thing called an

590.8

inner join which means that we are going

592.959

to only grab records where this next

595.92

thing

596.72

occurs so we're going to interjoin

599.36

uh tutorial

601.68

1

602.64

and we'll call it t o uh no we're not

604.88

because that's it doesn't like that

606.079

we'll call it t1

608.079

on so it says i'm going to take

610.64

data from this other table and i'm going

612.56

to join it where

614.8

uh it's on instead of where clause or tp

617.519

dot

619.959

t1 id

622.64

equals the record in t1 t1

628.8

t1 dot

631.04

t1 id

633.839

and so now

636.079

we can see that it pulls this very long

638

thing here but it's going to pull so we

639.92

can see the parent record here

642

uh a parent one id

644.32

username all the way through the

645.279

password and then

647.6

here's the matching record from the

650.079

tutorial1 table and so you can see down

652.24

here where it comes into address name

653.68

now you can get a little more

656.16

you know you can clean that up a little

657.36

bit so you don't see the duplicates

659.76

so i can instead and i'm going to use

662

these aliases that i showed

665.519

so i can do like tp dot

668.399

username

669.92

comma tp.email

673.04

comma

674.399

tp dot

675.839

user login

678.399

comma i'm not going to show the password

680.32

so i'm going to tp dot

682.399

address name

685.04

comma

686.64

oh that's not tp now that's t1

689.2

because it's in the t1 table

694.88

t1.city

698.48

and t1.state

703.839

oh and t1.zip

709.12

so now i can clean that up a little bit

711.6

and i have the username email and user

713.279

login from the parent and then from the

715.279

address i have the name city state zip

718.24

so that gives me a join

720.56

that i can do now another thing i wanted

722.639

to

723.44

touch on in this episode

726.639

we've talked about is being able to

728.32

alter a table

731.04

so once i've created a table we've seen

732.56

that like here

733.92

once i've created a table before we

735.519

would drop it and then recreate it

738.16

but instead i can do an alter

746.8

and let me see if i've got one that's

748.56

like

749.519

that'll probably work

751.44

no don't want that

753.92

there we go

755.68

and in that what you do is so i can take

757.92

let's say i want to

760.639

add a column

762.399

we can use this for modify as well but

763.839

for right now let's go ahead and do

765.12

alter

769.44

table

772.48

uh let's see what i want to add let's go

774.079

ahead and add

775.839

to the user table let's

779.279

uh we'll add a login date so because we

781.44

haven't seen dates before so we're going

782.48

to alter table

785.519

tutorial parent

792.639

add column

796.399

and we're going to give it a name so it

797.68

will be

798.639

let's uh creation

803.12

whoops

805.04

creation date

807.2

it's going to be a date time

809.44

and it can be null

814.079

actually i don't think it's co i don't

815.44

think it's column let's go see i bet it

817.36

is

821.279

way back i think it is

827.36

it's just a col

834.079

as we are scrolling back through a lot

836.399

of

838.639

commands to see if i can find where i

840.56

last use it which a long time ago

842.72

apparently

854.48

wow i got way back on that

859.92

okay it may be

865.68

okay i'm just gonna do it this way

868.48

it's better to go if i can do the pace

870.72

there okay i got right

872.639

all right so now

875.12

if we look

876.32

uh describe tutorial parent

884.56

we can see that now i it's this date

887.04

this creation date date time is in there

889.44

and if i look

893.68

from there

898.32

oops i will see

902.16

oops i need to do an f

907.199

if i look here and see that it's null

909.04

anywhere now i can do an update i'm just

910.959

going to make it very simple right now

912.16

just to show you

913.36

uh update

915.519

tutorial

917.6

parent

919.44

and i'm going to set the

921.56

creation date

925.44

to

928.72

now

932.959

so now i can see where now i can see

934.959

dates there

936.959

now you have uh there are variations and

939.6

dates

940.639

you can do a date time which is what we

942.8

did

944.88

where you've got a time you have a date

947.44

22 4 1 and then you have a time you can

950.16

also do just a date or just a time or a

954.399

time stamp or a year now time stamp

958.32

i do want to do that

960.88

is if i've got that here here we go so

963.6

let's add

965.519

so that was

966.839

created and let's get the column

970.079

updation update date

976.88

time stamp

982.56

and i will get into some of the

985.04

uh update there we go

987.759

and he's going to be default by null so

989.36

we should be good

991.759

so if we come in here

994

and now if we do the same thing we do

995.839

that update

997.36

in the timestamp instead we do

1001.44

update date and show you what a

1003.519

timestamp looks like and we will get

1004.8

more into dates and times and everything

1006.639

else at a later date and

1008.839

later discussions

1011.36

but here because it's going to format it

1013.199

even though it's a time stamp it's going

1014.32

to give me that full date time

1016.24

but depending on what you pull out of it

1018.959

you'll get a

1020.16

long integer which is basically a system

1022.399

time kind of thing

1023.839

this one's nice enough to format stuff

1026.4

if i wanted to do

1031.12

oops if i wanted to add let's

1036.839

say creation

1039.76

let's see

1041.199

that's called start year

1046.319

and make it a year

1051.36

then we can look at it here

1053.28

let's see they're all known but if we

1054.72

update it

1059.44

then we will find out

1069.28

oh it doesn't like to do that so start

1071.2

year it doesn't like to do it with the

1072.799

now so i have to format that out so

1074.64

instead let's do 2022

1077.84

and we can see that there you go got all

1079.76

our data there

1082.96

so i think that'll probably cover for

1084.24

this time

1085.2

we're in a good spot

1086.64

so we have added a child apparent child

1088.96

relationship in our data

1090.96

we have talked about dates and we're

1092.72

going to continue digging into those as

1094.64

we go into future episodes

1096.559

so for now we'll wrap this one up uh

1098.72

we'll have show notes for a lot of these

1100.72

calls as well as always

1102.799

and uh as always go out there have

1104.4

yourself a great day a great week and we

1106.48

will talk to you next time

1123.76

you