WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

MySQL · Intermediate · question 39 of 100

What are the differences between the various types of MySQL locks: shared locks, exclusive locks, and intention locks?

📕 Buy this interview preparation book: 100 MySQL questions & answers — PDF + EPUB for $5

MySQL Locks are used to manage concurrent accesses to the same data, ensuring data consistency and preventing race conditions. MySQL supports several types of locks, including shared locks, exclusive locks, and intention locks.

Shared Locks: Shared locks (also known as Read lock) allow multiple sessions to read a specific resource simultaneously, while preventing any of them from modifying the resource. A shared lock does not conflict with another shared lock, but it conflicts with an exclusive lock.

To acquire a shared lock on a resource, you can use the ‘SELECT ... LOCK IN SHARE MODE‘ statement. For example, the following query acquires a shared lock on the ‘employees‘ table:

SELECT * FROM employees LOCK IN SHARE MODE;

Exclusive Locks: Exclusive locks (also known as Write lock) allow only one session to modify a specific resource, while preventing any other sessions from reading or modifying the resource. An exclusive lock conflicts with any shared or exclusive locks.

To acquire an exclusive lock on a resource, you can use the ‘SELECT ... FOR UPDATE‘ statement. For example, the following query acquires an exclusive lock on the ‘employees‘ table:

SELECT * FROM employees WHERE id = 1 FOR UPDATE;

Intention Locks: Intention locks are used to indicate the intention to acquire a shared or exclusive lock on a resource at a higher level of granularity. These locks do not prevent other sessions from acquiring the same intention lock.

There are two types of intention locks: ‘INTENTION SHARED‘ (IS) and ‘INTENTION EXCLUSIVE‘ (IX). An ‘IS‘ lock indicates the intention to acquire a shared lock on a resource at a lower level of granularity, while an ‘IX‘ lock indicates the intention to acquire an exclusive lock on a resource at a lower level of granularity.

For example, to acquire an intention lock on a table, you can use the ‘LOCK TABLES‘ statement:

LOCK TABLES employees INTENTION SHARED;

This statement acquires an ‘IS‘ lock on the ‘employees‘ table, indicating the intention to acquire a shared lock on the table at a lower level of granularity.

In summary, shared locks allow multiple sessions to read a specific resource, exclusive locks allow only one session to modify a specific resource, and intention locks indicate the intention to acquire a lock on a resource at a lower level of granularity. It’s important to choose the right type of lock depending on the use case to ensure proper concurrency control, prevent deadlocks and optimize performance.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic MySQL interview — then scores it.
📞 Practice MySQL — free 15 min
📕 Buy this interview preparation book: 100 MySQL questions & answers — PDF + EPUB for $5

All 100 MySQL questions · All topics