MySQL:将表迁移到新数据库 - 外键

MySQL: Migrating Tables to a new DB - foreign keys

我昨天了解到(通过 Google 的魔法)我可以在另一个数据库中的值上有外键引用。 (MySQL InnoDB foreign key between different databases) 我有两个项目(目前在一个数据库中设置),我想将它们移动到 3 个数据库中:每个项目 1 个,管理员和用户信息 1 个(由两个项目共享).我想这样做是为了让以后的迁移更容易

我的问题是:是否可以将我的管理表从当前数据库移动到一个新的数据库中,复制它的数据并自动更新我的项目数据库中的外键?关于自动更新外键的问题确实是我正在寻求答案的问题......有没有办法在SQL(或其他方式)中做到这一点?

测试很容易:

mysql> use test
Database changed

mysql> create table parent (id int primary key );
Query OK, 0 rows affected (0.06 sec)

mysql> insert into parent values (42);
Query OK, 1 row affected (0.02 sec)

mysql> create table child (parent_id int, foreign key (parent_id) references parent(id));
Query OK, 0 rows affected (0.06 sec)

mysql> insert into child values (42);
Query OK, 1 row affected (0.02 sec)

mysql> rename table child to test2.child;
Query OK, 0 rows affected (0.03 sec)

mysql> show create table test2.child\G
*************************** 1. row ***************************
       Table: child
Create Table: CREATE TABLE `child` (
  `parent_id` int(11) DEFAULT NULL,
  KEY `parent_id` (`parent_id`),
  CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `test`.`parent` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

请注意 child 中的约束现在将引用的 table 限定为 test.parent