MySQLサーバでSQLクエリを実行するPHPスクリプトを使用して、データベースを新しいデータベースにコピーしようとしています。私はSOFAR IS持つコード: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せずにビューをコピーしますか?