SQL> select empno, ename, sal from emp where deptno = 10;
EMPNO ENAME SAL
---------- ---------- ----------
7782 CLARK 2450
7839 KING 5000
7934 MILLER 1300
SQL> variable empno number
SQL> variable ename varchar2(20)
SQL> variable sal number
SQL> exec :empno := 7934; :ename := 'MILLER'; :sal := 1300;
PL/SQL procedure successfully completed
SQL> select empno, ename, sal
2 from emp
3 where empno = :empno
4 and ename = :ename
5 and sal = :sal
6 for update nowait;
EMPNO ENAME SAL
---------- ---------- ----------
7934 MILLER 1300
SQL> update emp
2 set ename = :ename, sal = :sal
3 where empno = :empno;
1 row updated.
SQL> commit;
Commit complete.
Update table
Set column1 = :new_column1, column2 = :new_column2, ....
Where primary_key = :primary_key
And column1 = :old_column1
And column2 = :old_column2
...
SQL> create table dept_1
2 (deptno number(2),
3 dname varchar2(14),
4 loc varchar2(13),
5 last_mod timestamp with time zone default systimestamp not null,
6 constraint dept_1_pk primary key(deptno)
7 );
Table created.
SQL> insert into dept_1( deptno, dname, loc )
2 select deptno, dname, loc
3 from dept;
4 rows created.
SQL> commit;
Commit complete.
SQL> variable deptno number
SQL> variable dname varchar2(14)
SQL> variable loc varchar2(13)
SQL> variable last_mod varchar2(50)
SQL> begin
2 :deptno := 10;
3 select dname, loc, last_mod
4 into :dname,:loc,:last_mod
5 from dept_1
6 where deptno = :deptno;
7 end;
8 /
PL/SQL procedure successfully completed.
SQL> select :deptno dno, :dname dname, :loc loc, :last_mod lm
2 from dual;
DNO DNAME LOC LM
---- ---------- ------------ -----------------------------------
10 ACCOUNTING NEW YORK 22-OCT-14 08.25.00.875694 AM +09:00
SQL> update dept_1
2 set dname = initcap(:dname),
3 last_mod = systimestamp
4 where deptno = :deptno
5 and last_mod = to_timestamp_tz(:last_mod);
1 row updated.
SQL> update dept_1
2 set dname = upper(:dname),
3 last_mod = systimestamp
4 where deptno = :deptno
5 and last_mod = to_timestamp_tz(:last_mod);
0 rows updated.
SQL> alter table dept_1 drop column last_mod;
Table altered.
SQL> variable deptno number
SQL> variable dname varchar2(14)
SQL> variable loc varchar2(13)
SQL> variable hash number
SQL> begin
2 select deptno, dname, loc,
3 ora_hash(dname || '/' || loc) hash
4 into :deptno, :dname, :loc, :hash
5 from dept_1
6 where deptno = 10;
7 end;
8 /
PL/SQL procedure successfully completed.
:DEPTNO :DNAME :LOC :HASH
---------- ------------- ------------ ----------
10 ACCOUNTING NEW YORK 401273349
SQL> exec :dname := lower(:dname);
PL/SQL procedure successfully completed.
SQL> update dept_1
2 set dname = :dname
3 where deptno = :deptno
4 and ora_hash(dname || '/' || loc) = :hash
5 /
1 row updated.
SQL> select dept_1.*,
2 ora_hash(dname|| '/' || loc) hash
3 from dept_1
4 where deptno = :deptno ;
DEPTNO DNAME LOC HASH
---------- -------------- ------------- ----------
10 accounting NEW YORK 2818855829
SQL> update dept_1
2 set dname = :dname
3 where deptno = :deptno
4 and ora_hash(dname || '/' || loc) = :hash
5 /
0 rows updated.
SQL> alter table dept_1
2 add hash as
3 (ora_hash(dname || '/' || loc) );
SQL> select *
2 from dept_1
3 where deptno =:deptno
.....
SQL> create table demo ( x int primary key );
Table created.
SQL> create or replace trigger demo_bifer
before insert on demo
for each row
declare
l_lock_id number;
resource_busy exception;
pragma exception_init( resource_busy, -54 );
begin
l_lock_id :=
dbms_utility.get_hash_value( to_char( :new.x ), 0, 1024 );
if ( dbms_lock.request
( id => l_lock_id,
lockmode => dbms_lock.x_mode,
timeout => 0,
release_on_commit => TRUE ) <> 0 )
then
raise resource_busy;
end if;
end;
Trigger created.
SQL> insert into demo values ( 1 );
1 row created.
SQL> insert into demo values ( 1 );
insert into demo values ( 1 )
*
ERROR at line 1:
ORA-00054: resource busy and acquire with NOWAIT specified
ORA-06512: at "UBACK.DEMO_BIFER", line 14
ORA-04088: error during execution of trigger 'UBACK.DEMO_BIFER'
Session A | Session B |
Table A 수정 | Table B 수정 |
Blocking | Table A 수정 |
Table B 수정 | Deadlock |
update a set x = x+1
*
ERROR at line 1:
ORA-00060: deadlock detected while waiting for resource
*** 2005-04-25 15:53:01.455
*** ACTION NAME:() 2005-04-25 15:53:01.455
*** MODULE NAME:(SQL*Plus) 2005-04-25 15:53:01.455
*** SERVICE NAME:(SYS$USERS) 2005-04-25 15:53:01.455
*** SESSION ID:(145.208) 2005-04-25 15:53:01.455
DEADLOCK DETECTED
Current SQL statement for this session:
update a set x = 1
The following deadlock is not an ORACLE error. It is a
deadlock due to user error in the design of an application
or from issuing incorrect ad-hoc SQL. The following
information may aid in determining the deadlock:...
SQL> create table p ( x int primary key );
Table created.
SQL> create table c ( x references p );
Table created.
SQL> insert into p values ( 1 );
1 row created.
SQL> insert into p values ( 2 );
1 row created.
SQL> commit;
Commit complete.
SQL> insert into c values ( 2 );
1 row created.
SQL> delete from p where x = 1;
SQL> update dept set deptno=:1,dname=:2,loc=:3 where rowid=:4
2 ;
SQL> column columns format a30 word_wrapped
SQL> column tablename format a15 word_wrapped
SQL> column constraint_name format a15 word_wrapped
SQL> select table_name, constraint_name,
cname1 || nvl2(cname2,','||cname2,null) ||
nvl2(cname3,','||cname3,null) || nvl2(cname4,','||cname4,null) ||
nvl2(cname5,','||cname5,null) || nvl2(cname6,','||cname6,null) ||
nvl2(cname7,','||cname7,null) || nvl2(cname8,','||cname8,null)
columns
from ( select b.table_name,
b.constraint_name,
max(decode( position, 1, column_name, null )) cname1,
max(decode( position, 2, column_name, null )) cname2,
max(decode( position, 3, column_name, null )) cname3,
max(decode( position, 4, column_name, null )) cname4,
max(decode( position, 5, column_name, null )) cname5,
max(decode( position, 6, column_name, null )) cname6,
max(decode( position, 7, column_name, null )) cname7,
max(decode( position, 8, column_name, null )) cname8,
count(*) col_cnt
from (select substr(table_name,1,30) table_name,
substr(constraint_name,1,30) constraint_name,
substr(column_name,1,30) column_name,
position
from user_cons_columns ) a,
user_constraints b
where a.constraint_name = b.constraint_name
and b.constraint_type = 'R'
group by b.table_name, b.constraint_name
) cons
where col_cnt > ALL
( select count(*)
from user_ind_columns i
where i.table_name = cons.table_name
and i.column_name in (cname1, cname2, cname3, cname4,
cname5, cname6, cname7, cname8 )
and i.column_position <= cons.col_cnt
group by i.index_name
)
/ TABLE_NAME CONSTRAINT_NAME COLUMNS
---------------------------- --------------- ------------------------
C SYS_C00108609 X
- 강좌 URL : http://www.gurubee.net/lecture/4019
- 구루비 강좌는 개인의 학습용으로만 사용 할 수 있으며, 다른 웹 페이지에 게재할 경우에는 출처를 꼭 밝혀 주시면 고맙겠습니다.~^^
- 구루비 강좌는 서비스 제공을 위한 목적이나, 학원 홍보, 수익을 얻기 위한 용도로 사용 할 수 없습니다.