MySQL 将表从一个数据库复制到另一个数据库的最简单方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12242772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Easiest way to copy a table from one database to another?
提问by Sparky
What is the best method to copy the data from a table in one database to a table in another database when the databases are under different users?
当数据库在不同用户下时,将数据从一个数据库中的表复制到另一个数据库中的表的最佳方法是什么?
I know that I can use
我知道我可以使用
INSERT INTO database2.table2 SELECT * from database1.table1
But here the problem is that both database1
and database2
are under different MySQL users. So user1
can access database1
only and user2
can access database2
only. Any idea?
但这里的问题是,无论是database1
和database2
是在不同的MySQL用户。所以user1
只能访问database1
并且user2
只能访问database2
。任何的想法?
回答by helmor
If you have shell access you may use mysqldump
to dump the content of database1.table1
and pipe it to mysql
to database2
. The problem here is that table1
is still table1
.
如果您有 shell 访问权限,您可以mysqldump
用来转储 的内容database1.table1
并将其通过管道传输mysql
到database2
. 这里的问题table1
是仍然是table1
.
mysqldump --user=user1 --password=password1 database1 table1 \
| mysql --user=user2 --password=password2 database2
Maybe you need to rename table1
to table2
with another query. On the other way you might use sed to change table1 to table2 between the to pipes.
也许您需要使用另一个查询重命名table1
为table2
。另一方面,您可以使用 sed 在 to 管道之间将 table1 更改为 table2。
mysqldump --user=user1 --password=password1 database1 table1 \
| sed -e 's/`table1`/`table2`/' \
| mysql --user=user2 --password=password2 database2
If table2 already exists, you might add the parameters to the first mysqldump which dont let create the table-creates.
如果 table2 已经存在,您可以将参数添加到第一个不允许创建 table-creates 的 mysqldump。
mysqldump --no-create-info --no-create-db --user=user1 --password=password1 database1 table1 \
| sed -e 's/`table1`/`table2`/' \
| mysql --user=user2 --password=password2 database2
回答by mainmeat
CREATE TABLE db1.table1 SELECT * FROM db2.table1
CREATE TABLE db1.table1 SELECT * FROM db2.table1
where db1 is the destination and db2 is the source
其中 db1 是目标,db2 是源
回答by Mekey Salaria
If you are using PHPMyAdmin,it could be really simple. Suppose you have following databases:
如果您使用PHPMyAdmin,它可能非常简单。假设您有以下数据库:
DB1 & DB2
DB1 have a table users which you like to copy to DB2
DB1 有一个表 users,您想将其复制到 DB2
Under PHPMyAdmin, open DB1, then go to users table.
在 PHPMyAdmin 下,打开 DB1,然后转到用户表。
On this page, click on the "Operations"tab on the top right. Under Operations, look for section Copy table to (database.table):
在此页面上,单击右上角的“操作”选项卡。在 Operations 下,查找Copy table to (database.table) 部分:
& you are done!
&你完成了!
回答by mmdemirbas
MySql Workbench: Strongly Recommended
MySql 工作台:强烈推荐
This will easily handle migration problems. You can migrate selected tables of selected databases between MySql and SqlServer. You should give it a try definitely.
这将轻松处理迁移问题。您可以在 MySql 和 SqlServer 之间迁移选定数据库的选定表。你一定要试一试。
回答by Nick M
I use Navicat for MySQL...
我使用 Navicat for MySQL...
It makes all database manipulation easy !
它使所有数据库操作变得容易!
You simply select both databases in Navicat and then use.
您只需在 Navicat 中选择两个数据库,然后使用。
INSERT INTO Database2.Table1 SELECT * from Database1.Table1
回答by Mir Adnan
I know this is old question, just answering so that anyone who lands here gets a better approach.
我知道这是一个老问题,只是回答以便任何登陆这里的人都能获得更好的方法。
As of 5.6.10 you can do
从 5.6.10 开始,您可以执行
CREATE TABLE new_tbl LIKE orig_tbl;
Refer documentation here: https://dev.mysql.com/doc/refman/5.7/en/create-table-like.html
请参阅此处的文档:https: //dev.mysql.com/doc/refman/5.7/en/create-table-like.html
回答by Weston Ganger
If your tables are on the same mysql server you can run the following
如果你的表在同一个 mysql 服务器上,你可以运行以下命令
CREATE TABLE destination_db.my_table SELECT * FROM source_db.my_table;
ALTER TABLE destination_db.my_table ADD PRIMARY KEY (id);
ALTER TABLE destination_db.my_table MODIFY COLUMN id INT AUTO_INCREMENT;
回答by MojganK
Here is another easy way:
这是另一种简单的方法:
- use DB1; show create table TB1;
- copy the syntax here in clipboard to create TB1 in DB2
- use DB2;
- paste the syntax here to create the table TB1
- 使用 DB1;显示创建表 TB1;
- 在剪贴板中复制此处的语法以在 DB2 中创建 TB1
- 使用 DB2;
- 在此处粘贴语法以创建表 TB1
INSERT INTO DB2.TB1 SELECT * from DB1.TB1;
INSERT INTO DB2.TB1 SELECT * from DB1.TB1;
回答by biniam
Use MySql Workbench's Export and Import functionality.
Steps:
1. Select the values you want
使用 MySql Workbench 的导出和导入功能。步骤:
1. 选择你想要的值
E.g. select * from table1;
- Click on the Export button and save it as CSV.
create a new table using similar columns as the first one
E.g. create table table2 like table1;
select all from the new table
E.g. select * from table2;
Click on Import and select the CSV file you exported in step 2
- 单击“导出”按钮并将其另存为 CSV。
使用与第一个相似的列创建一个新表
E.g. create table table2 like table1;
从新表中全选
E.g. select * from table2;
单击导入并选择您在步骤 2 中导出的 CSV 文件
回答by Doug Krugman
With MySQL Workbench you can use Data Export to dump just the table to a local SQL file (Data Only, Structure Only or Structure and Data) and then Data Import to load it into the other DB.
使用 MySQL Workbench,您可以使用数据导出将表转储到本地 SQL 文件(仅数据、仅结构或结构和数据),然后使用数据导入将其加载到其他数据库中。
You can have multiple connections (different hosts, databases, users) open at the same time.
您可以同时打开多个连接(不同的主机、数据库、用户)。