Increment value in MySQL update query
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
To increment a numeric column in MySQL, update the column based on its current value instead of reading the value in application code and writing it back later. The standard pattern is SET column = column + 1, which is simple, efficient, and safer under concurrent access than a separate read-then-write sequence.
The Basic Increment Pattern
Here is the canonical query:
This tells MySQL to take the existing view_count value and add one directly in the database.
You can increment by any amount:
The same idea works for decrementing as well:
Why This Is Better Than Reading First
A common but weaker pattern is:
SELECT view_count FROM counters WHERE id = 42- Add one in application code.
UPDATE counters SET view_count = ? WHERE id = 42
That approach is vulnerable to lost updates when multiple requests run at the same time. Letting MySQL perform the increment in one statement is usually the correct approach.
Increment Multiple Columns at Once
You can increment several fields in the same UPDATE.
This is useful when the update represents one logical event and those counters should change together.
Handle NULL Carefully
If a column can be NULL, adding to it keeps the result NULL.
If view_count is NULL, the new value remains NULL.
Use COALESCE when needed:
That treats a missing value as zero before incrementing.
Conditional Increments
You can combine increments with business rules in the WHERE clause.
After running the query, check the affected row count. If zero rows were updated, the coupon may already be exhausted or the code may not exist.
Transactions for Related Changes
If the increment must stay consistent with other updates, use a transaction.
The increment syntax itself is the same, but a transaction helps keep several dependent changes together.
Common Pitfalls
The most serious mistake is forgetting the WHERE clause and incrementing every row in the table.
Another common mistake is doing the increment in application code after a separate SELECT, which increases the chance of race conditions.
Watch out for NULL values too. If the column was not initialized properly, column + 1 may not behave the way you expect.
Finally, make sure the column type is actually numeric. Increment logic belongs on integer, decimal, or other numeric columns, not text fields.
Summary
- Increment numeric columns in MySQL with
SET column = column + 1. - Let the database perform the increment instead of doing a read-then-write round trip.
- Use a
WHEREclause so only the intended rows change. - Use
COALESCEif the column may beNULL. - Wrap related updates in a transaction when the increment is part of a larger state change.
Related reading
- Index all except one item in python
- Index of a maximum element in TensorFlow tensor
- Index of Currently Selected Row in DataGridView
- Index zero based must be greater than or equal to zero
- Indexing on nested field
- Indexing ranked permutations into other ranked permutations
- Infinite flux and bulk-write to database
- Infinite Recursion with Jackson JSON and Hibernate JPA issue

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.