What are database transactions
· Category: SQL & Databases
Short answer
A transaction is a sequence of operations treated as a single unit of work. It must be Atomic, Consistent, Isolated, and Durable (ACID). For schema design context, see how to design a database schema from scratch. For migration safety, see how to implement database migrations.
Steps
- Start a transaction:
BEGIN; - Execute one or more SQL statements
- Commit if all succeed:
COMMIT; - Rollback on error:
ROLLBACK; - Choose the appropriate isolation level for your use case
Tips
- Use the lowest isolation level that meets correctness requirements
- Keep transactions short to reduce locking contention
- For query optimization, see what are database indexes and how do they work