2011-08-10 50 views
5

嘗試使用在MySQL服務器上運行SQL查詢的PHP腳本將數據庫複製到新數據庫中。我有SOFAR是代碼:在MySQL中複製數據庫,如何複製視圖?

$dbh->exec("CREATE DATABASE IF NOT EXISTS $new_news CHARACTER SET UTF8;"); 
$results = $dbh->query("SHOW TABLES FROM $old_news"); 
$table_list = $results->fetchAll(PDO::FETCH_NUM); 

foreach($table_list as $table_row){ 
    foreach($table_row as $table){ 
     $results = $dbh->query("SELECT table_type FROM information_schema.tables where table_schema = '$old_news' and table_name = '$table'"); 
     $table_type = $results->fetch(PDO::FETCH_ASSOC); 
     $table_type = $table_type['table_type']; 
     if($table_type == 'BASE TABLE'){ 
      echo "Creating table $table and populating...\n"; 
      $dbh->exec("CREATE TABLE $new_news.$table LIKE $old_news.$table"); 
      $dbh->exec("INSERT INTO $new_news.$table SELECT * FROM $old_news.$table"); 
     }else if($table_type == 'VIEW'){ 
      //echo "Creating view $table...\n"; 
      //$dbh->exec("CREATE VIEW $new_news.$table LIKE $old_news.$table"); 
      echo "$table is a view, which cannot be copied atm\n"; 
     }else{ 
      echo "Skipping $table_type $table, unsupported type\n"; 
     } 
    } 
} 

這目前看起來在$ old_news所有表,發現在INFORMATION_SCHEMA表型,並在$ new_news根據類型創建一個相同的表。對於表格,它會創建相同的表格結構,然後'INSERT INTO SELECT'來填充它們。

如何在不使用mysqldump整個數據庫的情況下複製視圖?

回答