We are asked: "What is a 'slowly changing dimension' (SCD) in a data warehouse? Please what is an SCD, describe Type 1 SCD. How are changes handled, describe Type 2 SCD. How are changes handled? What new columns are typically added to the dimension table, and provide an example of a dimension where you would use Type 2 SCD (e.g., a customer's address)."
We need to provide a comprehensive answer covering all parts.
First, define SCD: Slowly Changing Dimensions (SCD) are dimensions in a data warehouse where the data changes infrequently and unpredictably, as opposed to rapidly changing facts. They are attributes that describe the context of facts and may change over time (like customer address, product price, etc.). SCD types define how to handle historical changes.
We need to describe Type 1 and Type 2 separately.
Type 1 SCD: Overwrite old value with new value; no history is kept. That means whenever an attribute changes, we simply update the existing row in the dimension table, losing the previous value. This is used for corrections or when history is not needed. Changes are handled by simply updating the dimension row. For example, if a customer's phone number changes, we update it, no record of old phone number.
Type 2 SCD: We keep full history by adding new rows. That is, when an attribute changes, we expire the current row (by setting an end date or flag) and insert a new row with the new attribute value and a new effective start date. This allows us to track changes over time. Typically, we add columns like start_date, end_date, and a flag (is_current) to manage versions. Often we also have a surrogate key that uniquely identifies each version. For example, for a customer's address, we might have multiple address records with different effective dates, so that historical facts can be linked to the correct address version.
We need to mention what new columns are typically added: In Type 2, we often add:
- Effective start date (e.g., start_date)
- Effective end date (e.g., end_date) – or sometimes a flag to indicate current row.
- Sometimes a version number or sequence.
- Possibly a surrogate key (already exists but becomes important to differentiate versions).
- And possibly a current flag (is_current).
Also we might add a natural key (like customer_id) that remains same across versions.
Example: Customer dimension where we track address changes using Type 2. So we have a surrogate key (cust_sk), natural key (cust_id), name, address, etc. When address changes, we insert a new row with new address and new surrogate key, and set the old row's end_date to the day before the change, and the new row's start_date to the change date. That way, when we join fact tables with date ranges, we can get the correct address.
We need to structure answer: Start with definition, then Type 1 description and handling, then Type 2 description and handling, then typical added columns, then example.
Make it clear and thorough.
Let's produce final answer.
What is a Slowly Changing Dimension (SCD)?
A Slowly Changing Dimension (SCD) refers to a dimension table in a data warehouse where attribute values change infrequently and unpredictably over time. Unlike fact tables that record transactional events, dimensions provide contextual descriptors (e.g., customer, product, geography). The challenge of SCDs lies in how to handle these changes – whether to overwrite old values, preserve history, or employ a hybrid approach. Different “types” of SCDs define the methodology used, with Type 1 and Type 2 being the most common.
Type 1 SCD
Description
Type 1 SCD is the simplest approach: when an attribute changes, the old value is overwritten with the new value. No historical record of the previous value is retained.
How changes are handled
- An
UPDATE statement modifies the existing row in the dimension table.
- No new rows are added; the dimension row always reflects the most current state.
- This method is suitable when historical accuracy is not required, or when the change is a correction (e.g., a data entry error).
Example
If a customer's phone number changes from 555‑1234 to 555‑5678, the original row is updated to store 555‑5678. The previous number is lost forever.
Type 2 SCD
Description
Type 2 SCD preserves full history by creating a new row for every change. The old row is “expired” and the new row becomes the active version. This allows accurate reporting of past facts with the dimension values that were valid at the time of the transaction.
How changes are handled
- Identify the current active row for the entity (e.g., customer).
- Update that row by setting an end date (or an
is_current flag) to indicate it is no longer active.
- Insert a new row with:
- A new surrogate key (primary key of the dimension table).
- The same natural/business key (e.g.,
customer_id).
- The updated attribute values.
- A start date indicating when this version becomes effective.
- An open-ended end date (e.g.,
9999-12-31) or is_current = 1 to show it is the current version.
Typical added columns
To implement Type 2 SCD, the dimension table usually includes:
effective_start_date (or start_date) – when this version became active.
effective_end_date (or end_date) – when this version was superseded; open-ended for the current version.
is_current (or current_flag) – a boolean indicating the active row.
- Sometimes a
version or sequence number for ordering.
A surrogate key is essential to uniquely identify each version, while the natural key groups all versions of the same entity.
Example: Customer Address
Consider a customer dimension where we need to track address changes. The table structure might include:
| surrogate_key (sk) | customer_id (nk) | name | address | city | state | zip | start_date | end_date | is_current |
|---|
| 101 | CUST001 | John | 123 Main | Anytown | CA | 12345 | 2020-01-01 | 2021-06-30 | 0 |
| 102 | CUST001 | John | 456 Oak | Anytown | CA | 12345 | 2021-07-01 | 9999-12-31 | 1 |
- The first row represents the original address effective from Jan 1 2020 until June 30 2021.
- When John moved on July 1 2021, the old row was expired (
end_date = 2021-06-30, is_current = 0) and a new row inserted with start_date = 2021-07-01 and is_current = 1.
When a sales fact table records a transaction on a specific date (e.g., 2021-03-15), it joins with the customer dimension on customer_id and the date range (start_date ≤ transaction_date ≤ end_date) to retrieve the correct address (123 Main). For a transaction on 2021-08-10, the join yields the new address (456 Oak). This preserves the contextual accuracy of historical reports.
When to use Type 2 SCD
Type 2 is appropriate whenever historical tracking is required or when the past value of an attribute influences analysis (e.g., compliance, auditing, trend analysis). Common examples include:
- Customer address changes (as shown)
- Product price changes
- Employee department transfers
- Geographic region re‑assignments
In contrast, Type 1 is used when history is irrelevant or for correcting mistakes. The choice depends on business requirements for historical reporting.