主外來鍵約束之主表插入未提交導致外來鍵表插入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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- oracle主外來鍵鎖_lock_約束Oracle
- 批量刪除MSSQL 中主外來鍵約束SQL
- Javaweb-約束-外來鍵約束JavaWeb
- SQL的主鍵和外來鍵約束SQL
- Oracle定義約束 外來鍵約束Oracle
- oracle外來鍵約束的總結Oracle
- 教你mysql如何增加外來鍵約束MySql
- MySQL禁用恢復外來鍵約束MySql
- 【實驗】【外來鍵】小議外來鍵約束對應用程式的影響
- SQL外來鍵約束的含義及建立SQL
- Oracle外來鍵約束中NULL的處理OracleNull
- mysql 刪除老是報外來鍵約束MySql
- 查詢(看)表的主鍵、外來鍵、唯一性約束和索引索引
- 利用虛擬列實現虛擬刪除的主外來鍵約束
- ORACLE查詢表之間的主外來鍵關係Oracle
- ORACLE: 查詢(看)表的主鍵、外來鍵、唯一性約束和索引Oracle索引
- SQL SERVER中找出拙劣的約束,索引,外來鍵SQLServer索引
- enq: TM - contention解決之道——外來鍵無索引導致鎖爭用ENQ索引
- AppBoxFuture(七): 分散式外來鍵約束APP分散式
- mysql不能新增外來鍵約束怎麼辦MySql
- mysql啟動和關閉外來鍵約束MySql
- 詳解外來鍵約束(foreign key)
- 修改外來鍵為validate時需要驗證資料是否符合外來鍵約束
- 為什麼在資料倉儲中很少使用主外來鍵約束
- Oracle 查詢表與表之間的 主外來鍵關係Oracle
- 解決無法刪除表,提示被外來鍵約束引用
- 關於主外來鍵表資料提交過程中的一點疑惑
- 資料完整性約束:主鍵、外來鍵、各種約束的建立刪除語句
- enq: TM - contention解決之道——外來鍵無索引導致鎖爭用(上)ENQ索引
- enq: TM - contention解決之道——外來鍵無索引導致鎖爭用(下)ENQ索引
- 遷移後處理外來鍵約束的問題
- PostgreSQL11preview-支援陣列外來鍵約束SQLView陣列
- 外來鍵約束drop table cascade constraintsAI
- 生成指令碼,得到所有表的外來鍵約束,然後刪除並重建這些約束指令碼
- [20121020]主外來鍵約束以及NULL問題.txtNull
- 求主外來鍵的關係
- 資料庫約束 主鍵-唯一性-Check-外來鍵資料庫
- 查詢外來鍵約束、子表欄位等資訊的SQLSQL