Updated February 27, 2023
Introduction to Oracle LOCK TABLE
Lock Table in Oracle can be defined as a statement which can be used to lock one or more tables, table partitions or even table sub partitions which explicitly as when we use this statement basically overrides the automatic locking feature already present in the oracle and locks the table or tables in a specified mode as defined by the user which means the user can permit or deny certain operations on the locked table to other users for the duration of the user’s operation.
Given below is the syntax of Oracle Lock table:
LOCK TABLE table_name IN lock_mode MODE [WAIT | NOWAIT ];
- table_name: It refers to the name of the table.
- Lock_mode: It refers to the mode on which we are going to lock the table. There are many modes.
- ROW_SHARE: This mode allows concurrent access to the table in which it is applied. Here concurrent access means that it allows multiple users to access the table simultaneously and the users who are accessing the table are not allowed to lock the entire table which means no exclusive access.
- ROW_EXCLUSIVE: This mode is just like the above allows concurrent access to the table. It means multiple users can access it simultaneously and the users who are accessing the table are not allowed to lock the entire table. The difference of this mode with ROW_SHARE is that it also does not allow locking in share mode.
- SHARE UPDATE: It is similar like ROW_SHARE. It allows concurrent access to the table. It allows multiple user access to the table. It does not allow users to lock the entire table.
- SHARE: It is similar to SHARE UPDATE. It allows concurrent access to the table. It allows multiple user access to the table. It does not allow users to lock the entire table. It also prohibits updates to the locked table.
- SHARE ROW EXCLUSIVE: This mode allows Users to view records in the table but it does not allow the users to update the table or from locking the table in SHARE mode.
- EXCLUSIVE: This mode allows only queries to be executed on the Locked table. No other activities are allowed other than that.
- WAIT: This keyword when used allows the database to wait until the table is available and then lock the table and returns the control to the user.
- NOWAIT: This condition refers to the condition when the database does not wait for the lock to be released. This is useful when the user wants the database to return control to him/her immediately.
How LOCK TABLE Statement works in Oracle?
- When we use LOCK Table statement and after it gets executed the database overrides the manual automatic locking available in Oracle and permits or denies other users to view or update the table for a specified time as mentioned in the statement.
- The permission actually depends on the type of mode of Lock that the user has chosen.
- In case suppose the user has chosen the mode EXCLUSIVE then other users can only use queries on that table and the database will not allow any update on that table from other users.
- In case it is ROW SHARE mode then the database will not allow the user to lock the whole table and will allow concurrent access to the table.
- The duration of time is also specified in the statement or queries itself.
- So, it actually depend on the type of mode u provide to the database and depending on that the database will lock your table or tables.
Examples of Oracle LOCK TABLE
Given below are the examples mentioned:
EXCLUSIVE MODE WITH NO WAIT.
Here we will look at the Exclusive mode of operation. We will lock the table employee in the Exclusive mode with NO WAIT which means that it will not wait if another user has already locked the table.
LOCK TABLE employee
IN EXCLUSIVE MODE NOWAIT;
As we can see in the output screenshot the output says that Lock succeeded which means that the employee is successfully locked in Exclusive mode.
SHARE MODE WITH NO WAIT.
In this case, we will slightly change the situation we will issue a lock on the employee table present in the database in SHARE MODE with NO WAIT. One important point to note is that once the share lock is obtained, exclusive locks cannot be obtained. NO WAIT means that it will not wait for a lock to be released.
LOCK TABLE employee
IN SHARE MODE NOWAIT;
As per the screenshot, we can see that the SHARE lock has been successfully obtained on the Employee table.
EXCLUSIVE MODE WITH WAIT.
In this example, we will look at the Exclusive mode of operation. We will lock the table employee in the Exclusive mode with WAIT which means that the database will wait until the table is available and then lock the table.
LOCK TABLE employee
IN EXCLUSIVE MODE WAIT 5;
In the above query, the wait time is mentioned as 5 seconds.
As per the screenshot, we can see the lock was successfully obtained.
In this article, we saw the definition of the LOCK TABLE statement and its syntax. We also saw the various modes of LOCK available and how they work. To get a better understanding we also went through a few examples.
This is a guide to Oracle LOCK TABLE. Here we discuss the introduction to Oracle LOCK TABLE along with the working and examples respectively. You may also have a look at the following articles to learn more –