我想在 MySQL 中执行 SCD 过程,我有两个完全相同的表,table1 总是被截断,它只有新记录。table2 从 table1 获取记录并维护历史记录。在我的场景中,如果现有数据进入 table1,我想覆盖 table2,如果新记录进入 table1,我想作为新记录插入 table2。
create table table1(id int, name varchar(2),title varchar(10));
create table table2(id int, name varchar(2),title varchar(10));
这是我上面的示例表结构。如果 table 包含像 (1,a,hockey) 这样的值,那么我只想将这些数据插入 table2,然后 table1 将被截断。如果我再次将具有相同 ID 的记录插入到 table2 但标题可能会更改在这种情况下,我想覆盖 table2 中的数据,如果有任何新 ID 出现,那么它将作为新行插入到 table2 中。请解释如何做这个场景?