Optimistic Locking Version Column
Implement optimistic concurrency with a version column for conflict-free updates.
Code
-- Read current version
SELECT id, data, version FROM items WHERE id = 42;
-- Attempt update (fails if version changed)
UPDATE items
SET data = :new_data, version = version + 1, updated_at = NOW()
WHERE id = 42 AND version = :expected_version;
-- Check affected rows: 0 means conflict, retryHow it works
Several statements share this file — SELECT / UPDATE, with items among the objects involved.
Breaking the statements into their working parts: a WHERE clause limiting the rows one statement is allowed to reach.
Applying it
Run the statement against the items table.
Statements that matter
UPDATE items
SET data = :new_data, version = version + 1, updated_at = NOW()
WHERE id = 42 AND version = :expected_version;
SELECT id, data, version FROM items WHERE id = 42;
Interactive Tools for This Topic
CSV Column AnalyzerProfile every CSV column with inferred types, empty-cell counts, unique values, and numeric min and max ranges.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.UUID Version DetectorValidate the 8-4-4-4-12 UUID layout and read its version (v1 to v8) and variant nibbles per RFC 9562, with full field decomposition.
FAQ
- Which tables does this file reference?
- `items` — referenced directly, no join needed.
Report a problem with this exampleOpens a pre-filled issue on GitHub.