Classify incoming identities before a proposed synchronization
For each incoming itemid, report action=update when targetitems already has that identity and action=insert otherwise. Do not write any rows. Target rows absent from the incoming batch are outside this report, not candidates for deletion.
MySQL dialect.
sqlCREATE TABLE target_items (item_id INT PRIMARY KEY,label VARCHAR(40) NOT NULL); CREATE TABLE incoming (item_id INT PRIMARY KEY,label VARCHAR(40) NOT NULL); INSERT INTO target_items VALUES (1,'Old'),(2,'Keep'); INSERT INTO incoming VALUES (1,'New'),(3,'Add');
Required ordering: Ascending incoming item_id.
Edge cases:
- An empty target classifies every supplied input as insert.
- Empty incoming input produces no action row.
INSERT INTO target_items VALUES (1,'Old'),(2,'Keep'); INSERT INTO incoming VALUES (1,'New'),(3,'Add');
item_id action 1 update 3 insert
Explanation: The diagnostic makes source membership and identity-based branch choice visible without applying a native MERGE.
- Each incoming identity appears once.
- Classification only, no accepted-state mutation.
Hint 1
Match identity with a preserving join.
Hint 2
Use the target’s non-NULL primary key as the match marker.
The diagnostic makes source membership and identity-based branch choice visible without applying a native MERGE.
The diagnostic makes source membership and identity-based branch choice visible without applying a native MERGE.
Related Problems
- Apply a narrow MySQL insert-or-replace-quantity policyMedium · sqlSolve Problem
- Select one student occurrence by identityEasy · sqlSolve Problem
- Create a disposable database with explicit text defaultsEasy · sqlSolve Problem