有没有办法对 MySQL 中的相关表进行重新排序?

Is there a way to re-sequence related tables in MySQL?

我需要 "archive" 来自 2 个关联表的数据,以便匿名化客户数据。

为了做到这一点,我将从 TABLE1 和 TABLE2 中执行 INSERT....SELECT 到另外两个相同的表:TABLE1_anonymous、TABLE2_anonymous。

TABLE1的主键会随着自增而变化,那么TABLE2呢? 我会失去关系。

有没有已知的方法可以做到这一点?

我唯一的想法是避免自动增量,首先保留原始 ID,然后 运行 使用随机 ID 更新两个表...

你需要这样的东西

INSERT INTO TABLE1_anonymous (fields except id)
SELECT (fields except id)
FROM TABLE1;

INSERT INTO TABLE2_anonymous (fields except id and table1_id), table1_id
SELECT (fields from TABLE2 except id and table1_id),       
       TABLE1_anonymous.id
FROM TABLE1
JOIN TABLE1_anonymous USING (fields except id)
JOIN TABLE2 ON TABLE1.id = TABLE2.table1_id;

最后一个查询中的 JOIN 允许您用新分配的 auto_incremented [=11= 替换引用 table1_id 的源 table 中的 id 值] 来自匿名 table 副本的值。


或者,您可以更改匿名 table 结构,添加 old_id 字段,在复制期间将 id 值插入其中,然后使用该值恢复引用完整性,最后将字段删除.