📺 Develpreneur YouTube Episode

Video + transcript

SQL Tutorial - Day 3

2022-04-12 •Youtube

Detailed Notes

1. Create table create table tutorial_one( t_one_id INT NOT NULL AUTO_INCREMENT, name VARCHAR(100) NOT NULL, city VARCHAR(50) NOT NULL, state varchar(2), zip integer, PRIMARY KEY ( t_one_id ) );

2. primary keys 3. data sizes 4. null vs not null 5. auto_increment 6. numbers 7 simple select

Transcript Text
[Music]
well hello and welcome back we are
continuing our series we're looking at
mysql mariadb and getting really just
using those as sort of a
sql sql tutorial
today we're getting started we've
created our database and so now we're
going to create tables
now the concept of a table is if it was
a
spreadsheet it would be one of the
worksheets within that spreadsheet you
have columns you have rows
and
one of the things we will talk about is
that you have a way to talk about every
specific row
that may not have anything to do with
the values that are on it
so let's start with looking at creating
a table
so with a create table you start with
create
and then table and then give it a name
so we're going to call this table
tutorial underscore one
and then you've got some parentheses you
can see open and close parentheses and
then it's going to be
a series of fields
and then there's going to be some
additional things that may show up that
have to do with
rules around those fields
and we'll talk about each one of these
so
first one we've got
t underscore one underscore id
this is
uh this next whole things which is the
name so that's the name of our field
would be like our column name
this is the data type the next thing in
this case it's an int which is an
integer you can also see we can get away
with integer as our type
and we have some other types here
which we'll talk about ah we'll go ahead
and talk about now so we have
a varchar now notice an int is just so
it's just a number it knows roughly how
big this number is supposed to be by
default
a varchar is a
a string or a text
so a var char would be
a length of in this case up to a hundred
characters
for uh for this value for this value
that we're going to call the name is
name
and we'll call this
actually let's call this address name
we'll change it to that
and so
it's
because it's a var char
that hundred spaces is up to the or
characters in length is the maximum that
we'll take up in space
since it's of that with that var part of
it it's different from a just hr that we
see below
a char automatically allocates
a spot for every possible
item that could be in there so if it was
a char 100 it would allocate 100
characters in memory
the var variable one here var
is only going to allocate what's needed
for
that string so if it's a
a 10 character string we're saving 90
characters in memory
which as database grows can be very
crucial
now the chars as i said same thing it's
also a text or string kind of field it
also has a length but it automatically
allocates out to that length so you want
it to be something that is
pretty close to
static or
from record to record about the same in
length
ideally
like state would be perfect or country
code if you use a three-letter country
code then it would be a char 3
those kinds of things where
all the fields are going to use exactly
that amount so that helps quite a bit
we have integer which is our zip code
now our zip code could be an integer or
it could be a string depends on how we
want to do it we're going to leave it as
an integer just because because there's
some things we can work with
now the next thing after the type is
either going to be null or not null
and these are not all required we'll
show you a little further down here so
not in all means it is a required field
i have to have a value for this on every
record doesn't mean i have to send it
one when i create the record because
there may be a default value that it
will assign that we even if i don't
assign a value which is what we're going
to find in this case for our
primary key
and auto increment
is for an integer and it's basically
every time you put a record in there
it's going to start at one or you can
you can set it to a surf a certain
starting number and then it's just going
to add one to that each time so if you
do
so we do record one we'll see it's going
to have an id one i the t1id of one
the next record is going to have t1 id
of 2 and so forth
now notice so we've got null or not null
means it's required we have null which
means
it is not required so i don't have to
enter a state at all it can be empty and
it's not going to care
and this one has nothing
by default
it is null which means you you don't
have to set a value and then everything
else is defaulted out essentially so i
can do a sort of shorthand for creating
this table
and then here
i'm going to say that this
with this line
t1 id is the primary key which means i
only have
it is a unique value across the database
or across the table
so row one if t1id because that's my
primary key if it's one
i can't put another row in there that
has a t1 id equal of one
and that allows us to specifically
address certain
rows in the table
so i'm going to take that and i'm going
to pop over here
and somewhere in here there we go
going to use my tutorial database which
is one we're going to use through most
of this stuff
and now let's see do i have tables in
here i don't have any tables in here
because i haven't created anything yet
but now if i create the table
so there we go create okay zero rows
affected now
uh
jump up there now if i do show tables i
can see that i have table tutorial one
and if i use d-e-s-c as in like describe
let's pull this up a little bigger
oh there and the table name
tutorial
two tor
eo1
then it shows me what i just created and
you can see here where it's like integer
it has it actually set a size to integer
so that's going to be out to 11
characters 11 bytes
um you can see your char with that 100
that was up here
varchar 50 your chart two so you can see
all of those now
i can i think we looked at this before
but in order to see what's in a table i
do select and star means just give me
all of the columns so i'm going to
select star from because it's basically
select
columns from table name
tutorial 1.
in this case i don't care about anything
else i just give me all the tape all the
rows and there are no rows however
i can do this
see
i'm not going to use t1id
i'm going to use address name
i'm going to use city
state
zip and just name c states that good
okay and i'm gonna do my values
and let's just do
name one and i'm using a single quote
around each of these string type things
so name one
city one
oops the name of quotes
and i'll show you actually let me do it
this way so it'll be state one
and uh zip code one two three four five
now i'm gonna do that notice i didn't
put my quotes around s1 so it's going to
complain it says unknown column it
thinks that's a column name instead of a
string
so i can put quotes around it but note
that with the number since it's a number
i don't it since it's which has to do
with some of the naming conventions that
since it knows it's a number we don't
have to worry about it getting confused
we don't need quotes now
if we do our select
we can see our record in there if we do
it again we're going to certain another
record so let's just one two three four
five six
state two
city two
name two
and then we do select star we can see
here
that we have these
these two rows are in there and we can
see that the id that t1 id was one for
the first one two for the second one now
something about that if we do delete
from so we're going to delete a record
which is delete from tutorial
one and we tell it where
and we can give it some information so
where
t1 id equals two
if we do that
boom we're down to one but now if we go
back and just reinsert that record that
nate record two
and just select
you can see that the t1id is now three
because it deleted that but it didn't
reset the
the count the auto increment for that
so we're good it says okay i'm just
going to keep on going and so now my new
id is three now i can also
if i come in let's do three
three
and three but now what i'm gonna do is
i'm gonna give it
because that's using the default if i
instead give it
a t
one id
in this case then i'm going to see
now it entered it and it's set as 1. if
i tried to insert it as 1
100 again
now it's saying there's a duplicate but
if i come in and i create a record for
without entering one so let's go look
here
so now i'm gonna go back and i'm gonna
say okay use the default what's the
default after i just gave it a 100 it is
101. so it said okay you get you set it
to a value so i'm going to reset
the value the count value
now
with each of these let's see what else
did i want to
cover
uh i don't record number simple select
okay good we're in good shape
so
the one other thing we can look at is if
we want to get rid of a table we can do
it's just drop table and then table name
so tutorial one
now if i do that and then i show tables
i have no tables so now i got to go back
and create my table again but let me do
this
i have
this time i'm going to use
if i look at table1.sql
i have a creation here
and oops i'm going to change it to
uh address name
and note that if you want to use
comments
these little you can do a hyphen hyphen
it gives you a comment
so if i save this
there we go
and i'll go back to the database
and
uh what's my password
i'm going to use tutorial
and then i use that source if you
remember that from last time i do source
sable1.101.sql
then it doesn't tell me what it ran but
i can go back and look and i'm going to
see that that table exists
but
it doesn't have any data now because we
dropped it when we drop a table we drop
all of the data in it as well
now if you want to
sort of reset a table like that you can
use a script like i had
and nice little bonuses is you can come
in and you can say i'm going to drop the
table if it exists because otherwise
i just do this if i try to create it and
it does exist
me do that let's see if
then
if i try to create it and it does exist
it's going to say it already exists so
what i'm going to want to do is i'm
going to come out of here
and i'm going to want to say okay i'm
going to go ahead and drop it but first
i want to make sure i'm in the right
database
so if i use this as my script now i can
come in and regardless of where i'm at
i can do sourcetable1.sql
and it's going to give me there oh and
it's probably going to be there i'm
already in the table or the correct
database and i will see
boom my table is there so there you go
that is it for today we're going to come
back we're going to continue to work
through data types and more tables and
more records and
lots of different ways we can look at
the database we are just getting started
so
hope you go out there and have yourself
a great day a great week and we will
talk to you
next time
three two one
you
Transcript Segments
0.48

[Music]

26.48

well hello and welcome back we are

28.24

continuing our series we're looking at

29.92

mysql mariadb and getting really just

33.52

using those as sort of a

35.2

sql sql tutorial

37.76

today we're getting started we've

39.84

created our database and so now we're

42.48

going to create tables

45.2

now the concept of a table is if it was

48.239

a

49.2

spreadsheet it would be one of the

51.199

worksheets within that spreadsheet you

53.12

have columns you have rows

55.84

and

56.8

one of the things we will talk about is

58.32

that you have a way to talk about every

60.8

specific row

62.559

that may not have anything to do with

64.239

the values that are on it

66.72

so let's start with looking at creating

69.2

a table

70.64

so with a create table you start with

73.76

create

74.96

and then table and then give it a name

77.04

so we're going to call this table

78.64

tutorial underscore one

80.799

and then you've got some parentheses you

82.479

can see open and close parentheses and

84

then it's going to be

85.28

a series of fields

87.28

and then there's going to be some

88.56

additional things that may show up that

90.24

have to do with

91.84

rules around those fields

93.92

and we'll talk about each one of these

95.439

so

96.32

first one we've got

98.159

t underscore one underscore id

101.6

this is

103.04

uh this next whole things which is the

104.64

name so that's the name of our field

107.04

would be like our column name

108.799

this is the data type the next thing in

110.72

this case it's an int which is an

112.399

integer you can also see we can get away

115.2

with integer as our type

117.439

and we have some other types here

119.52

which we'll talk about ah we'll go ahead

121.2

and talk about now so we have

123.68

a varchar now notice an int is just so

126.56

it's just a number it knows roughly how

128.72

big this number is supposed to be by

130.319

default

131.68

a varchar is a

133.68

a string or a text

136.239

so a var char would be

138.959

a length of in this case up to a hundred

141.28

characters

142.64

for uh for this value for this value

144.8

that we're going to call the name is

146.72

name

147.92

and we'll call this

149.599

actually let's call this address name

151.519

we'll change it to that

154.879

and so

156.72

it's

157.519

because it's a var char

160.16

that hundred spaces is up to the or

163.44

characters in length is the maximum that

165.599

we'll take up in space

167.92

since it's of that with that var part of

170.64

it it's different from a just hr that we

173.12

see below

174.72

a char automatically allocates

177.28

a spot for every possible

180.08

item that could be in there so if it was

181.76

a char 100 it would allocate 100

184.64

characters in memory

186.319

the var variable one here var

189.28

is only going to allocate what's needed

191.599

for

192.319

that string so if it's a

194.159

a 10 character string we're saving 90

196.64

characters in memory

198.48

which as database grows can be very

201.519

crucial

203.28

now the chars as i said same thing it's

206.08

also a text or string kind of field it

208.72

also has a length but it automatically

210.879

allocates out to that length so you want

212.879

it to be something that is

215.28

pretty close to

217.519

static or

218.64

from record to record about the same in

220.799

length

221.68

ideally

223.36

like state would be perfect or country

225.36

code if you use a three-letter country

226.799

code then it would be a char 3

229.04

those kinds of things where

231.12

all the fields are going to use exactly

233.68

that amount so that helps quite a bit

236.239

we have integer which is our zip code

238.4

now our zip code could be an integer or

240.08

it could be a string depends on how we

241.519

want to do it we're going to leave it as

242.72

an integer just because because there's

244.319

some things we can work with

246.56

now the next thing after the type is

248.64

either going to be null or not null

251.599

and these are not all required we'll

253.519

show you a little further down here so

254.879

not in all means it is a required field

257.28

i have to have a value for this on every

259.68

record doesn't mean i have to send it

261.6

one when i create the record because

264.16

there may be a default value that it

265.759

will assign that we even if i don't

268.639

assign a value which is what we're going

270.479

to find in this case for our

272.4

primary key

274.08

and auto increment

276.16

is for an integer and it's basically

279.04

every time you put a record in there

280.56

it's going to start at one or you can

283.28

you can set it to a surf a certain

286

starting number and then it's just going

288

to add one to that each time so if you

289.68

do

290.4

so we do record one we'll see it's going

292.08

to have an id one i the t1id of one

295.36

the next record is going to have t1 id

297.199

of 2 and so forth

301.12

now notice so we've got null or not null

304.4

means it's required we have null which

306.4

means

307.28

it is not required so i don't have to

308.96

enter a state at all it can be empty and

310.479

it's not going to care

312.479

and this one has nothing

314.479

by default

316.4

it is null which means you you don't

318.56

have to set a value and then everything

320.32

else is defaulted out essentially so i

321.919

can do a sort of shorthand for creating

323.919

this table

325.199

and then here

326.56

i'm going to say that this

330.32

with this line

331.6

t1 id is the primary key which means i

334.4

only have

336.8

it is a unique value across the database

338.88

or across the table

340.56

so row one if t1id because that's my

343.919

primary key if it's one

345.759

i can't put another row in there that

347.36

has a t1 id equal of one

349.919

and that allows us to specifically

352.479

address certain

354.16

rows in the table

355.759

so i'm going to take that and i'm going

357.919

to pop over here

360.639

and somewhere in here there we go

366

going to use my tutorial database which

368.08

is one we're going to use through most

369.12

of this stuff

370.4

and now let's see do i have tables in

372.24

here i don't have any tables in here

374.16

because i haven't created anything yet

376.8

but now if i create the table

379.52

so there we go create okay zero rows

381.6

affected now

383.759

uh

385.039

jump up there now if i do show tables i

387.44

can see that i have table tutorial one

389.919

and if i use d-e-s-c as in like describe

393.199

let's pull this up a little bigger

398.08

oh there and the table name

401.4

tutorial

403.36

two tor

405.039

eo1

408.4

then it shows me what i just created and

411.28

you can see here where it's like integer

413.039

it has it actually set a size to integer

415.919

so that's going to be out to 11

417.199

characters 11 bytes

419.52

um you can see your char with that 100

422.8

that was up here

424.319

varchar 50 your chart two so you can see

426.56

all of those now

429.12

i can i think we looked at this before

431.12

but in order to see what's in a table i

433.039

do select and star means just give me

436.72

all of the columns so i'm going to

437.84

select star from because it's basically

439.68

select

441.36

columns from table name

444.24

tutorial 1.

447.039

in this case i don't care about anything

448.319

else i just give me all the tape all the

449.84

rows and there are no rows however

455.68

i can do this

462.639

see

465.759

i'm not going to use t1id

468.879

i'm going to use address name

472.16

i'm going to use city

474.16

state

476.639

zip and just name c states that good

478.56

okay and i'm gonna do my values

481.28

and let's just do

483.919

name one and i'm using a single quote

486.16

around each of these string type things

488.8

so name one

490.8

city one

493.039

oops the name of quotes

496.16

and i'll show you actually let me do it

497.36

this way so it'll be state one

499.68

and uh zip code one two three four five

502.24

now i'm gonna do that notice i didn't

503.599

put my quotes around s1 so it's going to

505.44

complain it says unknown column it

507.68

thinks that's a column name instead of a

509.52

string

510.639

so i can put quotes around it but note

512.959

that with the number since it's a number

516.24

i don't it since it's which has to do

518.32

with some of the naming conventions that

520.32

since it knows it's a number we don't

522.08

have to worry about it getting confused

523.599

we don't need quotes now

526.56

if we do our select

529.04

we can see our record in there if we do

530.8

it again we're going to certain another

532.32

record so let's just one two three four

533.68

five six

535.76

state two

537.76

city two

539.519

name two

541.04

and then we do select star we can see

543.12

here

545.68

that we have these

547.6

these two rows are in there and we can

550.48

see that the id that t1 id was one for

552.88

the first one two for the second one now

555.519

something about that if we do delete

558.16

from so we're going to delete a record

560.399

which is delete from tutorial

562.64

one and we tell it where

564.24

and we can give it some information so

565.6

where

566.64

t1 id equals two

570.8

if we do that

572.959

boom we're down to one but now if we go

575.12

back and just reinsert that record that

577.44

nate record two

579.36

and just select

580.8

you can see that the t1id is now three

582.959

because it deleted that but it didn't

584.72

reset the

586.56

the count the auto increment for that

589.76

so we're good it says okay i'm just

591.519

going to keep on going and so now my new

593.519

id is three now i can also

600.72

if i come in let's do three

603.839

three

605.44

and three but now what i'm gonna do is

607.12

i'm gonna give it

609.6

because that's using the default if i

611.279

instead give it

613.04

a t

614.64

one id

617.36

in this case then i'm going to see

621.839

now it entered it and it's set as 1. if

623.519

i tried to insert it as 1

626.24

100 again

628.32

now it's saying there's a duplicate but

630.399

if i come in and i create a record for

633.44

without entering one so let's go look

635.839

here

640.88

so now i'm gonna go back and i'm gonna

642.32

say okay use the default what's the

643.68

default after i just gave it a 100 it is

647.44

101. so it said okay you get you set it

650.079

to a value so i'm going to reset

652.56

the value the count value

656.88

now

657.92

with each of these let's see what else

660.24

did i want to

662.16

cover

664

uh i don't record number simple select

666

okay good we're in good shape

667.839

so

669.2

the one other thing we can look at is if

671.6

we want to get rid of a table we can do

673.6

it's just drop table and then table name

676.959

so tutorial one

679.6

now if i do that and then i show tables

682.959

i have no tables so now i got to go back

685.279

and create my table again but let me do

689.2

this

690.56

i have

691.839

this time i'm going to use

694.959

if i look at table1.sql

697.76

i have a creation here

701.279

and oops i'm going to change it to

706.24

uh address name

711.839

and note that if you want to use

713.36

comments

714.399

these little you can do a hyphen hyphen

716.88

it gives you a comment

718.88

so if i save this

722

there we go

723.04

and i'll go back to the database

727.279

and

728.399

uh what's my password

731.6

i'm going to use tutorial

735.839

and then i use that source if you

737.279

remember that from last time i do source

738.56

sable1.101.sql

740.959

then it doesn't tell me what it ran but

743.12

i can go back and look and i'm going to

745.44

see that that table exists

748.399

but

752.24

it doesn't have any data now because we

755.12

dropped it when we drop a table we drop

757.2

all of the data in it as well

760

now if you want to

762.079

sort of reset a table like that you can

764.24

use a script like i had

766.56

and nice little bonuses is you can come

769.04

in and you can say i'm going to drop the

770.32

table if it exists because otherwise

776.399

i just do this if i try to create it and

778.959

it does exist

785.12

me do that let's see if

790.16

then

794.959

if i try to create it and it does exist

796.88

it's going to say it already exists so

798.959

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

800.32

going to come out of here

804.24

and i'm going to want to say okay i'm

806.72

going to go ahead and drop it but first

808.399

i want to make sure i'm in the right

809.519

database

810.88

so if i use this as my script now i can

814.399

come in and regardless of where i'm at

817.92

i can do sourcetable1.sql

821.04

and it's going to give me there oh and

822.639

it's probably going to be there i'm

823.68

already in the table or the correct

825.44

database and i will see

827.12

boom my table is there so there you go

831.36

that is it for today we're going to come

833.76

back we're going to continue to work

835.279

through data types and more tables and

837.44

more records and

839.36

lots of different ways we can look at

840.399

the database we are just getting started

842.48

so

843.199

hope you go out there and have yourself

844.8

a great day a great week and we will

847.199

talk to you

848.48

next time

850.56

three two one

867.519

you