© 2007 by prentice hall4-1 introduction to oracle 10g chapter 4 modifying data and auditing table...

40
© 2007 by Prentice Hall 4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

Upload: dwain-anthony

Post on 17-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-1

Introduction to Oracle 10gChapter 4Modifying Data and Auditing Table Operations

James Perry and Gerald Post

Page 2: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-2

Chapter Outline

• Inserting Rows into Tables• Updating Data• Deleting Rows and Truncating Tables• Merging Rows• Database Transactions• Creating and Using Database Triggers

Page 3: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-3

Table 4.1 Constraints and structure of the Agents table

Column Description Data type Constraint(s)

AgentID Unique number assigned to each agent

INTEGER Primary key

Gender Agent’s sex NVARCHAR2(10)

Check: only ‘M’, ‘m’, ‘F’ or ‘f’

Title Internal title assigned to each employee

NVARCHAR2(20)

Check: only ‘salesperson’ or ‘broker’ (any combination of upper- and lowercase letters allowed)

LicenseStatusID

Code representing an agent’s real estate license status

INTEGER Foreign key; value must be found in the LicenseStatus table’s primary key column, LicenseStatusID

Page 4: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-4

Table 4.2 Common date format model symbols

Format Model Symbol Displayed or Input Value

MONTHMonthMM

FEBRUARYFebruary02

DD 15

DDD 251

DAYDayDY

WEDNESDAYWednesdayWED

YYYYYY

200505

Page 5: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-5

Table 4.3 Oracle’s relational operators

Relational Operator

Meaning

= Equal to

<> or != Not equal to

> Greater than

< Less than

<= Less than or equal to

>= Greater than or equal to

Page 6: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-6

Table 4.4 Oracle’s logical operators

Logical Operator

Meaning WHERE Clause Example

AND True only if both conditions are true; false otherwise

WHERE State = ‘MN’ AND Gender = ‘M’

OR True if either condition is true; false otherwise

WHERE LicenseStatusID = 1001 ORLicenseStatusID = 1002

NOT Negate expression WHERE NOT State = ‘NE’

IN True if among set of discrete values listed

WHERE City IN(‘Arcata’, ‘Fortuna’, ‘Orick’)

LIKE Wildcard expression allowing “don’t care” conditions

WHERE LastName LIKE ‘Mc%’

BETWEEN … AND …

True if within the value range, inclusive

WHERE SqFt BETWEEN 1500 AND 2000

Page 7: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-7

Table 4.5 Annual bonus schedule based on employment longevity

Length of service (months)

Annual bonus amount

12 or less $0

13 to 24 $500

25 to 48 $700

49 to 72 $1,000

73 or greater $1,500

Page 8: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-8

Table 4.6 Values supplied in trigger body by correlation names NEW and OLD

SQL Statement

Correlation Name Value

INSERT NEW Value supplied for the column in the statement that originated the transaction.

OLD NULL

UPDATE NEW Value supplied for the column in the statement that originated the transaction.

OLD Value of the column that was last committed into the table prior to the transaction.

DELETE NEW NULL

OLD Value of the column that was last committed into the table before the transaction.

Page 9: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-9

Table 4.7 Columns available in the user_triggers data dictionary view

Column name Data type Meaning

Trigger_Name VARCHAR2(30) Name of trigger

Trigger_Type VARCHAR2(16) Type of trigger

Triggering_Event VARCHAR2(227) Event that causes trigger to fire

Table_Owner VARCHAR2(30) User who owns the table that the trigger references

Base_Object_Type

VARCHAR2(16) Type of object referenced by the trigger

Table_Name VARCHAR2(30) Table referenced by the trigger

Column_Name VARCHAR2(4000)

Column referenced by the trigger

Referencing_Names

VARCHAR2(128) Name of the OLD and NEW aliases.

When_Clause VARCHAR2(4000)

Trigger condition WHEN clause

Status VARCHAR2(8) Whether the trigger is enabled or disabled

Description VARCHAR2(4000)

Description of trigger

Action_Type VARCHAR2(11) Action type of the trigger

Trigger_Body LONG Code contained in trigger body

Page 10: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-10

4.1 Structure of two related Redwood Realty tables

AgentID (primary key)

FirstName

LastName

HireDate

BirthDate

Gender

WorkPhone

CellPhone

HomePhone

Title

TaxID

LicenseID

LicenseDate

LicenseExpire

LicenseStatusID (foreign key)

LicenseStatusID (primary key)

StatusText

LicenseStatus tableAgents table

Page 11: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-11

4.2 Inserting rows into the Agents table

Page 12: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-12

4.3 Correcting an integrity constraint violation

Page 13: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-13

4.4 Displaying the contents of the LicenseStatus table

Page 14: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-14

4.5 Displaying selected columns from the Agents table

Page 15: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-15

4.6 Inserting date values into a table

Page 16: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-16

4.7 Inserting rows from another table

Page 17: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-17

4.8 Creating a sequence and displaying its characteristics

Page 18: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-18

4.9 Reviewing sequence values and CURRVAL

Page 19: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-19

4.10 Updating multiple columns in a single row

Page 20: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-20

4.11 Updating multiple rows with a single expression

Page 21: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-21

4.12 Selected Agents rows before being updated

Page 22: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-22

4.13 Selected Agents rows after updating them

Page 23: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-23

4.14 Running a SQL statement containing substitution variables

Page 24: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-24

4.15 Processing substitution variables

Page 25: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-25

4.16 Displaying updated table

Page 26: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-26

4.17 Deleting selected rows from a table

Page 27: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-27

4.18 Truncating the Agents table

Page 28: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-28

4.19 MERGE example

LicenseStatus target table

UpdateLicenseStatus source table

1001 Licensed1002 Licensed NBA1003 Canceled Officer1004 Deceased1005 Expired1006 Government Service1007 Military Service1008 Conditional Suspension1009 Restricted1010 Revoked1011 Flag Suspended1012 Voided1013 Withheld Denied1014 17520 FC Suspended1015 11350.6 W and I Suspended1016 Surrendered

1004 Passed Away1005 Expired--Fee Not Paid1101 License Probationary1105 License Pending

1001 Licensed1002 Licensed NBA1003 Canceled Officer1004 Passed Away1005 Expired--Fee Not Paid1006 Government Service1007 Military Service1008 Conditional Suspension1009 Restricted1010 Revoked1011 Flag Suspended1012 Voided1013 Withheld Denied1014 17520 FC Suspended1015 11350.6 W and I Suspended1016 Surrendered1101 License Probationary1105 License Pending

LicenseStatus table following MERGE

updated columns (bold)

Inserted rows (bold)

MERGE

Page 29: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-29

4.20 Issuing MERGE to modify a table

Page 30: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-30

4.21 Illustration of transactions

Time

INSERT …

DELETE …

INSERT …

UPDATE …

COMM

IT;

COMM

IT;

INSERT …

INSERT …

DROP TABLE…

Transaction Transaction

Page 31: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-31

4.22 Using savepoints in a transaction

creating a savepoint

creating a savepoint

creating a savepoint

change rolled back to latest savepoint

Page 32: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-32

4.23 Modifying the Agents table

Page 33: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-33

4.24 Ending a transaction with COMMIT

Page 34: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-34

4.25 Creating a sequence and a BEFORE INSERT trigger

Page 35: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-35

4.26 Displaying Agents rows with trigger-supplied primary key values

Page 36: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-36

4.27 Creating an audit table and an AFTER UPDATE trigger

Page 37: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-37

4.28 Displaying the audit table

Page 38: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-38

4.29 A trigger execution error

Page 39: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-39

4.30 Correctly executing a statement-level trigger

Output produced by statement-level trigger.

Page 40: © 2007 by Prentice Hall4-1 Introduction to Oracle 10g Chapter 4 Modifying Data and Auditing Table Operations James Perry and Gerald Post

© 2007 by Prentice Hall4-40

4.31 Displaying trigger information