© 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
© 2007 by Prentice Hall4-3 Table 4.1 Constraints and structure of the Agents table ColumnDescriptionData typeConstraint(s) AgentIDUnique number assigned to each agent INTEGERPrimary key GenderAgent’s sexNVARCHAR2(10)Check: only ‘M’, ‘m’, ‘F’ or ‘f’ TitleInternal title assigned to each employee NVARCHAR2(20)Check: only ‘salesperson’ or ‘broker’ (any combination of upper- and lowercase letters allowed) LicenseStatusIDCode representing an agent’s real estate license status INTEGERForeign key; value must be found in the LicenseStatus table’s primary key column, LicenseStatusID
© 2007 by Prentice Hall4-4 Table 4.2 Common date format model symbols Format Model SymbolDisplayed or Input Value MONTH Month MM FEBRUARY February 02 DD15 DDD251 DAY Day DY WEDNESDAY Wednesday WED YYYY YY
© 2007 by Prentice Hall4-5 Table 4.3 Oracle’s relational operators Relational OperatorMeaning =Equal to <> or !=Not equal to >Greater than <Less than <=Less than or equal to >=Greater than or equal to
© 2007 by Prentice Hall4-6 Table 4.4 Oracle’s logical operators Logical OperatorMeaningWHERE Clause Example ANDTrue only if both conditions are true; false otherwise WHERE State = ‘MN’ AND Gender = ‘M’ ORTrue if either condition is true; false otherwise WHERE LicenseStatusID = 1001 OR LicenseStatusID = 1002 NOTNegate expressionWHERE NOT State = ‘NE’ INTrue if among set of discrete values listed WHERE City IN(‘Arcata’, ‘Fortuna’, ‘Orick’) LIKEWildcard 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
© 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$ to 48$ to 72$1, or greater$1,500
© 2007 by Prentice Hall4-8 Table 4.6 Values supplied in trigger body by correlation names NEW and OLD SQL StatementCorrelation NameValue INSERTNEWValue supplied for the column in the statement that originated the transaction. OLDNULL UPDATENEWValue supplied for the column in the statement that originated the transaction. OLDValue of the column that was last committed into the table prior to the transaction. DELETENEWNULL OLDValue of the column that was last committed into the table before the transaction.
© 2007 by Prentice Hall4-9 Table 4.7 Columns available in the user_triggers data dictionary view Column nameData typeMeaning Trigger_NameVARCHAR2(30)Name of trigger Trigger_TypeVARCHAR2(16)Type of trigger Triggering_EventVARCHAR2(227)Event that causes trigger to fire Table_OwnerVARCHAR2(30)User who owns the table that the trigger references Base_Object_TypeVARCHAR2(16)Type of object referenced by the trigger Table_NameVARCHAR2(30)Table referenced by the trigger Column_NameVARCHAR2(4000)Column referenced by the trigger Referencing_NamesVARCHAR2(128)Name of the OLD and NEW aliases. When_ClauseVARCHAR2(4000)Trigger condition WHEN clause StatusVARCHAR2(8)Whether the trigger is enabled or disabled DescriptionVARCHAR2(4000)Description of trigger Action_TypeVARCHAR2(11)Action type of the trigger Trigger_BodyLONGCode contained in trigger body
© 2007 by Prentice Hall 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
© 2007 by Prentice Hall Inserting rows into the Agents table
© 2007 by Prentice Hall Correcting an integrity constraint violation
© 2007 by Prentice Hall Displaying the contents of the LicenseStatus table
© 2007 by Prentice Hall Displaying selected columns from the Agents table
© 2007 by Prentice Hall Inserting date values into a table
© 2007 by Prentice Hall Inserting rows from another table
© 2007 by Prentice Hall Creating a sequence and displaying its characteristics
© 2007 by Prentice Hall Reviewing sequence values and CURRVAL
© 2007 by Prentice Hall Updating multiple columns in a single row
© 2007 by Prentice Hall Updating multiple rows with a single expression
© 2007 by Prentice Hall Selected Agents rows before being updated
© 2007 by Prentice Hall Selected Agents rows after updating them
© 2007 by Prentice Hall Running a SQL statement containing substitution variables
© 2007 by Prentice Hall Processing substitution variables
© 2007 by Prentice Hall Displaying updated table
© 2007 by Prentice Hall Deleting selected rows from a table
© 2007 by Prentice Hall Truncating the Agents table
© 2007 by Prentice Hall MERGE example LicenseStatus target table UpdateLicenseStatus source table LicenseStatus table following MERGE updated columns (bold) Inserted rows (bold) MERGE
© 2007 by Prentice Hall Issuing MERGE to modify a table
© 2007 by Prentice Hall Illustration of transactions Time INSERT …DELETE …INSERT …UPDATE …COMMIT; INSERT … DROP TABLE… Transaction
© 2007 by Prentice Hall Using savepoints in a transaction creating a savepoint change rolled back to latest savepoint
© 2007 by Prentice Hall Modifying the Agents table
© 2007 by Prentice Hall Ending a transaction with COMMIT
© 2007 by Prentice Hall Creating a sequence and a BEFORE INSERT trigger
© 2007 by Prentice Hall Displaying Agents rows with trigger-supplied primary key values
© 2007 by Prentice Hall Creating an audit table and an AFTER UPDATE trigger
© 2007 by Prentice Hall Displaying the audit table
© 2007 by Prentice Hall A trigger execution error
© 2007 by Prentice Hall Correctly executing a statement-level trigger Output produced by statement-level trigger.
© 2007 by Prentice Hall Displaying trigger information