【问题标题】:Copying a database in MySQL, how to copy views?在 MySQL 中复制数据库,如何复制视图?
【发布时间】:2011-10-24 05:48:13
【问题描述】:

尝试使用在 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"; 
        }
    }
}

这当前查看 $old_news 中的所有表,在 information_schema 中找到表类型,并根据类型在 $new_news 中创建一个相同的表。对于表,它会创建相同的表结构,然后使用“INSERT INTO SELECT”来填充它们。

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

【问题讨论】:

    标签: php mysql database view backup


    【解决方案1】:

    以下代码对我有用。它将所有视图从 source_database 复制到 target_database

    $link = mysql_connect('hostname', 'user', 'password',false,128) or die(mysql_error());
    mysql_select_db('source_database') or die(mysql_error());
    
    $qry=mysql_query("SHOW FULL TABLES IN source_database WHERE TABLE_TYPE LIKE 'VIEW'");
    
    while ($rows= mysql_fetch_object($qry)){
            $view_name=$rows->Tables_in_techdb_beta;
            $select_view=mysql_query("SHOW CREATE VIEW ".$view_name);
            while ($view_rows= mysql_fetch_object($select_view)){
                foreach($view_rows as $key=>$val){
                    if ($key=='Create View'){
                        $views_array[]=$val;
                    }
                }
            }
    }
    //echo "<pre>"; print_r($views_array);
    $link2 = mysql_connect('hostname', 'user', 'password',false,128) or die(mysql_error());
    mysql_select_db('target_database') or die(mysql_error());
    ini_set('MAX_EXECUTION_TIME', -1);
    foreach($views_array as $key => $val){
        $sql=trim($val);
        mysql_query($sql);
    }
    

    【讨论】:

      【解决方案2】:

      一种选择是为每个返回视图创建语法的视图使用SHOW CREATE VIEW

      详情请参阅12.4.5.10. SHOW CREATE VIEW Syntax

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-09-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-30
          • 1970-01-01
          • 2015-05-26
          相关资源
          最近更新 更多