Search code examples
databaseoracle-databasecommitddldml

Why DML commands executed before and after a DDL command are auto committed?


I know that DDL commands are auto committed but why are DML commands?

This is what I did:

  1. inserted data into an already existing table called table1.

  2. created a table called table2.

  3. inserted some more data into table1.

After i exited and logged in again the data entered in the steps one and three persisted without me having to give any explicit commit.

Why are the DML commands autocommited in this scenario?


Solution

  • DML is not committed by default.

    the data entered in the steps one and three persisted without me having to give any explicit commit

    The records inserted in step 1 were committed when you executed the CREATE TABLE statement in step 2. You know DDL commands in Oracle issue a commit before and after the DDL is executed, so you should not be surprised that an open transaction is committed when you create a table.

    The committal of the data from step 3 is different. I repeat again that DML is not committed automatically in Oracle. However, often the client software we use to connect to the database can be configured to issue commits after every statement.

    For instance, Autocommit is an option in IDEs like TOAD and PL/SQL Developer. If you use such a tool check Tools > Preferences for the pertinent setting (exact path may vary for a specific product). JDBC connections auto-commit by default. Same for Microsoft ODBC. SQL*Plus has AUTOCOMMIT = OFF by default, but it can be set ON.

    Also, SQL*Plus will commit rows on exit, whether that's by connecting to a new session or typing exit (but not by an uncontrolled exit such as killing the client process through Task Manager ). Since Oracle 11gR2 such behaviour is configurable through the EXITCOMMIT system variable; the default is ON.


    Here is a db<>fiddle demo which demonstrates that DML before a DDL statement is committed but - in a client where AUTOCOMMIT is off - DML after the statement can be rolled back.