69

What are the list of statements that need to be committed before further action on the table in order to avoid a lock? I am not talking about full transactions with multiple statements and transaction integrity; instead I am referring to single statements.

I know insert should be committed but truncate has an autocommit. What is the full list of statements that need to be committed?

Need to be committed (starter list):

UPDATE
INSERT
DELETE
5
  • 1
    truncate removes data directly without copying it into the Rollback Tablespace. Is a DDL statement. Mar 2, 2012 at 22:12
  • 2
    does DCL have to be comitted? ie grant + revoke
    – toop
    Mar 2, 2012 at 22:17
  • Merge. Also select for update takes locks, even though it doesn't modify anything. Mar 3, 2012 at 1:05
  • 3
    @toop: As far as I've been able to tell, Oracle lumps DCL in with DDL. Being DDL it has the two implicit commits before and after statement execution. Mar 3, 2012 at 1:07
  • 3
    I was surprised that grant require commit!
    – gavenkoa
    May 8, 2014 at 11:20

4 Answers 4

78

DML (Data Manipulation Language) commands need to be commited/rolled back. Here is a list of those commands.

Data Manipulation Language (DML) statements are used for managing data within schema objects. Some examples:

INSERT - insert data into a table
UPDATE - updates existing data within a table
DELETE - deletes records from a table, the space for the records remain
MERGE - UPSERT operation (insert or update)
CALL - call a PL/SQL or Java subprogram
EXPLAIN PLAN - explain access path to data
LOCK TABLE - control concurrency
8
  • 2
    do you have to commit after SELECT or EXPLAIN PLAN?
    – toop
    Mar 2, 2012 at 22:15
  • 1
    @toop After a SELECT ... FOR UPDATE you have to in order to release the lock. Mar 2, 2012 at 22:17
  • 1
    what does a select for update look like in full? Also how about just a standard select * from table where blah?
    – toop
    Mar 2, 2012 at 22:20
  • 1
    @Fabian, a ROLLBACK will also release the lock.
    – DCookie
    Mar 2, 2012 at 22:26
  • 1
    A SELECT without FOR UPDATE may also contain a function call that modifies the database.
    – rics
    Mar 2, 2012 at 22:29
16

In mechanical terms a COMMIT makes a transaction. That is, a transaction is all the activity (one or more DML statements) which occurs between two COMMIT statements (or ROLLBACK).

In Oracle a DDL statement is a transaction in its own right simply because an implicit COMMIT is issued before the statement is executed and again afterwards. TRUNCATE is a DDL command so it doesn't need an explicit commit because calling it executes an implicit commit.

From a system design perspective a transaction is a business unit of work. It might consist of a single DML statement or several of them. It doesn't matter: only full transactions require COMMIT. It literally does not make sense to issue a COMMIT unless or until we have completed a whole business unit of work.

This is a key concept. COMMITs don't just release locks. In Oracle they also release latches, such as the Interested Transaction List. This has an impact because of Oracle's read consistency model. Exceptions such as ORA-01555: SNAPSHOT TOO OLD or ORA-01002: FETCH OUT OF SEQUENCE occur because of inappropriate commits. Consequently, it is crucial for our transactions to hang onto locks for as long as they need them.

0
7

DML have to be committed or rollbacked. DDL cannot.

http://www.orafaq.com/faq/what_are_the_difference_between_ddl_dml_and_dcl_commands

You can switch auto-commit on and that's again only for DML. DDL are never part of transactions and therefore there is nothing like an explicit commit/rollback.

truncate is DDL and therefore commited implicitly.

Edit
I've to say sorry. Like @DCookie and @APC stated in the comments there exist sth like implicit commits for DDL. See here for a question about that on Ask Tom. This is in contrast to what I've learned and I am still a bit curious about.

4
  • 1
    do you have to commit after SELECT or EXPLAIN PLAN?
    – toop
    Mar 2, 2012 at 22:18
  • This is technically incorrect: DDL always issue commits, and actually they issue 2 of them, at the start and at the end of the process. If you issue a DML statement, then issue DDL, your DML is committed.
    – DCookie
    Mar 2, 2012 at 22:27
  • 2
    -1. Of course DDL is committed. DDL is essentially the issuance of changes to the data dictionary, which have to committed so they can be applied. The difference is simply that the commits are implicit, and as DCookie states, there are two of them. So, if we issue an update on a table and then execute a DDL statement which fails we cannot rollback the update. It has been committed even though the subsequent DDL was not applied.
    – APC
    Mar 2, 2012 at 23:54
  • Oracle's treatment of DDL is different from (say) SQL Server. In SQL Server DDL statements are not autocommitted and so we can rollback a DROP TABLE statement. In Oracle we cannot. As I understand it, MySQL is the same as Oracle, PostgreSQL is like MSSQL. So it really depends on DBMS flavour.
    – APC
    Mar 3, 2012 at 19:19
4

And a key point - although TRUNCATE TABLE seems like a DELETE with no WHERE clause, TRUNCATE is not DML, it is DDL. DELETE requires a COMMIT, but TRUNCATE does not.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.