在MySQL中是否可以重新对相关表进行安全保护?

问题描述 投票:0回答:1

为了匿名化客户数据,我需要从两个连接的表中“存档”数据。

为此,我将TABLE1和TABLE2中的INSERT....SELECT放入另外两个相同的表中:TABLE1_anonymous,TABLE2_anonymous。

TABLE1的主键会随着自动递增而改变,但是TABLE2呢?我会失去关系的。

是否有已知的方法?

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

mysql database-migration
1个回答
0
投票

您需要类似的东西

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 id值替换源表中已引用table1_idid值。

© www.soinside.com 2019 - 2024. All rights reserved.