ORACLE SQL總結二:集合操做符合和DML語句

三、集合操做符
3.1 集合操做符只能出如今order by從句以前。sql

四、DML語句
4.1 SAVEPOINT
摘自ORACLE sql references
Savepoint names must be distinct within a given transaction. If you create a second savepoint with the same identifier as an earlier savepoint, then the earlier savepoint is erased. After a savepoint has been created, you can either continue processing, commit your work, roll back the entire transaction, or roll back to the savepoint.
翻譯以下:
在一個給定的事務中Savepoint的命名必須是惟一的,若是你產生了一個和較早以前名字相同的savepoint,那麼較早以前的那個將被清除。在savepoint被產生以後,你能夠繼續操做,或者提交(commmit)你的工做或者回滾整個事務,或者回滾到某個savepoint。數據庫

注意COMMIT後全部savepoint都會被清除,示例以下:
oracle

SQL> update employees
  2  set salary=7000
  3  where last_name='Banda';

1 row updated.

SQL> savepoint banda_sal;

Savepoint created.

SQL> update employees
  2  set salary=7000
  3  where last_name='Greene';

1 row updated.

SQL> select last_name,salary from employees where last_name='Banda' or last_name
='Greene';

LAST_NAME                                              SALARY
-------------------------------------------------- ----------
Banda                                                    7000
Greene                                                   7000

SQL> savepoint greene_sal;

Savepoint created.

SQL> rollback to savepoint banda_sal;

Rollback complete.

SQL> select last_name,salary from employees where last_name='Banda' or last_name
='Greene';

LAST_NAME                                              SALARY
-------------------------------------------------- ----------
Banda                                                    7000
Greene                                                   9500

SQL> commit;

Commit complete.

SQL> rollback to savepoint greene_sal;
rollback to savepoint greene_sal
*
ERROR at line 1:
ORA-01086: savepoint 'GREENE_SAL' never established

關於這個問題,oracle sql references有比較詳細的解釋,摘錄以下:
Use the COMMIT statement to end your current transaction and make permanent all changes performed in the transaction. A transaction is a sequence of SQL statements that Oracle Database treats as a single unit. This statement also erases all savepoints in the transaction and releases transaction locks.

使用COMMIT能夠終止你當前的事務,並永久性地改變數據庫中的數據(寫到日誌文件)。一個事務是一組SQL語句有序組合,ORACLE數據庫將它(事務)做爲單個單元處理。COMMIT(提交後)也清除了全部的SAVEPOINT,並釋放了事務鎖。ide

Until you commit a transaction:翻譯

.You can see any changes you have made during the transaction by querying the modified tables, but other users cannot see the changes. After you commit the transaction, the changes are visible to other users' statements that execute after the commit.3d

.You can roll back (undo) any changes made during the transaction with the ROLLBACK statement .日誌

在你COMMIT一個事務以前:
在當前事務中,你能夠看到任何你改變的數據,但其餘用戶看不到這些改變。直到你COMMIT了事務,他們才能夠看見。
在當前事務中,你能夠ROLLBACK任何改變。code

Oracle Database issues an implicit COMMIT under the following circumstances:orm

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

-After any data definition language (DDL) statement that completes without an error

ORACLE數據庫隱式地執行COMMIT在以下情形下:
-在任何DDL語句語法驗證以前,甚至這條語句執行結果是錯誤的。
-在任何DDL語句沒有錯誤地執行完成以後。


4.2 ROLLBACK的疑問,DBA_2PC_PENDING爲何是空的?
ROLLBACK中的FORCE從句
FORCE Clause

Specify FORCE to manually roll back an in-doubt distributed transaction. The transaction is identified by the string containing its local or global transaction ID. To find the IDs of such transactions, query the data dictionary view DBA_2PC_PENDING.

A ROLLBACK statement with a FORCE clause rolls back only the specified transaction. Such a statement does not affect your current transaction.

SQL> desc dba_2pc_pending;
 Name                    Null?    Type
 ----------------------- -------- ----------------
 LOCAL_TRAN_ID           NOT NULL VARCHAR2(22)
 GLOBAL_TRAN_ID                   VARCHAR2(169)
 STATE                   NOT NULL VARCHAR2(16)
 MIXED                            VARCHAR2(3)
 ADVICE                           VARCHAR2(1)
 TRAN_COMMENT                     VARCHAR2(255)
 FAIL_TIME               NOT NULL DATE
 FORCE_TIME                       DATE
 RETRY_TIME              NOT NULL DATE
 OS_USER                          VARCHAR2(64)
 OS_TERMINAL                      VARCHAR2(255)
 HOST                             VARCHAR2(128)
 DB_USER                          VARCHAR2(30)
 COMMIT#                          VARCHAR2(16)

SQL> select * from dba_2pc_pending where db_user='OE';

no rows selected
相關文章
相關標籤/搜索