📺 Develpreneur YouTube Episode

Video + transcript

SQL Sync Part 3

2022-08-23 •Youtube

Detailed Notes

Focus for this episode: Create missing tables Part 1 Simple table creation script output and columns name,type,null

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]
well hello and welcome back we are
continuing this uh basically it's a
database sync
walkthrough learning about sql doing a
little bit of a program along the way
so last time around
we were working on um
missing tables so comparing two
databases
and finding the tables that we're
missing
now what i want to do before we go a
little further into this one is i'm
going to actually
clean this up a little bit so i've got
my connect to database one and two
i've got those little functions
and what i'm gonna do is i'm gonna do
let's see so we had three things we're
doing
we're gonna do
let's see i don't need oh i do need that
well let's take this let's get
this whole thing
and this is gonna be
let's go ahead and do it like this is
we're going to do these uh so we've got
let's do source tables uh let's see
missing source
oops if i can type that right we do
missing source tables
and we're going to do
missing target tables
so we're going to clean this up a little
bit this will be a little easier to see
so missing source tables is going to be
this one
uh let's see
and let's do it this way so we're gonna
send it
uh source and target
and we're going to come in here and
let's see so we're going to get
let's see source tables and target
tables and so that's just going to be
here
so we're going to do new
tables
equals missing
source tables and then this is going to
be
source tables and target tables
whoops
i'm gonna do this mostly for readability
so i'm gonna come in here
and that's gonna be that guy
and let's do it this way
so i get that new tables
okay which equals that
and then here
let's do it this way new tables equals
that and then we're going to
return new tables
whoops
let's say
we're gonna call it missing tables
uh gonna go here
and then we're gonna come here
uh missing two there we go so we're
gonna do that
we're gonna basically do the same thing
and actually what we can do
is where did we do that um
we're going to take the same thing so
we're going to say missing tables
because we can
and now
because all we have to do is we have to
just reverse the two so
where do we do that let's see there's
missing tables
those are the tables that are not in the
source
and then these are the tables that are
not in
source tables there we go so we can
clean that up a little bit shrink it
down simplify a little bit
now
what we're going to do is we're going to
do a we're going to do what's going to
call create
missing table
and what we're going to do is we're
going to give it a
uh let's see
oops this is going to be missing
so we're going to always do it in the
target let's do that well
yeah
let's think about that a second so let's
go ahead and do it so we can do it
either so we're going to do four
source which is going to be
uh dbc
um
in this case
oh no it's going to be actually in db2
so he's going to be db2
and we're going to give it the tables
and then we're going to come up here
well actually let's do it here because
we're going to do it
not what we're gonna do and then we'll
move it in a minute so we're gonna do
create missing tables
and we're going to give it
a controller
and a list uh we'll call it table list
and
so we have a dbc there we go
so let's take this whole
thing uh
right there
and let's oh we're gonna move this up
and then we're gonna talk about this a
little bit
okay so what we're doing here so now
what we're going to do is we're going to
look at
what
uh
okay let's go right up here so this is
basically what a create looks like which
we've seen this before it's not terribly
formatted but
essentially we want to be able to create
to
make this
uh sql query based on what we get back
if we look down here we're getting in
our rows that we get back when we do our
described table
we get a column name
a type
whether it is nullable or not
if it's got a whether it's primary key
or not
there is a default value and then
there's uh whether it's auto increment
or not
so
first thing we want to do
when we create the first thing we're
going to do is we're always going to
have create table
and then we're going to give it the
table name so as we're spinning through
our list
we're going to
uh let me do it this way
let's sort of look at it as we're going
through it so if we're going to come
through here create a missing table
print the table
uh let's see
why do i need describe query i need it
oh because i'm going to do it for
each column
so let's do it here um
let's take that query
dot
dot
there
okay
so i'm going to do this for my table
name and then the first thing i want to
do is i'm going to create table whatever
so let's just look at that right now
and we'll see that we are creating that
so if we run
oops i've got a key equals oops
um
[Music]
p k what is p key o is equal to that
oh i forgot this
let's see if that's gonna work there we
go okay
so
these tables are not sourced these table
are not in the target so let's see what
did i get wrong here so
source and target
oh i didn't flip those two i bet
uh
here we go they're not in the source
so that's missing equals that new tables
equals that so he's not doing that one
uh oh
i need to send it
wait is that right okay so i do source
tables
and i get target tip let's do look at
that
so
i missed something here so there's my
source tables here's my target tables
and if i look
so there's source
and then target is empty so it should be
source tables
missing tables okay sources oh
that's why
let's do it this way source and target
uh source
and target
there we go so now
okay so what uh tutorial a doesn't exist
okay
so
interesting
i didn't want oh because this is my list
let's try that
uh
let's see so there's address
okay tutorial that address doesn't exist
oh because i need to do it from the
source so here i need
uh so i need to get the source one which
is
dbc
yeah
that's a whoops so here
let's see dbc2
dbc1
so what i would need to do here is i'm
going to need two cursors probably
let's do cursor
let's do it this way
cursor source
and cursor destination
and dbc1 is my source dbc2 is my
destination
and now what i need to do is so i'm
going to go in and i'm going to describe
so i so i come into each table i get my
name
and then i'm going to do describe so i
can get all of the details about that
um and so he's going to come in and he's
going to do a
describe on the source
and so if i do let's do print
let's do the results
and now let's look at it let's see
if we can get it there we go so what's
going to come through
as it comes through on each table
and if we jump up here to address
there's address audit there's address so
it comes in and it's going to print out
what is the
um
wait am i there yeah okay i am so if i
go here
so
so we have table address and you can see
here oops it's the one above here um
i do that i do the results
here okay
right yeah
so here's the address and we can see
each row we don't see it super well
formatted here but then we can see
here's where i'm starting to create my
sql so i can see create table address
which now i've got this piece
done
so next thing i want to do
is i want to come in and start using
some of this information so now what i
want to do is come in with each of these
let's move my quit
oh i didn't have my quick before so i'm
going to come through and for each
column
oh and i do a print column in there for
each column i printed the column
information which you can see here
let's do this um
now for each one this time i'm going to
come through
and i have a couple things to do is
first i'm going to create a column
um
[Music]
oh i didn't actually grab the name out
of that did i
so i'm going to add a column
right here so interesting i said oh here
we go
nope
oh here it is okay
so i come in and i want to see if there
is
uh and we can see here if it's primary
key so if it is i'm going to say that
this is
this name i'm going to get the name for
this
primary key
because it's going to be this column i'm
going to use that to create my primary
key
and we'll look at that a little bit next
time around but
now if column two is yes this is whether
or not is it can it be null or not
so i'm going to have to
either
create this with my column name space
[Music]
column one which is the type
and then null or i'm going to do it with
not null so that's going to create
this thing column name type and either
null or not null and then i'm going to
come in and i'm going to say if i have
that auto increment because otherwise
it's empty if it's auto increment then
i'm going to make it an auto increment
right now i'm not going to mess with my
defaults
so if i come through here oh and that's
going to wrap it up sort of and it's
going to say
what's my primary key which gives me
this right here i'm not going to worry
about foreign keys right now
so
let's do that so if i print whoop let's
print let's move that print down
and then do a quit
and we will see
that now
i have this
create table
which if i go look at
something over here
um i think i can do a format
format sql
there we go
so now
i'm creating i've come out of this and i
can do create table address
column type
now the problem is i've got this auto
increment coming up every time so i'm
not resetting that so here
um
i want to do it here i want to move that
pk in
oh i'm sorry
i need to do pk here
oops
uh
interesting i want it to be
if he's that then primary key equals
that what am i doing let's see
oh i did auto increment my mistake um
oh here it is i did that twice i don't
need it to be auto increment there
and if i look
uh let's see auto increment should be
uh
where did i put that there
did i put a column a comma after i do
not
so
if we look i probably don't have oh i do
have comments
so i had a comment somewhere in here
come on come on come on come on oh
that's what it is
sophie's auto increment
let's do this
let's just do this so if he has
automatic command otherwise he's just a
comma
and so now if i take that and look at it
and do my little create table
well
let's do it this way because that's
going to be pain
so if i take that
and then format it
then we can see now i've got create
table address i've got a column i've got
an increment i've got either null or not
null and i come all the way down and so
if i wanted to create this in local
database
testing i believe
oh
and it's going to ask me to connect and
so let's see if i run this real quick
oh it says table address already
exists um
oh i think because tutorial it does not
let's go look here
okay so he does an end tutorial so now
if i put it in tutorial instead
i'm going to see that this table is
going to exist
integer display oh so i don't really
need that but so it created the table
let's do a refresh
and now i can see here's my table
here's my original table
so now when i run this a second time
through
address will not be in there because
it's not going to find it missing
and so it moves on to
address audit so the next thing we're
going to do is we're going to want to
actually execute this sql
that sounds like a good time thing for
us to do next time around so
i'll let you get back to it
as always go out there and have yourself
a great day a great week and we will
talk to you
next time
you
Transcript Segments
0.43

[Music]

26.96

well hello and welcome back we are

29.199

continuing this uh basically it's a

31.279

database sync

32.8

walkthrough learning about sql doing a

35.12

little bit of a program along the way

37.92

so last time around

39.76

we were working on um

42.32

missing tables so comparing two

44.879

databases

46.32

and finding the tables that we're

48.16

missing

49.52

now what i want to do before we go a

51.28

little further into this one is i'm

53.039

going to actually

54.48

clean this up a little bit so i've got

56.16

my connect to database one and two

59.359

i've got those little functions

61.84

and what i'm gonna do is i'm gonna do

64.64

let's see so we had three things we're

66.159

doing

67.439

we're gonna do

70.56

let's see i don't need oh i do need that

75.04

well let's take this let's get

79.119

this whole thing

81.28

and this is gonna be

89.04

let's go ahead and do it like this is

90.56

we're going to do these uh so we've got

94.159

let's do source tables uh let's see

96.56

missing source

99.2

oops if i can type that right we do

101.28

missing source tables

104.96

and we're going to do

108.079

missing target tables

115.92

so we're going to clean this up a little

117.759

bit this will be a little easier to see

120.64

so missing source tables is going to be

123.759

this one

126.479

uh let's see

133.44

and let's do it this way so we're gonna

135.12

send it

139.599

uh source and target

143.92

and we're going to come in here and

145.68

let's see so we're going to get

150.64

let's see source tables and target

152.239

tables and so that's just going to be

153.76

here

157.76

so we're going to do new

159.76

tables

161.04

equals missing

165.04

source tables and then this is going to

167.44

be

169.04

source tables and target tables

174.319

whoops

176.48

i'm gonna do this mostly for readability

178.879

so i'm gonna come in here

180.48

and that's gonna be that guy

187.599

and let's do it this way

192.4

so i get that new tables

196.879

okay which equals that

199.599

and then here

202.08

let's do it this way new tables equals

203.84

that and then we're going to

207.519

return new tables

210.319

whoops

217.92

let's say

219.2

we're gonna call it missing tables

224.84

uh gonna go here

229.04

and then we're gonna come here

234.319

uh missing two there we go so we're

236.239

gonna do that

237.599

we're gonna basically do the same thing

240.72

and actually what we can do

244.319

is where did we do that um

250.72

we're going to take the same thing so

252

we're going to say missing tables

256

because we can

261.12

and now

262.079

because all we have to do is we have to

263.36

just reverse the two so

266.08

where do we do that let's see there's

268

missing tables

269.919

those are the tables that are not in the

271.28

source

273.759

and then these are the tables that are

275.199

not in

280.4

source tables there we go so we can

282.72

clean that up a little bit shrink it

284.479

down simplify a little bit

286.479

now

288.24

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

290.24

do a we're going to do what's going to

292.32

call create

296.479

missing table

299.6

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

301.28

going to give it a

304.08

uh let's see

306

oops this is going to be missing

312.24

so we're going to always do it in the

313.759

target let's do that well

316.24

yeah

317.6

let's think about that a second so let's

319.039

go ahead and do it so we can do it

320.16

either so we're going to do four

323.28

source which is going to be

325.6

uh dbc

329.68

um

332

in this case

333.36

oh no it's going to be actually in db2

338

so he's going to be db2

339.919

and we're going to give it the tables

344.96

and then we're going to come up here

347.44

well actually let's do it here because

348.88

we're going to do it

350.96

not what we're gonna do and then we'll

351.84

move it in a minute so we're gonna do

353.36

create missing tables

357.84

and we're going to give it

360.88

a controller

362.8

and a list uh we'll call it table list

371.28

and

373.12

so we have a dbc there we go

376.24

so let's take this whole

378.84

thing uh

380.319

right there

382.08

and let's oh we're gonna move this up

388

and then we're gonna talk about this a

389.28

little bit

393.52

okay so what we're doing here so now

395.36

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

396.479

look at

401.12

what

403.12

uh

404.24

okay let's go right up here so this is

406.639

basically what a create looks like which

409.199

we've seen this before it's not terribly

411.12

formatted but

412.639

essentially we want to be able to create

414.72

to

415.84

make this

418.24

uh sql query based on what we get back

421.52

if we look down here we're getting in

423.52

our rows that we get back when we do our

427.039

described table

429.12

we get a column name

430.8

a type

432.24

whether it is nullable or not

435.039

if it's got a whether it's primary key

437.199

or not

438.4

there is a default value and then

440.4

there's uh whether it's auto increment

442.24

or not

444.16

so

445.12

first thing we want to do

446.639

when we create the first thing we're

448.08

going to do is we're always going to

449.039

have create table

451.759

and then we're going to give it the

452.72

table name so as we're spinning through

454.88

our list

456.8

we're going to

458.8

uh let me do it this way

461.52

let's sort of look at it as we're going

462.8

through it so if we're going to come

463.599

through here create a missing table

467.36

print the table

468.879

uh let's see

470.639

why do i need describe query i need it

472.72

oh because i'm going to do it for

474.879

each column

478.08

so let's do it here um

481.039

let's take that query

483.599

dot

484.479

dot

485.44

there

486.4

okay

488.16

so i'm going to do this for my table

490.4

name and then the first thing i want to

492

do is i'm going to create table whatever

494.96

so let's just look at that right now

500.08

and we'll see that we are creating that

504.479

so if we run

508.24

oops i've got a key equals oops

512

um

512.559

[Music]

514.159

p k what is p key o is equal to that

517.919

oh i forgot this

523.919

let's see if that's gonna work there we

525.12

go okay

527.12

so

528.08

these tables are not sourced these table

529.6

are not in the target so let's see what

530.959

did i get wrong here so

533.36

source and target

535.36

oh i didn't flip those two i bet

539.279

uh

540.08

here we go they're not in the source

548.56

so that's missing equals that new tables

550.959

equals that so he's not doing that one

557.36

uh oh

559.12

i need to send it

564.959

wait is that right okay so i do source

566.72

tables

568

and i get target tip let's do look at

569.68

that

572.56

so

573.6

i missed something here so there's my

575.279

source tables here's my target tables

580.56

and if i look

582.959

so there's source

585.76

and then target is empty so it should be

588.48

source tables

594.959

missing tables okay sources oh

597.6

that's why

599.04

let's do it this way source and target

602.72

uh source

607.12

and target

610.88

there we go so now

614.16

okay so what uh tutorial a doesn't exist

617.2

okay

618.48

so

629.76

interesting

631.2

i didn't want oh because this is my list

639.36

let's try that

646.32

uh

647.76

let's see so there's address

651.12

okay tutorial that address doesn't exist

656.32

oh because i need to do it from the

659.04

source so here i need

662.64

uh so i need to get the source one which

664.56

is

665.44

dbc

668.24

yeah

670.24

that's a whoops so here

674.8

let's see dbc2

676.8

dbc1

679.44

so what i would need to do here is i'm

681.2

going to need two cursors probably

684.56

let's do cursor

686.32

let's do it this way

687.68

cursor source

689.44

and cursor destination

692.32

and dbc1 is my source dbc2 is my

696.16

destination

698.72

and now what i need to do is so i'm

700.399

going to go in and i'm going to describe

702.8

so i so i come into each table i get my

705.279

name

706.32

and then i'm going to do describe so i

707.76

can get all of the details about that

711.04

um and so he's going to come in and he's

713.36

going to do a

715.279

describe on the source

723.44

and so if i do let's do print

729.44

let's do the results

733.04

and now let's look at it let's see

735.12

if we can get it there we go so what's

736.639

going to come through

739.279

as it comes through on each table

742.839

and if we jump up here to address

746.639

there's address audit there's address so

748.8

it comes in and it's going to print out

751.04

what is the

752.88

um

755.2

wait am i there yeah okay i am so if i

757.12

go here

758.72

so

759.839

so we have table address and you can see

762

here oops it's the one above here um

767.2

i do that i do the results

771.6

here okay

773.519

right yeah

774.8

so here's the address and we can see

776.399

each row we don't see it super well

778.56

formatted here but then we can see

780.399

here's where i'm starting to create my

782.88

sql so i can see create table address

784.959

which now i've got this piece

787.68

done

788.959

so next thing i want to do

790.959

is i want to come in and start using

793.12

some of this information so now what i

794.88

want to do is come in with each of these

797.2

let's move my quit

802.079

oh i didn't have my quick before so i'm

803.519

going to come through and for each

804.959

column

807.6

oh and i do a print column in there for

809.2

each column i printed the column

810.72

information which you can see here

814.16

let's do this um

817.279

now for each one this time i'm going to

818.72

come through

820.56

and i have a couple things to do is

822

first i'm going to create a column

824.88

um

825.35

[Music]

832.32

oh i didn't actually grab the name out

834

of that did i

837.519

so i'm going to add a column

839.519

right here so interesting i said oh here

841.44

we go

842.56

nope

846.16

oh here it is okay

848.72

so i come in and i want to see if there

850.959

is

852.24

uh and we can see here if it's primary

854.8

key so if it is i'm going to say that

857.519

this is

860.079

this name i'm going to get the name for

862

this

862.8

primary key

864.079

because it's going to be this column i'm

865.199

going to use that to create my primary

867.12

key

868.56

and we'll look at that a little bit next

869.839

time around but

871.279

now if column two is yes this is whether

873.199

or not is it can it be null or not

875.68

so i'm going to have to

878.24

either

880.16

create this with my column name space

883.87

[Music]

885.199

column one which is the type

888.48

and then null or i'm going to do it with

890.639

not null so that's going to create

893.279

this thing column name type and either

896

null or not null and then i'm going to

898.399

come in and i'm going to say if i have

899.44

that auto increment because otherwise

900.959

it's empty if it's auto increment then

903.199

i'm going to make it an auto increment

904.8

right now i'm not going to mess with my

906.72

defaults

908.16

so if i come through here oh and that's

910.079

going to wrap it up sort of and it's

912

going to say

913.36

what's my primary key which gives me

917.199

this right here i'm not going to worry

918.88

about foreign keys right now

921.44

so

922.24

let's do that so if i print whoop let's

924.24

print let's move that print down

927.279

and then do a quit

932.079

and we will see

933.36

that now

934.8

i have this

936.8

create table

938.24

which if i go look at

940.48

something over here

946.079

um i think i can do a format

953.12

format sql

955.519

there we go

956.8

so now

959.04

i'm creating i've come out of this and i

960.56

can do create table address

963.279

column type

965.759

now the problem is i've got this auto

967.199

increment coming up every time so i'm

968.639

not resetting that so here

971.759

um

973.36

i want to do it here i want to move that

975.12

pk in

978.16

oh i'm sorry

979.6

i need to do pk here

985.36

oops

986.399

uh

993.44

interesting i want it to be

999.519

if he's that then primary key equals

1001.279

that what am i doing let's see

1003.6

oh i did auto increment my mistake um

1008.32

oh here it is i did that twice i don't

1011.36

need it to be auto increment there

1014.56

and if i look

1017.759

uh let's see auto increment should be

1024.88

uh

1025.76

where did i put that there

1032.559

did i put a column a comma after i do

1036.24

not

1037.679

so

1038.64

if we look i probably don't have oh i do

1040.24

have comments

1041.439

so i had a comment somewhere in here

1042.959

come on come on come on come on oh

1046.88

that's what it is

1050.48

sophie's auto increment

1054.559

let's do this

1061.36

let's just do this so if he has

1062.88

automatic command otherwise he's just a

1064.559

comma

1067.2

and so now if i take that and look at it

1069.6

and do my little create table

1076.16

well

1077.28

let's do it this way because that's

1078.96

going to be pain

1080.72

so if i take that

1087.36

and then format it

1094

then we can see now i've got create

1095.36

table address i've got a column i've got

1097.919

an increment i've got either null or not

1099.76

null and i come all the way down and so

1101.76

if i wanted to create this in local

1104.799

database

1106.08

testing i believe

1110.96

oh

1113.44

and it's going to ask me to connect and

1115.76

so let's see if i run this real quick

1118.72

oh it says table address already

1120.84

exists um

1125.2

oh i think because tutorial it does not

1127.12

let's go look here

1128.48

okay so he does an end tutorial so now

1133.039

if i put it in tutorial instead

1137.36

i'm going to see that this table is

1138.72

going to exist

1144.64

integer display oh so i don't really

1146.96

need that but so it created the table

1151.76

let's do a refresh

1155.6

and now i can see here's my table

1158.08

here's my original table

1161.679

so now when i run this a second time

1164.4

through

1166.08

address will not be in there because

1167.52

it's not going to find it missing

1169.6

and so it moves on to

1171.679

address audit so the next thing we're

1173.679

going to do is we're going to want to

1174.48

actually execute this sql

1177.679

that sounds like a good time thing for

1179.28

us to do next time around so

1181.52

i'll let you get back to it

1183.6

as always go out there and have yourself

1184.96

a great day a great week and we will

1187.36

talk to you

1188.88

next time

1205.84

you