📺 Develpreneur YouTube Episode

Video + transcript

SQL tutorial - Day 9

2022-05-03 •Youtube

Detailed Notes

1. Date and time types DATE - format YYYY-MM-DD. DATETIME - format: YYYY-MM-DD HH:MI:SS. TIMESTAMP - format: YYYY-MM-DD HH:MI:SS. YEAR - format YYYY or YY. 2. Date comparisons 3. Date arithmetic

create table all_dates( type_date DATE, type_datetime DATETIME, type_timestamp TIMESTAMP, type_year YEAR ); insert into all_dates(type_date,type_datetime,type_timestamp,type_year) values (now(),now(),now(),now());

insert into all_dates(type_date,type_datetime,type_timestamp,type_year) values ('2000-01-01 01:00:00','2000-01-01 01:00:00','2000-01-01 01:00:00','2000'); insert into all_dates(type_date,type_datetime,type_year) values ('2000-01-01 01:00:00','2000-01-01 01:00:00','2000'); update all_dates set type_year = '2020' where type_year = '2000'

create table all_dates2( `date` DATE, `date time` DATETIME, `time stamp` TIMESTAMP, `year created` YEAR );

Transcript Text
[Music]
well hello and welcome back we are
continuing looking at sql uh
particularly mysql and mariadb and doing
some
uh you know basically some tutorials as
we're working our way through this
uh this episode we are going to look at
some of the date related types data
types
and so
in order to do this let's go ahead and
start by
creating a table and with this table
probably easier to do it this way i
create this table
uh oops
and i should probably get rid of that
comma there we go
and let's look at that i'm going to
create a table called all dates
and i've got four
types here i don't have a primary key
or an id or anything like that this is
just for mess around
and you'll see here
i have a date
a date time a time stamp in a year
now the interesting thing about and each
these are the four date types in sql the
interesting thing you probably see right
away
is that i've got a default current time
stamp and on update current time stamp
for this type timestamp
and it can't be null
so
that
while a
uh well i'll take a look i'll show you
what that data looks like in just a
second so let's start with that
so let's go ahead and just insert a
record
where
we're just going to use
now which is basically a way to say what
is the time so if i do
just do this if i do select now
from
[Music]
all dates
it's just going to show me
up and it's not going to show me that
darn it
oh because i don't have that so let's do
that for my addresses
there we go
so it's just going to do now which is
this is the date and time according to
the system that we're on to that server
date and time so now if i go in here and
i'm just going to insert a record that's
now
for each of those types
and we're going to see here
that it's not too happy about it oh
because
uh
i didn't do that right
let's do this uh
and this is going to be
a
type date whoops
hey when i struggle to type
okay so type date
type date time
type timestamp
and type year
okay so now we're gonna take that let's
just copy and paste that over here
and now
type year
tells me it's an in-price it's an
incorrect date time so what i'd have to
do here because type year is
just a year it's not a date time
i can do this i can take just the year
part of now
and now it's going to work
and we'll look at that in just a minute
so now if i do select star from
uh
oh
dates
so i can see that i have my
type date my first one it's just a date
it gives me a year and this is uh set to
your local
standards and looks it may look a little
different on yours depending on what
your environmental stuff is
but with the date note i have just the
date it's a month 04
day 20 and then the year and it's
actually it's why i want you know it's
this format
four digit year two digit month two
digit day
for a date time
i get the date
a space and then i get the 24 hour time
so this is this says it's one in the
afternoon 1pm
24 minutes after 44 seconds after
timestamp is going to display
in this case as a date time as well
sometimes with the timestamp you will
see an integer value and that is
it depends on what it is it's basically
it's a system time
and it is a way that this database can
store a time as a big integer
as opposed to this
this date format
and of course with the year
we see that the type year is
2022.
now with this one
uh let's see if i do select uh let me do
now from
all dates
let's play around with that for a little
bit
now
with that
there are some things i can do so i can
do year
and it's going to give me the year
i can do
i believe i can do time let's try that
and it gives me just the time
and these are all ways to quickly pull
do some quick formatting if i need a
date gives me just a date
i can do
i think i do month
yep so it gives me the month and this is
who
this is the
integer month months start january is is
month one
just to be sure uh up to december which
is month 12.
i can do
uh i can even do down like i can do hour
and it's going to give me the 24 the
military time hour
now if i do the same insert
and let's
uh let's change this around
let's do this
let's take this same insert
but now
instead of using the now i'm going to
change this up and i'm going to use
so let's say let's stay say the year
2000
and the time stamp will be uh 2 000
0101 one zero zero zero zero
so let's do that
and let's do the same thing whoops
over did it
let's do the same thing
so here
now note before when i did now
it made some adjustments let's see what
it does if it makes those same
adjustments when i do this as a string
okay i get the same thing i do or i get
it no error so if i just like start from
all dates
now we see
that i got the same thing it it went
ahead and again on the type date it just
truncated off the time
so i'm still good there
but notice
while that defaults
uh somewhere up here for timestamp is
the current timestamp
i'm able to overwrite it because that's
as we've seen before that is the default
but
if i take that same entry
and now
i get rid of the timestamp
and we'll see where this becomes very
useful
here
okay so i'm going to take that guy
and now i can see that he gives me a
timestamp
he gives me when this was created now
note note that uh 1329 now let me do
this let me do the same thing and let's
do update
all dates
set
year equal uh let's do 2020
where year
equals
2 000.
so let's take this and do an update
oh sorry it's not year it is type here
helps remember the column name
probably wouldn't like here anyways
there we go
oh
okay so now if we do select star from
all dates
now notice here
that um
so we just did an update on these two
fields i went from 2000 to 2020. and
notice before our update our timestamp
was 2000 it's 2022 at 13 29 21 well now
that time stamp because we didn't
include it
it is an update timestamp
so we have a last modified that we can
easily do
within the table now there are some
other things we can do with the
timestamp we could do it on created we
can set some information there we can
change around some of the defaults
particularly because if you see here
the default is current timestamp but
we'll see here it has this thing that
we're going to talk about later called a
trigger
that says on update
current timestamp so when i do an update
if i don't have this in there it's going
to give that current timestamp
now you notice that the other dates
stayed the same
that 2000 you know 1 1 2 000 that stuff
i'll stay the same but the time stamp
updated itself and of course a year did
because i told it to
now while we are
talking about this i do want to briefly
mention
if i've tried to create a table
oh let's call it all dates to
let me use some reserve words and let me
show you what happens here
this could get very confusing to the
database but so if i create this one
i'm going to get errors because these
are reserved words now what i can do
is i can escape them
essentially i can do these tick marks
just say look
quote this thing out and that means
though that
every time
oh yeah it's not going to like that at
all
oh my mistake
let's do this
okay and let me get rid of that so i
don't keep blowing that up on you guys
so let's do this here first whoops okay
give her the comma
let's see what
happens okay so i do get to do it
but
i think i'm going to get
insert
into let's see if this works
because i'm using reserved words so if i
do date comma date time
comma
time stamp
comma
year
values now
oh
whoop come here
down down now
oh
let's make that a year
let's see if works oh it does
so even though it could get pretty
confusing when you start doing queries
you can use those words as
column names
sometimes you'll run into issues with it
though and in that case you will want to
use
tick marks
also
you'll see this a lot if you are coming
from
a system where you have spaces
in the name so if this was like date
time time stamp
you're created you know something like
that you'll sometimes see those you'll
have to use those tick marks
in order to tell it that this is
actually a string i want to use instead
of a reserved word
now
we can do let's go back to select star
from all dates
from all dates there we go now let's do
that's a little arithmetic so i'm going
to do select star from
all dates
where
let's see
type
date
less than 20220101
and i'm going to get that i can see that
i can use greater than and less than
with my dates
i can probably even do
i haven't tried this before but we're
going to try this anyways i think i can
do a like with this so i can say where
it's like 2000
and yep sure enough it's going to give
me that so
and actually i think we have stumbled
into that before is it can treat it
almost like a string when you use a like
so we can use our less than we can use
our greater than
things like that we can even do an equal
uh we're type date
equals 2011.
and i can even do not equal
and that's going to give me the 2022
date
so i can go in and and do that fairly
easily
it's not going to it's not rocket
science it's not something that we're
gonna sit there and go oh wow this is
you know this is new and unusual because
it's basically the same thing we've seen
with a lot of other with numbers and
even strings things of that nature
now
the next thing we we want to play with
briefly is date arithmetic which is
where we are going to add something so
if i want to do select
so when i do select now from
uh all dates
let's do from all dates too
now
that's going to show me this time now if
i do a plus one
um i can do it like that
now i'm going to see it as
a
as this integer what i really want to do
is i want to do
select
let's do this so i can do select year
of now plus one
and so now i'm getting that year
i can see that i've got from 2022 to
2023 if i do dates let's see what the
date gives me
and now i see it's 4 21 instead if i did
the four
i'm sorry if i did that
that would be the 2022 but note that
when i go with the the plus
it's actually
converting it into this integer thing
and actually let's do it this way let's
do uh
now plus one comma now so you can see
the two
may be easier to see
so we see here this is at 13 38 21
and we can see it adds to the last part
here
so i can go
um
i can add them you know i can add a
minute i could i'm sorry that's a second
in that case because it's adding to that
but i could also do
uh let's see
that well yeah so let's start with that
noting that it gives me this slightly
different format when i do it when i'm
coming out of it
uh but i can do an update let's see if i
do update well let's do this
so we can see it
all dates
and then if i do
update
all dates
set
uh let's say type
date
type
whoops
type date
plus one
oops well let's do it on the dates so
it's that one
and now do it
we can see that here
instead of oh let's take this over here
that's probably easier to read here
sorry
if we look here we can see that it went
from 420
and one one to 421 it so it's on the
type date time we get the last at the
the uh what is that hour's minute second
on a date we get it at the day
and so it's just taken as adding one to
that less number in this case we're
doing at one
now we can do more complicated date
arithmetic and
that's like our very basics if we want
to get a little more complicated then
there's some additional functions that
we will come back and visit like i said
there's a lot about dates that
is worth our review time but i think
that's it for today that wraps up our
time so
i'll let you get back to it we'll come
back and we're just going to keep
digging our way through these things as
you've seen we've talked about things
like stored procedures we've or
mentioned and triggers and some things
like that that we will start getting
into along the way
that being said
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.199

well hello and welcome back we are

29.199

continuing looking at sql uh

31.359

particularly mysql and mariadb and doing

34.239

some

35.04

uh you know basically some tutorials as

36.88

we're working our way through this

38.96

uh this episode we are going to look at

41.28

some of the date related types data

44.239

types

45.76

and so

46.8

in order to do this let's go ahead and

48.879

start by

50.32

creating a table and with this table

55.199

probably easier to do it this way i

57.039

create this table

59.76

uh oops

62.559

and i should probably get rid of that

63.6

comma there we go

65.28

and let's look at that i'm going to

66.56

create a table called all dates

71.36

and i've got four

72.799

types here i don't have a primary key

75.28

or an id or anything like that this is

77.04

just for mess around

80

and you'll see here

81.6

i have a date

83.2

a date time a time stamp in a year

87.04

now the interesting thing about and each

89.119

these are the four date types in sql the

91.68

interesting thing you probably see right

93.68

away

94.56

is that i've got a default current time

96.56

stamp and on update current time stamp

99.04

for this type timestamp

102.96

and it can't be null

105.119

so

106.24

that

107.2

while a

108.479

uh well i'll take a look i'll show you

110.159

what that data looks like in just a

111.28

second so let's start with that

113.36

so let's go ahead and just insert a

114.96

record

116.159

where

117.04

we're just going to use

120.96

now which is basically a way to say what

123.52

is the time so if i do

128.08

just do this if i do select now

131.2

from

131.59

[Music]

133.52

all dates

135.2

it's just going to show me

137.12

up and it's not going to show me that

139.04

darn it

142.239

oh because i don't have that so let's do

143.76

that for my addresses

149.36

there we go

150.4

so it's just going to do now which is

152.4

this is the date and time according to

154.08

the system that we're on to that server

156.959

date and time so now if i go in here and

159.36

i'm just going to insert a record that's

160.959

now

162.64

for each of those types

164.8

and we're going to see here

166.64

that it's not too happy about it oh

170.48

because

172.4

uh

173.599

i didn't do that right

179.12

let's do this uh

185.76

and this is going to be

187.36

a

189.04

type date whoops

197.68

hey when i struggle to type

200.159

okay so type date

203.28

type date time

206.64

type timestamp

211.599

and type year

214.159

okay so now we're gonna take that let's

216.239

just copy and paste that over here

222.72

and now

224.48

type year

225.76

tells me it's an in-price it's an

227.68

incorrect date time so what i'd have to

229.84

do here because type year is

233.36

just a year it's not a date time

235.92

i can do this i can take just the year

238.48

part of now

241.04

and now it's going to work

243.519

and we'll look at that in just a minute

245.439

so now if i do select star from

249.04

uh

249.92

oh

250.72

dates

254.72

so i can see that i have my

257.12

type date my first one it's just a date

259.919

it gives me a year and this is uh set to

262.56

your local

264.72

standards and looks it may look a little

266.72

different on yours depending on what

267.919

your environmental stuff is

270.56

but with the date note i have just the

273.12

date it's a month 04

275.6

day 20 and then the year and it's

277.6

actually it's why i want you know it's

279.84

this format

282.96

four digit year two digit month two

284.88

digit day

286.479

for a date time

288.88

i get the date

290.32

a space and then i get the 24 hour time

292.72

so this is this says it's one in the

294.88

afternoon 1pm

296.639

24 minutes after 44 seconds after

299.919

timestamp is going to display

302.88

in this case as a date time as well

306

sometimes with the timestamp you will

307.52

see an integer value and that is

310.96

it depends on what it is it's basically

312.479

it's a system time

314.4

and it is a way that this database can

316.96

store a time as a big integer

319.6

as opposed to this

321.68

this date format

323.6

and of course with the year

325.36

we see that the type year is

327.84

2022.

329.759

now with this one

332.24

uh let's see if i do select uh let me do

336.4

now from

338.4

all dates

340.72

let's play around with that for a little

341.759

bit

342.72

now

343.52

with that

345.84

there are some things i can do so i can

347.36

do year

350.72

and it's going to give me the year

353.919

i can do

355.52

i believe i can do time let's try that

359.68

and it gives me just the time

362.08

and these are all ways to quickly pull

364.88

do some quick formatting if i need a

366.479

date gives me just a date

369.84

i can do

371.44

i think i do month

373.199

yep so it gives me the month and this is

375.12

who

375.84

this is the

377.039

integer month months start january is is

379.44

month one

380.4

just to be sure uh up to december which

382.88

is month 12.

385.44

i can do

386.72

uh i can even do down like i can do hour

390.8

and it's going to give me the 24 the

392.479

military time hour

395.759

now if i do the same insert

402.16

and let's

403.199

uh let's change this around

405.12

let's do this

406.639

let's take this same insert

409.44

but now

410.8

instead of using the now i'm going to

412.56

change this up and i'm going to use

422.4

so let's say let's stay say the year

424.8

2000

428.24

and the time stamp will be uh 2 000

434.84

0101 one zero zero zero zero

439.039

so let's do that

442.72

and let's do the same thing whoops

445.12

over did it

446.88

let's do the same thing

450.56

so here

452.16

now note before when i did now

454.4

it made some adjustments let's see what

456.16

it does if it makes those same

457.44

adjustments when i do this as a string

464.319

okay i get the same thing i do or i get

466.96

it no error so if i just like start from

468.879

all dates

472.56

now we see

475.68

that i got the same thing it it went

477.759

ahead and again on the type date it just

480.16

truncated off the time

482.96

so i'm still good there

484.56

but notice

486.24

while that defaults

487.919

uh somewhere up here for timestamp is

490.479

the current timestamp

493.36

i'm able to overwrite it because that's

495.12

as we've seen before that is the default

497.84

but

498.639

if i take that same entry

501.52

and now

502.879

i get rid of the timestamp

506.24

and we'll see where this becomes very

508

useful

511.039

here

512.56

okay so i'm going to take that guy

520.08

and now i can see that he gives me a

522

timestamp

523.519

he gives me when this was created now

526

note note that uh 1329 now let me do

528.88

this let me do the same thing and let's

530.959

do update

533.519

all dates

536.24

set

538

year equal uh let's do 2020

542.8

where year

546.839

equals

548.56

2 000.

552

so let's take this and do an update

558.88

oh sorry it's not year it is type here

565.04

helps remember the column name

569.44

probably wouldn't like here anyways

574.24

there we go

575.279

oh

576.72

okay so now if we do select star from

582.88

all dates

586.32

now notice here

588.959

that um

591.44

so we just did an update on these two

594.24

fields i went from 2000 to 2020. and

597.44

notice before our update our timestamp

600.32

was 2000 it's 2022 at 13 29 21 well now

605.68

that time stamp because we didn't

607.519

include it

608.72

it is an update timestamp

612

so we have a last modified that we can

614.959

easily do

616.72

within the table now there are some

618.88

other things we can do with the

620.16

timestamp we could do it on created we

621.839

can set some information there we can

623.36

change around some of the defaults

625.839

particularly because if you see here

629.76

the default is current timestamp but

632.399

we'll see here it has this thing that

634.16

we're going to talk about later called a

635.519

trigger

636.72

that says on update

638.8

current timestamp so when i do an update

640.64

if i don't have this in there it's going

642.48

to give that current timestamp

645.68

now you notice that the other dates

647.12

stayed the same

648.64

that 2000 you know 1 1 2 000 that stuff

651.36

i'll stay the same but the time stamp

652.959

updated itself and of course a year did

655.519

because i told it to

659.6

now while we are

661.68

talking about this i do want to briefly

663.92

mention

665.279

if i've tried to create a table

672.16

oh let's call it all dates to

678

let me use some reserve words and let me

680.16

show you what happens here

682.24

this could get very confusing to the

683.76

database but so if i create this one

687.519

i'm going to get errors because these

689.36

are reserved words now what i can do

691.92

is i can escape them

694.56

essentially i can do these tick marks

699.76

just say look

701.04

quote this thing out and that means

702.72

though that

703.92

every time

705.6

oh yeah it's not going to like that at

707.2

all

708.8

oh my mistake

713.36

let's do this

719.04

okay and let me get rid of that so i

720.639

don't keep blowing that up on you guys

722.56

so let's do this here first whoops okay

724.56

give her the comma

727.2

let's see what

728.839

happens okay so i do get to do it

732.56

but

733.519

i think i'm going to get

736.48

insert

738.16

into let's see if this works

741.76

because i'm using reserved words so if i

743.92

do date comma date time

748.24

comma

749.519

time stamp

752.8

comma

754

year

756.399

values now

759.92

oh

764.32

whoop come here

766.16

down down now

773.12

oh

775.2

let's make that a year

778.16

let's see if works oh it does

780.48

so even though it could get pretty

782.48

confusing when you start doing queries

784.399

you can use those words as

787.12

column names

788.72

sometimes you'll run into issues with it

790.32

though and in that case you will want to

792.24

use

793.2

tick marks

794.399

also

795.68

you'll see this a lot if you are coming

797.76

from

800

a system where you have spaces

802.639

in the name so if this was like date

804.32

time time stamp

806.959

you're created you know something like

809.2

that you'll sometimes see those you'll

811.279

have to use those tick marks

812.959

in order to tell it that this is

814.8

actually a string i want to use instead

816.8

of a reserved word

819.76

now

820.639

we can do let's go back to select star

823.6

from all dates

833.519

from all dates there we go now let's do

836.48

that's a little arithmetic so i'm going

837.839

to do select star from

840

all dates

841.839

where

842.959

let's see

844.639

type

846

date

847.36

less than 20220101

856.48

and i'm going to get that i can see that

857.839

i can use greater than and less than

859.76

with my dates

862.959

i can probably even do

866

i haven't tried this before but we're

867.199

going to try this anyways i think i can

868.8

do a like with this so i can say where

870.88

it's like 2000

872.88

and yep sure enough it's going to give

874.48

me that so

876.079

and actually i think we have stumbled

877.36

into that before is it can treat it

878.959

almost like a string when you use a like

883.68

so we can use our less than we can use

885.68

our greater than

887.76

things like that we can even do an equal

889.68

uh we're type date

893.92

equals 2011.

900.56

and i can even do not equal

903.519

and that's going to give me the 2022

904.959

date

906

so i can go in and and do that fairly

908.639

easily

909.839

it's not going to it's not rocket

911.68

science it's not something that we're

913.199

gonna sit there and go oh wow this is

915.12

you know this is new and unusual because

916.8

it's basically the same thing we've seen

919.199

with a lot of other with numbers and

921.92

even strings things of that nature

925.199

now

926.56

the next thing we we want to play with

929.199

briefly is date arithmetic which is

932.079

where we are going to add something so

934

if i want to do select

936.639

so when i do select now from

939.04

uh all dates

941.279

let's do from all dates too

944

now

946.079

that's going to show me this time now if

947.759

i do a plus one

952.88

um i can do it like that

955.68

now i'm going to see it as

962.56

a

963.279

as this integer what i really want to do

965.519

is i want to do

967.759

select

969.839

let's do this so i can do select year

973.759

of now plus one

978

and so now i'm getting that year

980.56

i can see that i've got from 2022 to

982.56

2023 if i do dates let's see what the

984.88

date gives me

987.199

and now i see it's 4 21 instead if i did

989.839

the four

992.16

i'm sorry if i did that

994.079

that would be the 2022 but note that

996.72

when i go with the the plus

1000.56

it's actually

1002.399

converting it into this integer thing

1005.44

and actually let's do it this way let's

1006.959

do uh

1008.56

now plus one comma now so you can see

1010.959

the two

1012.399

may be easier to see

1015.279

so we see here this is at 13 38 21

1020.079

and we can see it adds to the last part

1022.16

here

1025.439

so i can go

1027.679

um

1028.799

i can add them you know i can add a

1030.24

minute i could i'm sorry that's a second

1032.4

in that case because it's adding to that

1034.24

but i could also do

1038.319

uh let's see

1039.919

that well yeah so let's start with that

1043.439

noting that it gives me this slightly

1046.079

different format when i do it when i'm

1047.76

coming out of it

1049.28

uh but i can do an update let's see if i

1051.76

do update well let's do this

1054.96

so we can see it

1058.48

all dates

1059.919

and then if i do

1061.2

update

1063.36

all dates

1065.12

set

1066.88

uh let's say type

1070.24

date

1072.16

type

1073.12

whoops

1075.84

type date

1077.52

plus one

1078.88

oops well let's do it on the dates so

1081.12

it's that one

1083.52

and now do it

1085.039

we can see that here

1087.2

instead of oh let's take this over here

1090.88

that's probably easier to read here

1092.32

sorry

1099.12

if we look here we can see that it went

1100.88

from 420

1102.88

and one one to 421 it so it's on the

1105.84

type date time we get the last at the

1109.28

the uh what is that hour's minute second

1113.2

on a date we get it at the day

1116.48

and so it's just taken as adding one to

1118.799

that less number in this case we're

1120.24

doing at one

1123.12

now we can do more complicated date

1125.6

arithmetic and

1128.08

that's like our very basics if we want

1130.16

to get a little more complicated then

1132.799

there's some additional functions that

1134.24

we will come back and visit like i said

1136.08

there's a lot about dates that

1138.16

is worth our review time but i think

1140.4

that's it for today that wraps up our

1142.559

time so

1144.559

i'll let you get back to it we'll come

1146

back and we're just going to keep

1146.96

digging our way through these things as

1148.799

you've seen we've talked about things

1149.76

like stored procedures we've or

1151.12

mentioned and triggers and some things

1152.72

like that that we will start getting

1154.32

into along the way

1156.24

that being said

1157.52

go out there and have yourself a great

1158.799

day a great week and we will talk to you

1162.559

next time

1179.44

you