Chapter 09Manipulation 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:

Function Description
INSERT Adds a new row to the table
UPDATE Modifies existing rows in the table
DELETE Removes existing rows from the table
TRUNCATE Removes all rows from a table
COMMIT Makes all pending changes permanent
SAVEPOINT Is used to roll back to the savepoint marker
ROLLBACK Discard all pending data changes
FOR UPDATE clause in SELECT Locks rows indentified by the SELECT query.
原文地址:https://www.cnblogs.com/arcer/p/3026040.html