主外來鍵約束之主表插入未提交導致外來鍵表插入hang住的等待事件 TX-row lock contention
實驗如下
--建立外來鍵參考表t3
SQL> create table t3 (id number primary key,name varchar2(20),product_id number);
Table created.
--建立主表t2
SQL> create table t2 (id number primary key,name varchar2(20));
Table created.
--給t3表加入外來鍵約束
SQL> alter table t3 add constraint FK_PRODUCTSTAT_PRODUCTID foreign key (PRODUCT_id) references t2 (ID);
Table altered.
SQL>
SQL> insert into t2 values(1,'dh');
1 row created.
SQL> insert into t2 values(2,'cc');
1 row created.
SQL> insert into t2 values(3,'cc');
1 row created.
SQL> commit;
Commit complete.
SQL> select * from t2;
ID NAME
---------- --------------------
1 dh
2 cc
3 cc
SQL> select * from t3;
no rows selected
--注意區分:
session1:
SQL> insert into t2 values(4,'cc');
1 row created.
未提交。。。
session 2:
SQL> insert into t3 values(1,'tt',4);
1 row created.
hang住,一直不動。。。
session 3:查詢:
SET LINES 200
col OWNER for a10
col R_OWNER for a10
col R_CONSTRAINT_NAME for a15
select a.table_name,
a.owner,
a.constraint_name,
a.constraint_type,
a.r_owner,
a.r_constraint_name,
b.table_name
from dba_constraints a, dba_constraints b
where a.constraint_type = 'R'
and a.r_constraint_name = b.constraint_name
and a.r_owner = b.owner
and b.table_name = 'T2'
and b.owner='SYS';
TABLE_NAME OWNER CONSTRAINT_NAME C R_OWNER R_CONSTRAINT_NA TABLE_NAME
------------------------------ ---------- ------------------------------ - ---------- --------------- ------------------------------
T3 SYS FK_PRODUCTSTAT_PRODUCTID R SYS SYS_C0012328 T2
SQL> select sid,serial#,sql_id,program,username,event,blocking_session from v$session where blocking_session is not null;
SID SERIAL# SQL_ID PROGRAM USERNAME EVENT BLOCKING_SESSION
---------- ---------- ------------- ------------------------------------------------ ------------------------------ ---------------------------------------------------------------- ----------------
53 427 1cgjmu64ztjch sqlplus@wang (TNS V1-V3) SYS enq: TX - row lock contention 57
SQL> select sql_text from v$sql where sql_id='1cgjmu64ztjch';
SQL_TEXT
-------------------------------------------------------------
insert into t3 values(1,'tt',4)
SQL>
SQL> select sid,serial#,sql_id,program,username,event,blocking_session from v$session where sid=57;
SID SERIAL# SQL_ID PROGRAM USERNAME EVENT BLOCKING_SESSION
---------- ---------- ------------- ------------------------------------------------ ------------------------------
57 575 sqlplus@wang (TNS V1-V3) SYS SQL*Net message from client
SQL>
現在將session1提交:
SQL> commit;
Commit complete.
SQL>
--session2插入也完成:
SQL> insert into t3 values(1,'tt',4);
1 row created.
SQL>SQL> commit;
Commit complete.
完成!!!!1111
--建立外來鍵參考表t3
SQL> create table t3 (id number primary key,name varchar2(20),product_id number);
Table created.
--建立主表t2
SQL> create table t2 (id number primary key,name varchar2(20));
Table created.
--給t3表加入外來鍵約束
SQL> alter table t3 add constraint FK_PRODUCTSTAT_PRODUCTID foreign key (PRODUCT_id) references t2 (ID);
Table altered.
SQL>
SQL> insert into t2 values(1,'dh');
1 row created.
SQL> insert into t2 values(2,'cc');
1 row created.
SQL> insert into t2 values(3,'cc');
1 row created.
SQL> commit;
Commit complete.
SQL> select * from t2;
ID NAME
---------- --------------------
1 dh
2 cc
3 cc
SQL> select * from t3;
no rows selected
--注意區分:
session1:
SQL> insert into t2 values(4,'cc');
1 row created.
未提交。。。
session 2:
SQL> insert into t3 values(1,'tt',4);
1 row created.
hang住,一直不動。。。
session 3:查詢:
SET LINES 200
col OWNER for a10
col R_OWNER for a10
col R_CONSTRAINT_NAME for a15
select a.table_name,
a.owner,
a.constraint_name,
a.constraint_type,
a.r_owner,
a.r_constraint_name,
b.table_name
from dba_constraints a, dba_constraints b
where a.constraint_type = 'R'
and a.r_constraint_name = b.constraint_name
and a.r_owner = b.owner
and b.table_name = 'T2'
and b.owner='SYS';
TABLE_NAME OWNER CONSTRAINT_NAME C R_OWNER R_CONSTRAINT_NA TABLE_NAME
------------------------------ ---------- ------------------------------ - ---------- --------------- ------------------------------
T3 SYS FK_PRODUCTSTAT_PRODUCTID R SYS SYS_C0012328 T2
SQL> select sid,serial#,sql_id,program,username,event,blocking_session from v$session where blocking_session is not null;
SID SERIAL# SQL_ID PROGRAM USERNAME EVENT BLOCKING_SESSION
---------- ---------- ------------- ------------------------------------------------ ------------------------------ ---------------------------------------------------------------- ----------------
53 427 1cgjmu64ztjch sqlplus@wang (TNS V1-V3) SYS enq: TX - row lock contention 57
SQL> select sql_text from v$sql where sql_id='1cgjmu64ztjch';
SQL_TEXT
-------------------------------------------------------------
insert into t3 values(1,'tt',4)
SQL>
SQL> select sid,serial#,sql_id,program,username,event,blocking_session from v$session where sid=57;
SID SERIAL# SQL_ID PROGRAM USERNAME EVENT BLOCKING_SESSION
---------- ---------- ------------- ------------------------------------------------ ------------------------------
57 575 sqlplus@wang (TNS V1-V3) SYS SQL*Net message from client
SQL>
現在將session1提交:
SQL> commit;
Commit complete.
SQL>
--session2插入也完成:
SQL> insert into t3 values(1,'tt',4);
1 row created.
SQL>SQL> commit;
Commit complete.
完成!!!!1111
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31397003/viewspace-2147029/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 外來鍵約束
- 批量刪除MSSQL 中主外來鍵約束SQL
- [20180423]關於閃回表與主外來鍵約束.txt
- Javaweb-約束-外來鍵約束JavaWeb
- 約束外來鍵筆記筆記
- 關於外來鍵約束
- oracle外來鍵約束的總結Oracle
- enq: TM - contention解決之道——外來鍵無索引導致鎖爭用ENQ索引
- 教你mysql如何增加外來鍵約束MySql
- AppBoxFuture(七): 分散式外來鍵約束APP分散式
- 等待事件enq: TX - row lock contention事件ENQ
- SQL外來鍵約束的含義及建立SQL
- mysql不能新增外來鍵約束怎麼辦MySql
- 求主外來鍵的關係
- 生成指令碼,得到所有表的外來鍵約束,然後刪除並重建這些約束指令碼
- mysql~資料完整性考慮~外來鍵約束MySql
- 資料庫的主外來鍵關係資料庫
- MySQL唯 一鍵約束場景下卻能插入重複行?MySql
- 聊聊Oracle外來鍵約束(Foreign Key)的幾個操作選項Oracle
- MySQL建立資料表並建立主外來鍵關係MySql
- 使用mybatis-plus,由於表名關鍵字導致插入資料失敗MyBatis
- Oracle等待事件之enq: TM – contentionOracle事件ENQ
- oracle等待事件之enq: CF – contentionOracle事件ENQ
- oracle 主外來鍵關係及實驗Oracle
- Mysql-基本練習(06-唯一約束、外來鍵約束、新增、刪除單列)MySql
- 無索引的外來鍵之主表子表DML操作實驗及結論索引
- 主鍵和外來鍵
- EntityFramework Core 遷移忽略主外來鍵關係Framework
- MYSQL的外來鍵MySql
- MariaDB資料庫的外來鍵約束例項程式碼介紹詳解資料庫
- 資料遷移無法新增外來鍵約束,錯誤程式碼 1215
- sqlserver外來鍵SQLServer
- indexedDB 內鍵與外來鍵Index
- 淺談Oracle 主外來鍵刪除語法格式Oracle
- 關鍵詞加粗和插入關鍵詞
- Django(15)外來鍵和表關係Django
- 外來鍵的變種
- 通過外來鍵找主鍵
- 10.30 索引,外來鍵索引