【基础知识】什么时候需要commit?

今天有一童鞋来问我delete是不是要commit,我也只知道个大概,所以顺便就整理下。


COMMIT

对于commit,官方文档《SQL Language Reference》解释如下:

Purpose

Use the COMMIT statement to end your current transaction and makepermanent all changes performed in the transaction. A transaction is a sequence of SQL statements that Oracle Databasetreats as a single unit. This statement also erases all savepoints in thetransaction and releases transaction locks.

 

Until you commit a transaction:

  • You can see anychanges you have made during the transaction by querying the modified tables, butother users cannot see the changes. After you commit the transaction, thechanges are visible to other users' statements that execute after the commit.

  • You can rollback (undo) any changes made during the transaction with the ROLLBACK statement(seeROLLBACK.

 

Oracle Database issues an implicit COMMITunder the following circumstances:

  • Before any syntactically valid data definition language(DDL) statement, even if the statement results in an error

  • After any data definition language (DDL) statement thatcompletes without an error

 

You can also usethis statement to:

  • Commit an in-doubt distributed transaction manually

  • Terminate a read-only transaction begun by a SET TRANSACTIONstatement

 

Oracle recommends that you explicitly end every transactionin your application programs with a COMMIT or ROLLBACK statement, including thelast transaction, before disconnecting from Oracle Database. If you do not explicitly commit the transaction and the programterminates abnormally, then the last uncommitted transaction is automaticallyrolled back.

 

A normal exit from most Oracle utilitiesand tools causes the current transaction to be committed. A normal exit from anOracle precompiler program does not commit the transaction and relies on OracleDatabase to roll back the current transaction.


使用情况

其实DML操作时需要commit;
DDL操作不需要commit,DDL隐含了commit。


Data ManipulationLanguage (DML) Statements

Data manipulationlanguage (DML) statements query or manipulate data in existing schema objects.Whereas DDL statements enable you to change the structure of the database, DMLstatements enable you to query or change the contents. For example, ALTER TABLEchanges the structure of a table, whereas INSERT adds one or more rows to thetable.

DML statementsare the most frequently used SQL statements and enable you to:

  • Retrieve or fetchdata from one or more tables or views (SELECT).
  • Add new rows ofdata into a table or view (INSERT) by specifying a list of column values orusing a subquery to select and manipulate existing data.
  • Change columnvalues in existing rows of a table or view (UPDATE).
  • Update or insertrows conditionally into a table or view (MERGE).
  • Remove rows fromtables or views (DELETE).
  • View theexecution plan for a SQL statement (EXPLAIN PLAN). See "How OracleDatabase Processes DML".
  • Lock a table or view, temporarily limiting access by other users (LOCK TABLE).


Data DefinitionLanguage (DDL) Statements

Data definitionlanguage (DDL) statements define, structurally change, and drop schema objects.For example, DDL statements enable you to:

  • Create, alter,and drop schema objects and other database structures, including the databaseitself and database users. Most DDL statements start with the keywordsCREATE,ALTER, orDROP.
  • Delete all thedata in schema objects without removing the structure of these objects(TRUNCATE).
Note:Unlike DELETE,TRUNCATE generates no undo data, which makes it faster than DELETE. Also,TRUNCATE does not invoke delete triggers.
  • Grant and revokeprivileges and roles (GRANT,REVOKE).
  • Turn auditingoptions on and off (AUDIT, NOAUDIT).
  • Add a comment tothe data dictionary (COMMENT).

DDL enables you to alter attributes of an object without altering the applications that accessthe object. For example, you can add a column to a table accessed by a humanresources application without rewriting the application. You can also use DDLto alter the structure of objects while database users are performing work inthe database.


你可能感兴趣的:(oracle,commit)