在MySQL中复制数据库,如何复制视图?

Rip*_*or 6 php mysql database backup view

尝试使用在MySQL服务器上运行SQL查询的PHP脚本将数据库复制到新数据库中.我所熟悉的代码是:

$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"; 
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这当前查看$ old_news中的所有表,在information_schema中查找表类型,并根据类型在$ new_news中创建相同的表.对于表,它创建表结构相同,然后'INSERT INTO SELECT'来填充它们.

如何在没有mysqldump的情况下复制整个数据库的视图?

Mat*_*hen 5

您可以使用INFORMATION_SCHEMA.VIEWS查看视图定义,或使用SHOW CREATE VIEW查看整个语句.