Wednesday, September 10, 2008

Will DDL statement commit automatically

I have read in some books that issuing ddl statement will commit the transaction automatically irrespective of the success of the DDL statement.

I performed a small test and came to a conclusion that,
  • If ddl statement is executed successfully, the transaction is committed automatically.
  • If ddl statement fails due to logical error, the transaction is committed automatically.
  • If ddl statement fails due to syntax error, the transaction is neither committed nor rolled back, the transaction is still open.
Details of the test performed

SQL> create table testddl ( a number, b date);
Table created.
SQL> insert into testddl values (1, sysdate);
1 row created.
SQL> insert into testddl values (2, sysdate);
1 row created.
Not committed.
SQL> alter table testddl add constraint a_uq unique (a);
Table altered.
Automatically commits - ddl statement succeed.

SQL> rollback;
Rollback complete.

SQL> select * from testddl;
A B
-- --------
1 10-SEP-08
2 10-SEP-08

SQL> insert into testddl values (3, sysdate);
1 row created.
SQL> insert into testddl values (4, sysdate);
1 row created.
not committed

SQL> alter table testddl add constraint b_fk b references test (b);
alter table testddl add constraint b_fk b references test (b)
*
ERROR at line 1:ORA-01430: column being added already exists in table

Error but automatically commits. DDL Failed due to logical error.

SQL> rollback;
Rollback complete.

SQL> select * from testddl;
A B
-- --------
1 10-SEP-08
2 10-SEP-08

3 10-SEP-08
4 10-SEP-08


SQL> insert into testddl values (5, sysdate);
1 row created.
SQL> insert into testddl values (6, sysdate);
1 row created.
not committed

SQL> alter table testddl drp constraint a_uq;
alter table testddl drp constraint a_uq
*
ERROR at line 1:ORA-01735: invalid ALTER TABLE option

Error. ddl failed due to syntax error.Will not commit automatically.

SQL> rollback;
Rollback complete.

SQL> select * from testddl;
A B
-- --------
1 10-SEP-08

2 10-SEP-08
3 10-SEP-08

4 10-SEP-08

This is how the ddl statement is processed.
DDL parse
If success
commit
execute ddl
commit
end if
else
do nothing.

Hope you have enjoyed...

1 comment:

Anonymous said...

Interesting.

Jacobs

Followers

Powered By Blogger
 

Oracle Database Administration. Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com