简体   繁体   English

mysql备份并还原到具有不同列名的表

[英]mysql backup and restore to a table with different column names

I have a table - customers_old . 我有一张桌子-customers_old。 I want to copy all the data from this table, so I will use mysqldump. 我想复制该表中的所有数据,因此将使用mysqldump。

I need to restore data into a table , customers_new, which has column names different from customers_old table. 我需要将数据还原到表customers_new中,该表的列名与customers_old表不同。

How do I do this? 我该怎么做呢?

INSERT..SELECT is only for manually copying each row? INSERT..SELECT是否仅用于手动复制每一行?

Kindly advise 好心提醒

Thanks 谢谢

To expand on PMV's answer: 扩展PMV的答案:

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: 您可以通过简单的ctas(创建表AS)操作来完成此操作:

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 参见: 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. 一种选择是将转储加载到临时数据库/表中,然后执行一些ALTER TABLE查询以将列重命名为所需的名称。 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). 或者,您可以执行INSERT / SELECT,但是最终将获得两倍的数据(新重命名的表/字段+旧的原始表/字段)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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