Configure Type 2 SCD on existing tables in SQL Server
0
votes
0
answers
434
views
I've got two tables,
FACILITIES
and CONTRACTS
. It's a 1:M facility->contract. The tables have unique keys generated by the upstream system and are truncate-reload every night. Neither table has any primary key or foreign key/Unique key enforcement. Users typically join on the facility ID which exists on the contracts table.
I am trying to set up SCD Type 2 for these two tables and have created the appropriate and working code using the MERGE
command and have tested it successfully. I know that I will need to set up new primary/foreign keys since Type 2 will cause the business key to be duplicated (what they are currently using as a unqiue identifier).
However, I'm unsure how to handle existing data. Both tables have several thousand rows, so I don't know how to link existing contracts and facilities using the new PK/FK combos I set up. I know that going forward they will work fine, but not sure about now.
How do I link the existing data with the new PK/FK setup so the associations for the new keys are correct for SCD going forward?
Here's how they look now:
| FacilityID | Facility_PK (the one I set up) |
| -----------| -------------------------------|
| 12345 | 1 |
| 23456 | 2 |
| ContractID | Contract_PK | Facility ID | Facility_FK |
| ---------- | -------------|---------------------------------| ------------|
| 7890 | 1 | 12345 | ??? |
| 4444 | 2 | 12345 | ??? |
| 2345 | 3 | 23456 | ??? |
EDIT: Further context:
Once the Slowly Changing Dimension logic is implemented, the existing ID fields will be duplicated and really considered business keys.
| FacilityID | Facility_PK (the one I set up) | USER | Active Indicator |
| -----------| -------------------------------|----------|------------------|
| 12345 | 1 | User A |0 |
| 23456 | 2 | User A |1 |
| 12345 | 3 | User B |1 |
Now there is no longer a unique link to the CONTRACTS
table. So I can add in a PK to this FACILITIES
table and keep them unique, but how do I update the data in the CONTRACTS
Foreign key so that this works properly?
If you were to now join based on FacilityID, you'd get back both records and would be unable to tell which contract is associated with which point-in-time facility record.
Asked by seve
(1 rep)
Mar 13, 2021, 05:06 PM
Last activity: Mar 14, 2021, 12:29 AM
Last activity: Mar 14, 2021, 12:29 AM