Understanding Lock Types: Shared and Exclusive Locks in MySQL
📖 Scenario: You are managing a small library database where multiple users can read book information simultaneously, but only one user can update the book details at a time to avoid conflicts.
🎯 Goal: Build a simple MySQL transaction example that demonstrates acquiring a shared lock for reading and an exclusive lock for updating a book record using separate sessions.
📋 What You'll Learn
Create a table called
books with columns id (INT, primary key), title (VARCHAR(100)), and author (VARCHAR(100))Insert exactly three book records with given values
Start a transaction and acquire a shared lock on the book with
id = 1In another session, start another transaction and acquire an exclusive lock on the book with
id = 2Commit both transactions
💡 Why This Matters
🌍 Real World
Databases use locks to manage multiple users reading and writing data safely without conflicts.
💼 Career
Understanding locks is essential for database administrators and developers to ensure data integrity and performance.
Progress0 / 4 steps