Chapter 09-Manipulation Data - 03

Rolling Back Changers to a Marker

  • Create a marker in the current transaction by using the SAVEPOINT statement.
  • Roll back to that marker by using the ROLLBACK TO SAVEPOINT statement.
UPDATE ...
SAVEPOINT update_done;
INSERT ...
ROLLBACK TO update_done;

Implict Transaction Processing

  • An automatic commit occurs in the following circumstances:
    • -A DDL statement is issued
    • -A DCT statement is issued
    • -Normal exit from SQL Developer or SQL*Plus,without explicitly issuing COMMIT or ROLLBACK statements.(Normal exit->commit;Abnormal exit->rollback;)
  • An automatic rollback occurs when there is an abormal termination of SQL Developer or SQL*Plus or a system failure.

 State of the Data Before COMMIT or ROLLBACK

  • The previous state of the data can be recovered.
  • The current user can review the resutls of the DML operations by using the SELECT statement.
  • Other users cannot view the results of the DML statements issued by the current user.
  • The affected rows are locked;other users cannot change the data in the affected rows.

State of the Data After COMMIT

  • Data changes are saved in the database.
  • The previous state of the data is overwritten.
  • All users can view the results.
  • Locks on the affected rows are released;those rows are available for other users to manipulate.
  • All savepoints are erased.

Committing Data

  • Make the changes:
DELETE FROM employees

WHERE employee_id = 999999;


INSERT INTO departments

VALUES(290,'Corporate Tax',NULL,1700);
  • Commit the changes
COMMIT;
COMMIT successded;

State of the Data After ROLLBACK

Discard all pending changes by using the ROLLBACK statement.

  • Data changes are undone.
  • Previous state of the data is restored.
  • Locks on the affected rows are released.
DELETE FROM copy_emp;

ROLLBACK;

State of the Data After ROLLBACK :Example

ROLLBACK DEMO
DELETE FROM test;

25,000 rows deleted.

ROLLBACK;

Rollback complete.

DELETE FROM test WHERE id=100;

1 row deleted.

SELECT * FROM test WHERE id=100;

No rows selected.

COMMIT;

commit complete.

Statement-Level Rollback

  • If a single DML statement fails during execution,only that statement is rolled back.
  • The Oracle server implements an implicit savepoint.
  • All other changes are retained.
  • The user should terminate transactions explicitly by executing a COMMIT or ROLLBACK statement.

Read Consistency

  • Read consistency guarantees a consistent view of the data at all times.
  • Changes made by one user do not conflict with the changes made by another user.
  • Read consistency ensures that ,on the same data:
    • -Readers do not wait for writers
    • -Writers do not wait for readers
    • -Writers wait for writers

Implementing Read Consistency

FOR UPDATE clause in a SELECT Statement

  • Locks the rows in the EMPLOYEES table where job_id is SA_REP.
SELECT employee_id,salary,commission_pct,job_id
FROM employees
WHERE job_id = 'SA_REP'
FROM UPDATE
ORDER BY employee_id;
  • Lock is released only when you issue a ROLLBACK or a COMMIT.
  • If the SELECT statement attempts to lock a row that is locked by another user,then the database waits unitl the row is available,and then returns the results of the SELECT statement.

FOR UPDATE Clause:Examples

  • You can use the FOR UPDATE clause in a SELECT statement against multiple tables.
FOR UPDATE
SELECT e.employee_id,e.salary,e.commission_pct 

FROM employees e JOIN departments d

USING(department_id)

WHERE job_id = 'ST_CLEAR' AND job_id = 1500

FROM UPDATE 

ORDER BY e.employee_id;
  • Rows from both the EMPLOYEES and DEPARTMENTS tables are locked.
  • Use FOR UPDATE OF column_name to qualify the column you intend to change,then only the rows from that specific table are locked.

Summary

In this lesson,you should have learned how to use the following statements:

FunctionDescription
INSERTAdds a new row to the table
UPDATEModifies existing rows in the table
DELETERemoves existing rows from the table
TRUNCATERemoves all rows from a table
COMMITMakes all pending changes permanent
SAVEPOINTIs used to roll back to the savepoint marker
ROLLBACKDiscard all pending data changes
FOR UPDATE clause in SELECTLocks rows indentified by the SELECT query.

转载于:https://www.cnblogs.com/arcer/archive/2013/04/17/3026040.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值