Search code examples
mysqldatabasescd

MySQL Slowly changing dimensions SCD2 + ON DUPLICATE KEY UPDATE


Im looking to have MySQL handle Slowly changing dimensions The table is set up to use 'effective date' as the control - so table structure is similar to here http://en.wikipedia.org/wiki/Slowly_changing_dimension Type II bottom example.

Is it possible to use this function to update / insert or is it best to seperate the functions eg insert = check for existing then insert, update = update original column, insert new column

Thanks


Solution

  • No, it's not possible. This statement would then update the row if it already exists, so you would end up with only one record and loose historical info. You need to update the old record first (set end_date to current date) and then insert a new record (with end_date being NULL).