Bird
Raised Fist0
dbtdata~10 mins

Unique key for merge behavior in dbt - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Unique key for merge behavior
Start Merge Operation
↓
Identify Unique Key
↓
Check for Duplicates
↓
Error or Handle
↓
Match Records on Unique Key
↓
Update or Insert Records
↓
End
The merge process starts by identifying a unique key to match records. If duplicates exist in the key, it must be handled before merging. Then records are matched and updated or inserted accordingly.
Execution Sample
dbt
merge into target_table as t
using source_table as s
on t.unique_key = s.unique_key
when matched then update set t.col = s.col
when not matched then insert (unique_key, col) values (s.unique_key, s.col);
This SQL-like merge matches records on a unique key to update existing rows or insert new ones.
Execution Table
StepActionUnique Key CheckMatch FoundResult
1Start mergeUnique key identifiedN/AReady to merge
2Check duplicates in sourceNo duplicates foundN/AProceed
3Match source row 1 with targetUnique key validYesUpdate target row
4Match source row 2 with targetUnique key validNoInsert new row
5Match source row 3 with targetUnique key validYesUpdate target row
6Merge completeN/AN/AAll rows processed
💡 All source rows processed with unique keys matched or inserted
Variable Tracker
VariableStartAfter Step 2After Step 4Final
unique_key_checkNot startedNo duplicatesNo duplicatesValidated
matched_rows0012
inserted_rows0011
Key Moments - 2 Insights
Why must the unique key have no duplicates before merging?
Duplicates in the unique key cause ambiguity in matching rows, leading to errors or incorrect updates as shown in execution_table step 2.
What happens if a source row's unique key does not match any target row?
The row is inserted as a new record, demonstrated in execution_table step 4 where no match is found and insertion occurs.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result at step 3?
AInsert new row
BError due to duplicate key
CUpdate target row
DSkip row
💡 Hint
Refer to execution_table row with Step 3 under Result column
At which step does the merge insert a new row?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Check execution_table rows for 'Insert new row' in Result column
If duplicates were found in the unique key at step 2, what would likely happen?
AMerge proceeds normally
BMerge errors or stops
CMerge inserts duplicate rows
DMerge updates all rows
💡 Hint
See key_moments explanation about duplicates causing errors at step 2
Concept Snapshot
Unique key for merge behavior:
- Identify a unique key to match records
- Ensure no duplicates in unique key
- Match source and target on unique key
- Update matched rows
- Insert unmatched rows
- Prevents ambiguous merges and errors
Full Transcript
In dbt merge operations, a unique key is essential to match source and target records. The process starts by identifying this key and checking for duplicates. If duplicates exist, the merge can fail or produce errors. When no duplicates are found, each source row is matched to the target using the unique key. If a match is found, the target row is updated. If no match exists, the source row is inserted as new. This ensures data integrity and correct merge behavior.

Practice

(1/5)
1.

What is the main purpose of the unique_key in dbt's merge behavior?

easy
A. To delete rows from the source table
B. To sort the data before merging
C. To create new columns in the target table
D. To identify which rows to update or insert during a merge

Solution

  1. Step 1: Understand the role of unique_key in merge

    The unique_key tells dbt which rows in the target table correspond to rows in the source table during a merge operation.
  2. Step 2: Identify the correct behavior of unique_key

    It helps dbt decide whether to update existing rows or insert new rows based on matching keys.
  3. Final Answer:

    To identify which rows to update or insert during a merge -> Option D
  4. Quick Check:

    unique_key matches rows for merge = D [OK]
Hint: unique_key matches rows to update or insert [OK]
Common Mistakes:
  • Thinking unique_key sorts data
  • Believing unique_key creates columns
  • Confusing unique_key with deleting rows
2.

Which of the following is the correct way to specify a composite unique_key in a dbt model's merge config?

merge:
  unique_key: ?
easy
A. ['id', 'date']
B. 'id, date'
C. {id, date}
D. id + date

Solution

  1. Step 1: Recall syntax for composite unique_key

    dbt expects a list of column names for composite keys, written as ['col1', 'col2'].
  2. Step 2: Match syntax options

    ['id', 'date'] uses a list syntax with square brackets and quotes, which is correct.
  3. Final Answer:

    ['id', 'date'] -> Option A
  4. Quick Check:

    Composite unique_key = list of columns [OK]
Hint: Use list syntax ['col1', 'col2'] for composite keys [OK]
Common Mistakes:
  • Using a string with commas instead of a list
  • Using curly braces which is invalid syntax
  • Trying to add columns with + operator
3.

Given the following dbt merge config:

merge:
  unique_key: 'user_id'

And source data:

| user_id | value |
|---------|-------|
| 1       | 100   |
| 2       | 200   |
| 1       | 300   |

What will happen when merging into a target table with existing user_id 1 and 2?

medium
A. The row with user_id 1 will cause an error due to duplicate keys
B. The last row with user_id 1 will update the target, overwriting previous
C. Both rows with user_id 1 will be inserted as new rows
D. Only the first row with user_id 1 will update, others ignored

Solution

  1. Step 1: Understand unique_key behavior with duplicates in source

    dbt uses unique_key to match rows. If source has duplicates, the last row usually overwrites previous ones during merge.
  2. Step 2: Analyze the source data effect

    Two rows with user_id 1 exist; the last row with value 300 will update the target row with user_id 1.
  3. Final Answer:

    The last row with user_id 1 will update the target, overwriting previous -> Option B
  4. Quick Check:

    Duplicate keys in source overwrite last row = C [OK]
Hint: Last duplicate row in source overwrites target row [OK]
Common Mistakes:
  • Expecting error on duplicate keys in source
  • Thinking all duplicates insert as new rows
  • Assuming only first duplicate updates
4.

Consider this dbt merge config snippet:

merge:
  unique_key: ['id']

But the source data has duplicate id values. What is the likely issue?

medium
A. Merge will fail or produce incorrect results due to non-unique keys
B. Merge will ignore duplicates and update only one row
C. Merge will create new columns for duplicates
D. Merge will automatically remove duplicates before merging

Solution

  1. Step 1: Identify problem with non-unique keys in merge

    Unique_key must uniquely identify rows. If duplicates exist, dbt cannot correctly match rows.
  2. Step 2: Understand merge behavior with duplicates

    Duplicates cause merge to fail or produce wrong updates because dbt doesn't know which row to update.
  3. Final Answer:

    Merge will fail or produce incorrect results due to non-unique keys -> Option A
  4. Quick Check:

    Non-unique keys cause merge failure = A [OK]
Hint: unique_key must be unique; duplicates cause merge errors [OK]
Common Mistakes:
  • Assuming merge ignores duplicates silently
  • Thinking merge creates new columns for duplicates
  • Believing merge auto-removes duplicates
5.

You want to merge data from a source table into a target table using dbt. The source has columns user_id, event_date, and event_type. You want to update existing rows only if both user_id and event_date match. Which unique_key should you use?

hard
A. unique_key: 'user_id'
B. unique_key: ['event_type']
C. unique_key: ['user_id', 'event_date']
D. unique_key: ['user_id', 'event_type']

Solution

  1. Step 1: Identify columns needed to uniquely match rows

    Since updates depend on matching both user_id and event_date, both must be part of unique_key.
  2. Step 2: Choose correct unique_key syntax

    unique_key: ['user_id', 'event_date'] correctly uses a list with both columns to uniquely identify rows for merge.
  3. Final Answer:

    unique_key: ['user_id', 'event_date'] -> Option C
  4. Quick Check:

    Composite key matches both columns = B [OK]
Hint: Use all columns needed to uniquely identify rows [OK]
Common Mistakes:
  • Using only user_id when event_date is needed
  • Using event_type which is not unique key
  • Mixing wrong columns in unique_key
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard