0
0
dbtdata~10 mins

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

Choose your learning style9 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.