如何复制一个mysql表,添加一些列和删除一些列,改变顺序并保留与新表相关的任何数据


how to make a copy of a mysql table, adding some columns and taking some out, changing the order and retaining any data relevant to the new table?

我希望wordpress用户能够改变数据库表的结构,同时保留未更改列中的数据。

到目前为止,我的策略是:

    创建一个名为table_temp的新表,并使用新的结构。
  1. 对于存在于新表中的旧表中的每个列,循环遍历旧表的行并将值插入到新表中表列。
  2. 删除旧表并将新表重命名为旧表表。

步骤2是我出错的地方,所以我还没有打扰步骤3。第一列可以正常工作,但是插入到第二个保留列中的值从插入到第一个列中的最后一个值后面的行开始。

function update_table($_POST) {
global $wpdb;
//collect the updated table's metadata from the $_POST

$slug = $_POST[slug].'_temp';
$WB_table = $_POST[slug];
unset($_POST[slug]);
unset($_POST[tblAppendGrid_rowOrder]);
unset($_POST[status]);
// create the new temp table
$wpdb->query("
CREATE TABLE $slug (
     id INT (6) NOT NULL AUTO_INCREMENT,
     PRIMARY KEY (id)
     ) ENGINE=MyISAM;
");
//remove reference to the old meta in WB_table _meta
$wpdb->delete( 'WB_table_meta', array( 'WB_table' => $WB_table ) );
// add the new meta to WB_table _meta
// loop through the $_POST array
for($i=1;$i<=count($_POST)/5;$i++) {
// pull the array elemnts from $_POST
$name = $_POST['tblAppendGrid_name_'.$i];
$display = $_POST['tblAppendGrid_display_'.$i];
$type = $_POST['tblAppendGrid_type_'.$i];
$maxlength = $_POST['tblAppendGrid_maxlength_'.$i];
$width = $_POST['tblAppendGrid_width_'.$i];
// Add column names to the new table
$wpdb->query("ALTER TABLE ".$slug." ADD ".$name." text(160)");
// Add meta to WB_table_meta
$wpdb->insert('WB_table_meta',
               array(
                     'WB_table' => $WB_table,
                     'name' => $name,
                     'display' => $display,
                     'type' => $type,
                     'maxlength' => $maxlength,
                     'width' => $width),
               array('%s','%s','%s','%s','%s','%d','%d')
               );
//copy data to the new table from the old table if the column exists in the new table
if ($wpdb->query("SELECT ".$name." from ".$WB_table."", A_ARRAY) == true) {
     $rows = $wpdb->get_col("SELECT ".$name." from ".$WB_table."");
        foreach($rows as $row){
        $wpdb->query("INSERT INTO $slug ($name) VALUES ('$row')");
        $wpdb->show_errors();
        } //for each
} // if
} // for loop
} // function update_table
创建的新表如下所示,但是它应该只有两行。
14  kitchen NULL    NULL    NULL    NULL    NULL
15  kitchen NULL    NULL    NULL    NULL    NULL
16  NULL    laundry NULL    NULL    NULL    NULL
17  NULL    laundry NULL    NULL    NULL    NULL
18  NULL    NULL    office  NULL    NULL    NULL
19  NULL    NULL    office  NULL    NULL    NULL
20  NULL    NULL    NULL    8:00    NULL    NULL
21  NULL    NULL    NULL    8:00    NULL    NULL
22  NULL    NULL    NULL    NULL    8:10    NULL
23  NULL    NULL    NULL    NULL    8:10    NULL
24  NULL    NULL    NULL    NULL    NULL    8:15
25  NULL    NULL    NULL    NULL    NULL    8:15

谢谢VMai。你的建议很有效。下面的代码生成了一个包含正确字段的新表。

// build the column string
if ($wpdb->query("SELECT ".$name." from ".$WB_table."", A_ARRAY) == true) {
     $cols .= $name.',';
     } // if
} // for loop

$col = substr($cols, 0, -1);
//copy data to the new table from the old table if the column exists in the new table
$wpdb->query("
     INSERT INTO ".$slug."
     (".$col.")
     SELECT ".$col."
     FROM ".$WB_table."
     ");
     $wpdb->show_errors();