InterBase Quick Start: Part V - More Practice with Generators and Triggers
From InterBase
Go Up to InterBase Quick Start: Part V - Advanced Topics
The remainder of this section on triggers takes you through the process of creating another generator and three more triggers. There are two ways to complete this section:
- Follow the insctructions and manually enter the remaining items.
- Use the
TRIGGERS.SQL
script.
Contents
Using the Script
If you choose to use the script do the following:
- Load the
TRIGGERS.SQL
script. - Execute the query.
- If you have successfully executed the
TRIGGER.SQL
script, we recommend that you back up your database. - Advance to the page Stored Procedures.
Manually Entering the Remaining Items
We recommend that you commit your work every time after you sucessfully create a trigger or a stored procedure.
More Practice with Generators and Triggers
- The next trigger that you create uses the
cust_no_gen
generator.CREATE generator cust_no_gen
SET generator cust_no_gen TO 1015
- Now execute the following
CREATE TRIGGER
statement to create theset_cust_no
trigger:CREATE TRIGGER set_cust_no FOR customer BEFORE INSERT AS BEGIN NEW.cust_no = gen_id ( cust_no_gen, 1 ); END
- To test this trigger, first confirm that the highest customer number is
1015
:SELECT MAX(cust_no) FROM customer
- Then insert the following row:
INSERT INTO customer (customer, contact_first, contact_last, phone_no, address_line1, address_line2, city, state_province, country, postal_code, on_hold) VALUES ('Big Rig', 'Henry', 'Erlig', '701-555-1212', '100 Big Rig Way', NULL, 'Atlanta', 'GA', 'USA', '70008', NULL)
- To confirm that the new customer number is
1016
, enter the following statement:SELECT cust_no FROM customer WHERE customer = 'Big Rig'