0% found this document useful (0 votes)
3 views

Database Fundamentals Lab07

Uploaded by

M A
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Database Fundamentals Lab07

Uploaded by

M A
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Database Fundamental (TIS 1101)

Tutorial 7

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. Start DB2 and 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

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 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 named it as Product2 as follows:

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’?

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

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

14. Type ‘SELECT * FROM PRODUCT2’ again, what is the value of p_reorder column
for item ‘A0007’ now?
Extra 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:

You might also like