Set transaction 允许设置不同的事务属性,如事务的隔离级别以及事务时只读的还是可读可写的。
原子性
语句级原子性
SQL> create table t2(cnt int);
Table created.
SQL> insert into t2 values(0);
1 row created.
SQL> create table t(x int check(x>0));
Table created.
SQL> create or replace trigger t_trigger 2 before insert or delete on t for each row 3 begin 4 if ( inserting ) then 5 update t2 set cnt=cnt+1; 6 else 7 update t2 set cnt=cnt-1; 8 end if; 9 dbms_output.put_line(‘I fired and updated‘||sql%rowcount||‘rows‘); 10 end; 11 /
SQL> set serveroutput on SQL> select * from t2;
CNT ---------- 0
SQL> select * from t;
no rows selected
SQL> insert into t values(1); I fired and updated1rows
1 row created.
SQL> insert into t values(-1); I fired and updated1rows insert into t values(-1) * ERROR at line 1: ORA-02290: check constraint (EODA.SYS_C007120) violated
SQL> select * from t2;
CNT ---------- 1
过程级原子性 Oracle把PL/SQL匿名块也当做是语句
SQL> create or replace procedure p 2 as 3 begin 4 insert into t values(1); 5 insert into t values(-1); 6 end; 7 /
Procedure created.
SQL> delete from t;
0 rows deleted.
SQL> update t2 set cnt=0;
1 row updated.
SQL> commit;
Commit complete.
SQL> select * from t;
no rows selected
SQL> select * from t2;
CNT ---------- 0
SQL> begin 2 p; 3 end; 4 / begin * ERROR at line 1: ORA-02290: check constraint (EODA.SYS_C007120) violated ORA-06512: at "EODA.P", line 5 ORA-06512: at line 2
SQL> set serveroutput on SQL> / I fired and updated1rows I fired and updated1rows begin * ERROR at line 1: ORA-02290: check constraint (EODA.SYS_C007120) violated ORA-06512: at "EODA.P", line 5 ORA-06512: at line 2
SQL> update parent set pk=2; update parent set pk=2 * ERROR at line 1: ORA-02292: integrity constraint (EODA.CHILD_FK_PARENT) violated - child record found
由于约束是IMMEDIATE模式,update失败,换为deferred模式 SQL> set constraint child_fk_parent deferred;
Constraint set.
SQL> update parent set pk=2;
1 row updated.
SQL> set constraint child_fk_parent immediate; set constraint child_fk_parent immediate * ERROR at line 1: ORA-02291: integrity constraint (EODA.CHILD_FK_PARENT) violated - parent key not found