在MySQL中将表从一个数据库移动到另一个数据库

时间:2022-09-15 22:19:44

How to move a table from one Database to another Database without using phpMyAdmin? It will be better if it is possible by PHP.

如何在不使用phpMyAdmin的情况下将一个表从一个数据库移动到另一个数据库?如果可以使用PHP,那就更好了。

2 个解决方案

#1


93  

ALTER TABLE .. can be used to move tables from one database to another :

ALTER TABLE . .可用于将表从一个数据库移动到另一个数据库:

alter table my_old_db.mytable rename my_new_db.mytable


Warning : as you asked, this is a move, not a copy to the new database!
But you will keep table data (and not integrity constraints if they apply in your case)

警告:正如您所要求的,这是一个移动,而不是新数据库的副本!但是您将保留表数据(如果适用于您的情况,则不使用完整性约束)

Regarding php, php is able to run sql commands so it won't be a problem (i can be more precise if you wish).

关于php, php可以运行sql命令,因此不会出现问题(如果您愿意,我可以更精确地说)。

#2


15  

Entire Database (all tables):

整个数据库(所有表):

mysqldump -u root databasename > dump.sql
mysql -u root databasename < dump.sql

One Table:

一个表:

mysqldump -u root -p yourpass dbname tablename | mysql -u root -p pass secondDB

PHP:

PHP:

Run PHP SELECT FROM SOURCE-DB TABLE and Run INSERT INTO Table IN TARGET-DB

在SOURCE-DB表中运行PHP SELECT,并在TARGET-DB表中运行INSERT

#1


93  

ALTER TABLE .. can be used to move tables from one database to another :

ALTER TABLE . .可用于将表从一个数据库移动到另一个数据库:

alter table my_old_db.mytable rename my_new_db.mytable


Warning : as you asked, this is a move, not a copy to the new database!
But you will keep table data (and not integrity constraints if they apply in your case)

警告:正如您所要求的,这是一个移动,而不是新数据库的副本!但是您将保留表数据(如果适用于您的情况,则不使用完整性约束)

Regarding php, php is able to run sql commands so it won't be a problem (i can be more precise if you wish).

关于php, php可以运行sql命令,因此不会出现问题(如果您愿意,我可以更精确地说)。

#2


15  

Entire Database (all tables):

整个数据库(所有表):

mysqldump -u root databasename > dump.sql
mysql -u root databasename < dump.sql

One Table:

一个表:

mysqldump -u root -p yourpass dbname tablename | mysql -u root -p pass secondDB

PHP:

PHP:

Run PHP SELECT FROM SOURCE-DB TABLE and Run INSERT INTO Table IN TARGET-DB

在SOURCE-DB表中运行PHP SELECT,并在TARGET-DB表中运行INSERT