Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Database Fundamental (TIS 1101)

Tutorial 7

Note to student: This tutorial is based on tables and value created in previous
tutorial (i.e., Tutorial 6)

Q1. Write the SQL statement to add an additional column called “Invoice_Price” into
the Invoice table.

ALTER table Invoice ADD COLUMN Invoice_Price decimal(7,2);

Q2. Update the newly created column Invoice_Price in to calculate the total amount
each buyer has to pay for each purchase based on the item quantity purchased and
the item unit price (obtained from the item table).

update Invoice
set Invoice_Price = Invoice_Qty *
(select Item_Price from Item where Item.Item_ID = Invoice.Item_ID);

Q3. List the most expensive item name and price in the company inventory.

select Item_Name, Item_Price


from Item
where Item_Price = (Select MAX(Item_Price) from Item)

Q4. Show the total number of active buyers.

select COUNT(*)
from Buyer
where Buyer_Status = 'Active'

Q5. Display the total price amount of invoice which have the item quantity purchase
more then or equals to 10.

select SUM(Invoice_Price)
from Invoice
where Invoice_Qty >= 10;
Q6. The company has decided to increase the stock of each item by 5 for items with
the type “Electrical”. Write the SQL statement to update each item price.

Update Item
Set Item_Bal = Item_Bal + 5
Where Item_Type = 'Electrical';

Q7. Create a new invoice with the following information :-


- Invoice ID = 10017
- Buyer name is Jerremy
- Invoice Quantity = 5
- Date of invoice 12th of December 2005
- Sales person in charge Clarry
- Item Purchase is LCD Monitor
- Invoice Price = NULL (we will use trigger to update this value in the next
question)

insert into Invoice values (10017,'2005-12-03', 1, 5, 659, 98665,NULL);

Extra Exercise:

Q8. Write the trigger so that when a buyer purchase an item, Item Price in invoice is
automatically updated based on the respective Item Price. Create the trigger.

create trigger Update_Invoice_Price


after insert on Invoice
for each row mode db2sql
update Invoice
set Invoice_Price = Invoice_Qty *
(select Item_Price from Item where Item.Item_ID = Invoice.Item_ID);

Q9. Create a new invoice again to test the created trigger in question 5 with the
following information :-
- Invoice ID = 10018
- Buyer name is Martin
- Invoice Quantity = 2
- Date of invoice 26/01/2008
- Sales person in charge Zelda
- Item Purchase is Nokia Phone
- Invoice Price = NULL (the trigger in question 5 should automatically update
this)

insert into Invoice values (10018,'2008-01-26', 8, 2, 989, 23598,NULL);

You might also like