php 在 MySQL 中将表从一个数据库移动到另一个数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15558461/
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
Move table from one database to another in MySQL
提问by Viswalinga Surya S
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 实现就更好了。
回答by user2196728
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 copyto 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 命令,所以不会有问题(如果你愿意,我可以更精确)。
回答by GGio
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-DBTABLEand Run INSERT INTO TableIN TARGET-DB
运行 PHP SELECT FROM SOURCE-DB TABLE并运行INSERT INTO TableIN TARGET-DB

