Two transactions both see one seat remaining and both sell it. How can the database prevent overselling?
Interactive Practice Mode
Formulate your thoughts or take notes before revealing the model solution.
Saved automatically
Model Answer & Deep-Dive Explanation
Make the availability check and decrement part of an atomic conditional update, such as updating only when remaining is greater than zero, and confirm that exactly one row was affected. Create the reservation in the same transaction and roll back if another required step fails. Row locking or an appropriate serializable transaction can also work, with deadlock or serialization-failure retry handling. A prior SELECT followed by an unprotected UPDATE is vulnerable to races. Define cancellation and expiration behavior so inventory is restored consistently.
Related Questions in SQL & Relational Databases
What are Clustered and Non-Clustered Indexes in SQL, and when should you use each?
Intermediate level • 387 views
Explain SQL ACID properties and the differences between READ COMMITTED and REPEATABLE READ isolation levels.
Intermediate level • 1 views
What is Database Indexing, how does a B+ Tree index work, and when should you avoid creating indexes?
Intermediate level • 1 views
Explain Common Table Expressions (CTEs) and SQL Window Functions (ROW_NUMBER, RANK, DENSE_RANK) with use cases.
Intermediate level • 1 views
Explain Database Normalization from 1NF through BCNF with real-world anomaly examples.
Intermediate level • 1 views