day2 session2

download day2 session2

of 30

Transcript of day2 session2

  • 8/12/2019 day2 session2

    1/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 1 of 1

    Database Transactions

    Begin when the first DML SQL statement isexecuted

    End with one of the following events:A COMMIT or ROLLBACK statement is issued

    A DDL or DCL statement executes (automaticcommit)

    The user exits iSQL*PlusThe system crashes

  • 8/12/2019 day2 session2

    2/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 2 of 1

    Advantages of COMMITand ROLLBACKStatements

    With COMMITand ROLLBACKstatements, you can:

    Ensure data consistency

    Preview data changes before making changespermanent

    Group logically related operations

  • 8/12/2019 day2 session2

    3/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 3 of 1

    Controlling Transactions

    SAVEPOINT B

    SAVEPOINT A

    DELETE

    INSERT

    UPDATE

    INSERT

    COMMITTime

    Transaction

    ROLLBACK

    to SAVEPOINT B

    ROLLBACK

    to SAVEPOINT AROLLBACK

  • 8/12/2019 day2 session2

    4/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 4 of 1

    UPDATE...

    SAVEPOINT update_done;Savepoint created.

    INSERT...ROLLBACK TO update_done;

    Rollback complete.

    Rolling Back Changesto a Marker

    Create a marker in a current transaction by usingthe SAVEPOINT statement.

    Roll back to that marker by using the ROLLBACK

    TO SAVEPOINT statement.

  • 8/12/2019 day2 session2

    5/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 5 of 1

    State of the Data afterCOMMIT

    Data changes are made permanent in thedatabase.

    The previous state of the data is permanently lost.

    All users can view the results.

    Locks on the affected rows are released; thoserows are available for other users to manipulate.

    All savepoints are erased.

  • 8/12/2019 day2 session2

    6/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 6 of 1

    COMMIT;

    Commit complete.

    Make the changes.

    Commit the changes.

    DELETE FROM EMP

    WHERE EMPNO = 7639;

    1 row deleted.

    INSERT INTO DEPT

    VALUES (290, 'Corporate Tax', NULL, 1700);

    1 row inserted.

    Committing Data

  • 8/12/2019 day2 session2

    7/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 7 of 1

    State of the Data After ROLLBACK

    Discard all pending changes by using theROLLBACK

    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.

  • 8/12/2019 day2 session2

    8/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 8 of 1

    Summary

    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

    Statement

    INSERT

    UPDATE

    DELETE

    COMMIT

    SAVEPOINT

    ROLLBACK

    In this lesson, you should have learned how to useDML statements and control transactions.

  • 8/12/2019 day2 session2

    9/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 9 of 1

    Producing Readable Outputwith iSQL*Plus

  • 8/12/2019 day2 session2

    10/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 10 of 1

    Substitution Variables

    I want to query

    different values.... salary = ?

    department_id = ?

    ... last_name = ? ...

    User

  • 8/12/2019 day2 session2

    11/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 11 of 1

    Substitution Variables

    Use iSQL*Plus substitution variables to:

    Temporarily store values

    Single ampersand (&)

    Double ampersand (&&)

    DEFINEcommand

    Pass variable values between SQL statements

    Dynamically alter headers and footers

  • 8/12/2019 day2 session2

    12/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 12 of 1

    Specifying Column Names,Expressions, and Text

    Use substitution variables to supplement thefollowing:

    WHEREconditions ORDER BYclauses

    Column expressions

    Table names

    Entire SELECTstatements

  • 8/12/2019 day2 session2

    13/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 13 of 1

    SELECT employee_id, last_name, job_id,

    &column_name

    FROM employees

    WHERE &condition

    ORDER BY &order_column ;

    Specifying Column Names,Expressions, and Text

  • 8/12/2019 day2 session2

    14/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 14 of 1

    Exercise

    Query I from exe-I document

  • 8/12/2019 day2 session2

    15/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 15 of 1

    Displaying Datafrom Multiple Tables

  • 8/12/2019 day2 session2

    16/30

  • 8/12/2019 day2 session2

    17/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 17 of 1

    Equijoin

    Non-equijoin

    Outer join

    Self join

    Types of Joins

    Oracle Proprietary

    Joins (8iand prior):

  • 8/12/2019 day2 session2

    18/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 18 of 1

    Joining Tables Using Oracle Syntax

    Use a join to query data from more than one table.

    Write the join condition in the WHERE clause.

    Prefix the column name with the table name when thesame column name appears in more than one table.

    Efficiency of joins increases when we join from thetable where less number of rows are there to thetable where more number of rows.

    SELECT table1.column, table2.columnFROM table1, table2

    WHERE table1.column1 =table2.column2;

  • 8/12/2019 day2 session2

    19/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 19 of 1

    What is an Equijoin?

    EMPLOYEES DEPARTMENTS

    Foreign key Primary key

  • 8/12/2019 day2 session2

    20/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 20 of 1

    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

    ORDER BY EMPNO;

  • 8/12/2019 day2 session2

    21/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 21 of 1

    The INTERSECTOperator

    A B

  • 8/12/2019 day2 session2

    22/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 22 of 1

    Using the INTERSECT

    Operator

    Display the employee IDs and job IDs of employeeswho currently have a job title that they held beforebeginning their tenure with the company.

    SELECT EMPNO, JOB

    FROM EMP

    INTERSECT

    SELECT EMPNO, JOB

    FROM JOB_HISTORY;

  • 8/12/2019 day2 session2

    23/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 23 of 1

    TheMINUSOperator

    A B

  • 8/12/2019 day2 session2

    24/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 24 of 1

    TheMINUSOperator

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

    SELECT EMPNO,JOB

    FROM EMP

    MINUS

    SELECT EMPNO,JOB

    FROM JOB_HISTORY;

  • 8/12/2019 day2 session2

    25/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 25 of 1

    SETOperator Guidelines

    The expressions in the SELECT lists must match innumber 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 thestatement

    Will accept the column name, aliases from thefirst SELECT statement, or the positionalnotation

  • 8/12/2019 day2 session2

    26/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 26 of 1

    The Oracle Server and SET

    Operators

    Duplicate rows are automatically eliminatedexcept in UNION ALL.

    Column names from the first query appear in theresult.

    The output is sorted in ascending order by defaultexcept in UNION ALL.

  • 8/12/2019 day2 session2

    27/30

  • 8/12/2019 day2 session2

    28/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 28 of 1

    Matching the SELECTStatement

    Using the UNION operator, display the employee ID, job ID,and salary of all employees.

    SELECT EMPNO, JOB,SAL

    FROM EMP

    UNION

    SELECT EMPNO, JOB,0

    FROM JOB_HISTORY;

  • 8/12/2019 day2 session2

    29/30

    Entry Level Technology Program

    Satyam Learning Center ORACLE 29 of 1

    Summary

    In this lesson, you should have learned how to:

    Use UNIONto return all distinct rows

    Use UNION ALLto returns all rows, including duplicates Use INTERSECTto return all rows shared by

    both queries

    Use MINUSto return all distinct rows selected by the firstquery but not by the second

    Use ORDER BYonly at the very end ofthe statement

  • 8/12/2019 day2 session2

    30/30

    Entry Level Technology Program

    Exercise

    Query III from exe-I document