Database Transactions

Entry Level Technology Program

 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

Satyam Learning Center

ORACLE

1 of 1

Entry Level Technology Program

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

Satyam Learning Center

ORACLE

2 of 1

Controlling Transactions
Entry Level Technology Program

Time

COMMIT

Transaction DELETE SAVEPOINT A INSERT

UPDATE
SAVEPOINT B INSERT ROLLBACK ROLLBACK ROLLBACK to SAVEPOINT B to SAVEPOINT A
Satyam Learning Center ORACLE 3 of 1

.  Roll back to that marker by using the ROLLBACK TO SAVEPOINT statement. INSERT. Satyam Learning Center ORACLE 4 of 1 .Entry Level Technology Program Rolling Back Changes to a Marker  Create a marker in a current transaction by using the SAVEPOINT statement. UPDATE. ROLLBACK TO update_done. SAVEPOINT update_done.. Rollback complete. Savepoint created...

Satyam Learning Center ORACLE 5 of 1 .  Locks on the affected rows are released.  All savepoints are erased. those rows are available for other users to manipulate.Entry Level Technology Program State of the Data after COMMIT  Data changes are made permanent in the database.  All users can view the results.  The previous state of the data is permanently lost.

1 row inserted. NULL. 1700).Committing Data Entry Level Technology Program  Make the changes. Commit the changes. Satyam Learning Center ORACLE 6 of 1 . DELETE FROM EMP WHERE EMPNO = 7639. 1 row deleted. 'Corporate Tax'. INSERT INTO DEPT VALUES (290. Commit complete. COMMIT.

 Previous state of the data is restored.  Locks on the affected rows are released. DELETE FROM copy_emp. 22 rows deleted.State of the Data After ROLLBACK Entry Level Technology Program Discard all pending changes by using the ROLLBACK statement:  Data changes are undone. Rollback complete. ROLLBACK. Satyam Learning Center ORACLE 7 of 1 .

Summary Entry Level Technology Program In this lesson. Statement INSERT UPDATE DELETE COMMIT SAVEPOINT ROLLBACK Description Adds a new row to the table Modifies existing rows in the table Removes existing rows from the table Makes all pending changes permanent Is used to rollback to the savepoint marker Discards all pending data changes Satyam Learning Center ORACLE 8 of 1 . you should have learned how to use DML statements and control transactions.

Entry Level Technology Program Producing Readable Output with iSQL*Plus Satyam Learning Center ORACLE 9 of 1 .

.. User Satyam Learning Center ORACLE 10 of 1 .Substitution Variables Entry Level Technology Program . last_name = ? ... I want to query different values... salary = ? … … department_id = ? … .

Substitution Variables Entry Level Technology Program Use iSQL*Plus substitution variables to:  Temporarily store values  Single ampersand (&)  Double ampersand (&&)  DEFINE command  Pass variable values between SQL statements  Dynamically alter headers and footers Satyam Learning Center ORACLE 11 of 1 .

and Text Use substitution variables to supplement the following:  WHERE conditions  ORDER BY clauses  Column expressions  Table names  Entire SELECT statements Satyam Learning Center ORACLE 12 of 1 . Expressions.Entry Level Technology Program Specifying Column Names.

Specifying Column Names. and Text Entry Level Technology Program SELECT FROM WHERE ORDER BY employee_id. &column_name employees &condition &order_column . Expressions. last_name. job_id. Satyam Learning Center ORACLE 13 of 1 .

Exercise Entry Level Technology Program  Query I from exe-I document Satyam Learning Center ORACLE 14 of 1 .

Entry Level Technology Program Displaying Data from Multiple Tables Satyam Learning Center ORACLE 15 of 1 .

always include a valid join condition in a WHERE clause.Cartesian Products Entry Level Technology Program  A Cartesian product is formed when:  A join condition is omitted  A join condition is invalid  All rows in the first table are joined to all rows in the second table  To avoid a Cartesian product. Satyam Learning Center ORACLE 16 of 1 .

Types of Joins Entry Level Technology Program Oracle Proprietary Joins (8i and prior):     Equijoin Non-equijoin Outer join Self join Satyam Learning Center ORACLE 17 of 1 .

column.column table1.  Efficiency of joins increases when we join from the table where less number of rows are there to the table where more number of rows.  Prefix the column name with the table name when the same column name appears in more than one table. Satyam Learning Center ORACLE 18 of 1 .column2. table2.column1 = table2. Write the join condition in the WHERE clause. table2 table1.Joining Tables Using Oracle Syntax Entry Level Technology Program Use a join to query data from more than one table. SELECT FROM WHERE table1.

What is an Equijoin? Entry Level Technology Program EMPLOYEES DEPARTMENTS … … Foreign key Primary key Satyam Learning Center ORACLE 19 of 1 .

DEPTNO FROM EMP UNION ALL SELECT EMPNO. JOB. ORDER BY Satyam Learning Center ORACLE 20 of 1 . SELECT EMPNO.Entry Level Technology Program Using the UNION ALL Operator Display the current and previous departments of all employees. DEPTNO FROM JOB_HISTORY EMPNO. JOB.

The INTERSECT Operator Entry Level Technology Program A B Satyam Learning Center ORACLE 21 of 1 .

Entry Level Technology Program 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. JOB FROM EMP INTERSECT SELECT EMPNO. JOB FROM JOB_HISTORY. Satyam Learning Center ORACLE 22 of 1 . SELECT EMPNO.

The MINUS Operator Entry Level Technology Program A B Satyam Learning Center ORACLE 23 of 1 .

SELECT EMPNO.JOB FROM MINUS EMP SELECT EMPNO. Satyam Learning Center ORACLE 24 of 1 .JOB FROM JOB_HISTORY.The MINUS Operator Entry Level Technology Program Display the employee IDs of those employees who have not changed their jobs even once.

SET Operator Guidelines Entry Level Technology Program  The expressions in the SELECT lists must match in number and data type. or the positional notation Satyam Learning Center ORACLE 25 of 1 .  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.

 The output is sorted in ascending order by default except in UNION ALL.  Column names from the first query appear in the result.Entry Level Technology Program The Oracle Server and SET Operators  Duplicate rows are automatically eliminated except in UNION ALL. Satyam Learning Center ORACLE 26 of 1 .

ORACLE 27 of 1 EMP TO_DATE(null) Satyam Learning Center .Matching the SELECT Statements Entry Level Technology Program Using the UNION operator. HIREDATE FROM UNION SELECT DEPTNO. SELECT DEPTNO. display the department ID. LOC. TO_CHAR(null) LOC. FROM DEPT. and hire date for all employees. location.

SELECT EMPNO.SAL FROM UNION EMP SELECT EMPNO. display the employee ID. job ID. Satyam Learning Center ORACLE 28 of 1 .0 FROM JOB_HISTORY.Matching the SELECT Statement Entry Level Technology Program  Using the UNION operator. JOB. JOB. and salary of all employees.

Summary Entry Level Technology Program In this lesson. including duplicates  Use INTERSECT to return all rows shared by both queries  Use MINUS to return all distinct rows selected by the first query but not by the second  Use ORDER BY only at the very end of the statement Satyam Learning Center ORACLE 29 of 1 . you should have learned how to:  Use UNION to return all distinct rows  Use UNION ALL to returns all rows.

Exercise Entry Level Technology Program  Query – III from exe-I document Satyam Learning Center ORACLE 30 of 1 .