CodeBlanks Logo Icon CodeBlanks

Shared and exclusive locks

Learn shared vs exclusive locks.

Shared and Exclusive

A shared lock allows reads, an exclusive lock only one operation.

-- Shared: SELECT (okuma kilidi)
-- Exclusive: UPDATE/DELETE (yazma kilidi)

Shared reads, exclusive writes.

Shared and Exclusive — details

Multiple shared locks can coexist. An exclusive lock is alone.

Readers don't block each other, but while one transaction writes (exclusive), others wait to read or change that row.

Practice Shared and Exclusive

Exclusively lock a row with SELECT ... FOR UPDATE.

Starter code

query.sql
-- exclusive lock
SELECT * FROM accounts WHERE id = 1

Sample solution

query.sql
SELECT * FROM accounts WHERE id = 1 FOR UPDATE;

Expected output

id  balance
1   500

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Exclusive

Question
____ → yazma kilidi
Answer
Exclusive → yazma kilidi

Explanation: Exclusive is the right answer.

2 How many operations does an exclusive lock allow?

  • Two
  • Unlimited
  • Three
  • One Correct answer

Explanation: An exclusive lock allows only one operation.

3 Shared and exclusive locks: match the terms

Term Meaning
shared read
exclusive write
many shared compatible
one exclusive exclusive

All lessons in Advanced