Skip to Main Content
  • Questions
  • Regarding Row locks on Parent and child tables.

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Balaji.

Asked: November 06, 2015 - 8:10 am UTC

Last updated: November 07, 2015 - 5:29 am UTC

Version: 12.1.02

Viewed 10K+ times! This question is

You Asked

Hi Tom,

I have a two tables TABLE-A ( Parent Table) and Table-B (Child Table).

TABLE-A has (COL-A).

TABLE-B has (COL-A Refrences Table-A(COL-A) and also COL-A is primary key in TABLE-B).

My question is if i am updating the TABLE-B( Child Table ) LIKE

UPDATE TABLE-B
SET SOME-COL = VAL
WHERE COL-A = SOME-VAL.

Will the row in parent table with COL-A gets locked.

Please let me know how row locks work in this kind of scenario.

and Connor said...

Some good information here:

http://asktom.oracle.com/Misc/something-i-recently-unlearned.html

But to paraphrase:

"If you:
a) update the parent primary key, or
b) delete from parent

Then you should probably index the foreign key in the child table - else there will be a full table lock placed on the child table"

The duration of that lock varies from release to release of Oracle, but in recent versions it will be whilst the statement is running.

Hope this helps.

Connor
@connor_mc_d

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Balaji, November 07, 2015 - 4:30 am UTC

Hi Connor,

Thanks for the update. It helps in understanding the row lock. But my question is while we update the child table (TABLE-B) where it has a column (COL-A) which is foreign key column on table(TABLE-A) and that (COL-A) also acts as a primary key in (TABLE-B). Basically a One-on-one relation. In this case while we update a particular row in child table, Want to understand how row lock works? And whether parent table gets locked or not while updating child table. Hope i am clear in explaining my question.

The document talks more about locks on child table while updating parent table.
Connor McDonald
November 07, 2015 - 5:28 am UTC

Best shown with an example


Session 1:
==========
SQL> create table PAR ( p int primary key, d date);

Table created.

SQL>
SQL> create table CHD ( p int primary key references PAR(p), d date);

Table created.

SQL>
SQL> insert into PAR values (1,sysdate);

1 row created.

SQL> insert into PAR values (2,sysdate);

1 row created.

SQL>
SQL> insert into CHD values (1,sysdate);

1 row created.

SQL>
SQL> commit;

Commit complete.

SQL> update CHD set d = sysdate + 1;

1 row updated.

Session 2
=========
SQL> lock table PAR in exclusive mode nowait;

Table(s) Locked.

Session 1:
==========
SQL> update CHD set p = 2;
[blocked, ie, waits]



So an exclusive lock on the parent stops the primary key update on CHD

But less extreme (and more likely usage) will be fine, eg (picking up from the point of Session 2 above)

Session 2
=========
SQL> update PAR set d = d + 1;

2 rows updated.

Session 1
=========
SQL> update CHD set p = 2;

1 row updated.



Hope this helps