【发布时间】:2013-03-11 14:35:59
【问题描述】:
如何在不使用 phpMyAdmin 的情况下将表从一个数据库移动到另一个数据库?如果能用PHP就更好了。
【问题讨论】:
如何在不使用 phpMyAdmin 的情况下将表从一个数据库移动到另一个数据库?如果能用PHP就更好了。
【问题讨论】:
ALTER TABLE .. 可用于将表从一个数据库移动到另一个:
alter table my_old_db.mytable rename my_new_db.mytable
警告:正如您所问,这是移动,而不是复制到新数据库!
但您将保留表数据(如果适用于您的情况,则不会保留完整性约束)
关于php,php可以运行sql命令,所以不会有问题。
【讨论】:
If there are any triggers associated with a table which is moved to a different database using RENAME TABLE, then the statement fails with the error Trigger in wrong schema. Foreign keys that point to the renamed table are not automatically updated. In such cases, you must drop and re-create the foreign keys in order for them to function properly. dev.mysql.com/doc/refman/5.7/en/rename-table.html 您需要将表导出到 sql 并导入新数据库。或者删除所有触发器,移动表并从备份 sql 代码创建所需的触发器。
insert into 相比,此命令立即生效
整个数据库(所有表):
mysqldump -u root databasename > dump.sql
mysql -u root databasename < dump.sql
一张桌子:
mysqldump -u root -p yourpass dbname tablename | mysql -u root -p pass secondDB
PHP:
运行 PHP SELECT FROM SOURCE-DB TABLE 并运行 INSERT INTO Table IN TARGET-DB
【讨论】: