Database Tutorial

Course Tutorial Site

Site Admin

CIT325: Lab 11 Instructions

without comments

Lab #10: Oracle Assignment

Objectives

The lab is designed to teach you how to write Data Manipulation Language (DML) triggers with Oracle’s PL/SQL. You create a logger table where you record changes to the rows in the item table. Those changes occur when you call an INSERT, UPDATE, or DELETE statement. You create two DML triggers and a manage_item package to accomplish this task, which lets you leverage overloading. Overloading lets you write procedures that logs new and old row information from an INSERT or UPDATE statements. Overloading also lets you write a procedure that logs old row information when you delete a row from the item table.

  • Learn how to create a event logging table.
  • Learn how to create insert, update, and delete DML triggers.
  • Learn how to leverage overloading in a package.
  • Learn how to test insert, update, and delete DML triggers.

Business Scenario

Application programming solutions often require the ability to capture and correct end-user inputs that violate rules. Sometimes, you simply correct the input, like ensuring multiple part names have a hyphen between each element. Other times, you need to log the attempt to enter the data while preventing the data entry. You typically raise an exception when when you want to prevent data entry.

You accomplish this part of application programming with triggering events. In the concept of databases, you accomplish this with data manipulation language (DML) database triggers. DML database triggers let you hid auditing code beneath the application programming interface. INSERT, UPDATE, and DELETE statements fire database triggers. Statement-level DML triggers fire once for each INSERT, UPDATE, or DELETE statement but row-level DML triggers fire once for every row touched by an INSERT, UPDATE, ore DELETE statement.

Help Section

The lab builds on some work done in prior labs. You should put the following two lines at the beginning of your apply_plsql_lab11.sql script:

@/home/student/Data/cit325/lib/cleanup_oracle.sql
@/home/student/Data/cit325/lib/Oracle12cPLSQLCode/Introduction/create_video_store.sql

After you have recreated a generic video store with the scripts from the Oracle Database 12c PL/SQL Programming book, you need to add a text_file_name column to the item table. The text_file_name column should use a VARCHAR2(30) data type. (HINT: You can add the column with the ALTER TABLE command.)

You can run a verification script to ensure you have the correct text_file_name column in the item table definition.

You will need to use the following components from the video store model:

  • The item and contact tables.
  • The item_s1 and contact_s1 sequences.

You will create the following components in this lab:

  • The logger table.
  • The logger_s sequence.
  • The overloaded insert_item autonomous procedures inside the manage_item package.
  • The item_trig trigger.
  • The item_delete_trig trigger (if you opt for two triggers).

This lab creates critical and non-critical triggers. However, this lab will leverage Oracle’s special feature of creating database triggers that work with INSERT, UPDATE, and DELETE statements. That means you’ll write:

  • One row-level trigger that manages all of the DML events created by an INSERT, UPDATE, or DELETE statement (e.g., a row-level trigger that captures and processes all unconditional DML statements), or
  • Two row-level triggers:
    • One manages the INSERT and UPDATE DML events conditionally. You accomplish that by leveraging the (a) OF column_name subclause and (b) the WHEN subclause. You can use the WHEN subclause with a regular expression function to limit the trigger to only managing events where the item_title contains a colon (check Appendix E in the Oracle Database 12c PL/SQL Programming textbook, pages 1000 to 1017).
    • One manages the DELETE DML event by capturing any deletion from the item table.

The following instructional material shows you:

  • How to write a stored package to manage insertions into the logging table.
  • How to test the overloaded procedures of a stored package.
  • How to write an insert, update, and delete trigger.
  • How to test the insert, update, and delete trigger.

The following two articles cover the basics of working with database triggers and trigger logs in an Oracle Database:

There are three elements to this lab. First, you create a logger table and logger_s sequence. Second, you create overloaded insert_item autonomous procedures in the manage_item package. Third, you create an item_trig trigger that manages INSERT, UPDATE, and DELETE statement events against the item table; and the overloaded insert_item autonomous procedure process or reject events while writing (or logging) both the new_ and old_ column values to the logger table.

  1. [5 points] Creates a logger table and logger_s sequence. (These have a different structure than the logger table you created in the last lab, which means you need to drop that table at the beginning of your script for this lab.)

    The sections cover the detailed tasks of the lab:

  1. [10 points] Create overloaded item_insert autonomous procedures inside a manage_item package. These item_insert procedures are being designed to insert the old_ and new_ pseudo columns from the item table into the logger table within the scope of a database trigger. (For these tasks, you may want to review the row-level trigger section of the textbook on pages 518-526.)

    The sections cover the detailed tasks of the lab:

  1. [10 points] You have two options with this step. You can create an item_trig trigger that manages INSERT, UPDATE, and DELETE row-level events. Alternatively, you can create an item_trig trigger that manages INSERT or UPDATE row-level events and an item_delete_trig that manages the DELETE row-level event.

    The sections cover the detailed tasks of the lab:

Written by michaelmclaughlin

November 27th, 2016 at 11:23 pm

Posted in