1、自动事务(一个语句要么全部执行,要么全不执行)
ALTER TABLE "SYSTEM"."STUDENT" ADD ( CHECK (height>0 and height<3) VALIDATE )
select * from student
insert into student values ( '000008','徐舟',1,TO_DATE('30-5月 -1994','dd-Mon-yyyy'),2.01);
select * from student
update student set height=height+1
select * from student
2、显式事务
CREATE TABLE TestBatch (Cola NUMBER PRIMARY KEY, Colb VARCHAR2(3))
2、1
下面的代码不能执行(查询检索通过不了)
BEGIN
INSERT INTO TestBatch VALUES (1, 'aaa');
INSERT INTO TestBatch VALUES (2, 'bbb');
-- Syntax error
INSERT INTO TestBatch VALUSE (3, 'ccc');
END;
/
select * from TestBatch
如果没有语句块,可以执行到错误语句之前
INSERT INTO TestBatch VALUES (1, 'aaa');
INSERT INTO TestBatch VALUES (2, 'bbb');
-- Syntax error
INSERT INTO TestBatch VALUSE (3, 'ccc');
--
delete from TestBatch
2、2
下面的代码会有自动事务管理,出现错误自动回退
BEGIN
INSERT INTO TestBatch VALUES (1,'aaa');
INSERT INTO TestBatch VALUES (2,'bbb');
-- Duplicate key error
INSERT INTO TestBatch VALUES (1,'ccc');
END;
/
select * from TestBatch
加入事务提交
BEGIN
INSERT INTO TestBatch VALUES (1,'aaa');
INSERT INTO TestBatch VALUES (2,'bbb');
commit;
-- Duplicate key error
INSERT INTO TestBatch VALUES (1,'ccc');
END;
/
select * from TestBatch
drop table TestBatch
3、自动提交
在SQLPLus中有一个与事务相关的环境变量autocommit,如果这个参数被启用,在SQLPLus中所有修改将在执行时自动提交,启动的方法是在
show autocommit;
update student set height=1;
select * from student;
rollback;
select * from student;
但是此时由于自动提交所以无法回退
set autocommit on;
show autocommit;
update student set height=1;
select * from student;
rollback;
select * from student;
set autocommit off;
注意:Oracle规定事务会在一些操作后自动提交,如DDL语句等。
[此贴子已经被作者于2010-12-11 20:21:44编辑过]