简体   繁体   中英

mysql backup and restore to a table with different column names

I have a table - customers_old . I want to copy all the data from this table, so I will use mysqldump.

I need to restore data into a table , customers_new, which has column names different from customers_old table.

How do I do this?

INSERT..SELECT is only for manually copying each row?

Kindly advise

Thanks

To expand on PMV's answer:

INSERT INTO newtable (column1_new, column2_new, ...) 
  SELECT column1_old, column2_old FROM oldtable

You can do it with a simple ctas (Create Table AS) operation:

CREATE TABLE customers_new AS SELECT col1 AS new_col1, col2 AS new_col2 FROM customers_old

By aliasing your old columns you can change the column names in the new table.

see: http://dev.mysql.com/doc/refman/5.5/en/create-table-select.html

您可以在sql转储文件中编辑列名和表名,或者导入新表,然后从旧表执行INSERT SELECT。

You can't do this directly, unless you manually hack up the dump file to change the column names in there. One option would be to load the dump into a temporary database/table and then do some ALTER TABLE queries to rename the columns to whatever you want. Or you could do the INSERT/SELECT, but the you'd end up with double the data (new renamed table/fields + old original table/fields).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM