Count page views without race conditions
A page's daily view counter — every hit either creates a row with one or increments the existing one. Doing "read → check → write" as separate statements is unsafe: concurrent hits lose increments.
The table counters(id, count) is empty. Implement insert-or-update: try to insert the row with id 1 and counter 1; on a conflict by id the existing counter goes up by one. Run the command five times — after five runs the value should be 5.
Just INSERT/UPDATE/DELETE — no trailing SELECT needed.
Sign in to see submission history
Sign inSign in to use AI Mentor
Sign in