【问题标题】:Move table from one database to another in MySQL [closed]在MySQL中将表从一个数据库移动到另一个[关闭]
【发布时间】:2013-03-11 14:35:59
【问题描述】:

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

【问题讨论】:

    标签: php mysql


    【解决方案1】:

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

    alter table my_old_db.mytable rename my_new_db.mytable
    

    警告:正如您所问,这是移动,而不是复制到新数据库!

    但您将保留表数据(如果适用于您的情况,则不会保留完整性约束)

    关于php,php可以运行sql命令,所以不会有问题。

    【讨论】:

    • 这会产生一个错误:ERROR 1435 (HY000): Trigger in wrong schema
    • @lal_bosdi 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 相比,此命令立即生效
    • 确保不要将表名放在 `` 符号中。这将导致“表不存在”错误。至少那是发生在我身上的事。 :)
    【解决方案2】:

    整个数据库(所有表):

    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

    【讨论】:

    • 注意:以上所有命令都是复制而不是移动。
    猜你喜欢
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 2012-04-07
    相关资源
    最近更新 更多