Locks help keep data safe when many people use the database at the same time. They stop conflicts by controlling who can read or change data.
Lock types (shared, exclusive) in MySQL
LOCK TABLES table_name READ; LOCK TABLES table_name WRITE;
READ lock is a shared lock. Many users can read but no one can write.
WRITE lock is an exclusive lock. Only one user can write or read until the lock is released.
employees table. Others can also read but cannot write.LOCK TABLES employees READ;
employees table. Only this session can read or write until unlocked.LOCK TABLES employees WRITE;
UNLOCK TABLES;
This locks the employees table for reading, runs a query to get sales department employees, then unlocks the table.
LOCK TABLES employees READ; SELECT * FROM employees WHERE department = 'Sales'; UNLOCK TABLES;
Always unlock tables after locking to avoid blocking others.
Shared locks allow multiple readers but block writers.
Exclusive locks block both readers and writers except the locker.
Shared locks let many users read data safely at the same time.
Exclusive locks let one user write data safely without interference.
Use locks to keep data correct when many users access the database.
Practice
Solution
Step 1: Understand shared lock behavior
A shared lock allows many users to read the same data at the same time safely.Step 2: Compare with other lock types
Exclusive locks prevent others from reading or writing, but shared locks only prevent writing.Final Answer:
Allow multiple users to read data simultaneously without conflicts -> Option AQuick Check:
Shared lock = multiple readers allowed [OK]
- Confusing shared lock with exclusive lock
- Thinking shared locks block reading
- Assuming shared locks allow writing
Solution
Step 1: Recall MySQL lock syntax
In MySQL, to acquire a shared lock, you useLOCK TABLES table_name READ;.Step 2: Eliminate incorrect options
WRITEis for exclusive locks,EXCLUSIVEandSHAREare not valid MySQL lock keywords.Final Answer:
LOCK TABLES table_name READ; -> Option DQuick Check:
Shared lock syntax uses READ keyword [OK]
- Using WRITE instead of READ for shared lock
- Using unsupported keywords like EXCLUSIVE or SHARE
- Confusing lock syntax with SQL SELECT statements
Solution
Step 1: Understand lock compatibility
Shared locks allow multiple readers but block exclusive locks until released.Step 2: Analyze the scenario
Transaction B must wait for Transaction A to release the shared lock before getting exclusive lock.Final Answer:
Transaction B waits until Transaction A releases the shared lock -> Option BQuick Check:
Exclusive lock waits for shared locks to release [OK]
- Assuming exclusive lock can override shared lock immediately
- Thinking shared lock releases automatically
- Believing both locks can coexist on same data
LOCK TABLES my_table SHARE;
What is the error and how to fix it?
Solution
Step 1: Identify invalid lock keyword
MySQL does not support 'SHARE' as a lock type; it uses 'READ' or 'WRITE'.Step 2: Correct the lock type
To lock for writing, useLOCK TABLES my_table WRITE;instead.Final Answer:
Error: Invalid lock type 'SHARE'. Fix: Use 'WRITE' instead of 'SHARE'. -> Option CQuick Check:
Use WRITE for exclusive lock, SHARE is invalid [OK]
- Using unsupported lock keywords
- Ignoring error messages about syntax
- Assuming SHARE is synonym for READ
Solution
Step 1: Understand shared and exclusive lock roles
Shared locks allow multiple readers; exclusive locks allow one writer.Step 2: Apply correct locks for reading and writing
Use shared locks for reading so many users can read safely, and exclusive locks for updating to prevent conflicts.Final Answer:
Use shared locks (READ) for reading and exclusive locks (WRITE) for updating -> Option AQuick Check:
Shared for read, exclusive for write ensures safe concurrency [OK]
- Using exclusive locks for reading blocks all users
- Using shared locks for writing causes conflicts
- Ignoring locks and risking data corruption
Ready to prove it?
Pass the test and this pattern moves unknown
