📺 Develpreneur YouTube Episode

Video + transcript

SQL Tutorial - Day 8

2022-04-28 •Youtube

Detailed Notes

1. Fix constraints and autoincrement. 2. setting primary key before autoincrement 3. drop a constraint 4. Add the primary keys 5. Create an index 6. Unique index creation

alter table address add constraint address_pk PRIMARY KEY (t_one_id); alter table app_user add constraint app_user_pk PRIMARY KEY (t_parent_id);

alter table address modify column t_one_id int auto_increment;

create index auth_inx on app_user(userLogin);

alter table address add constraint FOREIGN KEY (type_id) references lkp_type(type_id);

alter table app_user add constraint user_address_fk FOREIGN KEY (t_one_id) references address(t_one_id);

Transcript Text
[Music]
well hello and welcome back we are
continuing our series when we're going
through
my sequel maria db development type
things
and we're really just in this episode up
to day eight uh episode eight we are
continuing or actually start going back
and working through some constraints
and talk a little bit more about those
now in the last episode
one of the things we did
is we moved our tables we basically did
we used a create table as select star
from
and ended up taking our
tutorial 1 and tutorial parent table
and basically created address and app
user the problem with those so if we
look at like
address
is we didn't get the constraints or
anything else it just pulled
the field names the column names
the type whether they were null or not
and
basically that was it
i may have pulled a default oh i think
it did pull it a fault over but not in
all cases um because let's see or maybe
it did
we will
oh it did so pulled over the and it
pulled over the default values
the key no pun intended issue with this
is that it did not pull across primary
key it did not pull across auto
increment and it did not pull across
foreign key relationships so the first
thing i want to do is fix those
the way we do that is we can come in
after the fact
and
if i can get some stuff out of the way
so i can work my mouse
then and come in here and we're going to
alter table address we're going to do a
constraint that we're going to call the
address primary key
it's going to be on the t1 id
and it works fine we're gonna do the
same thing and we're gonna do app user
on that one and so now if we go back to
our describes so describe address we now
see the primary key as t1 id but it's
not doing the alternate the
auto increment and app user
okay we've got our primary key now
we're going to work on
adding that adjusting that auto
increment
a key thing about this is that order
matters when you do this
once you create when you create the
table from
just creating it from a query like we
did before where we do like uh
do something like this where we just
build out our whole table
notice that we can put on our increment
we can do primary key so if i want to
create two table tutorial one from that
then for some reason didn't like what it
printed but basically
so now if i do
promote one
i'm going to see
boom i have my primary key it's auto
increment is that extra notice if i do
describe
what's that address
so i managed to somehow lose name oh
because i probably changed over the name
i added that later and
i don't have my auto increment
if you do it then it's fine if you wait
till after the fact an auto increment
cannot be done and actually i guess you
could run into this during the script an
auto increment cannot be added to a
non-key
has to be a primary key first
so
we have created those so we're going to
be safe when we do that we go in here
and we're going to take the
table address here and we're going to
modify that column t1 ib to auto
increment
and if we describe address now we'll see
that's on our increment we can do the
same thing
with the
parent
id
and no we could have gone in because we
sort of you know we're changing stuff on
the fly we could do this and just drop a
table and rebuild it
or we could do alter and do alter the
column names as well however we're still
going to have to worry about things like
making sure that we get our keys and our
primary keys and our foreign keys
together
now if we wanted to
one thing we run into is once we start
adding data
there can be issues where we can't
delete a record because we still have a
constraint we've seen that
or we want to insert a record and
there's a constraint issue
and so one of the things we can do is we
can drop a constraint so we
let's see
so let's first create
a foreign key because i don't think we
have any right now yeah we don't have
any so let's create that foreign key
from
uh type id address
so again that's what we're going to do
is we're going to do alter table
that's address
i'm sorry it's add constraint
instead of primary like we did here
primary key it's gonna be foreign key
so let's do four and it doesn't have to
be all
caps but that sometimes helps and so the
foreign key is gonna be type id
and we have to tell it what it
references and so it's going to
references
look up
what's that called
lookup type
type id
whoops
okay
and now if i do describe address
and i didn't create i didn't
give the constraint a name which we
normally have done
so here when i do add constraint i give
it a constraint name i didn't this time
so let's see what happens what is that
so now if we do select star f
from
lookup type
let's get one that's invalid so it's one
two three so if we insert into
address
address name
cannot be no comma type id that'll be
the shortest one we can do
here values
whoops
there you go
got off of that somewhere so values um
address name will be just oop
i should hit the right key delete me
and its type is going to be four so this
should give us a constraint issue which
is going to boom
it does and now notice it just generated
a constraint name since we didn't give
it one it generates that but now we can
take that
and we can do
drop constraint
that guy
let's come over here and do
just to make sure i get the
i could do it exist but it should be
so if i go here oh it's alter table
i can't do just a straight drop
so i need to do
alter table
address
whoops
drop constraint there we go so now if i
come back and try to do the insert
it allows it
now looks what happens if i try to
create that constraint again
where was that constraint there we go
now i can't because
it's going to blow up when it tries to
do it because
i already have an issue so what i need
to do is
uh select star from
so you don't really want to drop
constraints unless you're like cleaning
stuff out or
if you know that you're 100 sure that
they're gonna you know they're getting
in the way but they're 100 sure when you
get your data in it's going to be clean
uh so i'll just like start from address
i'm going to delete
from address
where
t1 id equals
5.
and now i can go back i can create the
there we go and i can create the foreign
key i'm also going to go in
let's keep that here
and i also want to do
let me create one real quick i'm going
to alter table
this is going to be app user
and constraint and this will be
user
address
foreign key
and it's going to be on the t1 id
and it's going to reference the address
t1 id
so if i take that
whoops
uh ch
alter table app user a constraint user
okay i did something wrong
um
ultra table and constraint user address
so it's foreign key
references
oh
because it has to i have to spell the
table right
there okay so now if we look at address
we see that we have our order increments
and our primary key if we look at app
user
we're gonna see that
we have our
foreign key here and our primary key
so we're in pretty good shape
so with this
we've got our table sort of straightened
out but while we're here
let's talk about indexes
when you are doing a
search in a database when you're doing a
select with a where clause or an inner
join
the value or values that it joins on the
case where you say this value equals
that value
when you do that
for example if i do select star from
app user
where t1 id
equals two i think that's one that's um
there we go
so in this case where i do this t1 id
equals one
um i guess i could i'd probably want to
do it sorry but in searching this it's
actually going to do what's called a
table scan it's going to look through
the entire table to find that
then one of the things we can do is add
indexes now for a primary key it's
already going to be indexed
for a foreign key it's indexed because
it's
indexed on the the primary key your
foreign key always relates to a primary
key on another table however let's say
we wanted to do um we're going to search
by user login which
probably is not uncommon so what we
would want to do is we can do a
index and so we could try create index
uh
let's see email
look up
on
uh who's that app user
email
and i think or
sorry user login
and so now
we're not going to see here a difference
i don't think
oh yes it is it's going to say so you're
going to see that you've got a key there
which is an index which means now
there is a
sort of like a table of contents that is
going to more quickly direct you to
records
now when you're talking about a string
it may or may not be useful although you
can use hashes and things like that that
may get you there closely
but particularly in a
number and a strings assuming that
there's a
a fairly even distribution and that they
are
you know easily found by uh order you
know like if everything's starts with a
aaaaa then
it's not going to get you there or you
know or the same
multiple letters together
numbers are usually going to be the
fastest because it can very quickly jump
to
a number via an index it knows real fast
that i'm gonna you know if i'm looking
for something between
a hundred thousand and a hundred
thousand and ten it can very quickly go
oh i'm gonna jump to this index to the
hundred thousand point into the hundred
and ten thousand
and that is
going to get you huge
performance changes as you get into this
when you're using indexes properly then
your queries are going to be really fast
if not you're going to do these table
scans which means every time it looks
for something it being the database it
has to go through every single record in
the database
and that can be very time consuming very
quickly
and so it's very useful to note that you
can make you can create indexes real
quick
now you don't want too many because when
you insert
then all the indexes have to be updated
so you can you know that can slow down
your inserts but particularly if you've
got a database that is
mostly static data or data that exists
and
sometimes you're making changes to it
but mostly you're searching against it
or for example a reporting database then
you're indexing you know is going to be
awesome you want to make sure that
that's
put everywhere that you are
searching for that kind of value or you
can do multi
multi-column indexes we'll see later
when you do that if you hit that index
then it's going to be a lot faster and
there you can go search
like database performance and indexes
show plan things like s-h-o-w space
p-l-a-n-w i'm sorry p-l-a-n
just that you know that
string and you will see graphical
examples of such things
so you do want to have a you want to
have indexes like in your mind
you can also do um let's say i want
that was email lookup i want to be email
unique
so let's say that i don't want to allow
an index
email address to show up more than once
for an app user
creating xmlc on app user email
all right
so if i do this then what this does
is this means that i cannot add i will
get the same problem i had with the
primary keys
or the foreign keys when we tried it at
like foreign key we tried to add one
that doesn't exist
in this email as you can see it's got
uni which is unique so that means if i
try to add an app user
from app user that has an email address
that already exists so let's say user
test one
so now i do insert into
app user
what do i have to have i have to have a
username
an email
a user login
a password
and an update date oh which i don't
because that's automatically done so i
think i can go with just those
values so username can be anything so
he's going to be blah
this case i want my email to be this
because this is going to blow me out of
there and then user login can be blah
and password can be blah
if i use that
then what i'm going to do is i'm going
to see
that i get a duplicate entry for that
see so it it's telling me right now that
cannot be used i need a new one
so there's a lot of uses for indexes i
wanted to touch on those before we move
on to
some of the other things we will touch
on after this good time while we're
cleaning up our data a little bit our
database
that we could take a look at this
that being said i think we'll wrap this
one up so
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.92

continuing our series when we're going

32

through

32.88

my sequel maria db development type

35.36

things

36.399

and we're really just in this episode up

38.96

to day eight uh episode eight we are

41.52

continuing or actually start going back

43.6

and working through some constraints

45.92

and talk a little bit more about those

48.879

now in the last episode

51.28

one of the things we did

52.96

is we moved our tables we basically did

55.44

we used a create table as select star

58.079

from

59.52

and ended up taking our

61.92

tutorial 1 and tutorial parent table

64.799

and basically created address and app

66.96

user the problem with those so if we

69.119

look at like

70.56

address

73.6

is we didn't get the constraints or

75.119

anything else it just pulled

77.36

the field names the column names

79.759

the type whether they were null or not

82.159

and

83.439

basically that was it

85.119

i may have pulled a default oh i think

87.04

it did pull it a fault over but not in

89.04

all cases um because let's see or maybe

91.84

it did

93.04

we will

97.04

oh it did so pulled over the and it

98.72

pulled over the default values

101.439

the key no pun intended issue with this

104.64

is that it did not pull across primary

106.399

key it did not pull across auto

108.079

increment and it did not pull across

110.32

foreign key relationships so the first

112.32

thing i want to do is fix those

116.24

the way we do that is we can come in

118.799

after the fact

122.079

and

123.04

if i can get some stuff out of the way

124.24

so i can work my mouse

126.84

then and come in here and we're going to

130.56

alter table address we're going to do a

132.08

constraint that we're going to call the

133.44

address primary key

135.28

it's going to be on the t1 id

140

and it works fine we're gonna do the

141.68

same thing and we're gonna do app user

149.44

on that one and so now if we go back to

150.959

our describes so describe address we now

153.36

see the primary key as t1 id but it's

155.76

not doing the alternate the

159.36

auto increment and app user

162.4

okay we've got our primary key now

165.76

we're going to work on

168.239

adding that adjusting that auto

169.599

increment

170.48

a key thing about this is that order

173.04

matters when you do this

174.959

once you create when you create the

176.16

table from

178.08

just creating it from a query like we

180.08

did before where we do like uh

182.319

do something like this where we just

183.599

build out our whole table

186.56

notice that we can put on our increment

188.159

we can do primary key so if i want to

190.08

create two table tutorial one from that

193.36

then for some reason didn't like what it

195.36

printed but basically

197.84

so now if i do

199.76

promote one

201.12

i'm going to see

202.72

boom i have my primary key it's auto

205.04

increment is that extra notice if i do

207.599

describe

209.2

what's that address

212.879

so i managed to somehow lose name oh

215.519

because i probably changed over the name

217.599

i added that later and

220.4

i don't have my auto increment

222.879

if you do it then it's fine if you wait

224.879

till after the fact an auto increment

227.599

cannot be done and actually i guess you

228.959

could run into this during the script an

230.879

auto increment cannot be added to a

232.959

non-key

234.799

has to be a primary key first

236.72

so

238.879

we have created those so we're going to

240.319

be safe when we do that we go in here

245.28

and we're going to take the

249.439

table address here and we're going to

252.319

modify that column t1 ib to auto

254.4

increment

255.68

and if we describe address now we'll see

258.239

that's on our increment we can do the

260.239

same thing

263.12

with the

264.16

parent

266.4

id

270.639

and no we could have gone in because we

272.56

sort of you know we're changing stuff on

273.84

the fly we could do this and just drop a

275.6

table and rebuild it

277.04

or we could do alter and do alter the

279.04

column names as well however we're still

281.52

going to have to worry about things like

283.44

making sure that we get our keys and our

285.04

primary keys and our foreign keys

287.12

together

290.08

now if we wanted to

292.56

one thing we run into is once we start

295.12

adding data

296.8

there can be issues where we can't

298.72

delete a record because we still have a

300.56

constraint we've seen that

302.4

or we want to insert a record and

305.28

there's a constraint issue

307.68

and so one of the things we can do is we

309.12

can drop a constraint so we

312.4

let's see

313.6

so let's first create

316.08

a foreign key because i don't think we

317.6

have any right now yeah we don't have

319.28

any so let's create that foreign key

321.039

from

323.28

uh type id address

325.6

so again that's what we're going to do

327.12

is we're going to do alter table

331.52

that's address

335.759

i'm sorry it's add constraint

339.68

instead of primary like we did here

341.68

primary key it's gonna be foreign key

344.639

so let's do four and it doesn't have to

346

be all

346.88

caps but that sometimes helps and so the

348.96

foreign key is gonna be type id

351.6

and we have to tell it what it

352.479

references and so it's going to

353.919

references

356.319

look up

357.52

what's that called

362.319

lookup type

365.44

type id

368.56

whoops

375.039

okay

376.479

and now if i do describe address

382.479

and i didn't create i didn't

385.52

give the constraint a name which we

388.16

normally have done

390.479

so here when i do add constraint i give

392.4

it a constraint name i didn't this time

395.6

so let's see what happens what is that

397.6

so now if we do select star f

401.919

from

403.12

lookup type

404.72

let's get one that's invalid so it's one

406.16

two three so if we insert into

410

address

413.599

address name

416.24

cannot be no comma type id that'll be

419.28

the shortest one we can do

420.84

here values

423.759

whoops

428

there you go

431.44

got off of that somewhere so values um

434.4

address name will be just oop

437.12

i should hit the right key delete me

440.479

and its type is going to be four so this

442.88

should give us a constraint issue which

444.88

is going to boom

447.039

it does and now notice it just generated

450.24

a constraint name since we didn't give

451.68

it one it generates that but now we can

454.16

take that

456.96

and we can do

458.24

drop constraint

461.759

that guy

464.879

let's come over here and do

468.479

just to make sure i get the

472

i could do it exist but it should be

476.96

so if i go here oh it's alter table

480.319

i can't do just a straight drop

484.4

so i need to do

491.599

alter table

493.44

address

495.199

whoops

496.56

drop constraint there we go so now if i

498.56

come back and try to do the insert

500.96

it allows it

503.599

now looks what happens if i try to

504.96

create that constraint again

507.199

where was that constraint there we go

511.199

now i can't because

513.599

it's going to blow up when it tries to

515.44

do it because

517.039

i already have an issue so what i need

518.88

to do is

520.399

uh select star from

524.159

so you don't really want to drop

525.2

constraints unless you're like cleaning

526.72

stuff out or

528.24

if you know that you're 100 sure that

530.16

they're gonna you know they're getting

531.36

in the way but they're 100 sure when you

533.279

get your data in it's going to be clean

535.6

uh so i'll just like start from address

538.08

i'm going to delete

539.519

from address

541.36

where

543.32

t1 id equals

546.839

5.

548.64

and now i can go back i can create the

551.36

there we go and i can create the foreign

552.8

key i'm also going to go in

556.399

let's keep that here

563.44

and i also want to do

566.16

let me create one real quick i'm going

567.68

to alter table

570.16

this is going to be app user

575.04

and constraint and this will be

578.32

user

579.839

address

581.76

foreign key

583.839

and it's going to be on the t1 id

588.08

and it's going to reference the address

593.92

t1 id

597.6

so if i take that

602.48

whoops

605.36

uh ch

606.88

alter table app user a constraint user

612.399

okay i did something wrong

614.399

um

616.8

ultra table and constraint user address

618.88

so it's foreign key

624.399

references

626.64

oh

627.6

because it has to i have to spell the

629.6

table right

631.6

there okay so now if we look at address

636.24

we see that we have our order increments

638.48

and our primary key if we look at app

641.839

user

644.399

we're gonna see that

647.279

we have our

650

foreign key here and our primary key

653.44

so we're in pretty good shape

655.279

so with this

656.72

we've got our table sort of straightened

658.8

out but while we're here

661.04

let's talk about indexes

663.279

when you are doing a

665.44

search in a database when you're doing a

666.959

select with a where clause or an inner

669.6

join

670.8

the value or values that it joins on the

674.32

case where you say this value equals

675.839

that value

677.279

when you do that

679.6

for example if i do select star from

683.12

app user

686.839

where t1 id

690.64

equals two i think that's one that's um

694.8

there we go

695.839

so in this case where i do this t1 id

699.04

equals one

701.2

um i guess i could i'd probably want to

703.519

do it sorry but in searching this it's

705.92

actually going to do what's called a

707.279

table scan it's going to look through

708.48

the entire table to find that

711.44

then one of the things we can do is add

713.519

indexes now for a primary key it's

715.92

already going to be indexed

717.68

for a foreign key it's indexed because

720.56

it's

721.44

indexed on the the primary key your

723.36

foreign key always relates to a primary

725.76

key on another table however let's say

728.48

we wanted to do um we're going to search

731.68

by user login which

733.76

probably is not uncommon so what we

735.68

would want to do is we can do a

738.8

index and so we could try create index

743.279

uh

744.639

let's see email

746.959

look up

750.079

on

753.12

uh who's that app user

757.12

email

759.04

and i think or

760.639

sorry user login

766.8

and so now

768.32

we're not going to see here a difference

771.519

i don't think

773.68

oh yes it is it's going to say so you're

775.2

going to see that you've got a key there

777.279

which is an index which means now

780.32

there is a

783.12

sort of like a table of contents that is

785.04

going to more quickly direct you to

786.8

records

788.16

now when you're talking about a string

790.32

it may or may not be useful although you

792.8

can use hashes and things like that that

794.399

may get you there closely

797.279

but particularly in a

798.839

number and a strings assuming that

801.519

there's a

803.839

a fairly even distribution and that they

806.399

are

807.2

you know easily found by uh order you

810

know like if everything's starts with a

812.279

aaaaa then

814.399

it's not going to get you there or you

815.839

know or the same

818.079

multiple letters together

819.76

numbers are usually going to be the

820.88

fastest because it can very quickly jump

823.04

to

824.24

a number via an index it knows real fast

826.8

that i'm gonna you know if i'm looking

828.079

for something between

830

a hundred thousand and a hundred

831.279

thousand and ten it can very quickly go

833.92

oh i'm gonna jump to this index to the

835.519

hundred thousand point into the hundred

837.04

and ten thousand

840.639

and that is

845.36

going to get you huge

847.519

performance changes as you get into this

850.959

when you're using indexes properly then

853.92

your queries are going to be really fast

855.839

if not you're going to do these table

857.279

scans which means every time it looks

859.36

for something it being the database it

861.44

has to go through every single record in

863.44

the database

865.12

and that can be very time consuming very

867.76

quickly

868.959

and so it's very useful to note that you

870.639

can make you can create indexes real

872.399

quick

873.36

now you don't want too many because when

874.88

you insert

876.639

then all the indexes have to be updated

879.44

so you can you know that can slow down

881.12

your inserts but particularly if you've

882.639

got a database that is

884.88

mostly static data or data that exists

888.8

and

890.32

sometimes you're making changes to it

891.839

but mostly you're searching against it

893.519

or for example a reporting database then

896.32

you're indexing you know is going to be

898.72

awesome you want to make sure that

900.079

that's

901.12

put everywhere that you are

903.199

searching for that kind of value or you

905.519

can do multi

907.44

multi-column indexes we'll see later

910.399

when you do that if you hit that index

913.199

then it's going to be a lot faster and

915.279

there you can go search

917.76

like database performance and indexes

920.079

show plan things like s-h-o-w space

923.639

p-l-a-n-w i'm sorry p-l-a-n

927.04

just that you know that

929.44

string and you will see graphical

931.6

examples of such things

933.759

so you do want to have a you want to

935.839

have indexes like in your mind

938.16

you can also do um let's say i want

943.199

that was email lookup i want to be email

945.44

unique

950.639

so let's say that i don't want to allow

953.6

an index

954.959

email address to show up more than once

957.36

for an app user

958.88

creating xmlc on app user email

963.279

all right

965.199

so if i do this then what this does

968.639

is this means that i cannot add i will

971.04

get the same problem i had with the

972.639

primary keys

974.24

or the foreign keys when we tried it at

975.839

like foreign key we tried to add one

977.12

that doesn't exist

978.639

in this email as you can see it's got

980.48

uni which is unique so that means if i

983.36

try to add an app user

986.079

from app user that has an email address

989.68

that already exists so let's say user

992.079

test one

994.639

so now i do insert into

997.44

app user

999.6

what do i have to have i have to have a

1004.399

username

1007.04

an email

1010.639

a user login

1015.519

a password

1019.199

and an update date oh which i don't

1021.44

because that's automatically done so i

1022.639

think i can go with just those

1024.64

values so username can be anything so

1028.24

he's going to be blah

1031.199

this case i want my email to be this

1032.959

because this is going to blow me out of

1034.24

there and then user login can be blah

1037.6

and password can be blah

1040

if i use that

1042.4

then what i'm going to do is i'm going

1043.919

to see

1046.88

that i get a duplicate entry for that

1048.64

see so it it's telling me right now that

1050.88

cannot be used i need a new one

1053.44

so there's a lot of uses for indexes i

1055.12

wanted to touch on those before we move

1056.799

on to

1057.84

some of the other things we will touch

1059.2

on after this good time while we're

1061.12

cleaning up our data a little bit our

1062.96

database

1064

that we could take a look at this

1066.64

that being said i think we'll wrap this

1068.16

one up so

1069.76

as always go out there and have yourself

1071.36

a great day

1072.48

a great week and we will talk to you

1075.6

next time

1092.72

you