📺 Develpreneur YouTube Episode

Video + transcript

SQL Sync Part 4

2022-08-25 •Youtube

Detailed Notes

Focus for this episode: Deeper details for creating columns and execute our table creation needs.

This tutorial walks through the creation of a database synchronization tool in Python. It continues our Python and SQL Tutorials with a useful application that provides a deeper look at SQL structures and commands.

Repository For Code: git clone [email protected]:rbroadhead/dbsync.git

Transcript Text
[Music]
okay i had a bit of a
challenge with the latest recording so
we're going to run back through this a
little bit
and apologies as we build this out so
last time we were last episode we were
looking at
building out some create table sql
and we had a couple of issues or a
couple of values we were looking at
which were basically
the column name uh let me
do this let me run this real quick
um
and
let's do
this
so what we were doing was we were going
through and uh actually i'm just going
to quit after that
so we can see one
uh whoops
let's run it somewhere in here there we
go i mean okay
um we're looking at the description for
tables
here and we can see with each row we've
got a
column name a type
whether it is no or not
and then we've got
uh
this was
[Music]
let's say no or not
and then we had
whether or not i believe it's a primary
key or not
and then
default values and then some additional
stuff now what we were looking at last
time as we were looking at the
um
the function
which was
uh which we used in some of these which
would be the auto increment so if we
looked here
i think address was one
if we look here
uh yeah we did the auto increment
and so we'd see that but now one of the
other things we'll see because of what
that is is we can see that for example
uh
let's see if i can find a column that
does it oh address audit will have it
sometimes we have a default and
sometimes i'm going to have some extra
which extra would be in this case
extras the auto increment
so what we want to do is be able to take
care of default values and we also want
to be able to deal with the extras at
this point
so for default
for the extras instead of
so this is our primary key
let's do this
i'm sorry this is
let's say
allow us null
let's do that just for comment this a
little bit
this one is going to be our extras
and it's just going to be whatever it is
however
let's see this is going to be primary
key
if it exists
which it may not so if we have a primary
key if one of those columns is primary
key
then great which is uh up here if it's a
primary then it's going to go ahead and
create that
otherwise we're just going to close out
our
command this is our sql command
and
here we go here's our defaults
let's do
and the little default values
if they exist now this one's a little
tricky
because we have two different types of
default values
we could have a default value that is
either auto increment or
as it ends here whoops
uh address i think's got it
hello
um
it's the current current timestamp which
if you look at those
here
it's actually a function you can see
that it's actually a function being
called but if you were to look at one of
the other ones so let's just not quit
but let's go through and show all of
those
uh let's see don't have any extra prints
nope that should work so if i do this
then we're gonna see some for example
i believe
uh
where did that go
[Music]
we had one
that was a default value non-assigned
let me go look for that real quick
did i have that over here
i did not
let's go to grab this
let's do it like that
and let's just do this real quick um
i think it was this there we go so here
this one username has a default value of
not assigned
now if we just bring them over
then in this case it's not going to it's
going to give us
something that's not correct so i'm
going to show let me go ahead and do the
create sql real quick
and let me get rid
of some of those pieces so it's a little
easier to read
and so we run into
what was that end users
let's see dates dates app user there we
go
so let's actually look at both of these
so if we look at all dates
and let's just go ahead and format these
now if we look at all dates 2
let's go ahead
then here
uh
oh we didn't have anything super special
here uh oh yeah we do i'm sorry is we're
to come in we can do create not null
default is going to be this current
timestamp so here we want it to be
the function we don't want it to be a
string however down here on
um app user
the default is a string
we don't want to call it so we're going
to say if it's a function which we've
seen a little bit
uh if it's a function
then which basically we're just going to
say hey if we've got a parentheses in
there it's a function and so in that
case we're just going to take the value
out of the column otherwise if you can
see here we're going to wrap it in
single quotes
now if we do all of that
then we're gonna get all of our little
uh
executes
our sql statements let's go ahead and
see what we've got here
um so we're gonna print it out and then
we're gonna execute it so this time
we're gonna try to actually create
tables
and we're going to end up having an
error
which is on all dates
and what you're going to see here
is another case we're going to have to
deal with um
let's
pretty print it
and if you see here we come in and we do
create dates
and
let's clean this up a little bit so it's
easier to read
now if we come through here let's see
what it's doing it does type date uh
date time
not all default okay so let's see what
he does
let's see what he complains about
uh oh all dates already exists
oh so we already have that probably so
if we go to
uh tutorial
tables
uh let's go ahead and delete all of
those tables
and now let's run it
okay so we are going to have an issue
with address
and here what we're going to see is this
little integer display width is
deprecated will be moved in a future
release and that problem is that we have
that integer there so
let's try to fix that one okay first
let's get rid of our tables
uh
here
and so it was just that address but see
now it came back and it actually gave us
an error so now what we could do
is we could come back up here
and do the raison warnings and say false
and let it just go ahead and create it
but we don't want to right now we're
going to go ahead and bail out so
into doing
we can drop this table
and what we're going to do here is we're
going to come into the types
where is our types
here we go
which is going to be column 1.
ah that's the allows no
let's see
and it's gonna be a fix
for
data
types int
and we're going to do
we're going to call type
equals
column one
so now we're going to change this over
because we're going to use kyle types
instead because we can't directly check
it
it's the way it tends to work so okay so
we sign it out and now we're going to do
we're going to say if
if
call type dot starts with
i think that's how it works uh if it
starts with int
then
call type equals
call type
dot split and we're going to split it
on that first parenthesis
and we're just going to take the part to
the left of the parentheses
so now let's see what we've got here
and we're going to see
uh so he's still not doing that he's not
picking that up right
so
let's go look at
uh
here oh
and let's drop that table
now let's go look at this so
uh that is column type
so let's go look here let's quit
after this just to be safe
and we're going to print call type
and let's see what that looks like
so if we look here call type is ant oh
it's gonna be lowercase
let's do this let's do
i believe it is to
upper
we're gonna find out in a second
okay so it's not too upper let's try
upper
there we go
okay so
now if we look here let's just print it
again make sure it works there we go so
now we've got our ant
so let's get rid of those
and let's see if it runs
and we run into one now with create
table all dates
so year four
is running into the same thing
so we want to take
uh here
let's do that we're just gonna do an ore
so if it's that
or
it starts with
year whoops all upper starts with year
uh it doesn't like an ore
so i think it's this ore
there we go
that's a that's it my mistake
so let's drop all those tables again
[Music]
just because
and we're going to drop these tables
and now
if we come through
looks like it all ran so now
we're gonna see
uh let's see what am i printing i'm
printing out some extra crap i don't
need so creating the table oh and i did
my sql beforehand
so
which i'm going to leave that because
it makes sense to do so i'm going to cut
a little line out here
and i don't need this stuff
so i don't really need to worry about
these tables anymore um i'm going to
comment them out though
and so now if i run it again
it'll probably do nothing there we go
connect to source connect to the target
and now if i look
i'm going to see
in testing tables i'm going to see all
my tables that are also in my tutorial
well i've got to redo that
that are here
so i've got
one two three
let's see
so i've got my 12 tables there and i
should have
here
i should have 12 tables here
so we have our tables now that we can
create tables from one database to
another
and i think that's a good stopping point
because now we're going to take it to
the next level we're going to start
playing around with the columns
and that'll be where we pick up next
time
so go out there and have yourself a
great day a great week and we will talk
to you
next time
[Music]
you
Transcript Segments
0.43

[Music]

27.519

okay i had a bit of a

30.4

challenge with the latest recording so

33.2

we're going to run back through this a

34.48

little bit

35.6

and apologies as we build this out so

39.12

last time we were last episode we were

41.6

looking at

43.28

building out some create table sql

47.12

and we had a couple of issues or a

49.12

couple of values we were looking at

50.8

which were basically

52.559

the column name uh let me

57.039

do this let me run this real quick

60.399

um

63.92

and

65.36

let's do

70.159

this

74.32

so what we were doing was we were going

75.92

through and uh actually i'm just going

78.159

to quit after that

80.4

so we can see one

82.56

uh whoops

83.68

let's run it somewhere in here there we

85.439

go i mean okay

88.4

um we're looking at the description for

92.479

tables

94.32

here and we can see with each row we've

96.72

got a

99.2

column name a type

101.439

whether it is no or not

104

and then we've got

105.92

uh

107.119

this was

107.92

[Music]

109.52

let's say no or not

112.399

and then we had

114.399

whether or not i believe it's a primary

115.92

key or not

117.04

and then

118.64

default values and then some additional

120.96

stuff now what we were looking at last

122.88

time as we were looking at the

125.119

um

125.92

the function

128.399

which was

130.319

uh which we used in some of these which

132.4

would be the auto increment so if we

134.959

looked here

136.72

i think address was one

139.12

if we look here

140.879

uh yeah we did the auto increment

143.2

and so we'd see that but now one of the

145.12

other things we'll see because of what

147.12

that is is we can see that for example

151.68

uh

152.959

let's see if i can find a column that

154.319

does it oh address audit will have it

157.92

sometimes we have a default and

159.68

sometimes i'm going to have some extra

161.599

which extra would be in this case

164.56

extras the auto increment

167.12

so what we want to do is be able to take

168.48

care of default values and we also want

170.4

to be able to deal with the extras at

172.959

this point

174.72

so for default

176.72

for the extras instead of

181.2

so this is our primary key

184.239

let's do this

185.84

i'm sorry this is

189.519

let's say

190.56

allow us null

192.4

let's do that just for comment this a

194

little bit

195.04

this one is going to be our extras

202.08

and it's just going to be whatever it is

203.92

however

206.48

let's see this is going to be primary

208.48

key

214.48

if it exists

217.04

which it may not so if we have a primary

219.2

key if one of those columns is primary

221.36

key

223.12

then great which is uh up here if it's a

226.4

primary then it's going to go ahead and

227.76

create that

229.36

otherwise we're just going to close out

230.72

our

231.519

command this is our sql command

234.799

and

236.159

here we go here's our defaults

238.239

let's do

241.599

and the little default values

244.239

if they exist now this one's a little

246.64

tricky

248.959

because we have two different types of

250.56

default values

252

we could have a default value that is

254.56

either auto increment or

257.6

as it ends here whoops

260

uh address i think's got it

263.44

hello

264.639

um

267.52

it's the current current timestamp which

269.28

if you look at those

271.52

here

272.96

it's actually a function you can see

274.88

that it's actually a function being

276.24

called but if you were to look at one of

278.639

the other ones so let's just not quit

280.88

but let's go through and show all of

283.12

those

285.68

uh let's see don't have any extra prints

288

nope that should work so if i do this

292.08

then we're gonna see some for example

295.759

i believe

301.68

uh

302.56

where did that go

303.53

[Music]

306.96

we had one

310.8

that was a default value non-assigned

314.72

let me go look for that real quick

318.24

did i have that over here

321.44

i did not

328.56

let's go to grab this

334.72

let's do it like that

338.72

and let's just do this real quick um

344

i think it was this there we go so here

347.759

this one username has a default value of

349.759

not assigned

351.759

now if we just bring them over

354.4

then in this case it's not going to it's

356.4

going to give us

358.8

something that's not correct so i'm

360.319

going to show let me go ahead and do the

361.84

create sql real quick

365.039

and let me get rid

366.639

of some of those pieces so it's a little

369.52

easier to read

373.28

and so we run into

375.44

what was that end users

380.08

let's see dates dates app user there we

382.72

go

385.68

so let's actually look at both of these

394.639

so if we look at all dates

397.199

and let's just go ahead and format these

404.4

now if we look at all dates 2

407.52

let's go ahead

408.88

then here

414.8

uh

416

oh we didn't have anything super special

417.919

here uh oh yeah we do i'm sorry is we're

420.96

to come in we can do create not null

422.639

default is going to be this current

424.8

timestamp so here we want it to be

428.639

the function we don't want it to be a

430.88

string however down here on

434.88

um app user

437.039

the default is a string

438.96

we don't want to call it so we're going

440.24

to say if it's a function which we've

442.319

seen a little bit

443.599

uh if it's a function

445.44

then which basically we're just going to

446.8

say hey if we've got a parentheses in

448.24

there it's a function and so in that

450.16

case we're just going to take the value

452.4

out of the column otherwise if you can

454.56

see here we're going to wrap it in

456.479

single quotes

459.44

now if we do all of that

462.319

then we're gonna get all of our little

464.4

uh

465.199

executes

466.639

our sql statements let's go ahead and

468.8

see what we've got here

471.759

um so we're gonna print it out and then

473.919

we're gonna execute it so this time

475.68

we're gonna try to actually create

476.639

tables

477.599

and we're going to end up having an

478.72

error

479.52

which is on all dates

484

and what you're going to see here

486.16

is another case we're going to have to

487.36

deal with um

489.12

let's

490.08

pretty print it

493.36

and if you see here we come in and we do

496.08

create dates

498.879

and

500.24

let's clean this up a little bit so it's

502.08

easier to read

506.8

now if we come through here let's see

508.639

what it's doing it does type date uh

511.44

date time

513.519

not all default okay so let's see what

515.2

he does

516.719

let's see what he complains about

518.959

uh oh all dates already exists

522.32

oh so we already have that probably so

524.8

if we go to

528.16

uh tutorial

530.959

tables

535.36

uh let's go ahead and delete all of

537.76

those tables

542.88

and now let's run it

548.16

okay so we are going to have an issue

549.92

with address

553.92

and here what we're going to see is this

556.48

little integer display width is

558.56

deprecated will be moved in a future

560.839

release and that problem is that we have

565.279

that integer there so

569.2

let's try to fix that one okay first

571.2

let's get rid of our tables

573.279

uh

574.48

here

578.64

and so it was just that address but see

580.8

now it came back and it actually gave us

582.56

an error so now what we could do

584.959

is we could come back up here

587.6

and do the raison warnings and say false

589.68

and let it just go ahead and create it

591.76

but we don't want to right now we're

593.68

going to go ahead and bail out so

596.8

into doing

598.64

we can drop this table

606.079

and what we're going to do here is we're

607.92

going to come into the types

611.2

where is our types

613.44

here we go

614.88

which is going to be column 1.

621.44

ah that's the allows no

623.68

let's see

626.079

and it's gonna be a fix

629.12

for

630.16

data

631.2

types int

634.16

and we're going to do

635.6

we're going to call type

637.12

equals

640.399

column one

642

so now we're going to change this over

643.279

because we're going to use kyle types

644.72

instead because we can't directly check

646.64

it

649.04

it's the way it tends to work so okay so

651.44

we sign it out and now we're going to do

653.12

we're going to say if

659.04

if

661.2

call type dot starts with

670.32

i think that's how it works uh if it

672.56

starts with int

676.64

then

678.88

call type equals

681.92

call type

683.2

dot split and we're going to split it

686.56

on that first parenthesis

688.8

and we're just going to take the part to

690

the left of the parentheses

693.76

so now let's see what we've got here

697.04

and we're going to see

701.519

uh so he's still not doing that he's not

703.2

picking that up right

706.399

so

707.279

let's go look at

710.639

uh

713.04

here oh

718.24

and let's drop that table

722.72

now let's go look at this so

724.72

uh that is column type

732.079

so let's go look here let's quit

736.24

after this just to be safe

738.88

and we're going to print call type

744.72

and let's see what that looks like

748.32

so if we look here call type is ant oh

753.92

it's gonna be lowercase

756.24

let's do this let's do

761.04

i believe it is to

763.519

upper

767.92

we're gonna find out in a second

771.68

okay so it's not too upper let's try

773.76

upper

780.48

there we go

781.519

okay so

787.76

now if we look here let's just print it

789.92

again make sure it works there we go so

791.92

now we've got our ant

793.76

so let's get rid of those

795.68

and let's see if it runs

800

and we run into one now with create

802.88

table all dates

804.72

so year four

806.24

is running into the same thing

808.72

so we want to take

812.56

uh here

818.24

let's do that we're just gonna do an ore

820.32

so if it's that

823.04

or

824.639

it starts with

826.959

year whoops all upper starts with year

840.839

uh it doesn't like an ore

844.72

so i think it's this ore

846.639

there we go

852.48

that's a that's it my mistake

854.959

so let's drop all those tables again

857.92

[Music]

860.16

just because

864

and we're going to drop these tables

871.12

and now

873.76

if we come through

875.92

looks like it all ran so now

879.04

we're gonna see

881.6

uh let's see what am i printing i'm

883.519

printing out some extra crap i don't

885.12

need so creating the table oh and i did

887.36

my sql beforehand

889.839

so

891.12

which i'm going to leave that because

895.839

it makes sense to do so i'm going to cut

898

a little line out here

899.68

and i don't need this stuff

903.36

so i don't really need to worry about

904.56

these tables anymore um i'm going to

907.36

comment them out though

915.279

and so now if i run it again

918.32

it'll probably do nothing there we go

920.24

connect to source connect to the target

922.079

and now if i look

924.24

i'm going to see

925.519

in testing tables i'm going to see all

928.079

my tables that are also in my tutorial

931.6

well i've got to redo that

934.079

that are here

935.36

so i've got

937.36

one two three

939.6

let's see

941.759

so i've got my 12 tables there and i

944.079

should have

946.32

here

947.759

i should have 12 tables here

950.88

so we have our tables now that we can

953.279

create tables from one database to

954.959

another

956.24

and i think that's a good stopping point

957.92

because now we're going to take it to

959.04

the next level we're going to start

960.16

playing around with the columns

962.56

and that'll be where we pick up next

963.92

time

964.8

so go out there and have yourself a

966.24

great day a great week and we will talk

969.12

to you

970.56

next time

972.1

[Music]

987.6

you