Previous | Table of Contents | Next

Page 454

Listing 16.2. PL/SQL block for the database trigger body.

begin

if inserting then

  insert into student_schedule_journal
  (operation, student_ID, Class_ID, Grade, Date_Grade_Assigned,
   Changed_By, Changed_Date)
  values
  (`I', :new.Student_ID, :new.Class_ID, :new.Grade,
   :new.Date_Grade_Assigned, user, sysdate);

elsif updating then

  insert into student_schedule_journal
  (operation, student_ID, Class_ID, Grade, Date_Grade_Assigned,
   Changed_By, Changed_Date)
  values
  (`U', :new.Student_ID, :new.Class_ID, :new.Grade,
   :new.Date_Grade_Assigned, user, sysdate);

elsif deleting then

  insert into student_schedule_journal
  (operation, student_ID, Class_ID, Grade, Date_Grade_Assigned,
   Changed_By, Changed_Date)
  values
  (`I', :new.Student_ID, :new.Class_ID, :new.Grade,
   :new.Date_Grade_Assigned, user, sysdate);

end if;

end;
ANALYSIS
As you can see in Listing 16.2, the trigger body consists of a PL/SQL block with an IF statement. Each clause in the IF statement tests for the operation that is being performed. For instance, the first portion of the IF statement tests to see if the triggering event is an INSERT; if so, a row is inserted into the Student_Schedule_Journal with the following values. Click Close to exit the Database Trigger Editor.
Operation I, because the operation is an INSERT
Student_ID :new.Student_ID, which contains the value for Student_ID that is being inserted in the Student_Schedule table
Class_ID :new.Class_ID, which contains the value for Class_ID that is being inserted in the Student_Schedule table
Grade :new.Grade, which contains the value for Grade that is being inserted in the Student_Schedule table

Page 455

Date_Grade_Assigned :new.Date_Grade_Assigned, which contains the value for Date_Grade_Assigned that is being inserted in the Student_Schedule table Changed_By USER, a built-in function that returns the name of the Oracle user that is executing the statement Changed_Date
SYSDATE, a built-in function that returns the current date and time

Testing the New Database Trigger

Let's test the trigger. You can do this by using either SQL*Plus or SQL Worksheet.

  1. Connect to the flugle account.
  2. To begin with, verify that the Student_Schedule_Journal table is empty (see Figure 16.20).

Figure 16.20.
Student_Schedule_Journal
table is empty.

  1. Retrieve all records from the Student_Schedule table in which the Student_ID is 10231324 (who happens to be Anna Anastatia). As you can see in Figure 16.21, there is only one class listed: 109100 (which happens to be Biology 101).
  2. Anna worked hard this semester—she deserves an A in the class. To assign her an A in the course, use an UPDATE statement to set the value of GRADE to an A (see Listing 16.3).

Page 456

Figure 16.21.
Viewingrecords in the
Student_Schedule
table.

Listing 16.3. Assigning a grade with an UPDATE statement.

update Student_Schedule
set
Grade = `A',
Date_Grade_Assigned = sysdate
where
Student_ID = `10231324' and

Class_ID = `109100';

As shown in Figure 16.22, the Student_Schedule_Journal now has a single row with the expected values, including:

Operation U, which corresponds to the UPDATE statement that was executed
Grade A, which is the grade that was changed
Date_Grade_Assigned 04-MAY-97, which is when the row was updated

Page 457

Figure 16.22.
Verifying that the
trigger has fired.

Summary

This lesson explored these key concepts regarding Oracle Graphics and Procedure Builder:

Page 458

What Comes Next?

On Day 17, "Introduction to Oracle Power Objects," you will learn about this intuitive, object-oriented tool for developing client/server applications. Power Objects differs significantly from Oracle Forms. It uses a scripting language named Oracle Basic.

Q&A

Q Does Oracle Graphics offer the capability to "drill-down" into a chart for the purpose of seeing the details of a chart element?

A Yes. You can specify a detail query that will be executed when the user clicks on an element in the chart. The main chart is referred to as the master chart. A parameter is created that is used to define the relationship between the master and detail charts.

Q What are some of the built-in packages that you can view with Procedure Builder?

A The built-in packages include

Workshop

The purpose of the Workshop is to allow you to test your knowledge of the material discussed in the lesson. See if you can correctly answer the questions in the quiz and complete the exercises before you continue with tomorrow's lesson.

Quiz

  1. What are the two file types used by Oracle Graphics?
  2. Name three chart types that can be specified with the Graphics Designer.
  3. True or false? You can use the Stored Program Unit Editor to modify a package specification but not a package body.

Exercises

  1. Create a bar chart that displays the number of courses offered by each department at Flugle College.
  2. Modify the trigger on the Student_Schedule table so that it records changes to the Student_Schedule table only if the value of the Grade column is A, B, or C.

Previous | Table of Contents | Next

Используются технологии uCoz