473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Compare Tables Between Databases


Hi,

I have recently migrated MSAccess Tables (nearly 120) to MySQL. To test
successful migration I want to compare all tables. I have linked MySQL
tables through ODBC connection.

I want to create a report that compares all records between all tables in
both databases. I am looking for a report with differences in number of
records and differences in fields if 2 records are different. Probabaly it
can be acheived through query, but I couldn't find any help on queried
between two databases.

Thanks for your help. Any pointers to help me create a report in MS Access
will be greatful.

Thanks,
Prakash.
Nov 13 '05 #1
1 4167
This is quite common problem of comparing two databases, not only when
they should be equal but also when they are slightly different, for
example, when you replicate some big database.

If you have primary or unique keys in all the tables, then you can
construct the SQL queries to show the differences.

First you link the tables of both databases into some Access MDB. For
example, with the name A_orig you link to table A in original Access
MDB, while with the name A_copy you link to your MySQL copy of the
table A over ODBC. Suppose that the table A has two-column unique key
on c1, c2 and some other columns c3, c4, ...

Then you can construct three queries, one for the diffenreces in
fields for the rows that exist in both tables, one to show the rows
that exist only in original table and one to show the rows that exist
only in copy.

First query would look like:

SELECT O.c1, O.c2, O.c3, O.c4, ..., C.c3, C.c4, ...
FROM A_orig O INNER JOIN A_copy C ON O.c1 = C.c1 AND O.c2 = C.c2
WHERE O.c3 <> C.c3 OR O.c4 <> C.c4 OR ...

Second query would look like:

SELECT O.c1, O.c2, O.c3, O.c4, ..., C.c3, C.c4, ...
FROM A_orig O LEFT JOIN A_copy C ON O.c1 = C.c1 AND O.c2 = C.c2
WHERE C.c1 IS NULL;

Third query would look like:

SELECT O.c1, O.c2, O.c3, O.c4, ..., C.c3, C.c4, ...
FROM A_orig O RIGHT JOIN A_copy C ON O.c1 = C.c1 AND O.c2 = C.c2
WHERE O.c1 IS NULL;

These three queries you can put into a union query and make
an Access report on it, if you want.

Considering that you have quite a lot of tables, you can write
the generating procedure, to automatically create all the above
queries for all the 120 tables. The sketch of the generating
code would be like this:

Sub Gen_compare()
Dim db As Database
Set db = CodeDb()
Dim td As TableDef
Set td = db.TableDefs
Dim ix As Index
Dim f As Field
For Each td In db.TableDefs
If td.Name Like "*_orig" Then
For Each ix In td.Indexes
If ix.Primary Then Exit For
Next i
Dim sName_root As String 'the initial part of the table name,
' without _orig

sName_root = Left(td.Name, Len(td.Name) - Len("_orig"))
Dim sSelect As String
Dim sFrom As String
Dim sWhere As String
sSelect = "SELECT"
sFrom = "FROM " & sName_root & "_orig O INNER JOIN " _
& sName_root & "_copy C ON"
sWhere = "WHERE"

Dim n As Integer 'to know when delimiter

n = 0
For Each f In ix.Fields
n = n + 1
If n > 1 Then
sFrom = sFrom & " AND"
End If
sFrom = sFrom & " O." & f.Name & " = C." & f.Name
Next f

n = 0
For Each f In td.Fields
n = n + 1
If n > 1 Then
sSelect = sSelect & " ,"
sWhere = sWhere & " OR"
End If
sSelect = sSelect & " O." & f.Name & ", C." & f.Name
sWhere = sWhere & " O." & f.Name & " <> C." & f.Name
Next f

End If
Dim qd As QueryDef
Set qd = db.CreateQueryDef(sName_root & "_inner", _
sSelect & vbCrLf & sFrom & vbCrLf & sWhere)
db.QueryDefs.Append qd
db.QueryDefs.Refresh
Next td
End Sub

Maks Romih

Prakash RudraRaju <pr*****@ece.arizona.edu> writes:
Hi,

I have recently migrated MSAccess Tables (nearly 120) to MySQL. To
test successful migration I want to compare all tables. I have linked
MySQL tables through ODBC connection.

I want to create a report that compares all records between all tables
in both databases. I am looking for a report with differences in
number of
records and differences in fields if 2 records are
different. Probabaly it can be acheived through query, but I couldn't
find any help on queried between two databases.

Thanks for your help. Any pointers to help me create a report in MS
Access will be greatful.

Thanks,
Prakash.

Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Cameron | last post by:
Using SQL data compare i get the following error message: Could not allocate space for object '(SYSTEM table id: -701903460)' in database 'TEMPDB' because the 'DEFAULT' filegroup is full. The...
3
by: Shock | last post by:
Hey all, I am currently researching ways to compare databases via an XSD schema. I wrote a small app that creates a dataset from a database and exports that dataset to XSD. This gives me an XSD...
3
by: Stephen | last post by:
I have to write a .Net application which can compare SQL Databases including things like: - DB structure, PK's, FK's, indexes and types of indexes i.e. should be able to detect if the same index...
8
by: Vincent | last post by:
has any one seen a program to compare mdbs'. I have ran into a few of them, but none seem to really do that job. Basically what I need to do is, take 2 access mdb's and check the differences...
1
by: Stephen | last post by:
I am trying to compare the tables in two similar databases using the SQLDMO object. I am able to use this object to access different SQL servers and choose two different databases. The versions of...
1
by: shanthimanooj | last post by:
hi i need the code in qtp Compare two different Databases .that means comp two schemas no of tables,no of colums in a tables,no of rows in a table,
1
by: chungiemo | last post by:
Hi thought I would do another thread as this one is a bit different from the previous problem I am looking for a solution to the relating problem Comparing 2 access databases with 2 tables,...
13
by: Charlotte | last post by:
Hi, i've googeled to find a asp-script that can compare all the records in two different access databases the mdb's have exactly the same tables what i want is that (the output) all the...
3
by: alter.fire | last post by:
Hey im trying to do the following in PHP: Connect to 2 different databases, one named complete and one incomplete. My goal is to find whats missing in the "incomplete" database. The fields...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.