Apply a narrow MySQL insert-or-replace-quantity policy
Use MySQL INSERT ON DUPLICATE KEY UPDATE to copy incoming itemid,quantity into inventory. A primary-key match replaces quantity exactly; a new identity is inserted. Preserve inventory identities absent from incoming. This is a narrow upsert, not native MERGE or full synchronization. Return itemid,quantity.
MySQL dialect.
sqlSET SESSION sql_mode='STRICT_ALL_TABLES'; CREATE TABLE inventory (item_id INT PRIMARY KEY,quantity INT NOT NULL CHECK(quantity>=0)) ENGINE=InnoDB; CREATE TABLE incoming (item_id INT PRIMARY KEY,quantity INT NOT NULL); INSERT INTO inventory VALUES (1,10),(2,8); INSERT INTO incoming VALUES (1,7),(3,4);
Required ordering: Ascending item_id.
Edge cases:
- Unrelated item 2 remains.
- Empty incoming leaves all targets unchanged.
- Empty inventory takes insertion paths.
INSERT INTO inventory VALUES (1,10),(2,8); INSERT INTO incoming VALUES (1,7),(3,4);
item_id quantity 1 7 2 8 3 4
Explanation: Protected identity selects the supported MySQL update branch while the source quantity supplies the explicit replacement value.
- One source row per identity and only one protected key in this fixture.
- Positive incoming quantities are nonnegative.
Hint 1
Map source roles explicitly.
Hint 2
Use the selected source quantity for replacement, not addition.
Protected identity selects the supported MySQL update branch while the source quantity supplies the explicit replacement value.
Protected identity selects the supported MySQL update branch while the source quantity supplies the explicit replacement value.
Related Problems
- Classify incoming identities before a proposed synchronizationEasy · sqlSolve Problem
- Duplicate EnrollmentsMedium · sqlSolve Problem
- Employees With No Real DepartmentMedium · sqlSolve Problem