如何从一个表中转储数据并插入到另一个表中

发布于 2024-11-29 12:53:04 字数 362 浏览 3 评论 0原文

我有两个数据库。我想从第一个数据库中的一个表中转储数据,然后插入到第二个数据库中具有另一个名称的另一个表中。
因此,我的 DB1 包含表 tbl1 和 tabl2,DB2 包含表 tbl3 和 tbl4。我知道 tabl1 和 tabl3 具有相同的结构。如何使用 mysqldump 命令将数据从一个复制到另一个?
我尝试过这样做,但行不通。

mysqldump --user root --password=password --no-create-info DB1 tbl1 > c:/dump.sql
mysql --user root --password=password DB2 tbl3 < c:/dump.sql

I have two databases. I want to dump data from one table in 1st database and insert to another table with an another name in 2nd database.
So I have DB1 that has tables tbl1 and tabl2, and DB2 that has tables tbl3 and tbl4. I know that tabl1 and tabl3 have the same structure. How to copy data from one to another by using mysqldump command?
I've tried to do this, but it's not work.

mysqldump --user root --password=password --no-create-info DB1 tbl1 > c:/dump.sql
mysql --user root --password=password DB2 tbl3 < c:/dump.sql

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

絕版丫頭 2024-12-06 12:53:05

由于表名不同,这将不起作用

如果两个数据库位于使用相同守护程序的同一服务器中,

insert into DB2.tbl3 select * from DB1.tbl1;

,如果 DB2 中不存在 tbl1,则可以直接使用,
伪代码:

# import as tbl1 from DB1 into tbl1 in DB2
mysqldump DB1 tbl1 | mysql DB2

# then rename tbl1 in DB2 to tbl3
mysql DB2 -N <<< "rename table tbl1 to tbl3"

This is not going to work due to different table name

if both database are sitting in the same server using the same daemon, you can directly

insert into DB2.tbl3 select * from DB1.tbl1;

if tbl1 is not existing in DB2,
pseudo code for this :

# import as tbl1 from DB1 into tbl1 in DB2
mysqldump DB1 tbl1 | mysql DB2

# then rename tbl1 in DB2 to tbl3
mysql DB2 -N <<< "rename table tbl1 to tbl3"
烈酒灼喉 2024-12-06 12:53:05

我在 Linux shell 命令行中使用

mysqldump --user=username --password=xxxx dbname | mysql --host=remotehost.com --user=username --password=xxxx -C dname 

它,将其从本地主机传输到远程主机,即整个数据库。

I am using in a linux shell command line

mysqldump --user=username --password=xxxx dbname | mysql --host=remotehost.com --user=username --password=xxxx -C dname 

this transfers it from the local host to a remote host, the whole database.

笑饮青盏花 2024-12-06 12:53:05

如果您还想复制表的内容,您可以执行以下操作:

CREATE TABLE `new_table_name` LIKE `old_table_name`;
INSERT INTO `new_table_name` SELECT * FROM `old_table_name`;

如果您必须将表从一个数据库复制到另一个数据库,则使用以下内容

 CREATE TABLE `db1.new_table_name` LIKE `db2.old_table_name`;
 INSERT INTO `db1.new_table_name` SELECT * FROM `db2.old_table_name`;

它对我有用,因为转储单个表并导入会引发 MariaDB 语法错误

IF you want to also copy the contents of the table you can do:

CREATE TABLE `new_table_name` LIKE `old_table_name`;
INSERT INTO `new_table_name` SELECT * FROM `old_table_name`;

If you have to copy table from one database to another database then use following

 CREATE TABLE `db1.new_table_name` LIKE `db2.old_table_name`;
 INSERT INTO `db1.new_table_name` SELECT * FROM `db2.old_table_name`;

It works for me as dumping single table and importing was throwing syntax error with MariaDB

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文