2012-10-01 31 views
1

我有2个数据库,只有DB1中的1个表和DB2中的2个表。 DB1.table1中的每条记录都被分割并分别存储在DB1.table1和DB @ .table2中。测试2个mysql数据库之间的内容

For example, DB1 has a table1 which looks like 

     Student_Name Id Address Attendance Marks 
     ------------ -- ------- ---------- ----- 
     John   1 90th st  70   90 

The records that are transferred from DB1.table1 are stored in DB2.table and DB2.table2 in the following manner 

DB2.table 1: Id Student_Name Address 
      -- ------------ ------- 
      1  John   90th st 


DB2.table 2: Id Attendance Marks 
      -- ---------- ----- 
      1  70   90 

我想写一个测试用例来确保所有来自DB1的数据都被复制到DB2。我已经写了一些查询来确定来自DB1的记录是否未复制到DB2。除了找出遗漏的记录之外,我还想检查每个记录的逐列,以确保DB1和DB2中的值相同。

从上面的例子,我想检查ID = 1,如果DB2.table1 Student_name = DB1.table1 Student_name,DB2.table1地址= DB1.table1地址,等等..

如果我有1000列?我应该写一个长脚本来检查每一列吗?没有。做这种测试的最好方法是什么?有没有我可以使用的工具,或者我应该写下脚本?

+0

您正在寻找数据库复制? – JvdBerg

+0

no ..我想测试每个值是否被正确复制 – ben

回答

0

这将找到db1.table1中没有匹配的db2.table1db2.table2中的任何Id行。

它假定列在两个表中都被命名为相同,并且任何存在db2.table1db2.table2的列在db1.table1中都应具有匹配的列名称。因此,如果db2.table2有一列名为Foo的列,则db1.table1也必须有一列名为Foo的列。如果db2.table1具有名为Bar的列,则db1.table1还必须具有名为Bar的列。如果该列存在于db2而不是db1中,则会出现MySQL错误。

希望这是你正在寻找的!

header("Content-type: text/plain"); 

// connect with mysqli 

// get a list of columns in db2.table1 and db2.table2 
$columns = array(); 
$query = mysqli_query("SELECT table_name, column_name FROM information_schema.columns WHERE table_schema = 'db2' AND table_name IN ('table1', 'table2')"); 
while ($row = $mysqli_fetch_assoc($query)) { 
    $columns[$row["table_name"]][] = "db1.table1.{$row["column_name"]} = db2.{$row["table_name"]}.{$row["column_name"]}"; 
} 

$query = mysqli_query(" 
    SELECT db1.table1.Id 
    FROM 
     db1.table1 
     LEFT JOIN db2.table1 
      ON ". implode(" AND ", $columns["table1"]) ." 
     LEFT JOIN db2.table2 
      ON ". implode(" AND ", $columns["table2"]) ." 
    WHERE 
     db2.table1.Id IS NULL 
     OR db2.table2.Id IS NULL 
"); 

while (list($id) = mysqli_fetch_row($query)) { 
    echo "ID {$id} does not match\n"; 
} 
+0

而尝试你的脚本我遇到了一个常见的错误'警告:mysql_fetch_row()期望参数1是资源,布尔给定在... 。“但是可以解决它。我看到$查询已经执行,仍然是错误。 – ben

+0

在这两个'mysqi_query'行上,在分号前加':or die(mysqli_error())'。这会告诉你它得到的错误。我没有真正能够测试获取列的第一个查询,结果我无法真正测试第二个查询,并在其中包含implode内容。获得错误应该指向正确的方向。 – Travesty3

0

您可以用UNION ALL和GROUP BY做到这一点:这个查询的

select Student_Name, Id, Address, Attendance, Marks, 
     (case when max(which) = 'db1' then 'Missing in db2' 
      when min(which) = 'db2' then 'Missing in db1' 
     end) as why 
from ((select 'db1' as which, Student_Name, Id, Address, Attendance, Marks 
     from db1.table1 t 
    ) union all 
     (select 'db2' as which, t1.Student_Name, t1.id, t1.Address, t2.Attendance, t2.Marks 
     from db2.table1 t1 join 
      db2.table2 t2 
      on t1.id = t2.id 
    ) 
    ) u 
group by Student_Name, Id, Address, Attendance, Marks 
having count(distinct which) = 1 
0

结果会给你的DB2是从DB1不同的行数,这也算,如果行没有按不存在,但它应该。如果结果是0,一切都很好。

SELECT 
    COUNT(*) AS difference 
FROM 
    DB1.table1 AS d1t1 
    LEFT JOIN DB2.table1 AS d2t1 
     USING (Id) 
    LEFT JOIN DB2.table2 AS d2t2 
     USING (Id) 
WHERE 
    (d1t1.Student_Name <> d2t1.Student_Name) 
    OR (d1t1.Address <> d2t1.Address) 
    OR (d1t1.Attendace <> d2t2.Attendace) 
    OR (d1t1.Marks <> d2t2.Marks) 
相关问题