You are on page 1of 4

TIS1101/TDB2111 Database Fundamentals/Database Systems

Tutorial 8

SQL - Triggers

Objective: To create simple triggers in DB2 and examine the effects when the triggers are activated.
Since different DBMSs provide different ways of creating triggers, this practical is intended to give
just a ‘flavour’ of triggers. You will need to find out from the DBMS that you use how triggers can
be created. For DB2, more details can be found from the Information Centre.

1. Create the following table and insert five records into the table as shown in the table below:

p_code p_descript p_onhand p_min p_price p_discount p_min_order p_reorder


A0001 Book 8 5 12.67 0.00 25 0
A0002 Pencil 10 15 0.50 0.05 50 0
A0003 Ruler 18 12 0.80 0.00 50 0
A0004 Eraser 15 8 0.30 0.00 35 0
A0005 Pen 23 5 1.20 0.05 25 0

Create table product


(
p_code char(10) not null,
p_descript varchar(30) not null,
p_onhand integer,
p_min integer,
p_price decimal(6,2),
p_discount decimal(4,2),
p_min_order integer,
p_reorder integer,
primary key (p_code)
)

Insert into product values ('A0001', 'Book', 8, 5, 12.67, 0.00, 25, 0);
Insert into product values ('A0002', 'Pencil', 10,15,0.50, 0.00, 50, 0);
Insert into product values ('A0003', 'Ruler', 18, 12, 0.80, 0.00, 50, 0);
Insert into product values ('A0004', 'Pen', 15, 8, 0.30, 0.00, 35, 0);
Insert into product values ('A0005', 'Pen', 23, 5, 1.20, 0.05, 25, 0);
2. Enter the following commands to create a trigger. When will this trigger be activated?
What will happen to the database?

Create trigger reorder1


After insert on product
For each row mode db2sql
Update product
Set p_reorder = 1
Where p_onhand <= p_min

3. Type ‘SELECT * FROM PRODUCT’ to examine the contents of the table. Take note of the
p_reorder column.

4. Insert the following record into the table:

p_code p_descript p_onhand p_min p_price p_discount p_min_order p_reorder


A0006 Bag 8 10 22.75 0.05 25 0

Insert into product values ('A0006', 'Bag', 8, 10, 22.75, 0.05, 25, 0)

5. Type ‘SELECT * FROM PRODUCT’ to examine the contents of the table. What has
happened to the p_reorder column?

6. In DB2, you need to create different triggers for different events. Create the following trigger
for the same table.

Create trigger reorder2


After update of p_onhand, p_min on product
For each row mode db2sql
Update product
Set p_reorder = 1
Where p_onhand <= p_min

7. Execute the following SQL to command to change the quantity on hand of item ‘A0001’:

Update product
Set p_onhand = 4
Where p_code = 'A0001'
8. Type ‘SELECT * FROM PRODUCT’ to examine the contents of the table. Is the
p_reorder column updated for the item ‘A0001’?

9. Recreate the product table and name it as Product2:

Create table product2


(
p_code char(10) not null,
p_descript varchar(30) not null,
p_onhand integer,
p_min integer,
p_price decimal(6,2),
p_discount decimal(4,2),
p_min_order integer,
p_reorder integer,
primary key (p_code)
)

10. Insert the following records into Product2 table:

Insert into product2 values ('A0006', 'Book', 3, 6, 35.99, 0.00, 25, 0);
Insert into product2 values ('A0007', 'Case', 30, 10, 0.50, 0.00, 50, 0);
Insert into product2 values ('A0008', 'Ruler', 10, 12, 0.80, 0.00, 50, 0);
Insert into product2 values ('A0009', 'Staple', 15, 11, 0.80, 0.00, 50, 0);

11. Remove trigger reorder2 using the command ‘DROP TRIGGER REORDER2’. Rewrite
the trigger as the following:

Create trigger reorder2


No cascade before update of p_onhand, p_min on product2
Referencing new as n
For each row mode db2sql
Set n.p_reorder =
Case
When p_onhand > p_min then 0

When p_onhand <= p_min then 1


End

12. Execute the following SQL command to change the quantity on hand of item ‘A0007’.

Update product2
Set p_onhand = 8
Where p_code = 'A0007'
13. Type ‘SELECT * FROM PRODUCT2’. What is the value of p_reorder column for item
‘A0007’?

14. This time execute another SQL command to change the p_min of item ‘A0007’.

Update product2
Set p_min = 7
Where p_code = 'A0007'

15. Type ‘SELECT * FROM PRODUCT2’. Again, what is the value of p_reorder column
for item ‘A0007’ now?

Exercises:

Please use the Product table for the following exercises:

1. Remove the trigger reorder1 using the command ‘DROP TRIGGER REORDER1’.
Rewrite the trigger so that p_reorder is also updated when p_onhand is less than
p_min_order. Create and test the trigger.

2. Create a new trigger that will set the discount of an item automatically when a new item
record is inserted. The discount is 0.05 for item below 15.00 and 0.10 for 15.00 and
above.

3. What is the value of p_discount when the following record is inserted:

INSERT INTO Product VALUES (‘A0010’, ‘Calendar’, 15, 11, 29.99, 0.00, 50, 0)

You might also like