Page 385
Figure 14.5.
Using the PL/SQL
Editor to specify the
POST-QUERY trigger.
Listing 14.2. Specifying a POST-QUERY trigger.
select title into :class.course_title from course where course_id = :class.course_id and department_id = :department.department_id;
Let's try running the form to see if the trigger works correctly.
Now, let's see what happens when a new class is entered.
Page 386
Figure 14.6.
Testing the trigger.
Figure 14.7.
Post-query doesn't
work when a new
record is inserted.
Page 387
Figure 14.8.
Specifying the WHEN-
VALIDATE-ITEM
trigger for Course_ID.
Listing 14.3. WHEN-VALIDATE-ITEM trigger for Course_ID field.
begin select title into :class.course_title from course where course_id = :class.course_id and department_id = :department.department_id; exception when no_data_found then message (`Invalid Course ID. Please enter a valid Course ID.'); raise form_trigger_failure; end;
Page 388
Let's run the form again to test the item-level trigger.
Figure 14.9.
Testing the WHEN-
VALIDATE-ITEM
trigger for Course_ID.
Let's work through another sample trigger. Follow these steps to create a form that, when invoked, will automatically execute a query:
Page 389
Figure 14.10.
Creating an Instructor
form.
Page 390
Figure 14.11.
Testing the WHEN-
WINDOW-ACTIVATED
trigger.
In this section, you investigate another use for an item-level trigger: to validate user input. Suppose that the college president has told you that if an instructor is added to a department that already has at least one instructor who is a full professor, the new instructor cannot also be a full professorhe or she must be either an assistant or associate professor.
To do this, you will modify the existing WHEN-VALIDATE-ITEM trigger for the Position text item by expanding the Position text item and double-clicking the icon to the left of the WHEN-VALIDATE-ITEM trigger. As you can see in Figure 14.12, the existing trigger performs the same logic as the CHECK constraint on the Position column in the Instructor table. This trigger was automatically generated when you selected the Integrity Constraints when the block was created.
Listing 14.4 shows the WHEN-WINDOW-ACTIVATED trigger after it has been modified to perform this logic.
Listing 14.4. Modified WHEN-WINDOW-ACTIVATED trigger.
declare num_full_professors integer; begin if not( :INSTRUCTOR.POSITION IN (`ASSISTANT PROFESSOR', Â'ASSOCIATE PROFESSOR', `PROFESSOR') ) then message( `WHEN-VALIDATE-ITEM trigger failed on field - ` Â|| :system.trigger_field );
Page 391
raise form_trigger_failure; end if; select count(*) into num_full_professors from Instructor where Department_ID = :Instructor.Department_ID and ÂPosition = `PROFESSOR'; if num_full_professors > 0 and :Instructor.Position = `PROFESSOR' then message (`Cannot add another full professor to this department.'); raise form_trigger_failure; end if; end;
ANALYSIS
First, the PL/SQL statements are contained in an anonymous block. A variable named num_full_professors is declared. The first section of the block is used to enforce the CHECK constraint. Next, a SELECT statement is used to determine how many full professors exist in the department. If that number is greater than zero, a message is displayed, and a built-in exception, FORM_TRIGGER_FAILURE, is raised which prevents the user from continuing until the value is corrected.
Let's test the trigger. To create a new instructor, select Record | Insert from the menu.
Try entering a new instructor for the anthropology department which already has two
full professors, Boris Hitchcock and Ranier Poulson. As you can see in Figure 14.13, the
trigger is working. As intended, the trigger will not prevent you from adding a full professor to
the math department because that department does not already have a full professor.
Figure 14.12.
Existing code in the
WHEN-WINDOW-
ACTIVATED trigger.