2013-02-18 2 views
1

두 테이블 aa_history을 가지고 있는데, a이 변경되었거나, 열이 제거 또는 추가 또는 수정 된 경우 어떻게 결정할 수 있습니까? a_history으로 변경하십시오.두 표 사이의 차이점을 얻으시겠습니까?

a_history 당신을 "코드"a에서 int를 변경하는 경우, 다음 a_history는 "코드"VARCHAR

에게 있어야합니다 당신이 a에 "코드"VARCHAR를 추가 예를 들어,

a = (id,name,status) 

a_history = (id,name,status,id_history,operation,date_op,...) 

의 기록을 유지, a_history은 "코드"가 있어야합니다 intvarchar

+0

보기는 –

+1

'TRIGGER's을 information_schema.columns' ..... –

답변

1

다음은이 코드를 비교하기 위해 사용하는 스크립트입니다. 두 MySQL 데이터베이스의 구조 : 테이블`에서

<?php 
//------------------------------------------------------------------------------ 
// Define the variables we'll be using. 
//------------------------------------------------------------------------------ 
$db1_con = NULL; 
$db1_constraints = array(); 
$db1_dbname = 'db1'; 
$db1_host = 'localhost'; 
$db1_password = 'password1'; 
$db1_tables = array(); 
$db1_username = 'username1'; 

$db2_con = NULL; 
$db2_constraints = array(); 
$db2_dbname = 'db2'; 
$db2_host = '123.123.123.123'; 
$db2_password = 'password2'; 
$db2_tables = array(); 
$db2_username = 'username2'; 

//------------------------------------------------------------------------------ 
// Connect to the databases. 
//------------------------------------------------------------------------------ 
try{ 
    $db1_con = new PDO("mysql:host=$db1_host;dbname=information_schema", $db1_username, $db1_password); 
    $db1_con->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE); // Try to use the driver's native prepared statements. 
    $db1_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Let's use exceptions so we can try/catch errors. 
}catch(PDOException $e){ 
    echo "<p>Connection failed for $db1_host: " . $e->getMessage() . '</p>'; 
    exit; 
} 

try{ 
    $db2_con = new PDO("mysql:host=$db2_host;dbname=information_schema", $db2_username, $db2_password); 
    $db2_con->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE); // Try to use the driver's native prepared statements. 
    $db2_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Let's use exceptions so we can try/catch errors. 
}catch(PDOException $e){ 
    echo "<p>Connection failed for $db2_host: " . $e->getMessage() . '</p>'; 
    exit; 
} 

if (NULL !== $db1_con && NULL !== $db2_con){ 
    echo "<h2>Column Analysis</h2>"; 
    $sql = 'SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ? ORDER BY TABLE_NAME, ORDINAL_POSITION'; 
    $statement1 = $db1_con->prepare($sql); 
    $statement1->bindValue(1, $db1_dbname); 

    $statement2 = $db2_con->prepare($sql); 
    $statement2->bindValue(1, $db2_dbname); 

    if (TRUE === $statement1->execute()){ 
     while ($row = $statement1->fetch(PDO::FETCH_ASSOC)){ 
      $db1_tables[$row['TABLE_NAME']][$row['COLUMN_NAME']] = array(); 
      foreach ($row AS $key => $value){ 
       $db1_tables[$row['TABLE_NAME']][$row['COLUMN_NAME']][$key] = $value; 
      } 
     } 
    } 

    if (TRUE === $statement2->execute()){ 
     while ($row = $statement2->fetch(PDO::FETCH_ASSOC)){ 
      $db2_tables[$row['TABLE_NAME']][$row['COLUMN_NAME']] = array(); 
      foreach ($row AS $key => $value){ 
       $db2_tables[$row['TABLE_NAME']][$row['COLUMN_NAME']][$key] = $value; 
      } 
     } 
    } 

    foreach ($db1_tables AS $table => $info){ 
     if (!isset($db2_tables[$table])){ 
      echo "<p>Table <strong>$table</strong> does not exist in the SECOND database!</p>"; 
     }else{ 
      foreach ($info AS $column => $data){ 
       if (!isset($db2_tables[$table][$column])){ 
        echo "<p>Column <strong>$column</strong> does not exist in table <strong>$table</strong> in the SECOND database!</p>"; 
       }else{ 
        if (count($data)){ 
         foreach ($data AS $key => $value){ 
          if ($db1_tables[$table][$column][$key] !== $db2_tables[$table][$column][$key]){ 
           echo "<p>Column <strong>$column</strong> in table <strong>$table</strong> has differing characteristics for <strong>$key</strong> (". $db1_tables[$table][$column][$key] ." vs. ". $db2_tables[$table][$column][$key] .")</p>"; 
          } 
         } 
        } 
       } 
      } 
     } 
    } 

    foreach ($db2_tables AS $table => $info){ 
     if (!isset($db1_tables[$table])){ 
      echo "<p>Table <strong>$table</strong> does not exist in the FIRST database!</p>"; 
     }else{ 
      foreach ($info AS $column => $data){ 
       if (!isset($db1_tables[$table][$column])){ 
        echo "<p>Column <strong>$column</strong> does not exist in table <strong>$table</strong> in the FIRST database!</p>"; 
       }else{ 
        if (count($data)){ 
         foreach ($data AS $key => $value){ 
          if ($db2_tables[$table][$column][$key] !== $db1_tables[$table][$column][$key]){ 
           echo "<p>Column <strong>$column</strong> in table <strong>$table</strong> has differing characteristics for <strong>$key</strong> (". $db2_tables[$table][$column][$key] ." vs. ". $db1_tables[$table][$column][$key] .")</p>"; 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
    echo "<h2>Constraint Analysis</h2>"; 

    $sql = 'SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = ? ORDER BY TABLE_NAME, ORDINAL_POSITION'; 
    $statement1 = $db1_con->prepare($sql); 
    $statement1->bindValue(1, $db1_dbname); 

    $statement2 = $db2_con->prepare($sql); 
    $statement2->bindValue(1, $db2_dbname); 

    if (TRUE === $statement1->execute()){ 
     while ($row = $statement1->fetch(PDO::FETCH_ASSOC)){ 
      foreach ($row AS $key => $value){ 
       $db1_constraints[$row['TABLE_NAME']][$row['COLUMN_NAME']][$key] = $value; 
      } 
     } 
    } 

    if (TRUE === $statement2->execute()){ 
     while ($row = $statement2->fetch(PDO::FETCH_ASSOC)){ 
      foreach ($row AS $key => $value){ 
       $db2_constraints[$row['TABLE_NAME']][$row['COLUMN_NAME']][$key] = $value; 
      } 
     } 
    } 

    foreach ($db1_constraints AS $table => $info){ 
     foreach ($info AS $column => $data){ 
      if (isset($db2_constraints[$table][$column])){ 
       if (count($data)){ 
        foreach ($data AS $key => $value){ 
         if ('CONSTRAINT_NAME' !== $key && $db1_constraints[$table][$column][$key] !== $db2_constraints[$table][$column][$key]){ 
          echo "<p>Column <strong>$column</strong> in table <strong>$table</strong> has differing characteristics for <strong>$key</strong> (". $db1_constraints[$table][$column][$key] ." vs. ". $db2_constraints[$table][$column][$key] .")</p>"; 
         } 
        } 
       } 
      }else{ 
       echo "<p>Column <strong>$column</strong> in table <strong>$table</strong> is missing a constraint in the SECOND database!</p>"; 
      } 
     } 
    } 

    foreach ($db2_constraints AS $table => $info){ 
     foreach ($info AS $column => $data){ 
      if (isset($db1_constraints[$table][$column])){ 
       if (count($data)){ 
        foreach ($data AS $key => $value){ 
         if ('CONSTRAINT_NAME' !== $key && $db2_constraints[$table][$column][$key] !== $db1_constraints[$table][$column][$key]){ 
          echo "<p>Column <strong>$column</strong> in table <strong>$table</strong> has differing characteristics for <strong>$key</strong> (". $db2_constraints[$table][$column][$key] ." vs. ". $db1_constraints[$table][$column][$key] .")</p>"; 
         } 
        } 
       } 
      }else{ 
       echo "<p>Column <strong>$column</strong> in table <strong>$table</strong> is missing a constraint in the FIRST database!</p>"; 
      } 
     } 
    } 
} 
?> 
관련 문제