Sei sulla pagina 1di 12

Database Transactions

Begin when the first DML SQL statement is executed End with one of the following events:

A COMMIT or ROLLBACK statement is issued A DDL or DCL statement executes (automatic commit) The user exits iSQL*Plus The system crashes

Advantages of COMMIT and ROLLBACK Statements


With COMMIT and ROLLBACK statements, you can: Ensure data consistency Preview data changes before making changes permanent Group logically related operations

Rolling Back Changes to a Marker


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

State of the Data after COMMIT

Data changes are made permanent in the database. The previous state of the data is permanently lost. 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 EMP WHERE EMPNO = 7639; 1 row deleted. INSERT INTO DEPT VALUES (290, 'Corporate Tax', NULL, 1700); 1 row inserted.

Commit the changes.


COMMIT; Commit complete.

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; 22 rows deleted. ROLLBACK; Rollback complete.

Using the UNION ALL Operator Display the current and previous departments of all employees.
SELECT EMPNO, JOB, DEPTNO FROM EMP

UNION ALL
SELECT EMPNO, JOB, DEPTNO FROM JOB_HISTORY EMPNO;

ORDER BY

Using the INTERSECT Operator


Display the employee IDs and job IDs of employees who currently have a job title that they held before beginning their tenure with the company.
SELECT EMPNO, JOB FROM EMP

INTERSECT SELECT EMPNO, JOB FROM JOB_HISTORY;

The MINUS Operator


Display the employee IDs of those employees who have not changed their jobs even once.

SELECT EMPNO,JOB

FROM
MINUS

EMP

SELECT EMPNO,JOB FROM JOB_HISTORY;

SET Operator Guidelines

The expressions in the SELECT lists must match in number and data type. Parentheses can be used to alter the sequence of execution. The ORDER BY clause:

Can appear only at the very end of the statement Will accept the column name, aliases from the first SELECT statement, or the positional notation

The Oracle Server and SET Operators

Duplicate rows are automatically eliminated except in UNION ALL. Column names from the first query appear in the result. The output is sorted in ascending order by default except in UNION ALL.

Matching the SELECT Statements


Using the UNION operator, display the department ID, location, and hire date for all employees.

SELECT DEPTNO, TO_CHAR(null) LOC, HIREDATE FROM UNION SELECT DEPTNO, LOC, FROM DEPT; TO_DATE(null) EMP

Potrebbero piacerti anche