📺 Develpreneur YouTube Episode

Video + transcript

SQL Tutorial - Day 1

2022-04-05 •Youtube

Detailed Notes

We start our series on SQL scripting and development.

Topics 1. SQL Overview 2. Create Database 3. Create Tables 4. Insert/Select 5. Update 6. Delete 7. Stored Procedures 8. Views 9. Users and permissions 10. MySQL/MariaDB 11. SQL differences

Transcript Text
[Music]
well hello and welcome back and welcome
to
if you haven't aren't coming back but
welcome to
a series on
sql
we're going to get into database
development
really focused on the database side and
we're going to
yes start with some very simple sort of
general basic kinds of rules
and we're going to work our way up to
some more complicated things
i haven't seen a whole lot of good
tutorials other than
the ones that are really more like a
straight not as much a tutorial as they
like a reference
it says you know hey these are all the
different ways you can use this
command
and so we decided this would be a great
way to
do a new series and it's also a
foundational piece of so many other
applications that we've dealt with or
will deal with
particularly when you go to for example
the django stuff that we did
there's a lot of database
knowledge within there
and i think it makes sense to just sort
of you know take a step back and let's
talk about some database stuff
so in this series
we are going to go through
from
installation to
whatever you know backups and
maintenance and stuff like that uh this
first episode is really focused on just
it's really an overview of the class
what are we gonna order this series i
guess
and
what kinds of things are we going to
talk about
now in general i want to talk about
what's a database a database is really
just a way to store
data i know it seems very simplistic but
you have it and the terminology by the
way will change a little bit depending
on which vendor you're dealing with they
have different ways that they refer to
the various things we're going to be
dealing with as far as users and
databases and tables and things like
that
the naming can change a little but
the key here will be to get to the
concepts
and to try to keep them as generic as
possible
even though our focus is going to be
on the my sequel maria database
and that's a slash because it used to be
my sequel now oracle owns it and has
been renaming it more or less to maria
but you'll find
for example just
running the command line stuff is still
going to have all those my sequel
references so
don't let it confuse you if you haven't
messed around with database for a while
and you see this mariadb stuff coming up
instead of mysql
and we're going to we're going to go
through
all kinds of fun things we're going to
start with we're going to create
databases we're going to create users
we're going to set some permissions
we're going to talk about creating
tables
and inserting data updating data
deleting data
now from those concepts a table is
if you can think of a spreadsheet like
excel or something like that you've got
rows and columns
that's a table in a database you have
rows of data and then you have these
columns
and within those columns they have
things constraints which are like a type
something like that so your column may
have
all the data in there maybe a number it
may be a character of some sort it may
be a time time stamp
we will talk about those as we get
further into it
you can insert data which basically you
think could be this
you've got columns and rows you can add
a new row put some data into it
and that is your insert
update is you go select a row that has
data in it change some of that data and
that is the update
of course delete if you go to that row
and you want to remove it completely not
clear it out but actually delete that
row
so that now there's one less row
that's a delete
and we're going to get into stored
procedures
now databases typically are
queries or commands and this sql is
actually structured query language
and it's
primary purpose is to ask something of
the database or to tell it to do
something that's
more or less like a
anatomic task you know like
insert this record update these values
delete this record
stored procedures
allow us to group multiples
of those commands together so instead of
saying
give me the data from this table
starts where this field starts with a
and then that's it now it's give me that
data and then i want you to look at each
of those records and update them if this
other column is equal to you know it's
greater than zero or something like that
and so now instead of just a command at
a time stored procedures allow us to do
multiple commands sort of you know stack
them up cue them up
and then generate those
and it
works like
another language is a function or
something or procedure subroutine
depending on what it's called in another
language
where you can send
values in
to this list of commands
and then generate all you know do all
the commands and then kick out some sort
of result
now it's a database
so it may be that the results come at
the end or you know like
return back as part of the stored
procedure or it may be that those
results are really just side effects the
updates and changes made to data and
maybe even the database itself
when that runs
and we're also going to talk about
views a a table typically we talk about
these
commands so we can query a table and say
give me all the like i said all the
records that start with a
okay great
what a view does
is it takes a query it takes results of
a query
and gives you
that as
a table in itself
so if you think of a spreadsheet if
you've got a spreadsheet and you've got
a hundred records on it
and then you take that
and you turn around and take 50 of those
records
and i'm going to do a quick example i'm
here
if this will come up while i'm talking
then
you can bait so let's say this is a
database and you've got you know you've
got your columns a b
c d e
and you've got your values so we're just
going to keep it very simple here
two two two two
and we've got that so we've got this
little
table
and so right now what i'm doing is like
here would be insert a row
whoops let me do that let's update this
row so i'm going to go through i update
these values that would be an update
i can delete it
that would be a delete now this is not a
database but i'm seeing i'm showing you
roughly what this stuff would look like
now in a view
what i can do is let me do this
is i could say let me i'm going to often
ask give me all the values greater than
2.
and so this would be the result set
and so
if i add another sheet
that would be the result set now what i
can do
is go back to sheet 1
table 1. and that's my
primary results that's my source but now
i could have this view which i'm going
to call
view one
and its data is based on what's in table
1.
i will get deeper into views later but
that's roughly your view
so you have stored procedures so you can
stack up commands you've got views so
you can essentially store
solution or result sets
you have users and permissions this is
your typical security kind of stuff is
that you have people that have
access to the overall database
people are users that can have access to
either sometimes it's called a schema or
a that's sometimes referred to as a
database within
the overall database engine
and then those permissions may be limits
to
oops that's not what i want maybe the
limits to things like can they insert
can they update can they delete then
they create another database can they
create a table
and so you will have users and
permissions which we will explore and
we'll set one up because
much like security in general
you have a
what's in most databases known as a is
the owner or the root user that has
access to everything
and then you're going to have users that
have
limited access depending on what your
needs are
and ideally you don't use that root or
super user or owner account
basically ever
all of your stuff should be all your
work instead should be
a user created and then assign
permissions for
that type of work
and you'll see this a lot in a database
where
you may have a like a developer account
that a person can connect to and they
can create tables they can delete tables
they can insert update delete records
and all kinds of stuff
separately you may have a reporting user
that can only do queries that return
results they can't do inserts they can't
do updates
they can't do deletes
and i guess that's the one command i
didn't really talk about is the select
would be your other
is let's say
oh i don't have
that so let's just put that in there
just because
and we'll just put here insert select
it's not really the same but that's okay
so
we can
go in we we select our data
and
we can't do anything with it because we
are in this case instead of that that
reporting user and then you may have
some that are
call them power users or something like
that you may have this other
permissions set up where
maybe they can insert and update records
but they can't delete
or maybe
they can't directly do any of that but
they can call stored procedures and
store procedures can be built in a way
that can
provide spot permissions basically so
for example i could say that no user is
allowed to none of my users are allowed
to update data however
i can create my stored procedures
so that they can update data so there's
a very specific now a controlled way to
deal with it another value for stored
procedures and functions
and so within that
we can grant a user or users
access to a procedure and then that
procedure will do the updates
and then
i want to i don't want to leave this
first little step without mentioning
that we're going to go through examples
that are
essentially standard sql
you may find differences depending on
what database you use now if you use
what we're using you're probably going
to have essentially the same
but depending on how you configure
things you may even see a few
differences
for example
sometimes you're going to find that
database queries and databases in
general what they display back are case
sensitive
and sometimes they're not
so if you wanted to write
select star from
my table
that may be fine even if the table is
called
capital my capital t table
and it may be that this works no matter
what or if this is the name of your
table my table it may be that when it's
uh when you try to do this query
it says no my table doesn't exist
because it doesn't with that case
sensitivity
so something to consider if you're going
through examples and it's not quite
right
just sort of a heads up
one make sure that they're saying things
like
uh spacing and
uh case sensitivity and if there's
things like a semicolon or not make sure
those things are you double check those
and then when in doubt you can always do
a google query for that or a google
search or whatever your favorite search
engine is for
that query and see where there may be
some issues and particularly if you have
that like a query and then
whatever the error is
you can probably find a lot of ways to
get you know pointers to the different
ways you can break
a query
i think that's going to do it for this
one i just really wanted to give sort of
an overview talk about some of the
things we're going to get into give you
just sort of like a you know crash
course here's a database here's what
we're going to talk about
and then we're going to come back in the
next episode and get started now
before i wrap this one up though i do
want to say
this is not going to be intended this is
not intended to be
a database engine or version specific
so while i am going to be personally i'm
going to be while we're going through
these working in a mysql database
and specifically
i'm going to
get you this real quick
so you can see what the version is
uh specifically the one i'm working on
is version
10.3.34 of mariadb
or
you may want the whole server version
and
there may be some variations in that but
most of what we should we're doing you
should be able to i don't know if you've
got a sql server microsoft sql server if
you're working in that if you're working
in oracle if you're working in db2
you are going to see some differences
particularly when we get into
creating databases a little bit of
creating tables because data types can
be a little different how they're
handled
and then when we get into
we do any of them the administrative
type stuff we're going to talk about
like database backups and things like
that
that may be a little different
but
hopefully you'll see that the the bulk
of what we're covering
is applicable to whatever engine you're
using
so in leaving
this one
you may want to go ahead and go search
for
download maria db latest version
something like that
or go search for this version how to
download it for whatever
platform you're working on
this one is actually a um
this is a centos version 10
operating system on the back end
don't know that's going to matter much
but just so you've got a little bit of
context for what we're going to be
sort of the foundational tools we're
using as we go forward with that
that being said
we'll wrap this one up
let you get back to it and come back
next time around and we're gonna start
diving into our database creation
have a great day
you
Transcript Segments
0.48

[Music]

26.64

well hello and welcome back and welcome

29.76

to

30.4

if you haven't aren't coming back but

32.239

welcome to

33.36

a series on

35.28

sql

36.48

we're going to get into database

38.239

development

39.44

really focused on the database side and

41.6

we're going to

42.8

yes start with some very simple sort of

45.76

general basic kinds of rules

48

and we're going to work our way up to

50.399

some more complicated things

52.239

i haven't seen a whole lot of good

53.76

tutorials other than

56.559

the ones that are really more like a

58.079

straight not as much a tutorial as they

60.719

like a reference

62.719

it says you know hey these are all the

64.879

different ways you can use this

67.04

command

68.32

and so we decided this would be a great

69.76

way to

71.04

do a new series and it's also a

72.88

foundational piece of so many other

74.72

applications that we've dealt with or

76.72

will deal with

78.159

particularly when you go to for example

80.24

the django stuff that we did

82.64

there's a lot of database

85.68

knowledge within there

87.92

and i think it makes sense to just sort

89.52

of you know take a step back and let's

91.6

talk about some database stuff

94.479

so in this series

96.32

we are going to go through

98.72

from

99.759

installation to

101.68

whatever you know backups and

103.28

maintenance and stuff like that uh this

105.28

first episode is really focused on just

107.84

it's really an overview of the class

109.28

what are we gonna order this series i

110.799

guess

112.079

and

113.6

what kinds of things are we going to

115.28

talk about

117.119

now in general i want to talk about

119.52

what's a database a database is really

122.159

just a way to store

124.399

data i know it seems very simplistic but

127.759

you have it and the terminology by the

130.479

way will change a little bit depending

132.239

on which vendor you're dealing with they

134.8

have different ways that they refer to

137.92

the various things we're going to be

139.12

dealing with as far as users and

141.36

databases and tables and things like

143.92

that

145.28

the naming can change a little but

147.599

the key here will be to get to the

149.12

concepts

150.319

and to try to keep them as generic as

152.879

possible

154.08

even though our focus is going to be

156.8

on the my sequel maria database

160.879

and that's a slash because it used to be

162.8

my sequel now oracle owns it and has

165.2

been renaming it more or less to maria

167.44

but you'll find

169.12

for example just

170.48

running the command line stuff is still

172.56

going to have all those my sequel

174.16

references so

175.76

don't let it confuse you if you haven't

177.44

messed around with database for a while

179.92

and you see this mariadb stuff coming up

182.8

instead of mysql

186.159

and we're going to we're going to go

187.599

through

188.319

all kinds of fun things we're going to

190.72

start with we're going to create

192

databases we're going to create users

194.56

we're going to set some permissions

196.48

we're going to talk about creating

198.159

tables

199.599

and inserting data updating data

202.56

deleting data

204.64

now from those concepts a table is

207.76

if you can think of a spreadsheet like

209.28

excel or something like that you've got

211.12

rows and columns

212.879

that's a table in a database you have

214.879

rows of data and then you have these

216.64

columns

217.76

and within those columns they have

220.4

things constraints which are like a type

222.879

something like that so your column may

224.72

have

225.599

all the data in there maybe a number it

228

may be a character of some sort it may

230.879

be a time time stamp

234.4

we will talk about those as we get

235.92

further into it

238.239

you can insert data which basically you

240.56

think could be this

242.319

you've got columns and rows you can add

243.92

a new row put some data into it

246.56

and that is your insert

248.64

update is you go select a row that has

251.2

data in it change some of that data and

253.599

that is the update

255.76

of course delete if you go to that row

257.759

and you want to remove it completely not

259.44

clear it out but actually delete that

261.199

row

262.079

so that now there's one less row

264.639

that's a delete

267.199

and we're going to get into stored

268.479

procedures

269.759

now databases typically are

272.96

queries or commands and this sql is

276.16

actually structured query language

279.6

and it's

281.36

primary purpose is to ask something of

284.32

the database or to tell it to do

285.84

something that's

287.36

more or less like a

289.28

anatomic task you know like

291.84

insert this record update these values

295.04

delete this record

297.44

stored procedures

299.04

allow us to group multiples

302.32

of those commands together so instead of

305.199

saying

306.4

give me the data from this table

309.759

starts where this field starts with a

313.44

and then that's it now it's give me that

315.68

data and then i want you to look at each

317.84

of those records and update them if this

319.919

other column is equal to you know it's

321.52

greater than zero or something like that

324

and so now instead of just a command at

326.24

a time stored procedures allow us to do

329.199

multiple commands sort of you know stack

332

them up cue them up

333.6

and then generate those

335.919

and it

336.8

works like

338.4

another language is a function or

340.08

something or procedure subroutine

342.4

depending on what it's called in another

343.759

language

344.8

where you can send

346.88

values in

348.479

to this list of commands

350.72

and then generate all you know do all

352.4

the commands and then kick out some sort

354.56

of result

356.319

now it's a database

358.16

so it may be that the results come at

360.24

the end or you know like

362.4

return back as part of the stored

364

procedure or it may be that those

365.84

results are really just side effects the

369.039

updates and changes made to data and

372

maybe even the database itself

374.56

when that runs

376.88

and we're also going to talk about

378.84

views a a table typically we talk about

382.4

these

383.28

commands so we can query a table and say

386.319

give me all the like i said all the

387.919

records that start with a

389.6

okay great

391.28

what a view does

392.96

is it takes a query it takes results of

396

a query

397.199

and gives you

398.479

that as

400.16

a table in itself

401.919

so if you think of a spreadsheet if

403.44

you've got a spreadsheet and you've got

404.56

a hundred records on it

407.28

and then you take that

408.88

and you turn around and take 50 of those

411.28

records

412.56

and i'm going to do a quick example i'm

414.88

here

417.599

if this will come up while i'm talking

422

then

423.68

you can bait so let's say this is a

425.44

database and you've got you know you've

427.36

got your columns a b

429.52

c d e

431.84

and you've got your values so we're just

433.52

going to keep it very simple here

436.08

two two two two

440.639

and we've got that so we've got this

441.919

little

442.88

table

444.72

and so right now what i'm doing is like

446.56

here would be insert a row

448.639

whoops let me do that let's update this

450.639

row so i'm going to go through i update

451.84

these values that would be an update

454.72

i can delete it

457.28

that would be a delete now this is not a

459.44

database but i'm seeing i'm showing you

461.44

roughly what this stuff would look like

464.639

now in a view

466.56

what i can do is let me do this

470.639

is i could say let me i'm going to often

473.12

ask give me all the values greater than

476.16

2.

477.199

and so this would be the result set

480

and so

481.44

if i add another sheet

484.879

that would be the result set now what i

486.4

can do

488.08

is go back to sheet 1

490.56

table 1. and that's my

494.56

primary results that's my source but now

496.879

i could have this view which i'm going

498.639

to call

500.24

view one

502.639

and its data is based on what's in table

506.879

1.

508.479

i will get deeper into views later but

511.12

that's roughly your view

514.399

so you have stored procedures so you can

515.839

stack up commands you've got views so

517.919

you can essentially store

520.8

solution or result sets

523.12

you have users and permissions this is

525.36

your typical security kind of stuff is

527.279

that you have people that have

529.2

access to the overall database

532.64

people are users that can have access to

535.68

either sometimes it's called a schema or

537.68

a that's sometimes referred to as a

539.36

database within

541.2

the overall database engine

544.08

and then those permissions may be limits

546

to

546.72

oops that's not what i want maybe the

548.8

limits to things like can they insert

550.72

can they update can they delete then

552.399

they create another database can they

554.72

create a table

556.959

and so you will have users and

558.72

permissions which we will explore and

560.32

we'll set one up because

562.399

much like security in general

565.12

you have a

566.72

what's in most databases known as a is

568.959

the owner or the root user that has

571.92

access to everything

574.24

and then you're going to have users that

575.68

have

576.64

limited access depending on what your

578.16

needs are

579.2

and ideally you don't use that root or

582.24

super user or owner account

585.44

basically ever

587.12

all of your stuff should be all your

588.959

work instead should be

590.72

a user created and then assign

592.959

permissions for

594.72

that type of work

596.72

and you'll see this a lot in a database

598.48

where

599.279

you may have a like a developer account

602.079

that a person can connect to and they

604.64

can create tables they can delete tables

607.04

they can insert update delete records

609.04

and all kinds of stuff

611.36

separately you may have a reporting user

614.16

that can only do queries that return

616.56

results they can't do inserts they can't

618.32

do updates

619.6

they can't do deletes

622.64

and i guess that's the one command i

624.24

didn't really talk about is the select

625.92

would be your other

627.44

is let's say

629.279

oh i don't have

630.839

that so let's just put that in there

633.519

just because

636.8

and we'll just put here insert select

641.519

it's not really the same but that's okay

644.88

so

646.88

we can

648.079

go in we we select our data

650.8

and

652

we can't do anything with it because we

654.16

are in this case instead of that that

656.24

reporting user and then you may have

658.48

some that are

661.839

call them power users or something like

663.36

that you may have this other

665.519

permissions set up where

667.519

maybe they can insert and update records

670.079

but they can't delete

671.839

or maybe

673.279

they can't directly do any of that but

675.12

they can call stored procedures and

676.88

store procedures can be built in a way

678.959

that can

680.24

provide spot permissions basically so

683.279

for example i could say that no user is

685.519

allowed to none of my users are allowed

687.44

to update data however

690.64

i can create my stored procedures

693.12

so that they can update data so there's

695.2

a very specific now a controlled way to

697.68

deal with it another value for stored

700.48

procedures and functions

703.12

and so within that

704.64

we can grant a user or users

707.76

access to a procedure and then that

709.76

procedure will do the updates

714.079

and then

715.44

i want to i don't want to leave this

717.68

first little step without mentioning

719.44

that we're going to go through examples

722

that are

724.079

essentially standard sql

726.8

you may find differences depending on

728.88

what database you use now if you use

730.639

what we're using you're probably going

731.839

to have essentially the same

734.079

but depending on how you configure

736.079

things you may even see a few

737.68

differences

739.12

for example

740.639

sometimes you're going to find that

741.839

database queries and databases in

744.959

general what they display back are case

746.88

sensitive

748.16

and sometimes they're not

749.839

so if you wanted to write

751.6

select star from

754.24

my table

756.399

that may be fine even if the table is

759.44

called

760.56

capital my capital t table

764

and it may be that this works no matter

766

what or if this is the name of your

768.16

table my table it may be that when it's

770.72

uh when you try to do this query

772.88

it says no my table doesn't exist

775.2

because it doesn't with that case

777.92

sensitivity

780.88

so something to consider if you're going

783.12

through examples and it's not quite

785.44

right

787.12

just sort of a heads up

789.04

one make sure that they're saying things

791.04

like

792

uh spacing and

794.399

uh case sensitivity and if there's

796.56

things like a semicolon or not make sure

798.32

those things are you double check those

801.44

and then when in doubt you can always do

803.76

a google query for that or a google

806

search or whatever your favorite search

807.839

engine is for

810.16

that query and see where there may be

812.24

some issues and particularly if you have

813.68

that like a query and then

816.079

whatever the error is

818.56

you can probably find a lot of ways to

820.8

get you know pointers to the different

822.639

ways you can break

824.399

a query

827.12

i think that's going to do it for this

828.48

one i just really wanted to give sort of

830.399

an overview talk about some of the

832

things we're going to get into give you

833.6

just sort of like a you know crash

835.44

course here's a database here's what

837.199

we're going to talk about

838.88

and then we're going to come back in the

840.24

next episode and get started now

845.04

before i wrap this one up though i do

846.48

want to say

848

this is not going to be intended this is

851.04

not intended to be

853.279

a database engine or version specific

857.279

so while i am going to be personally i'm

860.16

going to be while we're going through

861.199

these working in a mysql database

864.399

and specifically

867.839

i'm going to

869.12

get you this real quick

871.519

so you can see what the version is

874.32

uh specifically the one i'm working on

876.399

is version

877.88

10.3.34 of mariadb

881.199

or

882.16

you may want the whole server version

886.399

and

887.6

there may be some variations in that but

889.68

most of what we should we're doing you

891.36

should be able to i don't know if you've

892.959

got a sql server microsoft sql server if

895.12

you're working in that if you're working

896.88

in oracle if you're working in db2

899.6

you are going to see some differences

902

particularly when we get into

904.72

creating databases a little bit of

907.519

creating tables because data types can

909.44

be a little different how they're

910.639

handled

912.48

and then when we get into

914.32

we do any of them the administrative

916.399

type stuff we're going to talk about

917.519

like database backups and things like

919.279

that

920.48

that may be a little different

922.8

but

923.6

hopefully you'll see that the the bulk

925.44

of what we're covering

926.88

is applicable to whatever engine you're

929.279

using

930.399

so in leaving

932.32

this one

933.519

you may want to go ahead and go search

935.279

for

936.72

download maria db latest version

939.279

something like that

940.72

or go search for this version how to

942.639

download it for whatever

944.48

platform you're working on

946.399

this one is actually a um

950.079

this is a centos version 10

954.8

operating system on the back end

957.12

don't know that's going to matter much

959.12

but just so you've got a little bit of

960.959

context for what we're going to be

963.36

sort of the foundational tools we're

964.959

using as we go forward with that

967.36

that being said

968.72

we'll wrap this one up

970.48

let you get back to it and come back

973.04

next time around and we're gonna start

974.72

diving into our database creation

977.92

have a great day

994.399

you