SlideShare a Scribd company logo
1 of 97
PL/SQL
INTERNET CAFE
AISLING FALLON
HIGHER DIPLOMA IN COMPUTING 2014
AISLING FALLON-A00203593
INTERNET CAFE Page 1
Question1 –BEFORE INSERT TRIGGER
Create a before insert trigger on the Gadget table, incorporatingIF statements to test the three followingrules to decide ifan insert will
proceed:
a) When a gadgets cost price is less than 25, the café year opened must be LESS greater than 2006
b) A gadget oftype laptop must be from a supplier in the United States
c) When a gadgets operatingsystem is LINUX the suppliers specialismmust be Servers
Drop TriggerGadget_Supplier_Internet_Cafe;
Create or Replace Trigger Gadget_Supplier_Internet_Cafe
Before Inserton Gadget
For Each Row
Declare
V_Country Supplier.Country%Type;
V_Year_Opened Internet_Cafe.Year_Opened%Type;
V_Specialism Supplier.Specialism%Type;
Begin
Select Year_OpenedInto V_Year_Opened
From Internet_Cafe
Where Internet_Cafe_Id=:New.Internet_Cafe_Id;
Select Country, SpecialismIntoV_Country,V_Specialism
From Supplier
Where Supplier_Id=:New.Supplier_Id;
/*(A) When a gadgets cost price is less than 25, the café year openedmust be LESS greater than 2006*/
If :New.Cost_Price< 25 And V_Year_Opened > 2006Then
Raise_Application_Error(-20101, ‘A)When a gadgets cost price is less than 25, the year openedmust be greater
than 2006’);
End If;
/*( B) A gadget oftype laptop must befrom a supplier in the United States*/
If :New.Type= ‘Laptop’ And V_Country<>’UnitedStates’ Then
Raise_Application_Error(-20101, ‘ B) A gadget oftype laptop must be from a supplier in the United States’);
End If;
/*(C) When a gadgets operatingsystem is LINUX the suppliers specialismmust be Servers*/
If :New.Operating_System=’Linux’AndV_Specialism<> ‘Servers’ Then
Raise_Application_Error(-20101, ‘C) When a gadgets operatingsystem is LINUX the suppliers specialismmust be
Servers’);
End If;
End;
/
Sho err
Drop TriggerGadget_Supplier_Internet_Cafe;
AISLING FALLON-A00203593
INTERNET CAFE Page 2
/*(A) When a gadgets cost price is less than 25, the café year openedmust be LESS than 2006*/
INVALID INSERT
Insert into Internet_Cafe values (177,'Cruise TheNet Cafe','MainStreet','Shercock','Cavan','Cruise The Net
Cafe','cruisethenet@hotmail.com',2008,5,'Colddrinks');
Insert into Suppliervalues (19,'Taecht.','Toronto','UnitedStates','www.techdere.com',2009,'PCs',55000);
Insert into Gadget values (8115,'Acer','PC',17.3,3.5,2,'Vista','Yes','No','13-Nov-10',20,36,550,177,19);
VALID INSERT
Insert into Internet_Cafe values (137,'Cruise TheNet Cafe','MainStreet','Shercock','Cavan','Cruise The Net
Cafe','cruisethenet@hotmail.com',2005,5,'Colddrinks');
Insert into Supplier values (21,'Taecht.','Toronto','UnitedStates','www.techdere.com',2003,'PCs',55000);
Insert into Gadget values (8117,'Acer','PC',17.3,3.5,2,'Vista','Yes','No','13-Nov-10',20,20,550,137,21);
AISLING FALLON-A00203593
INTERNET CAFE Page 3
/*( B) A gadget oftype laptop must befrom a supplier in the United States*/
VALID INSERT
Insert into Internet_Cafe values (217,'Cruise TheNet Cafe','Main Street','Shercock','Cavan','Cruise The Net
Cafe','cruisethenet@hotmail.com',2005,5,'Colddrinks');
Insert into Suppliervalues (82,'Taecht.','Toronto','United States','www.techdere.com',2009,'PCs',55000);
Insert into Gadget values (8136,'Acer','Laptop',17.3,3.5,2,'Vista','Yes','No','13-Nov-10',35,36,550,217,82);
INVALID INSERT
Insert into Internet_Cafe values (717,'Cruise TheNet Cafe','MainStreet','Shercock','Cavan','Cruise The Net
Cafe','cruisethenet@hotmail.com',2005,5,'Colddrinks');
Insert into Suppliervalues (61,'Taecht.','Toronto','Canada','www.techdere.com',2009,'PCs',55000);
Insert into Gadget values (7116,'Acer','Laptop',17.3,3.5,2,'Vista','Yes','No','13-Nov-10',35,36,550,717,61);
AISLING FALLON-A00203593
INTERNET CAFE Page 4
/*(C) When a gadgets operating system is LINUX the suppliers specialismmust be Servers*/
VALID INSERT
Insert into Internet_Cafe values (917,'Cruise TheNet Cafe','MainStreet','Shercock','Cavan','Cruise The Net
Cafe','cruisethenet@hotmail.com',2003,5,'Colddrinks');
Insert into Suppliervalues (39,'Taecht.','Toronto','UnitedStates','www.techdere.com',2009,'Servers',55000);
Insert into Gadget values (8664,'Acer','PC',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',20,36,550,917,39);
INVALID INSERT
Insert into Internet_Cafe values (375,'Cruise TheNet Cafe','MainStreet','Shercock','Cavan','Cruise The Net
Cafe','cruisethenet@hotmail.com',2003,5,'Colddrinks');
Insert into Suppliervalues (89,'Taecht.','Toronto','UnitedStates','www.techdere.com',2009,'PCs',55000);
Insert into Gadget values (7103,'Acer','PC',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',20,36,550,375,89);
AISLING FALLON-A00203593
INTERNET CAFE Page 5
AISLING FALLON-A00203593
INTERNET CAFE Page 6
Question 2-BEFORE UPDATE TRIGGER
Create n after update trigger on the Gadget table, incorporatingIF statements to test the three followingrules to decide ifan updatewill
proceed:
a) When the gadgets Name is Apple and the internet cafe county is in Roscommon and the year founded is greater than 2007,
update the cost price by 50euro
b) When the gadget operatingsystem is ‘Linux’ andthe Rate Per Hour in the internet cafe is greater than the Annual
Turnover, update the resale value bydecreasingit by 100
c) When the resale value ofa gadget is greater than 550 and the suppliers name contains am“a”,update the RAM_Size to 3
d) When the gadget type is laptop the year foundedofthe supplier shouldbe before the year opened ofthe internet cafe
Set Serveroutput On Size 1000000
Drop TriggerCUB_CU_Gadget;
Create or Replace Trigger CUB_CU_Gadget
Before Update on Gadget
For Each Row
Declare
V_County Internet_Cafe.County%Type;
V_Year_Founded Supplier.Year_Founded%Type;
V_Name Supplier.Name%Type;
V_Rate_Per_Hour Internet_Cafe.Rate_Per_Hour%Type;
V_Annual_Turnover Supplier.Annual_Turnover%Type;
V_Year_Opened Internet_Cafe.Year_Opened%Type;
Begin
Select County, Rate_Per_Hour, Year_OpenedInto V_County,V_Rate_Per_Hour,V_Year_Opened
From Internet_Cafe
Where Internet_Cafe_Id=:Old.Internet_Cafe_Id;
Select Name, Year_Founded, Annual_Turnover Into V_Name, V_Year_Founded, V_Annual_Turnover
From Supplier
Where Supplier.Supplier_Id=:Old.Supplier_Id;
/*(A)When the gadgets Name is Appleand the internet cafe countyis in Roscommon andthe year foundedis greater than
2007, update the cost price by50euro*/
If :Old.Name=’Apple’And V_County<>’Roscommon’Or V_Year_Founded < 2007Then
Raise_Application_Error(-20101, ‘A)The gadget must be apple andinternet café in Roscommon’);
EndIf;
/*(B)When the gadget operating system is ‘Linux’and the Rate Per Hour in the internet cafe is greater than the Annual
Turnover, update the resale value bydecreasingit by 100.*/
If :Old.Operating_System=’Linux’And V_Rate_Per_Hour > V_Annual_TurnoverThen
Raise_Application_Error(-20101, ‘B) Theanuual turnover must be greater than the rate per hour’);
EndIf;
/*(C)When the resale value ofa gadget is greater than 550and the suppliers namecontains am “a”, updatethe RAM_Size to 3
.*/
If :Old.Resale_Value>550 And V_Name Like ‘%a%’Then
Raise_Application_Error(-20101, ‘C) Thesuppliers name must not contain an a for the update to proceed’);
EndIf;
/*(D)When the gadget type is laptopthe year foundedofthe supplier should bebefore the year openedofthe internet cafe .*/
If :Old.Type=’Laptop’ And V_Year_Opened <V_Year_Founded Then
Raise_Application_Error(-20101, ‘D)The year founded ofthe supplier shouldbe before the year openedofthe
internet cafe’);
EndIf;
End;
/
Sho err
AISLING FALLON-A00203593
INTERNET CAFE Page 7
/*(A)When the gadgets Name is Appleand the internet cafe countyis in Roscommon andthe year foundedis greater than 2007, update
the cost price by 50euro*/
INVALID UPDATE
Insert into Internet_Cafe values (196,'Cruise TheNet Cafe','MainStreet','Shercock','Galway','Cruise TheNet
Cafe','cruisethenet@hotmail.com',2005,5,'Colddrinks');
Insert into Supplier values (37,'Tech Data Ltd.','Toronto','Canada','www.techdata.com', 2006,'PCs',55000);
Insert into Gadget values (256,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,196,37);
Update Gadget
Set Cost_Price= Cost_Price+50
Where Gadget_Id=256;
VALID UPDATE
Insert into Internet_Cafe values (150,'Cruise TheNet Cafe','MainStreet','Shercock','Roscommon','Cruise The Net
Cafe','cruisethenet@hotmail.com',2005,5,'Colddrinks');
Insert into Supplier values (09,'Tech Data Ltd.','Toronto','Canada','www.techdata.com',2009,'PCs',55000);
Insert into Gadget values (1050,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,150,09);
Update Gadget
Set Cost_Price= Cost_Price+50
Where Gadget_Id=1050;
AISLING FALLON-A00203593
INTERNET CAFE Page 8
/*(B)When the gadget operatingsystem is ‘Linux’and the Rate Per Hour in the internet cafe is greater than the Annual Turnover,
update the resale value by decreasingit by 100.*/
INVALID UPDATE
Insert into Internet_Cafe values (231,'Cruise TheNet Cafe','MainStreet','Shercock','Roscommon','Cruise TheNet
Cafe','cruisethenet@hotmail.com',2005,9,'Colddrinks');
Insert into Supplier values (92,'Tech Data Ltd.','Toronto','Canada','www.techdata.com', 2009,'PCs',8);
Insert into Gadget values (148,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,231,92);
Update Gadget
Set Resale_Value=Resale_Value-100
Where Gadget_Id=148;
VALID UPDATE
Insert into Internet_Cafe values (231,'Cruise TheNet Cafe','MainStreet','Shercock','Roscommon','Cruise TheNet
Cafe','cruisethenet@hotmail.com',2005,9,'Colddrinks');
Insert into Supplier values (92,'Tech Data Ltd.','Toronto','Canada','www.techdata.com', 2009,'PCs',50000);
Insert into Gadget values (148,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,231,92);
Update Gadget
Set Resale_Value=Resale_Value-100
Where Gadget_Id=148;
AISLING FALLON-A00203593
INTERNET CAFE Page 9
/*(C)When the resale value ofa gadget is greater than 550and the suppliers namecontains am “a”, updatethe RAM_Size to 3 .*/
INVALID UPDATE
Insert into Internet_Cafe values (231,'Cruise TheNet Cafe','MainStreet','Shercock','Roscommon','Cruise TheNet
Cafe','cruisethenet@hotmail.com',2005,9,'Colddrinks');
Insert into Supplier values (92,'Teacha Ltd.','Toronto','Canada','www.techdata.com', 2009,'PCs',50000);
Insert into Gadget values (148,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,560,231,92);
Update Gadget
Set RAM_Size=3
Where Gadget_Id=148;
VALID UPDATE
Insert into Internet_Cafe values (231,'Cruise TheNet Cafe','MainStreet','Shercock','Roscommon','Cruise TheNet
Cafe','cruisethenet@hotmail.com',2005,9,'Colddrinks');
Insert into Supplier values (92,'TeeckeLtd.','Toronto','Canada','www.techdata.com', 2009,'PCs',50000);
Insert into Gadget values (148,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,560,231,92);
Update Gadget
Set RAM_Size=3
Where Gadget_Id=148;
AISLING FALLON-A00203593
INTERNET CAFE Page 10
/*(D)When the gadget type is laptopthe year foundedofthe supplier should bebefore the year openedofthe internet cafe .*/
VALID UPDATE
Insert into Internet_Cafe values (231,'Cruise TheNet Cafe','MainStreet','Shercock','Roscommon','Cruise TheNet
Cafe','cruisethenet@hotmail.com',2012,9,'Colddrinks');
Insert into Supplier values (92,'TeeckeLtd.','Toronto','Canada','www.techdata.com', 2009,'PCs',50000);
Insert into Gadget values (148,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,560,231,92);
Update Gadget
Set RAM_Size=3
Where Gadget_Id=148;
INVALID UPDATE
Insert into Internet_Cafe values (231,'Cruise TheNet Cafe','MainStreet','Shercock','Roscommon','Cruise TheNet
Cafe','cruisethenet@hotmail.com',2009,9,'Colddrinks');
Insert into Supplier values (92,'TeeckeLtd.','Toronto','Canada','www.techdata.com', 2012,'PCs',50000);
Insert into Gadget values (148,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,560,231,92);
Update Gadget
Set RAM_Size=3
Where Gadget_Id=148;
AISLING FALLON-A00203593
INTERNET CAFE Page 11
AISLING FALLON-A00203593
INTERNET CAFE Page 12
Question 3-BEFORE DELETE TRIGGER
Before deletingcafé users bookingthe followingconditions need to bemet:
A) The amount due should be between 5 and10, the county shouldnot be Galwayand the cost priceshould begreater than 300.
B) A café user bookingwith payment methodcash, may not be deletedifthe user is a student or the ram size is less than 2.
C) A café user bookingwhose activityis email, may not be deletedifthe length ofthe length of the type is less less than the length of the
county in letters.
Drop TriggerCafe_User_Booking_Gad_Cafe_U;
Create or Replace Trigger Cafe_User_Booking_Gad_Cafe_U
Before Delete on Cafe_User_Booking
For Each Row
Declare
V_County Cafe_User.County%Type;
V_Type Cafe_User.Type%Type;
V_Cost_Price Gadget.Cost_Price%Type;
V_RAM_Size Gadget.RAM_Size%Type;
Begin
Select County, Type Into V_County,V_Type
From Cafe_User
Where Cafe_User_Id=:Old.Cafe_User_Id;
Select Cost_Price, RAM_Size Into V_Cost_Price,V_RAM_Size
From Gadget
Where Gadget_Id=:Old.Gadget_Id;
/*(A) The amount due shouldbe between 5 and 10, the countyshould not beGalwayand the cost priceshould begreater than
300*/
If :Old.Amount_Due BETWEEN 5 AND 10And V_County<>’Galway’ And V_Cost_Price > 300Then
Raise_Application_Error(-20101, ‘A)Theuser must be from Galway,gadget cost price less than 300 andamount
due on bookingbetween 5 and10 ’);
End If;
/*(B) A café user bookingwith payment methodcash, may not bedeletedifthe user is a student or the ram size is less than 2*/
If :Old.Payment_Method=’Cash’ And( V_Type=’Student’ orV_RAM_Size < 2) Then
Raise_Application_Error(-20101, ‘B)Café User must not be a student or a gadget with a ram size less than 2 ’);
End If;
/*(C) A café user bookingwhose activityis email, may not bedeletedifthe length ofthe length of the type is less less than the
length ofthe county in letters, */
If :Old.Activity=’Email’ And(Length(V_Type) < Length(V_County))Then
Raise_Application_Error(-20101, ‘C)Café User whose activity is email must have the type in letters greater than the
county ’);
End If;
End;
/
Sho err
AISLING FALLON-A00203593
INTERNET CAFE Page 13
/*(A) When a café user bookings amount due is between 5 and 10, the café user must not be from Galwaywith a gadget cost price less
than 300 in order to delete a café user booking*/
INVALID DELETE
Insertinto Cafe_User values (49,'Colm','Hannon','Abbey Street','Shercock','Galway','Male','23-Jan-90','Irish',086-
1234567,'colmh@gmail.com','Student','Novice');
Insertinto Gadgetvalues (7000,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',200,36,550,101,50);
Insertinto Cafe_User_Booking values (49,5000,'01-Sep-13','09:30','10:30',7,'Email','Cash');
Delete
From Cafe_User_Booking
Where Cafe_User_Id=49;
INVALID DELETE
Insertinto Cafe_User values (99,'Colm','Hannon','AbbeyStreet','Shercock','Roscommon','Male','23-Jan-90','Irish',086-
1234567,'colmh@gmail.com','Student','Novice');
Insertinto Gadgetvalues (5000,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50);
Insertinto Cafe_User_Booking values (99,5000,'01-Sep-13','09:30','10:30',7,'Email','Cash');
Delete
From Cafe_User_Booking
Where Cafe_User_Id=99;
AISLING FALLON-A00203593
INTERNET CAFE Page 14
/*(B) When a café user bookingpayment method is cash, the type ofcafé user must not be student andthe ram size must be less than
2*/
INVALID DELETE
Insertinto Cafe_User values (49,'Colm','Hannon','AbbeyStreet','Shercock','Galway','Male','23-Jan-90','Irish',086-
1234567,'colmh@gmail.com','Student','Novice');
Insertinto Gadget values (7000,'Acer','Laptop',17.3,3.5,1,'Linux','Yes','No','13-Nov-10',200,36,550,101,50);
Insertinto Cafe_User_Booking values (49,5000,'01-Sep-13','09:30','10:30',7,'Email','Cash');
Delete
From Cafe_User_Booking
Where Cafe_User_Id=49;
VALID DELETE
Insertinto Cafe_User values (49,'Colm','Hannon','Abbey Street','Shercock','Galway','Male','23-Jan-90','Irish',086-
1234567,'colmh@gmail.com','Graduate','Novice');
Insertinto Gadgetvalues (7000,'Acer','Laptop',17.3,3.5,5,'Linux','Yes','No','13-Nov-10',200,36,550,101,50);
Insertinto Cafe_User_Booking values (49,5000,'01-Sep-13','09:30','10:30',7,'Email','Cash');
Delete
From Cafe_User_Booking
Where Cafe_User_Id=49;
AISLING FALLON-A00203593
INTERNET CAFE Page 15
/*(C) A café user bookingwhose activityis email, may not bedeletedifthe length ofthe type is less less than the length ofthe county in
letters, */
INVALID DELETE
Insertinto Cafe_User values (49,'Colm','Hannon','AbbeyStreet','Shercock','Galway','Male','23-Jan-90','Irish',086-
1234567,'colmh@gmail.com','OAP','Novice');
Insertinto Gadgetvalues (7000,'Acer','Laptop',17.3,3.5,5,'Linux','Yes','No','13-Nov-10',200,36,550,101,50);
Insertinto Cafe_User_Booking values (49,5000,'01-Sep-13','09:30','10:30',7,'Email','Cash');
Delete
From Cafe_User_Booking
Where Cafe_User_Id=49;
VALID DELETE
Insertinto Cafe_User values (49,'Colm','Hannon','AbbeyStreet','Shercock','Galway','Male','23-Jan-90','Irish',086-
1234567,'colmh@gmail.com','Students','Novice');
Insertinto Gadgetvalues (7000,'Acer','Laptop',17.3,3.5,5,'Linux','Yes','No','13-Nov-10',200,36,550,101,50);
Insertinto Cafe_User_Booking values (49,5000,'01-Sep-13','09:30','10:30',7,'Email','Cash');
Delete
From Cafe_User_Booking
Where Cafe_User_Id=49;
AISLING FALLON-A00203593
INTERNET CAFE Page 16
AISLING FALLON-A00203593
INTERNET CAFE Page 17
Question 4-AFTER INSERT TRIGGER
Create an AFTER insert trigger on the Gadget table, incorporating select statements to retrieve data, IF statements will
evaluate/compare the data retrieved from the table, andtherefore influence which ofa number of update Statements to relatedtables
will be executed after the successful Insert on the Child table.
a) Increase the rate per hour in internet cafes by 10% when the gadget is oftype laptop
b) Increase the year opened ofinternet cafes by 3 years ifthe gadget ifthe gadget name is Dell
c) Increase the annual turnover by 1000 euro when the ram size ofa gadget is 2
d) Information relatingto the Insert must also be recordedin a LOG table
Drop Trigger Gad_Int_Cafe_Supp_Updates;
Drop Table New_Gadget_Audit_Log;
Create Table New_Gadget_Audit_Log(User_Involved_NameVarchar2(15),Current_Date Date,Gadget_Id Number (4));
Create Or Replace TriggerGad_Int_Cafe_Supp_Updates
After Insert On Gadget
For Each Row
Declare
V_Rate_Per_Hour Internet_Cafe.Rate_Per_Hour%Type;
V_Annual_Turnover Supplier.Annual_Turnover%Type;
V_Year_Opened Internet_Cafe.Year_Opened%Type;
Begin
Select Rate_Per_Hour, Year_Opened IntoV_Rate_Per_Hour, V_Year_Opened
From Internet_Cafe
Where Internet_Cafe_Id=:New.Internet_Cafe_Id;
Select Annual_TurnoverInto V_Annual_Turnover
From Supplier
Where Supplier_Id=:New.Supplier_Id;
Insertinto New_Gadget_Audit_Log Values (User , Sysdate,:New.Gadget_Id);
/*( A) Increase the rate per hour in internet cafes by 10% when the gadget is oftype laptop*/
If :New.Type= ‘Laptop’Then
Update Internet_Cafe
Set Rate_Per_Hour= Rate_Per_Hour*1.1
Where Internet_Cafe_Id=:New.Internet_Cafe_Id;
Endif;
/*( B) Increase the year opened ofinternet cafes by 3 years ifthe gadget ifthe gadget name is Dell*/
If :New.Name = ‘Dell’ Then
Update Internet_Cafe
Set Year_Opened= Year_Opened+3
Where Internet_Cafe_Id=:New.Internet_Cafe_Id;
Endif;
/*( C) Increase the annual turnover by 1000 eurowhen the ram size ofa gadget is 2*/
If :New.RAM_Size= 2 Then
Update Supplier
Set Annual_Turnover=Annual_Turnover+1000
Where Supplier_Id=:New.Supplier_Id;
Endif;
End;
/
Sho Err
/*( A) Increase the rate per hour in internet cafes by 10% when the gadget is oftype laptop*/
AISLING FALLON-A00203593
INTERNET CAFE Page 18
Select I.Rate_Per_Hour,I.Internet_Cafe_Id, G.Type
From Internet_Cafe I, Gadget G
Where I.Internet_Cafe_Id=G. Internet_Cafe_Id
And G.Type=’Laptop’;
Insert into Internet_Cafe values (197,'Cruise TheNet Cafe','MainStreet','Shercock','Cavan','Cruise The Net
Cafe','cruisethenet@hotmail.com',2008,5,'Colddrinks');
Insert into Suppliervalues (91,'Taecht.','Toronto','UnitedStates','www.techdere.com',2009,'PCs',55000);
Insert into Gadget values (7115,'Acer','Laptop',17.3,3.5,2,'Vista','Yes','No','13-Nov-10',20,36,550,197,91);
Select I.Rate_Per_Hour,I.Internet_Cafe_Id, G.Type
From Internet_Cafe I, Gadget G
Where I.Internet_Cafe_Id=G. Internet_Cafe_Id
And G.Type=’Laptop’;
AISLING FALLON-A00203593
INTERNET CAFE Page 19
/*(B) Increase the year openedofinternet cafes by 3 years ifthe gadget if the gadget name is Dell*/
Select I.Year_Opened, I.Internet_Cafe_Id, G.Name
From Internet_Cafe I, Gadget G
Where I.Internet_Cafe_Id=G. Internet_Cafe_Id
And G.Name=’Dell’
Insert into Internet_Cafe values (297,'Cruise TheNet Cafe','Main Street','Shercock','Cavan','Cruise The Net
Cafe','cruisethenet@hotmail.com',2008,5,'Colddrinks');
Insert into Suppliervalues (72,'Taecht.','Toronto','UnitedStates','www.techdere.com',2009,'PCs',55000);
Insert into Gadget values (7225,'Dell','Laptop',17.3,3.5,2,'Vista','Yes','No','13-Nov-10',20,36,550,297,72);
Select I.Year_Opened, I.Internet_Cafe_Id, G.Name
From Internet_Cafe I, Gadget G
Where I.Internet_Cafe_Id=G. Internet_Cafe_Id
And G.Name=’Dell’
AISLING FALLON-A00203593
INTERNET CAFE Page 20
/*( C) Increase the annual turnover by 1000 eurowhen the ram size ofa gadget is 2*/
Select S.Annual_Turnover, S.Supplier_Id, G.RAM_Size
From Supplier S, Gadget G
Where S.Supplier_Id=G.Supplier_Id
And G.Ram_Size=2;
Insert into Internet_Cafe values (397,'Cruise TheNet Cafe','MainStreet','Shercock','Cavan','Cruise The Net
Cafe','cruisethenet@hotmail.com',2008,5,'Colddrinks');
Insert into Suppliervalues (12,'Taecht.','Toronto','UnitedStates','www.techdere.com',2009,'PCs',55000);
Insert into Gadget values (7215,'Dell','Laptop',17.3,3.5,2,'Vista','Yes','No','13-Nov-10',20,36,550,397,12);
Select S.Annual_Turnover, S.Supplier_Id, G.RAM_Size
From Supplier S, Gadget G
Where S.Supplier_Id=G.Supplier_Id
And G.Ram_Size=2;
AISLING FALLON-A00203593
INTERNET CAFE Page 21
Select *
From New_Gadget_Audit_Log;
AISLING FALLON-A00203593
INTERNET CAFE Page 22
AISLING FALLON-A00203593
INTERNET CAFE Page 23
Question 5-AFTER UPDATE OF A COLUMN TRIGGER
After updatingthe details ofgadget repair the folloeingupdates shouldoccur providedthe conditions are met:
a) When the gadget repairs details are updatedto ‘fix system’,the hourly rate must be greater than the minimum hourly rate of
all technicians for the cost ofthe repair to increase by 10%
b) When the gadget repair details are updatedto ‘update window’, the technicians hourly rate must begreater than the maxram
size ofany gadget in order for the resale value to decrease by100euro.
c) When the gadget repair details are updatedto ‘Broken monitor’, the expected life ofthe gadget must be less than the average
screen size in order to update the hourly rate by cuttingit in half
Set Serveroutput On Size 1000000
Drop Table New_Gad_Rep_Audit_Log;
Create Table New_Gad_Rep_Audit_Log(Details Varchar2(35), User_Involved_Name Varchar2(15),Current_Date Date, Gadget_Id
Number (4));
Drop TriggerGadget_Rep_Tech_Gad;
Create or Replace Trigger Gadget_Rep_Tech_Gad
After Update of Details on Gadget_Repair
For Each Row
Declare
V_Min_Hourly_Rate Technician.Hourly_Rate%Type;
V_Max_RAM_Size Gadget.RAM_Size%Type;
V_Hourly_Rate Technician.Hourly_Rate%Type;
V_Avg_Screen_Size_Inches Gadget. Screen_Size_Inches%Type;
V_Expected_Life Gadget.Expected_Life%Type;
Begin
Select Min(Hourly_Rate) Into V_Min_Hourly_Rate
From Technician;
Select Hourly_Rate Into V_Hourly_Rate
From Technician
Where Technician_Id=:New.Technician_Id;
Select Max(RAM_Size) Into V_Max_RAM_Size
From Gadget;
Select Avg(Screen_Size_Inches)Into V_Avg_Screen_Size_Inches
From Gadget;
Select Expected_Life Into V_Expected_Life
From Gadget
Where Gadget_Id=:New.Gadget_Id;
/*(A)When the gadget repairs details are updated to ‘fix system’, the hourly rate must begreater than the minimumhourly rate
of all technicians for the cost of the repair to increase by 10%*/
If :Old.Details=’Remove Virus’ And:New.Details=’FixSystem’And V_Hourly_Rate > V_Min_Hourly_Rate Then
Update Gadget
Set Cost_Price= Cost_Price*1.1
Where Gadget.Gadget_Id= :New.Gadget_Id;
Insertinto New_Gad_Rep_Audit_Log Values(‘Cost_Price updated’,User, Sysdate,:New.Gadget_Id);
End If;
/*(B)When the gadget repair details are updated to ‘updatewindow’,the technicians hourly rate must be greater than the max
ram size of any gadget in order for the resale value to decrease by 100euro.*/
If :Old.Details=’Remove Virus’ And:New.Details=’Update Windows’ And V_Hourly_Rate > V_Max_RAM_Size Then
Update Gadget
Set Resale_Value=Resale_Value-100
Where Gadget.Gadget_Id= :New.Gadget_Id;
Insertinto New_Gad_Rep_Audit_Log Values(‘Resale_Value updated’, User, Sysdate,:New.Gadget_Id);
End If;
/*(C)When the gadget repair details are updated to ‘Brokenmonitor’,the expectedlife ofthe gadget must be less than the
average screen size in order to update the hourly rate by cuttingit in half*/
If :Old.Details=’Remove Virus’ And:New.Details=’Broken monitor’ And V_Expected_Life < V_Avg_Screen_Size_Inches Then
Update Technician
Set Hourly_Rate= Hourly_Rate /2
Where Technician.Technician_Id=:New.Technician_Id;
Insertinto New_Gad_Rep_Audit_Log Values(‘Hourly_Rate updated’, User, Sysdate, :New.Technician_Id);
End If;
End;
AISLING FALLON-A00203593
INTERNET CAFE Page 24
/
Sho err
/*(A)When the gadget repairs details are updated to ‘fix system’, the hourly rate must begreater than the minimumhourly rate of all
technicians for the cost ofthe repair to increase by 10%*/
VALID UPDATE
Insert into Gadget values (1127,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50);
Insert into Technicianvalues (2089,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',25,'23-Oct-08',086-4536578);
Insert into Gadget_Repair values (1127,'01-Nov-11',‘Remove Virus',30,120,2089);
Select Cost_Price
From Gadget
Where Gadget_Id=1127;
Update Gadget_Repair
Set Details=’Fix System’
Where Gadget_Id=1127;
Select Cost_Price
From Gadget
Where Gadget_Id=1127;
AISLING FALLON-A00203593
INTERNET CAFE Page 25
INVALID UPDATE
Insert into Gadget values (1127,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50);
Insert into Technicianvalues (2089,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',14,'23-Oct-08',086-4536578);
Insert into Gadget_Repair values (1127,'01-Nov-11',‘Remove Virus',30,120,2089);
Select Cost_Price
From Gadget
Where Gadget_Id=1127;
Update Gadget_Repair
Set Details=’Fix System’
Where Gadget_Id=1127;
Select Cost_Price
From Gadget
Where Gadget_Id=1127;
AISLING FALLON-A00203593
INTERNET CAFE Page 26
/*(B)When the gadget repair details are updated to ‘updatewindow’,the technicians hourly rate must be greater than the max ramsize
of any gadget in order for the resale value to decrease by 100euro.*/
VALID UPDATE
Insert into Gadget values (1139,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50);
Insert into Technicianvalues (2039,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',14,'23-Oct-08',086-4536578);
Insert into Gadget_Repair values (1139,'01-Nov-11',‘Remove Virus',90,120,2039);
Select Resale_Value
From Gadget
Where Gadget_Id=1139;
Update Gadget_Repair
Set Details=’Update Windows’
Where Gadget_Id=1139;
Select Resale_Value
From Gadget
Where Gadget_Id=1139;
AISLING FALLON-A00203593
INTERNET CAFE Page 27
INVALID UPDATE
Insert into Gadget values (1139,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50);
Insert into Technicianvalues (2039,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',1,'23-Oct-08',086-4536578);
Insert into Gadget_Repair values (1139,'01-Nov-11',‘Remove Virus',10,120,2039);
Select Resale_Value
From Gadget
Where Gadget_Id=1139;
Update Gadget_Repair
Set Details=’Update Windows’
Where Gadget_Id=1139;
Select Resale_Value
From Gadget
Where Gadget_Id=1139;
AISLING FALLON-A00203593
INTERNET CAFE Page 28
/*(C)When the gadget repair details are updated to ‘broken monitor’, the expectedlife ofthe gadget must be less than the average screen
size in order to update the hourly rate by cuttingit in half.*/
INVALID UPDATE
Insert into Gadget values (1189,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50);
Insert into Technicianvalues (2079,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',14,'23-Oct-08',086-4536578);
Insert into Gadget_Repair values (1189,'01-Nov-11',‘Remove Virus',30,120,2079);
Select Hourly_Rate
From Technician
Where Technician_Id=2079;
Update Gadget_Repair
Set Details=’Brokenmonitor’
Where Technician_Id=2079;
Select Hourly_Rate
From Technician
Where Technician_Id=2079;
AISLING FALLON-A00203593
INTERNET CAFE Page 29
VALID UPDATE
Insert into Gadget values (1189,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,5,550,101,50);
Insert into Technicianvalues (2079,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',14,'23-Oct-08',086-4536578);
Insert into Gadget_Repair values (1189,'01-Nov-11',‘Remove Virus',30,120,2079);
Select Hourly_Rate
From Technician
Where Technician_Id=2079;
Update Gadget_Repair
Set Details=’Brokenmonitor’
Where Technician_Id=2079;
Select Hourly_Rate
From Technician
Where Technician_Id=2079;
AISLING FALLON-A00203593
INTERNET CAFE Page 30
AISLING FALLON-A00203593
INTERNET CAFE Page 31
Question 6-AFTER DELETE TRIGGER
After deletinga gadget repair,amendments canbe made to othertables providedthefollowingconditions are met:
A)When the gadget repairs details are updatedto ‘fix system’, the hourly rate must be greater than the minimumhourly rate of all
technicians for the cost ofthe repair to increase by 10%, when a delete occurs.
B)When the gadget repair details are ‘REMOVE VIRUS’, the technicians hourly rate must be greater than the max ramsize ofany
gadget in order for the resale value to decrease by100euro.
C)When the gadget repair details are updated to ‘Broken monitor’,the expectedlife ofthe gadget must be less than the average screen
size in order to update the hourly rate by cuttingit in half
Set Serveroutput On Size 1000000
Drop Table Old_G_Rep_Gad_Tech_Audit_Log;
Create Table Old_G_Rep_Gad_Tech_Audit_Log(Details Varchar2(35),User_Involved_Name Varchar2(15), Current_Date Date,
Gadget_IdNumber (4));
Drop TriggerGad_Rep_Tech_Gad;
Create or Replace Trigger Gad_Rep_Tech_Gad
After Delete on Gadget_Repair
For Each Row
Declare
V_Min_Hourly_Rate Technician.Hourly_Rate%Type;
V_Max_RAM_Size Gadget.RAM_Size%Type;
V_Hourly_Rate Technician.Hourly_Rate%Type;
V_Avg_Screen_Size_Inches Gadget. Screen_Size_Inches%Type;
V_Expected_Life Gadget.Expected_Life%Type;
Begin
Select Min(Hourly_Rate) Into V_Min_Hourly_Rate
From Technician;
Select Hourly_Rate Into V_Hourly_Rate
From Technician
Where Technician_Id=:Old.Technician_Id;
Select Max(RAM_Size) Into V_Max_RAM_Size
From Gadget;
Select Avg(Screen_Size_Inches)Into V_Avg_Screen_Size_Inches
From Gadget;
Select Expected_Life Into V_Expected_Life
From Gadget
Where Gadget_Id=:Old.Gadget_Id;
/*(A)When the gadget repairs details are updated to ‘fix system’, the hourly rate must begreater than the minimumhourly rate
of all technicians for the cost of the repair to increase by 10%, when a delete occurs.*/
If :Old.Details=’Remove Virus’ And V_Hourly_Rate > V_Min_Hourly_Rate Then
Update Gadget
Set Cost_Price= Cost_Price*1.1
Where Gadget.Gadget_Id= :Old.Gadget_Id;
Insertinto Old_G_Rep_Gad_Tech_Audit_LogValues
(‘Cost_Price updated’, User, Sysdate, :Old.Gadget_Id);
End If;
/*(B)When the gadget repair details are ‘REMOVE VIRUS’, the technicians hourly rate must be greater than the maxram size
of any gadget in order for the resale value to decrease by 100euro.*/
If :Old.Details=’Remove Virus’ And V_Hourly_Rate > V_Max_RAM_Size Then
Update Gadget
Set Resale_Value=Resale_Value-100
Where Gadget.Gadget_Id= :Old.Gadget_Id;
Insertinto Old_G_Rep_Gad_Tech_Audit_LogValues (‘Resale_Value updated’, User, Sysdate, :Old.Gadget_Id);
End If;
/*(C)When the gadget repair details are updated to ‘Brokenmonitor’,the expectedlife ofthe gadget must be less than the
average screen size in order to update the hourly rate by cuttingit in half*/
If :Old.Details=’Remove Virus’ And V_Expected_Life< V_Avg_Screen_Size_Inches Then
Update Technician
Set Hourly_Rate= Hourly_Rate /2
Where Technician.Technician_Id=:Old.Technician_Id;
Insertinto Old_G_Rep_Gad_Tech_Audit_Log Values(‘Hourly_Rate updated’, User, Sysdate, :Old.Technician_Id);
End If;
End;
/
Sho er
/*(A)When the gadget repairs details are updated to ‘fix system’, the hourly rate must begreater than the minimumhourly rate of all
technicians for the cost ofthe repair to increase by 10%*/
AISLING FALLON-A00203593
INTERNET CAFE Page 32
VALID DELETE
Insert into Gadget values (1397,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50);
Insert into Technicianvalues (2059,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',25,'23-Oct-08',086-4536578);
Insert into Gadget_Repair values (1397,'01-Nov-11',‘Remove Virus',30,120,2059);
Select Cost_Price
From Gadget
Where Gadget_Id=1397;
Delete
From Gadget_Repair
Where Repair_Date='01-Nov-11'
And Gadget_Id=1397;
Select Cost_Price
From Gadget
Where Gadget_Id=1397;
AISLING FALLON-A00203593
INTERNET CAFE Page 33
INVALID DELETE
Insert into Gadget values (2597,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50);
Insert into Technicianvalues (7159,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',6,'23-Oct-08',086-4536578);
Insert into Gadget_Repair values (2597,'01-Nov-11',‘Remove Virus',30,120,7159);
Select Cost_Price
From Gadget
Where Gadget_Id=2597;
Delete
From Gadget_Repair
Where Repair_Date='01-Nov-11';
And Gadget_Id=2597;
Select Cost_Price
From Gadget
Where Gadget_Id=2597;
AISLING FALLON-A00203593
INTERNET CAFE Page 34
/*(B)When the gadget repair details are remove virus, the technicians hourly rate must be greater than the max ramsize ofany gadget in
order for the resale value to decrease by 100 euro.*/
VALID DELETE
Insert into Gadget values (1141,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50);
Insert into Technicianvalues (2039,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',14,'23-Oct-08',086-4536578);
Insert into Gadget_Repair values (1141,'01-Nov-11',‘Remove Virus',90,120,2039);
Select Resale_Value
From Gadget
Where Gadget_Id=1141;
Delete
From Gadget_Repair
Where Repair_Date='01-Nov-11';
And Gadget_Id=1141;
Select Resale_Value
From Gadget
Where Gadget_Id=1141;
#
AISLING FALLON-A00203593
INTERNET CAFE Page 35
INVALID DELETE
Insert into Gadget values (7139,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50);
Insert into Technicianvalues (7039,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',1,'23-Oct-08',086-4536578);
Insert into Gadget_Repair values (7139,'03-Nov-11',‘Remove Virus',10,120,7039);
Select Resale_Value
From Gadget
Where Gadget_Id=7139;
Delete
From Gadget_Repair
Where Repair_Date='03-Nov-11'
And Gadget_Id=7139;
Select Resale_Value
From Gadget
Where Gadget_Id=7139;
AISLING FALLON-A00203593
INTERNET CAFE Page 36
/*(C)When the gadget repair details are updated to ‘broken monitor’, the expectedlife ofthe gadget must be less than the
average screen size in order to update the hourly rate by cuttingit in half .*/
INVALID DELETE
Insert into Gadget values (1369,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50);
Insert into Technicianvalues (2091,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',14,'23-Oct-08',086-4536578);
Insert into Gadget_Repair values (1369,'05-Nov-11',‘Remove Virus',30,120,2091);
Select Hourly_Rate
From Technician
Where Technician_Id=2091;
Delete
From Gadget_Repair
Where Repair_Date='05-Nov-11'
And Gadget_Id=1369;
Select Hourly_Rate
From Technician
Where Technician_Id=2091;
AISLING FALLON-A00203593
INTERNET CAFE Page 37
VALID DELETE
Insert into Gadget values (1369,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,5,550,101,50);
Insert into Technicianvalues (2091,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',14,'23-Oct-08',086-4536578);
Insert into Gadget_Repair values (1369,'05-Nov-11',‘Remove Virus',30,120,2091);
Select Hourly_Rate
From Technician
Where Technician_Id=2091;
Delete
From Gadget_Repair
Where Repair_Date='05-Nov-11'
And Gadget_Id=1369;
Select Hourly_Rate
From Technician
Where Technician_Id=2091;
Select *
From Old_G_Rep_Gad_Tech_Audit_Log;
AISLING FALLON-A00203593
INTERNET CAFE Page 38
AISLING FALLON-A00203593
INTERNET CAFE Page 39
Question 7-BEFOREINSERT/UPDATE/DELETE TRIGGER
Before inserts, updates and deletes can onlybe performedprovidedthe followingconditions are met:
A) When a gadgets cost price is less than 25, the café year openedmust be LESS than 2006*/
B)When the gadgets Name is Apple and the internet cafe county is in Roscommon andthe year foundedis greater than 2007, update the
cost price by 50euro*/
C) When the type ofgadget is PC, the specialism msut be Servers and the length ofthe email contain an”a”
Set Serveroutput On Size 1000000
Drop TriggerGadget_Yr_Opened;
Create or Replace Trigger Gadget_Yr_Opened
Before Insert or Update or Delete onGadget
For Each Row
Declare
V_Year_Opened Internet_Cafe.Year_Opened%Type;
V_County Internet_Cafe.County%Type;
V_Specialism Supplier.Specialism%Type;
V_Email Internet_Cafe.Email%Type;
Begin
/*(A) When a gadgets cost price is less than 25, the café year openedmust be LESS than 2006*/
If Inserting Then
Select Year_OpenedInto V_Year_Opened
From Internet_Cafe
Where Internet_Cafe_Id=:New.Internet_Cafe_Id;
If :New.Cost_Price< 25 And V_Year_Opened > 2006 Then
Raise_Application_Error(-20101, ‘A)When a gadgets cost price is less than 25, the year openedmust be greater
than 2006’);
End If;
/*(B)When the gadgets Name is Appleand the internet cafe countyis in Roscommon andthe year foundedis greater than
2007, update the cost price by50euro*/
Elsif UpdatingThen
Select County IntoV_County
From Internet_Cafe
Where Internet_Cafe_Id=:New.Internet_Cafe_Id;
If :Old.Name=’Apple’And V_County<>’Roscommon’Then
Raise_Application_Error(-20101, ‘A)The gadget must be apple andinternet café in Roscommon’);
End If;
/*(C) When the type ofgadget is PC, the specialism msut be Servers and the length ofthe email contain an”a”*/
Elsif DeletingThen
Select Specialism Into V_Specialism
From Supplier
Where Supplier.Supplier_Id=:Old.Supplier_Id;
Select Email IntoV_Email
From Internet_Cafe
Where Internet_Cafe_Id=:Old.Internet_Cafe_Id;
If :Old.Type=’PC’ And V_Specialism<>’Servers’Or V_Email LIKE‘%a%’ Then
Raise_Application_Error(-20101, ‘A)When the gadget typeis PC, the specialsm must be Servers andemail must
not contain an “a” ’);
End If;
End If;
End;
/
Sho err
AISLING FALLON-A00203593
INTERNET CAFE Page 40
INVALID INSERT
Insert into Internet_Cafe values (129,'Cruise TheNet Cafe','MainStreet','Shercock','Cavan','Cruise The Net
Cafe','cruisethenet@hotmail.com',2008,5,'Colddrinks');
Insert into Suppliervalues (71,'Taecht.','Toronto','UnitedStates','www.techdere.com',2009,'PCs',55000);
Insert into Gadget values (8635,'Acer','PC',17.3,3.5,2,'Vista','Yes','No','13-Nov-10',20,36,550,129,71);
VALID INSERT
Insert into Internet_Cafe values (137,'Cruise TheNet Cafe','MainStreet','Shercock','Cavan','Cruise The Net
Cafe','cruisethenet@hotmail.com',2005,5,'Colddrinks');
Insert into Supplier values (21,'Taecht.','Toronto','UnitedStates','www.techdere.com',2003,'PCs',55000);
Insert into Gadget values (8117,'Acer','PC',17.3,3.5,2,'Vista','Yes','No','13-Nov-10',20,20,550,137,21);
AISLING FALLON-A00203593
INTERNET CAFE Page 41
INVALID UPDATE
Insert into Internet_Cafe values (196,'Cruise TheNet Cafe','MainStreet','Shercock','Galway','Cruise TheNet
Cafe','cruisethenet@hotmail.com',2005,5,'Colddrinks');
Insert into Supplier values (37,'Tech Data Ltd.','Toronto','Canada','www.techdata.com', 2009,'PCs',55000);
Insert into Gadget values (256,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,196,37);
Update Gadget
Set Cost_Price= Cost_Price+50
Where Gadget_Id=256;
VALID UPDATE
Insert into Internet_Cafe values (150,'Cruise TheNet Cafe','MainStreet','Shercock','Roscommon','Cruise The Net
Cafe','cruisethenet@hotmail.com',2005,5,'Colddrinks');
Insert into Supplier values (09,'Tech Data Ltd.','Toronto','Canada','www.techdata.com',2009,'PCs',55000);
Insert into Gadget values (1050,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,150,09);
Update Gadget
Set Cost_Price= Cost_Price+50
Where Gadget_Id=1050;
AISLING FALLON-A00203593
INTERNET CAFE Page 42
INVALID DELETE
Insert into Internet_Cafe values (150,'Cruise TheNet Cafe','MainStreet','Shercock','Roscommon','Cruise TheNet
Cafe','cruas@hotmail.com',2005,5,'Colddrinks');
Insert into Supplier values (09,'Tech Data Ltd.','Toronto','Canada','www.techta.com',2009,'PC',55000);
Insert into Gadget values (1050,'Apple','PC',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,150,09);
Delete
From Gadget
Where Gadget_Id=1050;
VALID DELETE
Insert into Internet_Cafe values (150,'Cruise TheNet Cafe','MainStreet','Shercock','Roscommon','Cruise TheNet
Cafe','cruse@hotmail.com',2005,5,'Colddrinks');
Insert into Supplier values (09,'Tech Data Ltd.','Toronto','Canada','www.techdata.com',2009,'Servers',55000);
Insert into Gadget values (1050,'Apple','PC',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,150,09);
Delete
From Gadget
Where Gadget_Id=1050;
AISLING FALLON-A00203593
INTERNET CAFE Page 43
AISLING FALLON-A00203593
INTERNET CAFE Page 44
Question 8-AFTER INSERT/UPDATE/DELETE
Amendments made to gadget repair can bemade after insert, update anddelete provided the folloeingconditions are met:
A)When the gadget repair details are updated to ‘Broken monitor’,the expectedlife ofthe gadget must be less than the average screen
size in order to update the hourly rate by cuttingit in half*/
B)When the gadget repairs details are updatedto ‘fix system’, the hourly rate must be greater than the minimumhourly rate of all
technicians for the cost ofthe repair to increase by 10%*/
C)When the gadget repair details are REMOVE VIRUS the technicians hourly rate must be greater than the max ramsize ofany gadget
in order for the resale value to decrease by 100euro
Set Serveroutput On Size 1000000
Drop Trigger Gad_Tech_GR;
Create or Replace Trigger Gad_Tech_GR
After Insert or Update or Delete on Gadget_Repair
For Each Row
Declare
V_Min_Hourly_Rate Technician.Hourly_Rate%Type;
V_Hourly_Rate Technician.Hourly_Rate%Type;
V_Max_RAM_Size Gadget.RAM_Size%Type;
V_Avg_Screen_Size_Inches Gadget.Screen_Size_Inches%Type;
V_Expected_Life Gadget.Expected_Life%Type;
Begin
/*(A)When the gadget repair details are updated to ‘Broken monitor’, the expected life of the gadget must be less than the average screen size in
order to update the hourly rate by cutting it in half */
If Inserting Then
Select Avg(Screen_Size_Inches) Into V_Avg_Screen_Size_Inches
From Gadget;
Select Expected_Life Into V_Expected_Life
From Gadget
Where Gadget_Id=:New.Gadget_Id;
If :New.Details=’Remove Virus’ And V_Expected_Life < V_Avg_Screen_Size_Inches Then
Update Technician
Set Hourly_Rate= Hourly_Rate /2
Where Technician.Technician_Id=:New.Technician_Id;
End If;
/*(B)When the gadget repairs details are updated to ‘fix system’, the hourly rate must be greater than the minimum hourly rat e of all technicians
for the cost of the repair to increase by 10%*/
Elsif Updating Then
Select Min(Hourly_Rate) Into V_Min_Hourly_Rate
From Technician;
Select Hourly_Rate Into V_Hourly_Rate
From Technician
Where Technician_Id=:New.Technician_Id;
If :Old.Details=’Remove Virus’ And :New.Details=’Fix System’ And V_Hourly_Rate > V_Min_Hourly_Rate Then
Update Gadget
Set Cost_Price= Cost_Price*1.1
Where Gadget.Gadget_Id= :New.Gadget_Id;
End If;
Elsif Deleting Then
/*(C)When the gadget repair details are REMOVE VIRUS the technicians hourly rate must be greater than the max ram size of any gadget in
order for the resale value to decrease by 100 euro.*/
Select Max(RAM_Size) Into V_Max_RAM_Size
From Gadget;
Select Hourly_Rate Into V_Hourly_Rate
From Technician
Where Technician_Id=:Old.Technician_Id;
If :Old.Details=’Remove Virus’ And V_Hourly_Rate > V_Max_RAM_Size Then
Update Gadget
Set Resale_Value=Resale_Value-100
Where Gadget.Gadget_Id=:Old.Gadget_Id;
End If;
End If;
End;
/
Sho Err
AISLING FALLON-A00203593
INTERNET CAFE Page 45
/*(A)When the gadget repair details are updated to ‘Brokenmonitor’,the expectedlife ofthe gadget must be less than the average screen
size in order to update the hourly rate by cuttingit in half*/
INVALID INSERT
Select T.Hourly_Rate, GR.Details
From TechnicianT,Gadget_Repair GR
Where T.Technician_Id=GR.Technician_Id
And GR.Details=’Remove Virus’;
Insert into Gadget values (2300,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,3,550,101,50);
Insert into Technicianvalues (2110,'Aidan','Cassidy','Market Square','Shercock','Cavan','Certificate','Software',15,'23-Oct-08',086-
4536578);
Insert into Gadget_Repair values (2300,'13-Nov-11','Remove Virus',30,120,2110);
Select T.Hourly_Rate, GR.Details
From TechnicianT,Gadget_Repair GR
Where T.Technician_Id=GR.Technician_Id
And GR.Details=’Remove Virus’;
VALID INSERT
Select T.Hourly_Rate, GR.Details
From TechnicianT,Gadget_Repair GR
Where T.Technician_Id=GR.Technician_Id
And GR.Details=’Remove Virus’;
Insert into Gadget values (2300,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50);
Insert into Technicianvalues (2110,'Aidan','Cassidy','Market Square','Shercock','Cavan','Certificate','Software',15,'23-Oct-08',086-
4536578);
Insert into Gadget_Repair values (2300,'13-Nov-11','Remove Virus',30,120,2110);
Select T.Hourly_Rate, GR.Details
From TechnicianT,Gadget_Repair GR
Where T.Technician_Id=GR.Technician_Id
And GR.Details=’Remove Virus’;
AISLING FALLON-A00203593
INTERNET CAFE Page 46
/*(B)When the gadget repairs details are updated to ‘fix system’, the hourly rate must begreater than the minimumhourly rate of all
technicians for the cost ofthe repair to increase by 10%*/
VALID UPDATE
Insert into Gadget values (1927,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50);
Insert into Technicianvalues (2029,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',25,'23-Oct-08',086-4536578);
Insert into Gadget_Repair values (1927,'01-Nov-11',‘Remove Virus',30,120,2029);
Select Cost_Price
From Gadget
Where Gadget_Id=1927;
Update Gadget_Repair
Set Details=’Fix System’
Where Gadget_Id=1927;
Select Cost_Price
From Gadget
Where Gadget_Id=1927;
AISLING FALLON-A00203593
INTERNET CAFE Page 47
INVALID UPDATE
Insert into Gadget values (1927,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50);
Insert into Technicianvalues (2029,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',2,'23-Oct-08',086-4536578);
Insert into Gadget_Repair values (1927,'01-Nov-11',‘Remove Virus',30,120,2029);
Select Cost_Price
From Gadget
Where Gadget_Id=1927;
Update Gadget_Repair
Set Details=’Fix System’
Where Gadget_Id=1927;
Select Cost_Price
From Gadget
Where Gadget_Id=1927;
AISLING FALLON-A00203593
INTERNET CAFE Page 48
/*(C)When the gadget repair details remove virus, the technicians hourly rate must be greater than the max ramsize of any gadget in
order for the resale value to decrease by 100 euro.*/
VALID DELETE
Insert into Gadget values (7551,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50);
Insert into Technicianvalues (8019,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',14,'23-Oct-08',086-4536578);
Insert into Gadget_Repair values (7551,'08-Nov-11',‘Remove Virus',90,120,8019);
Select Resale_Value
From Gadget
Where Gadget_Id=7551;
Delete
From Gadget_Repair
Where Repair_Date='08-Nov-11'
And Gadget_Id=7551;
Select Resale_Value
From Gadget
Where Gadget_Id=7551;
AISLING FALLON-A00203593
INTERNET CAFE Page 49
INVALID DELETE
Insert into Gadget values (7139,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50);
Insert into Technicianvalues (7039,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',1,'23-Oct-08',086-4536578);
Insert into Gadget_Repair values (7139,'03-Nov-11',‘Remove Virus',10,120,7039);
Select Resale_Value
From Gadget
Where Gadget_Id=7139;
Delete
From Gadget_Repair
Where Repair_Date='03-Nov-11'
And Gadget_Id=7139;
Select Resale_Value
From Gadget
Where Gadget_Id=7139;
AISLING FALLON-A00203593
INTERNET CAFE Page 50
AISLING FALLON-A00203593
INTERNET CAFE Page 51
Question9-TABLE LEVEL TRIGGER
Create a table level trigger to allow the followingto occur on the gadget table:
A) You may onlydelete from the gadget table between8 and6, Mondayto Friday
B) You may onlyinsert from the gadget table between8 and6, Mondayto Friday
C) You may onlyupdate from the gadget tablebetween8 and 6,Monday to Friday
VALID INSERTS, UPDATES, DELETES
(Inserts updates anddeletes tookplacebetween 8am and6pm, MondaytoFriday)
Drop Trigger Gagdet_Date_Cost;
Create or ReplaceTrigger Gagdet_Date_Cost
Before Insert or Update or Delete onGadget
Begin
IF(TO_CHAR(Sysdate, ‘DY’)IN(‘SAT’, ‘SUN’)) OR (TO_CHAR(Sysdate,‘HH24’) NOT BETWEEN ‘08’AND ‘18’)Then
If DeletingThen
Raise_Application_Error(-20502,‘Youmay onlydeletefromgadget between8 and6, MondaytoFriday’);
Elsif InsertingThen
Raise_Application_Error(-20500, ‘Youmayonlyinsert fromgadget between8 and6, MondaytoFriday’);
Elsif Updating(‘RAM_Size’) Then
Raise_Application_Error(-20503, ‘Youmayonlyupdate from gadget between 8 and6, Monday toFriday’);
Else
Raise_Application_Error(-20503, ‘Youmayonlyupdate from gadget between 8 and6, Monday toFriday’);
EndIf;
EndIf;
End;
/
Sho Err
VALID UPDATE
Update Gadget
Set RAM_Size= RAM_Size+2
Where RAM_Size=2;
VALID INSERT
Insert into Internet_Cafe values (196,'Cruise TheNet Cafe','MainStreet','Shercock','Galway','Cruise TheNet
Cafe','cruisethenet@hotmail.com',2005,5,'Colddrinks');
Insert into Supplier values (37,'Tech Data Ltd.','Toronto','Canada','www.techdata.com', 2006,'PCs',55000);
Insert into Gadget values (256,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,196,37);
AISLING FALLON-A00203593
INTERNET CAFE Page 52
VALID DELETE
Delete
From Gadget
Where Gadget_Id=1000;
AISLING FALLON-A00203593
INTERNET CAFE Page 53
IINVALID INSERTS, UPDATES, DELETES
(Inserts updates anddeletes tookplacebetween 2pmand6 pm,Wednesday orThursday)
Drop Trigger Gagdet_Date_Cost_RAM;
Create or ReplaceTrigger Gagdet_Date_Cost_RAM
Before Insert or Update or Delete onGadget
Begin
IF(TO_CHAR(Sysdate, ‘DY’)IN(‘WED’,‘THURS’)) OR (TO_CHAR(Sysdate, ‘HH24’) NOT BETWEEN ‘14’ AND‘18’)Then
If DeletingThen
Raise_Application_Error(-20502,‘Youmay not delete onWednesday or Thursdayandbetween thehours of 2 and6’);
Elsif InsertingThen
Raise_Application_Error(-20500, ‘Youmayonlydelete on Wednesday or Thursdayandbetween thehours of 2 and6’);
Elsif Updating(‘RAM_Size’) Then
Raise_Application_Error(-20503, ‘Youmayonlyupdate onWednesday orThursdayandbetween thehours of 2 and6’);
Else
Raise_Application_Error(-20503, ‘Youmayonlyupdate from gadget between 8 and6, Monday toFriday’);
EndIf;
EndIf;
End;
/
Sho Err
INVALID INSERT
Insert into Internet_Cafe values (196,'Cruise TheNet Cafe','MainStreet','Shercock','Galway','Cruise TheNet
Cafe','cruisethenet@hotmail.com',2005,5,'Colddrinks');
Insert into Supplier values (37,'Tech Data Ltd.','Toronto','Canada','www.techdata.com', 2006,'PCs',55000);
Insert into Gadget values (256,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,196,37);
INVALID DELETE
Delete
From Gadget
Where Gadget_Id=1000;
AISLING FALLON-A00203593
INTERNET CAFE Page 54
INVALID UPDATE
Update Gadget
Set RAM_Size= RAM_Size+2
Where RAM_Size=2;
AISLING FALLON-A00203593
INTERNET CAFE Page 55
Question 10-TABLE LEVEL TRIGGER(BEFORE DELETE)
Create a Table level trigger on the Café User bookingto ensure you can only deletea bookingbetween 8am and6pm Mondayto Friday.
Drop Trigger Cafe_User_Book_Date;
Create or ReplaceTrigger Cafe_User_Booking_Date
Before Delete onCafe_User_Booking
Begin
IF(TO_CHAR(Sysdate, ‘DY’)IN(‘SAT’, ‘SUN’)) OR (TO_CHAR(Sysdate,‘HH24’) NOT BETWEEN ‘08’AND ‘18’)Then
If DeletingThen
Raise_Application_Error(-20502,‘Youmay onlydelete from café user bookingbetween8 and6, MondaytoFriday’);
EndIf;
EndIf;
End;
/
Sho Err
VALID DELETE
Delete
From Cafe_User_Booking
Where Payment_Method=’Cash’;
INVALID DELETE
You may only delete a café user booking ifit is not Wednesdayor Sunday between 3 pmand6pm.
(This trigger was executedon a Wednesday)
Drop Trigger Cafe_User_Book_Date;
Create or ReplaceTrigger Cafe_User_Booking_Date
Before Delete onCafe_User_Booking
Begin
IF(TO_CHAR(Sysdate, ‘DY’)IN(‘WED’, ‘SUN’)) OR (TO_CHAR(Sysdate, ‘HH24’)NOT BETWEEN ‘15’AND ‘18’)Then
If DeletingThen
Raise_Application_Error(-20502,‘Youmay onlydeletefromcaféuser bookingif it is not a wedor sun between 3 and
6’ );
EndIf;
EndIf;
End;
/
Sho Err
AISLING FALLON-A00203593
INTERNET CAFE Page 56
AISLING FALLON-A00203593
INTERNET CAFE Page 57
Question 11-PROCEDUREWITHOUTA CURSOR
Write a PLSQL procedure that will allow theuser to increase the annual turnover of a supplier, provided theannual turnover is not null, the
minimum increase is 500and themaximum is 1500, in cases where these exceptions are not met store in a logtable. Usingprint and
variable statements display theannual turnover for the supplier executed.
Set Serveroutput On Size 1000000
Drop Procedure Raise_Supplier_Annual_Turn;
Drop Table Supplier_Annual_Turnover_Audit;
Create Table Supplier_Annual_Turnover_Audit(Supplier_Numb Number(4) Not Null, Supplier_Details Varchar2(50));
Variable A_Turnover Number
Print A_Turnover
Create or Replace Procedure Raise_Supplier_Annual_Turn(Supp_Num IN Integer, Increase IN Real, Ann_Turnover OUT
Number)Is
V_Current_Annual_Turnover Number(8);
Annual_Turnover_Missing Exception;
Increase_Too_Big Exception;
Increase_Too_Small Exception;
Others Exception;
No_Data_Found Exception;
Begin
Dbms_Output.Put_Line(‘Viewthe data without update’);
Select Annual_TurnoverInto V_Current_Annual_Turnover
From Supplier
Where Supplier_Id=Supp_Num;
Dbms_Output.Put_Line(‘The original annual turnover for the supplier is: ‘ ||V_Current_Annual_Turnover);
If Increase<=500Then
Raise Increase_Too_Small;
Endif;
If Increase>1500 Then
Raise Increase_Too_Big;
Endif;
If V_Current_Annual_Turnover Is NULLThen
Raise Annual_Turnover_Missing;
Else
Update Supplier
Set Annual_Turnover=Annual_Turnover+ Increase
Where Supplier_Id=Supp_Num;
EndIf;
Dbms_Output.Put_Line(‘Viewdata afterchanges’);
Select Annual_TurnoverInto V_Current_Annual_Turnover
From Supplier
Where Supplier_Id=Supp_Num;
Dbms_Output.Put_Line(‘The original annual turnover for the supplier is: ‘ ||V_Current_Annual_Turnover);
Ann_Turnover:= V_Current_Annual_Turnover;
Exception
When No_Data_Found Then
Insert into Supplier_Annual_Turnover_Audit Values(Supp_Num, ‘Nosuch supplier number’);
When Annual_Turnover_Missing Then
Insert into Supplier_Annual_Turnover_Audit Values(Supp_Num, ‘Annual Turnover is NULL’);
When Increase_Too_Big Then
Insert into Supplier_Annual_Turnover_Audit Values(Supp_Num, ‘Excessive annual turnover increase’);
When Increase_Too_Small Then
Insert into Supplier_Annual_Turnover_Audit Values(Supp_Num, ‘Insufficient annual turnoverincrease’);
When Others Then
Insert into Supplier_Annual_Turnover_Audit Values(Supp_Num, ‘Unforseen Error’);
End Raise_Supplier_Annual_Turn;
/
Sho Err
AISLING FALLON-A00203593
INTERNET CAFE Page 58
VALID INCREASE
Execute Raise_Supplier_Annual_Turn(54,600, :A_Turnover);
INSUFFICIENTINCREASE
Execute Raise_Supplier_Annual_Turn(54,200, :A_Turnover);
AISLING FALLON-A00203593
INTERNET CAFE Page 59
EXCESSIVEINCREASE
Execute Raise_Supplier_Annual_Turn(54,1900,:A_Turnover);
UNFORSEEN ERROR
Execute Raise_Supplier_Annual_Turn(94,900, :A_Turnover);
AISLING FALLON-A00203593
INTERNET CAFE Page 60
NULL ANNUAL TURNOVER
Insert into Supplier values (90,'Tech Data Ltd.','Toronto','Canada','www.techdata.com',2009,'PCs',NULL);
Execute Raise_Supplier_Annual_Turn(90,900, :A_Turnover);
AISLING FALLON-A00203593
INTERNET CAFE Page 61
AISLING FALLON-A00203593
INTERNET CAFE Page 62
Question 12-PROCEDUREWITH IN AND OUTPARAMETERS
Create a procedure to give technicians an increasein hourly rate providedthe exceptions are met,otherwise store this information of the
technicians (technicianid, details) in a logtable where theamount is too largeor small, where thetechnicianid does not exist or where you
may have anerror at runtime, otherwise update the hourly rate where technicians earn €20 anhour.Usiing print statements andvariables
display for the updated theincreased hourly rate for the technicianexecutedat runtime.
Set Serveroutput On Size 1000000
Variable T_Hourly_Rate Number
Print T_Hourly_Rate
Drop Procedure Raise_Tech_Hourly_Rate;
Drop Table Tech_Hourly_Rate_Audit;
Create Table Tech_Hourly_Rate_Audit(Techn_NumbNumber(4)Not Null, Tech_Details Varchar2(50));
Create or Replace Procedure Raise_Tech_Hourly_Rate(Tech_Num IN Integer, Increase IN Real, Total_Hourly_Rate OUT Number)
Is
V_Current_Hourly_Rate Number(8);
Hourly_Rate_Missing Exception;
Increase_Too_Big Exception;
Increase_Too_Small Exception;
Others Exception;
No_Data_Found Exception;
Begin
Dbms_Output.Put_Line(‘Viewthe data without update’);
Select Hourly_Rate Into V_Current_Hourly_Rate
From Technician
Where Technician_Id=Tech_Num;
Dbms_Output.Put_Line(‘The original hourlyrate fortechnicians is: ‘ ||V_Current_Hourly_Rate);
If Increase<=5 Then
Raise Increase_Too_Small;
Endif;
If Increase>30Then
Raise Increase_Too_Big;
Endif;
If V_Current_Hourly_Rate Is NULL Then
Raise Hourly_Rate_Missing;
Else
Update Technician
Set Hourly_Rate= Hourly_Rate+ Increase
Where Hourly_Rate=20
And Technician_Id=Tech_Num;
EndIf;
Dbms_Output.Put_Line(‘Viewdata after changes’);
Select Hourly_Rate Into V_Current_Hourly_Rate
From Technician
Where Technician_Id=Tech_Num;
Dbms_Output.Put_Line(‘The adjustedhourly ratefor technicians is: ‘ ||V_Current_Hourly_Rate);
Total_Hourly_Rate := V_Current_Hourly_Rate;
Exception
When No_Data_FoundThen
Insert into Tech_Hourly_Rate_Audit Values(Tech_Num,‘Nosuch techniciannumber’);
When Hourly_Rate_MissingThen
Insert into Tech_Hourly_Rate_Audit Values(Tech_Num,‘Hourly Rate is NULL’);
When Increase_Too_BigThen
Insert into Tech_Hourly_Rate_Audit Values(Tech_Num,‘Excessive hourly rateincrease’);
When Increase_Too_Small Then
Insert into Tech_Hourly_Rate_Audit Values(Tech_Num,‘Insufficient hourlyrate increase’);
When Others Then
Insert into Tech_Hourly_Rate_Audit Values(Tech_Num,‘UnforseenError’);
End Raise_Tech_Hourly_Rate;
/
Sho Err
AISLING FALLON-A00203593
INTERNET CAFE Page 63
INVALID UPDATE(HOURLY_RATENOT€20)
Insert into Technicianvalues (2350,'Aidan','Cassidy','Market Square','Shercock','Cavan','Certificate','Software',15,'23-Oct-08',086-
4536578);
Execute Raise_Tech_Hourly_Rate(2350,25, :T_Hourly_Rate);
VALID INCREASE
Insert into Technicianvalues (2350,'Aidan','Cassidy','Market Square','Shercock','Cavan','Certificate','Software',20,'23-Oct-08',086-
4536578);
Execute Raise_Tech_Hourly_Rate(2350,25, :T_Hourly_Rate);
Print T_Hourly_Rate
AISLING FALLON-A00203593
INTERNET CAFE Page 64
EXCESSIVEHOURLY RATE
Insert into Technicianvalues (2350,'Aidan','Cassidy','Market Square','Shercock','Cavan','Certificate','Software',15,'23-Oct-08',086-
4536578);
Execute Raise_Tech_Hourly_Rate(2350,35, :T_Hourly_Rate);
INSUFFICIENTHOURLY RATE
Insert into Technicianvalues (2950,'Aidan','Cassidy','Market Square','Shercock','Cavan','Certificate','Software',3,'23-Oct-08',086-4536578);
Execute Raise_Tech_Hourly_Rate(2950,3,:T_Hourly_Rate);
NULL HOURLY RATE
Insert into Technicianvalues (2950,'Aidan','Cassidy','Market Square','Shercock','Cavan','Certificate','Software',NULL,'23-Oct-08',086-
4536578);
Execute Raise_Tech_Hourly_Rate(2950,15, :T_Hourly_Rate);
AISLING FALLON-A00203593
INTERNET CAFE Page 65
UNFORSEEN ERROR
Insert into Technicianvalues (2950,'Aidan','Cassidy','Market Square','Shercock','Cavan','Certificate','Software',NULL,'23-Oct-08',086-
4536578);
Execute Raise_Tech_Hourly_Rate(2960,15, :T_Hourly_Rate);
AISLING FALLON-A00203593
INTERNET CAFE Page 66
AISLING FALLON-A00203593
INTERNET CAFE Page 67
Question 13-PROCEDUREWITH CURSOR, LOOP AND IN AND OUTPARAMETERS
Write a PLSQL procedure to allow a user to increase theannual turnover of suppliers each time theapplication is run. There is a minimum
increase of €5 anda maximum of €30. Thenumber of suppliers youwish to updateis done so at execution, alongwith the amount youwish
to increase by. The sum of all annual turnovers is shown before any updatetakes place.The sum of the updated annual turnover alongwith
the number of suppliers is displayedusingprint andvariable statements.
Set Serveroutput On Size 1000000
Variable S_Annual_Turnover Number
Variable T_Suppliers_Updated Number
Print S_Annual_Turnover
Print T_Suppliers_Updated
Drop Table DML_Supplier_Log;
Create Table DML_Supplier_Log(Ora_User Varchar2(20), DML_Date Date, Details Varchar(40));
Drop Procedure Annual_Turn_Supplier;
Create or ReplaceProcedure Annual_Turn_Supplier(Num_Suppliers IN Number, Increase IN Number,Sum_Annual_Turnover OUT
Number, Num_Suppliers_Updated OUT Number) As
Cursor SuppcurIs Select Name, Annual_Turnover
From Supplier
Order By Annual_Turnover
For Update of Annual_Turnover;
V_Name Supplier.Name%Type;
V_Annual_Turnover Supplier.Annual_Turnover%Type;
V_Sum_Annual_Turnover Number(9,2);
V_Total_Suppliers_Updated Number(3) :=0;
Increase_Too_Big Exception;
Increase_Too_Small Exception;
Begin
If Increase<=5 Then
Raise Increase_Too_Small;
Endif;
If Increase>30Then
Raise Increase_Too_Big;
EndIf;
Open Suppcur;
Dbms_Output.Put_Line(‘Here are the annual turnoverfigures for suppliers’);
Select Sum(Annual_Turnover) Into V_Sum_Annual_Turnover
From Supplier;
Dbms_Output.Put_Line(‘The sum of the annual turnover is: ‘||V_Sum_Annual_Turnover);
For V_Count in 1..Num_Suppliers
Loop
Fetch Suppcur Into V_Name, V_Annual_Turnover ;
Exit WhenSuppcur% NotFound;
Dbms_Output.Put_Line(‘The names are: ‘||V_Name);
Dbms_Output.Put_Line(‘The annual turnovers are: ‘||V_Annual_Turnover);
Dbms_Output.Put_Line(‘****************************************’);
Update Supplier
Set Annual_Turnover =Annual_Turnover + Increase
Where Current Of Suppcur;
V_Sum_Annual_Turnover :=V_Sum_Annual_Turnover+ Increase;
V_Total_Suppliers_Updated := V_Total_Suppliers_Updated+1;
End Loop;
Sum_Annual_Turnover :=V_Sum_Annual_Turnover;
Num_Suppliers_Updated :=V_Total_Suppliers_Updated;
Close Suppcur;
Exception
When Increase_Too_Big Then
Insert into DML_Supplier_LogValues(User, Sysdate,‘Excessive annual turnover increase’);
When Increase_Too_Small Then
Insert into DML_Supplier_LogValues(User, Sysdate,‘Insufficient annual turnover increase’);
End;
/
Sho Err
AISLING FALLON-A00203593
INTERNET CAFE Page 68
VALID UPDATE
Execute Annual_Turn_Supplier(3,20, :S_Annual_Turnover, :T_Suppliers_Updated);
AISLING FALLON-A00203593
INTERNET CAFE Page 69
EXCESSIVEINCREASE
Execute Annual_Turn_Supplier(3,35, :S_Annual_Turnover, :T_Suppliers_Updated);
INSUFFICIENTINCREASE
Execute Annual_Turn_Supplier(3,2, :S_Annual_Turnover, :T_Suppliers_Updated);
AISLING FALLON-A00203593
INTERNET CAFE Page 70
AISLING FALLON-A00203593
INTERNET CAFE Page 71
Question 14-PROCEDURE WITH TWO CURSORS, LOOP AND IN AND OUT PARAMTERS
Create a procedure to find the highest and lowest rates per hour for internet cafes showing the rate and the name for each of them. Using variables and print
statements display the sum of the rates per hour and the number of internet cafes selected. Let the number of internet cafés you wish to see be selected at
execution time, and logging if too many or few rows executed in a log table.
Set Serveroutput On Size 1000000
Variable S_Rate_Per_Hour Number
Variable T_Internet_Cafe_UpdatedNumber
Print S_Rate_Per_Hour
Print T_Internet_Cafe_Updated
Drop Table DML_Int_Cafe_Log;
Create Table DML_Int_Cafe_Log(Ora_User Varchar2(20), DML_Date Date, Details Varchar(50));
Drop Procedure Int_Cafe_RatePHr;
Create or Replace Procedure Int_Cafe_RatePHr(Num_Internet_C IN Number, Total_Rate_Per_Hour OUT Number, Sum_Internet_Cafe_Updated
OUT Number) As
Cursor Rate_P_Hour_Min Is Select Name, Rate_Per_Hour
From Internet_Cafe
Order By Rate_Per_Hour ASC;
Cursor Rate_P_Hour_Max Is Select Name, Rate_Per_Hour
From Internet_Cafe
Order By Rate_Per_Hour DESC;
V_Name Internet_Cafe.Name%Type;
V_Rate_Per_Hour Internet_Cafe.Rate_Per_Hour%Type;
V_Sum_Rate_Per_Hour Number(9,2):=0;
V_Total_Internet_Cafe_Updated Number(3) :=0;
Too_Few_Rows Exception;
Too_many_Rows Exception;
Begin
If Num_Internet_C <5 Then
Raise Too_Few_Rows;
End If;
If Num_Internet_C >7 Then
Raise Too_Many_Rows;
End If;
Open Rate_P_Hour_Min;
For V_Count in 1.. Num_Internet_C
Loop
Fetch Rate_P_Hour_Min Into V_Name, V_Rate_Per_Hour;
Exit When Rate_P_Hour_Min %NotFound;
Dbms_Output.Put_Line(‘The names of the internet cafe with min rate per hour is: ‘|| V_Name);
Dbms_Output.Put_Line(‘The rate per hour is: ‘|| V_Rate_Per_Hour);
Dbms_Output.Put_Line(‘****************************************’);
V_Sum_Rate_Per_Hour :=V_Sum_Rate_Per_Hour + V_Rate_Per_Hour;
V_Total_Internet_Cafe_Updated :=V_Total_Internet_Cafe_Updated+1;
End Loop;
Total_Rate_Per_Hour := V_Sum_Rate_Per_Hour;
Sum_Internet_Cafe_Updated := V_Total_Internet_Cafe_Updated;
Close Rate_P_Hour_Min;
Open Rate_P_Hour_Max;
For V_Count in 1.. Num_Internet_C
Loop
Fetch Rate_P_Hour_Max Into V_Name, V_Rate_Per_Hour;
Exit When Rate_P_Hour_Max %NotFound;
Dbms_Output.Put_Line(‘The name of the internet cafe with the max rate per hour is: ‘|| V_Name);
Dbms_Output.Put_Line(‘The rate per hour is: ‘|| V_Rate_Per_Hour);
Dbms_Output.Put_Line(‘****************************************’);
V_Sum_Rate_Per_Hour :=V_Sum_Rate_Per_Hour + V_Rate_Per_Hour;
V_Total_Internet_Cafe_Updated :=V_Total_Internet_Cafe_Updated+1;
End Loop;
Total_Rate_Per_Hour := V_Sum_Rate_Per_Hour;
Sum_Internet_Cafe_Updated := V_Total_Internet_Cafe_Updated;
Close Rate_P_Hour_Max;
Exception
When Too_Few_Rows Then
Insert into DML_Int_Cafe_Log Values(User, Sysdate, ‘Not enough rows updated’);
When Too_Many_Rows Then
Insert into DML_Int_Cafe_Log Values(User, Sysdate, ‘Too many rows updated’);
End;
/
Sho Err
AISLING FALLON-A00203593
INTERNET CAFE Page 72
AISLING FALLON-A00203593
INTERNET CAFE Page 73
VALID EXECUTION
Execute Int_Cafe_RatePHr (6, :S_Rate_Per_Hour, :T_Internet_Cafe_Updated);
AISLING FALLON-A00203593
INTERNET CAFE Page 74
TOO FEW ROWS
Execute Int_Cafe_RatePHr (3, :S_Rate_Per_Hour, :T_Internet_Cafe_Updated);
TOO MANY ROWS
Execute Int_Cafe_RatePHr (8, :S_Rate_Per_Hour, :T_Internet_Cafe_Updated);
AISLING FALLON-A00203593
INTERNET CAFE Page 75
Question 15-PROCEDUREWITH CURSOR TO DELETE
Create a procedure to count the number of suppliers, the max annual turnover andthe sum of the annual turnover for all these suppliers.
Starting with the lowest annual turnover delete suppliers until the sum of eachof their annual turnovers exceeds 1200000. For eachsupplier
deleted show their name and annual turnover andthe number of rows after deletingat the end. Usinga variable andprint statement display
the number of suppliers deleted.
Set Serveroutput On Size 1000000
Variable No_Deleted Number
Print No_Deleted
Drop Table DML_Supplier_Log;
Create Table DML_Supplier_Log(Ora_User Varchar2(20), DML_Date Date, Details Varchar(30));
Drop Procedure Annual_Turn_Supplier;
Create or ReplaceProcedure Annual_Turn_Supplier(Num_Deleted Out Number) As
Cursor SuppcurIs Select Name, Annual_Turnover
From Supplier
Order By Annual_Turnover
For Update of Annual_Turnover;
V_Name Supplier.Name%Type;
V_Annual_Turnover Supplier.Annual_Turnover%Type;
V_Sum_Annual_Turnover Number(9,2);
V_Max_Annual_Turnover Number(9) :=0;
V_Number_Deleted Number(3) :=0;
V_Tot_Annual_Turnover Number(3);
Begin
Open Suppcur;
Select Sum(Annual_Turnover) Into V_Sum_Annual_Turnover
From Supplier;
Dbms_Output.Put_Line(‘The sum of the annual turnover is: ‘||V_Sum_Annual_Turnover);
Select Count(*)into V_Tot_Annual_Turnover
From Supplier;
Dbms_Output.Put_Line(‘The numberof rows before delete: ‘||V_Tot_Annual_Turnover);
V_Max_Annual_Turnover := 1200000;
Dbms_Output.Put_Line(‘The maxannual turnover is: ‘||V_Max_Annual_Turnover);
WhileV_Sum_Annual_Turnover> V_Max_Annual_Turnover
Loop
Fetch Suppcur Into V_Name, V_Annual_Turnover;
Exit WhenSuppcur% NotFound;
Dbms_Output.Put_Line(‘The names are: ‘||V_Name);
Dbms_Output.Put_Line(‘The annual turnovers are: ‘||V_Annual_Turnover);
Dbms_Output.Put_Line(‘****************************************’);
Delete
From Supplier
Where Current ofSuppcur;
V_Sum_Annual_Turnover := V_Sum_Annual_Turnover- V_Annual_Turnover;
V_Number_Deleted :=V_Number_Deleted+1;
End Loop;
Dbms_Output.Put_Line(‘The sum of the annual turnover after delete is: ‘||V_Sum_Annual_Turnover);
Select Count(*)into V_Tot_Annual_Turnover
From Supplier;
Dbms_Output.Put_Line(‘The numberof rows afterdelete: ‘||V_Tot_Annual_Turnover);
Dbms_Output.Put_Line(‘The numberof rows deleted: ‘||V_Number_Deleted);
Close Suppcur;
Num_Deleted:= V_Number_Deleted;
End;
/
Sho Err
AISLING FALLON-A00203593
INTERNET CAFE Page 76
Execute Annual_Turn_Supplier(:No_Deleted);
Print No_Deleted
Rollback;
AISLING FALLON-A00203593
INTERNET CAFE Page 77
AISLING FALLON-A00203593
INTERNET CAFE Page 78
Question 16-PACKAGEWITH 3 DIFFERENTPROCEDURES
Create a packagethat contains procedures whichwill ALLOW:
(A) A Gadget_Id and Name be insertedintothe Gadget table whenthe procedure within thepackage is executed.
(B) The name of thegadget be updatedwhenthe procedurewithin thepackage is executed.
(C) A record to be deleted whenthe Gadget_Idis executed withinthe package.
Drop Package Gadget_Pack;
Create or ReplacePackage Gadget_PackIs
Procedure Add_Gadget
(A_Gadget_Idin Gadget.Gadget_Id%Type,A_NameIn Gadget.Name%Type);
Procedure Upd_Gadget
(A_Gadget_Idin Gadget.Gadget_Id%Type,A_NameIn Gadget.Name%Type);
Procedure Del_Gadget
(A_Gadget_Idin Gadget.Gadget_Id%Type);
End Gadget_Pack;
/
Sho Err
Create or ReplacePackage BODY Gadget_PackIs
Procedure Add_Gadget
(A_Gadget_Idin Gadget.Gadget_Id%Type,A_NameIn Gadget.Name%Type) Is
Begin
Insert into Gadget(Gadget_Id, Name) Values(A_Gadget_Id, A_Name);
EndAdd_Gadget;
Procedure Upd_Gadget
(A_Gadget_Idin Gadget.Gadget_Id%Type,A_NameIn Gadget.Name%Type) Is
Begin
Update Gadget
Set Name=A_Name
Where Gadget_Id=A_Gadget_Id;
If Sql%NotFoundThen
Raise_Application_Error(-20203,’Gadget Iddoes not exist, cannot not modify now’);
EndIf;
EndUpd_Gadget;
Procedure Del_Gadget
(A_Gadget_Idin Gadget.Gadget_Id%Type)Is
Begin
Delete
From Gadget
Where Gadget_Id=A_Gadget_Id;
If Sql%NotFoundThen
Raise_Application_Error(-20203,’Gadget iddoes not exist so cannot removeit’);
EndIf;
EndDel_Gadget;
End Gadget_Pack;
/
Sho Err
AISLING FALLON-A00203593
INTERNET CAFE Page 79
TEST INSERT
Select Gadget_Id, Name
From Gadget;
Execute Gadget_Pack. Add_Gadget(‘1079’,’Sharp’);
AISLING FALLON-A00203593
INTERNET CAFE Page 80
TEST UPDATE
Execute Gadget_Pack. Upd_Gadget (‘1010’,’Dell’);
TEST DELETE
Execute Gadget_Pack. Del_Gadget (‘1010’);
AISLING FALLON-A00203593
INTERNET CAFE Page 81
AISLING FALLON-A00203593
INTERNET CAFE Page 82
Question 17-PACKAGE WITH THREE DIFFERENT PROCEDURES WITH DIFFERENT TABLES
Create a packagewhichwill makethe followingchanges to the necessary tables:
(A)Updates the annual turnover ofa supplier, at runtime selectingthe supplier andthe amount ofthe increase, displayingthe amount
before and after update.
(B)Upodates the hourly rate oftechnicians who are on a rate of€20,at runtime selectingthe technician and the amount ofthe increase,
displayingthe amount before and after update.
(C)Update the annual turnover ofa supplier by specifyinghow many suppliers at runtime you want to update, also showingthe sum of
annual turnovers before any update andthe nameand annual turnover ofthe supplier updated.
Set serveroutput onsize 1000000
Drop Package Modify_Pack;
Create Or Replace PACKAGE Modify_Pack Is
Procedure Raise_Supplier_Annual_Turn(Supp_Num IN Integer, Increase IN Real, Ann_Turnover OUTNumber);
Procedure Annual_Turn_Supplier(Num_Suppliers IN Number, Increase IN Number, Sum_Annual_Turnover OUT Number,
Num_Suppliers_UpdatedOUT Number);
Procedure Raise_Tech_Hourly_Rate(Tech_Num IN Integer, Increase IN Real, Total_Hourly_Rate OUT Number);
EndModify_Pack;
/
Sho Err
Create Or Replace Package Body Modify_Pack Is
Procedure Raise_Supplier_Annual_Turn(Supp_Num IN Integer, Increase IN Real, Ann_Turnover OUTNumber)Is
V_Current_Annual_Turnover Number(8);
Begin
Dbms_Output.Put_Line(‘Viewthe data without update’);
Select Annual_TurnoverInto V_Current_Annual_Turnover
From Supplier
Where Supplier_Id=Supp_Num;
Dbms_Output.Put_Line(‘The original annual turnover for the supplier is: ‘ ||V_Current_Annual_Turnover);
Update Supplier
Set Annual_Turnover=Annual_Turnover+ Increase
Where Supplier_Id=Supp_Num;
Dbms_Output.Put_Line(‘Viewdata afterchanges’);
Select Annual_TurnoverInto V_Current_Annual_Turnover
From Supplier
Where Supplier_Id=Supp_Num;
Dbms_Output.Put_Line(‘The original annual turnover for the supplier is: ‘ ||V_Current_Annual_Turnover);
Ann_Turnover:= V_Current_Annual_Turnover;
End Raise_Supplier_Annual_Turn;
AISLING FALLON-A00203593
INTERNET CAFE Page 83
Procedure Raise_Tech_Hourly_Rate(Tech_Num IN Integer, Increase IN Real, Total_Hourly_Rate OUT Number)Is
V_Current_Hourly_Rate Number(8);
Begin
Dbms_Output.Put_Line(‘Viewthe data without update’);
Select Hourly_Rate Into V_Current_Hourly_Rate
From Technician
Where Technician_Id=Tech_Num;
Dbms_Output.Put_Line(‘The original hourlyrate fortechnicians is: ‘ ||V_Current_Hourly_Rate);
Update Technician
Set Hourly_Rate= Hourly_Rate+ Increase
Where Hourly_Rate=20
And Technician_Id=Tech_Num;
Dbms_Output.Put_Line(‘Viewdata afterchanges’);
Select Hourly_Rate Into V_Current_Hourly_Rate
From Technician
Where Technician_Id=Tech_Num;
Dbms_Output.Put_Line(‘The adjustedhourly ratefor technicians is: ‘ ||V_Current_Hourly_Rate);
End Raise_Tech_Hourly_Rate;
Procedure Annual_Turn_Supplier(Num_Suppliers IN Number, Increase IN Number, Sum_Annual_Turnover OUT Number,
Num_Suppliers_Updated OUT Number)As
Cursor SuppcurIs Select Name, Annual_Turnover
From Supplier
Order By Annual_Turnover
For Update of Annual_Turnover;
V_Name Supplier.Name%Type;
V_Annual_Turnover Supplier.Annual_Turnover%Type;
V_Sum_Annual_Turnover Number(9,2);
V_Total_Suppliers_Updated Number(3) :=0;
Increase_Too_Big Exception;
Increase_Too_Small Exception;
Begin
Open Suppcur;
Dbms_Output.Put_Line(‘Here are the annual turnoverfigures for suppliers’);
Select Sum(Annual_Turnover) Into V_Sum_Annual_Turnover
From Supplier;
Dbms_Output.Put_Line(‘The sum of the annual turnover is: ‘||V_Sum_Annual_Turnover);
For V_Count in 1..Num_Suppliers
Loop
Fetch Suppcur Into V_Name, V_Annual_Turnover ;
Exit WhenSuppcur% NotFound;
Dbms_Output.Put_Line(‘The names are: ‘||V_Name);
Dbms_Output.Put_Line(‘The annual turnovers are: ‘||V_Annual_Turnover);
Dbms_Output.Put_Line(‘****************************************’);
Update Supplier
Set Annual_Turnover =Annual_Turnover + Increase
Where Current Of Suppcur;
V_Sum_Annual_Turnover :=V_Sum_Annual_Turnover+ Increase;
V_Total_Suppliers_Updated := V_Total_Suppliers_Updated+1;
End Loop;
Sum_Annual_Turnover :=V_Sum_Annual_Turnover;
Num_Suppliers_Updated :=V_Total_Suppliers_Updated;
Close Suppcur;
End Annual_Turn_Supplier;
EndModify_Pack;
/
Sho Err
AISLING FALLON-A00203593
INTERNET CAFE Page 84
Variable A_Turnover Number
Print A_Turnover
Variable T_Hourly_Rate Number
Print T_Hourly_Rate
Variable S_Annual_Turnover Number
Variable T_Suppliers_Updated Number
Print S_Annual_Turnover
Print T_Suppliers_Updated
Procedure Raise_Supplier_Annual_Turn
Execute Modify_Pack.Raise_Supplier_Annual_Turn(54,600, :A_Turnover);
Insertinto Supplier values (54,'Tech Data Ltd.','Toronto','Canada','www.techdata.com',2009,'PCs',59000);
Procedure Annual_Turn_Supplier
Insert into Technicianvalues (2350,'Aidan','Cassidy','Market Square','Shercock','Cavan','Certificate','Software',20,'23-Oct-08',086-
4536578);
Execute Modify_Pack.Raise_Tech_Hourly_Rate(2350,25,:T_Hourly_Rate);
Procedure Raise_Tech_Hourly_Rate
Execute Modify_Pack.Annual_Turn_Supplier(3,2, :S_Annual_Turnover,:T_Suppliers_Updated);
AISLING FALLON-A00203593
INTERNET CAFE Page 85
AISLING FALLON-A00203593
INTERNET CAFE Page 86
Question 18-CALLING PROCEDURE FROM A TRIGGER
Create a procedure which will be called from a Trigger. The procedure should:
(A) Increase annual turnover for suppliers by 10% where the type ofGadget is a Tablet andthe nameis Apple, otherwise increase the
annual turnover by 20% ifnot namedApple.
(B) Decrease annual turnover for suppliers by 10% where the type is Laptopand the name is Dell, otherwise decrease the annual
turnover by 20% ifnot namedDell.
Set Serveroutput On Size 1000000
Drop Procedure Raise_Supplier_Annual_Turn;
Create or Replace Procedure Raise_Supp_A_Turn(Supp_Num IN Number, Increase IN Number) Is
V_Current_Annual_Turnover Number(8);
Begin
Dbms_Output.Put_Line(‘Viewthe data without update’);
Select Annual_TurnoverInto V_Current_Annual_Turnover
From Supplier
Where Supplier_Id=Supp_Num;
Dbms_Output.Put_Line(‘The original annual turnover for the supplier is: ‘ ||V_Current_Annual_Turnover);
Update Supplier
Set Annual_Turnover=Annual_Turnover+ Increase
Where Supplier_Id=Supp_Num;
Dbms_Output.Put_Line(‘Viewdata after changes’);
Select Annual_TurnoverInto V_Current_Annual_Turnover
From Supplier
Where Supplier_Id=Supp_Num;
Dbms_Output.Put_Line(‘The updatedannual turnoverforthe supplieris: ‘ ||V_Current_Annual_Turnover);
End;
/
Sho Err
Create or Replace Procedure Decrease_Supp_A_Turn(Supp_Num IN Number, Increase IN Number) Is
V_Current_Annual_Turnover Number(8);
Begin
Dbms_Output.Put_Line(‘Viewthe data without update’);
Select Annual_TurnoverInto V_Current_Annual_Turnover
From Supplier
Where Supplier_Id=Supp_Num;
Dbms_Output.Put_Line(‘The original annual turnover for the supplier is: ‘ ||V_Current_Annual_Turnover);
Update Supplier
Set Annual_Turnover=Annual_Turnover - Increase
Where Supplier_Id=Supp_Num;
Dbms_Output.Put_Line(‘Viewdata afterchanges’);
Select Annual_TurnoverInto V_Current_Annual_Turnover
From Supplier
Where Supplier_Id=Supp_Num;
Dbms_Output.Put_Line(‘The updatedannual turnoverforthe supplieris: ‘ || V_Current_Annual_Turnover);
End;
/
Sho Err
AISLING FALLON-A00203593
INTERNET CAFE Page 87
Drop Trigger Modify_A_Turnover_Supp;
Create or ReplaceTrigger Modify_A_Turnover_Supp
After Update onGadget
For Each Row
Declare
V_Annual_Turnover Supplier.Annual_Turnover%Type;
Begin
Select Annual_TurnoverInto V_Annual_Turnover
From Supplier
Where Supplier.Supplier_Id= :New.Supplier_Id;
If :New.Type=’Tablet’Then
If :New.Name=’Apple’ Then
Raise_Supp_A_Turn(:New.Supplier_Id, V_Annual_Turnover*0.10);
Else
Raise_Supp_A_Turn(:New.Supplier_Id, V_Annual_Turnover*0.20);
End If;
End If;
If :New.Type=’Laptop’Then
If :New.Name=’Dell’ Then
Decrease_Supp_A_Turn(:New.Supplier_Id, V_Annual_Turnover*0.10);
Else
Decrease_Supp_A_Turn(:New.Supplier_Id, V_Annual_Turnover*0.20);
End If;
End If;
End;
/
Sho Err
AISLING FALLON-A00203593
INTERNET CAFE Page 88
(A) Increase annual turnover for suppliers by 10% where the type ofGadget is Tablet andthe nameis Apple, otherwise increase the
annual turnover by 20% ifnot namedApple.
Insert into Supplier values (98,'Tech Data Ltd.','Toronto','Ireland','www.techdata.com',2009,'Tablets',5000);
Insert into Gadget values (1326,'Apple’,’Tablet',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,196,98);
Select Annual_Turnover
From Supplier
Where Supplier_Id=98;
Update Gadget
Set Cost_Price=60
Where Supplier_Id=98;
Select Annual_Turnover
From Supplier
Where Supplier_Id=98;
Insert into Supplier values (98,'Tech Data Ltd.','Toronto','Ireland','www.techdata.com',2009,'Tablets',5000);
Insert into Gadget values (1326,'Dell’,’Tablet',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,196,98);
Select Annual_Turnover
From Supplier
Where Supplier_Id=98;
Update Gadget
Set Cost_Price=60
Where Supplier_Id=98;
Select Annual_Turnover
From Supplier
Where Supplier_Id=98;
AISLING FALLON-A00203593
INTERNET CAFE Page 89
(B) Decrease annual turnover for suppliers by 10% where the type is Laptopand the name is Dell, otherwise decrease the annual
turnover by 20% ifnot namedDell.
Insert into Supplier values (98,'Tech Data Ltd.','Toronto','Ireland','www.techdata.com',2009,'Tablets',5000);
Insert into Gadget values (1326,'Dell’,’Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,196,98);
Select Annual_Turnover
From Supplier
Where Supplier_Id=98;
Update Gadget
Set Cost_Price=60
Where Supplier_Id=98;
Select Annual_Turnover
From Supplier
Where Supplier_Id=98;
Insert into Supplier values (98,'Tech Data Ltd.','Toronto','Ireland','www.techdata.com',2009,'Tablets',5000);
Insert into Gadget values (1326,'Asus’,’Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,196,98);
Select Annual_Turnover
From Supplier
Where Supplier_Id=98;
Update Gadget
Set Cost_Price=60
Where Supplier_Id=98;
Select Annual_Turnover
From Supplier
Where Supplier_Id=98;
AISLING FALLON-A00203593
INTERNET CAFE Page 90
AISLING FALLON-A00203593
INTERNET CAFE Page 91
AISLING FALLON-A00203593
INTERNET CAFE Page 92
AISLING FALLON-A00203593
INTERNET CAFE Page 93
Question 19-TRIGGER USING A CURSOR
Create a trigger that uses a cursor to give thetwo lowest paidtechnicians an increaseof €10whenthe details of gadget repair are updated.
Set Serverout ON Size 1000000
Drop TriggerGadget_Rep_Tech_Gad;
Create or Replace Trigger Gadget_Rep_Tech_Gad
After Update of Details on Gadget_Repair
For Each Row
Declare
Cursor GadCur Is Select FName, Hourly_Rate
From Technician
Where Technician_Id=:New.Technician_Id
Order By Hourly_RateASC
For Update Of Hourly_Rate;
V_Hourly_Rate Technician.Hourly_Rate%Type;
V_Fname Technician.FName%Type;
V_Total_Hourly_Rate_Before Number(9,2);
V_Total_Hourly_Rate_After Number(9,2);
Begin
Open GadCur;
DBMS_Output.Put_Line(‘Total hourlyrate before increase is: ‘);
Select Sum(Hourly_Rate) Into V_Total_Hourly_Rate_Before
From TechnicianT
Where Technician_Id=:New.Technician_Id;
DBMS_Output.Put_Line(‘Sum of hourly ratebefore‘ || V_Total_Hourly_Rate_Before);
DBMS_Output.Put_Line(‘***********************************************************’);
For V_Count In 1..2
Loop
Fetch GadCur Into V_Fname,V_Hourly_Rate;
Exit WhenGadCur%NotFound;
DBMS_Output.Put_Line(‘Technician Name is: ‘ || V_FName);
DBMS_Output.Put_Line(‘VTechnician Hourly rate is: ‘ || V_Hourly_Rate);
Update Technician
Set Hourly_Rate=Hourly_Rate+10
Where Current of Gadcur;
End Loop;
Close GadCur;
Select Sum (Hourly_Rate)Into V_Total_Hourly_Rate_After
From TechnicianT
Where Technician_Id=:New.Technician_Id;
DBMS_Output.Put_Line(‘Sum of hourly ratebefore‘ || V_Total_Hourly_Rate_After);
DBMS_Output.Put_Line(‘***********************************************************’);
End;
/
Sho err
AISLING FALLON-A00203593
INTERNET CAFE Page 94
AISLING FALLON-A00203593
INTERNET CAFE Page 95
Select Sum(Hourly_Rate)
From Technician;
Update Gadget_Repair
Set Details=’Fix Monitor’
Where Technician_Id=2000;
Select Sum(Hourly_Rate)
From Technician;
AISLING FALLON-A00203593
INTERNET CAFE Page 96

More Related Content

Similar to PL/SQL INTERNET CAFE BEFORE TRIGGERS

Piyali Kamra - Building a Conversational AI Ecosystem on AWS Sage maker
Piyali Kamra - Building a Conversational AI Ecosystem on AWS Sage makerPiyali Kamra - Building a Conversational AI Ecosystem on AWS Sage maker
Piyali Kamra - Building a Conversational AI Ecosystem on AWS Sage makerAWS Chicago
 
Wiki manual floreantpos_1.4_56
Wiki manual floreantpos_1.4_56Wiki manual floreantpos_1.4_56
Wiki manual floreantpos_1.4_56Luis Hidalgo
 
S-Controls for Dummies
S-Controls for DummiesS-Controls for Dummies
S-Controls for Dummiesdreamforce2006
 
S-Controls for Dummies
S-Controls for DummiesS-Controls for Dummies
S-Controls for Dummiesdreamforce2006
 
Option #1 Mastery Finance CalculationsProblem 1Hologram Corpo.docx
Option #1 Mastery Finance CalculationsProblem 1Hologram Corpo.docxOption #1 Mastery Finance CalculationsProblem 1Hologram Corpo.docx
Option #1 Mastery Finance CalculationsProblem 1Hologram Corpo.docxcherishwinsland
 
Receiving Bills Electronically
Receiving Bills ElectronicallyReceiving Bills Electronically
Receiving Bills ElectronicallyEnergyCAP, Inc.
 
Synchronize using tally.net
Synchronize using tally.netSynchronize using tally.net
Synchronize using tally.netlovepashah
 
Akamai IR Summit 2013
Akamai IR Summit 2013Akamai IR Summit 2013
Akamai IR Summit 2013Liz Bradley
 
Universal OpenCart migration checklist.pdf
Universal OpenCart migration checklist.pdfUniversal OpenCart migration checklist.pdf
Universal OpenCart migration checklist.pdfCart2Cart2
 
Driving OEE with AWS IoT SiteWise - SVC204 - Atlanta AWS Summit
Driving OEE with AWS IoT SiteWise - SVC204 - Atlanta AWS SummitDriving OEE with AWS IoT SiteWise - SVC204 - Atlanta AWS Summit
Driving OEE with AWS IoT SiteWise - SVC204 - Atlanta AWS SummitAmazon Web Services
 
Create invoice by Oracle iSupplier portal
Create invoice by Oracle iSupplier portalCreate invoice by Oracle iSupplier portal
Create invoice by Oracle iSupplier portalHamdy Elbana
 
Ip Phone Apps Training
Ip Phone Apps TrainingIp Phone Apps Training
Ip Phone Apps Trainingbhillis1
 
7106506104 tl wa701-nd(eu)_2.0_qig
7106506104 tl wa701-nd(eu)_2.0_qig7106506104 tl wa701-nd(eu)_2.0_qig
7106506104 tl wa701-nd(eu)_2.0_qigCesar Estela Zarate
 
Verizon fios actiontec router setup
Verizon fios actiontec router setupVerizon fios actiontec router setup
Verizon fios actiontec router setupg4elan
 
Roberts Jerry
Roberts JerryRoberts Jerry
Roberts JerryCarl Ford
 

Similar to PL/SQL INTERNET CAFE BEFORE TRIGGERS (20)

Piyali Kamra - Building a Conversational AI Ecosystem on AWS Sage maker
Piyali Kamra - Building a Conversational AI Ecosystem on AWS Sage makerPiyali Kamra - Building a Conversational AI Ecosystem on AWS Sage maker
Piyali Kamra - Building a Conversational AI Ecosystem on AWS Sage maker
 
Wiki manual floreantpos_1.4_56
Wiki manual floreantpos_1.4_56Wiki manual floreantpos_1.4_56
Wiki manual floreantpos_1.4_56
 
Speed of Lightning
Speed of LightningSpeed of Lightning
Speed of Lightning
 
S-Controls for Dummies
S-Controls for DummiesS-Controls for Dummies
S-Controls for Dummies
 
S-Controls for Dummies
S-Controls for DummiesS-Controls for Dummies
S-Controls for Dummies
 
Option #1 Mastery Finance CalculationsProblem 1Hologram Corpo.docx
Option #1 Mastery Finance CalculationsProblem 1Hologram Corpo.docxOption #1 Mastery Finance CalculationsProblem 1Hologram Corpo.docx
Option #1 Mastery Finance CalculationsProblem 1Hologram Corpo.docx
 
Receiving Bills Electronically
Receiving Bills ElectronicallyReceiving Bills Electronically
Receiving Bills Electronically
 
Synchronize using tally.net
Synchronize using tally.netSynchronize using tally.net
Synchronize using tally.net
 
Akamai IR Summit 2013
Akamai IR Summit 2013Akamai IR Summit 2013
Akamai IR Summit 2013
 
Universal OpenCart migration checklist.pdf
Universal OpenCart migration checklist.pdfUniversal OpenCart migration checklist.pdf
Universal OpenCart migration checklist.pdf
 
CFMS
CFMSCFMS
CFMS
 
Driving OEE with AWS IoT SiteWise - SVC204 - Atlanta AWS Summit
Driving OEE with AWS IoT SiteWise - SVC204 - Atlanta AWS SummitDriving OEE with AWS IoT SiteWise - SVC204 - Atlanta AWS Summit
Driving OEE with AWS IoT SiteWise - SVC204 - Atlanta AWS Summit
 
Create invoice by Oracle iSupplier portal
Create invoice by Oracle iSupplier portalCreate invoice by Oracle iSupplier portal
Create invoice by Oracle iSupplier portal
 
Ip Phone Apps Training
Ip Phone Apps TrainingIp Phone Apps Training
Ip Phone Apps Training
 
7106506104 tl wa701-nd(eu)_2.0_qig
7106506104 tl wa701-nd(eu)_2.0_qig7106506104 tl wa701-nd(eu)_2.0_qig
7106506104 tl wa701-nd(eu)_2.0_qig
 
Verizon fios actiontec router setup
Verizon fios actiontec router setupVerizon fios actiontec router setup
Verizon fios actiontec router setup
 
Streaming API with Java
Streaming API with JavaStreaming API with Java
Streaming API with Java
 
Monetizing with PayPal on Mobile
Monetizing with PayPal on MobileMonetizing with PayPal on Mobile
Monetizing with PayPal on Mobile
 
Recharge portal
Recharge portalRecharge portal
Recharge portal
 
Roberts Jerry
Roberts JerryRoberts Jerry
Roberts Jerry
 

PL/SQL INTERNET CAFE BEFORE TRIGGERS

  • 1. PL/SQL INTERNET CAFE AISLING FALLON HIGHER DIPLOMA IN COMPUTING 2014
  • 2. AISLING FALLON-A00203593 INTERNET CAFE Page 1 Question1 –BEFORE INSERT TRIGGER Create a before insert trigger on the Gadget table, incorporatingIF statements to test the three followingrules to decide ifan insert will proceed: a) When a gadgets cost price is less than 25, the café year opened must be LESS greater than 2006 b) A gadget oftype laptop must be from a supplier in the United States c) When a gadgets operatingsystem is LINUX the suppliers specialismmust be Servers Drop TriggerGadget_Supplier_Internet_Cafe; Create or Replace Trigger Gadget_Supplier_Internet_Cafe Before Inserton Gadget For Each Row Declare V_Country Supplier.Country%Type; V_Year_Opened Internet_Cafe.Year_Opened%Type; V_Specialism Supplier.Specialism%Type; Begin Select Year_OpenedInto V_Year_Opened From Internet_Cafe Where Internet_Cafe_Id=:New.Internet_Cafe_Id; Select Country, SpecialismIntoV_Country,V_Specialism From Supplier Where Supplier_Id=:New.Supplier_Id; /*(A) When a gadgets cost price is less than 25, the café year openedmust be LESS greater than 2006*/ If :New.Cost_Price< 25 And V_Year_Opened > 2006Then Raise_Application_Error(-20101, ‘A)When a gadgets cost price is less than 25, the year openedmust be greater than 2006’); End If; /*( B) A gadget oftype laptop must befrom a supplier in the United States*/ If :New.Type= ‘Laptop’ And V_Country<>’UnitedStates’ Then Raise_Application_Error(-20101, ‘ B) A gadget oftype laptop must be from a supplier in the United States’); End If; /*(C) When a gadgets operatingsystem is LINUX the suppliers specialismmust be Servers*/ If :New.Operating_System=’Linux’AndV_Specialism<> ‘Servers’ Then Raise_Application_Error(-20101, ‘C) When a gadgets operatingsystem is LINUX the suppliers specialismmust be Servers’); End If; End; / Sho err Drop TriggerGadget_Supplier_Internet_Cafe;
  • 3. AISLING FALLON-A00203593 INTERNET CAFE Page 2 /*(A) When a gadgets cost price is less than 25, the café year openedmust be LESS than 2006*/ INVALID INSERT Insert into Internet_Cafe values (177,'Cruise TheNet Cafe','MainStreet','Shercock','Cavan','Cruise The Net Cafe','cruisethenet@hotmail.com',2008,5,'Colddrinks'); Insert into Suppliervalues (19,'Taecht.','Toronto','UnitedStates','www.techdere.com',2009,'PCs',55000); Insert into Gadget values (8115,'Acer','PC',17.3,3.5,2,'Vista','Yes','No','13-Nov-10',20,36,550,177,19); VALID INSERT Insert into Internet_Cafe values (137,'Cruise TheNet Cafe','MainStreet','Shercock','Cavan','Cruise The Net Cafe','cruisethenet@hotmail.com',2005,5,'Colddrinks'); Insert into Supplier values (21,'Taecht.','Toronto','UnitedStates','www.techdere.com',2003,'PCs',55000); Insert into Gadget values (8117,'Acer','PC',17.3,3.5,2,'Vista','Yes','No','13-Nov-10',20,20,550,137,21);
  • 4. AISLING FALLON-A00203593 INTERNET CAFE Page 3 /*( B) A gadget oftype laptop must befrom a supplier in the United States*/ VALID INSERT Insert into Internet_Cafe values (217,'Cruise TheNet Cafe','Main Street','Shercock','Cavan','Cruise The Net Cafe','cruisethenet@hotmail.com',2005,5,'Colddrinks'); Insert into Suppliervalues (82,'Taecht.','Toronto','United States','www.techdere.com',2009,'PCs',55000); Insert into Gadget values (8136,'Acer','Laptop',17.3,3.5,2,'Vista','Yes','No','13-Nov-10',35,36,550,217,82); INVALID INSERT Insert into Internet_Cafe values (717,'Cruise TheNet Cafe','MainStreet','Shercock','Cavan','Cruise The Net Cafe','cruisethenet@hotmail.com',2005,5,'Colddrinks'); Insert into Suppliervalues (61,'Taecht.','Toronto','Canada','www.techdere.com',2009,'PCs',55000); Insert into Gadget values (7116,'Acer','Laptop',17.3,3.5,2,'Vista','Yes','No','13-Nov-10',35,36,550,717,61);
  • 5. AISLING FALLON-A00203593 INTERNET CAFE Page 4 /*(C) When a gadgets operating system is LINUX the suppliers specialismmust be Servers*/ VALID INSERT Insert into Internet_Cafe values (917,'Cruise TheNet Cafe','MainStreet','Shercock','Cavan','Cruise The Net Cafe','cruisethenet@hotmail.com',2003,5,'Colddrinks'); Insert into Suppliervalues (39,'Taecht.','Toronto','UnitedStates','www.techdere.com',2009,'Servers',55000); Insert into Gadget values (8664,'Acer','PC',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',20,36,550,917,39); INVALID INSERT Insert into Internet_Cafe values (375,'Cruise TheNet Cafe','MainStreet','Shercock','Cavan','Cruise The Net Cafe','cruisethenet@hotmail.com',2003,5,'Colddrinks'); Insert into Suppliervalues (89,'Taecht.','Toronto','UnitedStates','www.techdere.com',2009,'PCs',55000); Insert into Gadget values (7103,'Acer','PC',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',20,36,550,375,89);
  • 7. AISLING FALLON-A00203593 INTERNET CAFE Page 6 Question 2-BEFORE UPDATE TRIGGER Create n after update trigger on the Gadget table, incorporatingIF statements to test the three followingrules to decide ifan updatewill proceed: a) When the gadgets Name is Apple and the internet cafe county is in Roscommon and the year founded is greater than 2007, update the cost price by 50euro b) When the gadget operatingsystem is ‘Linux’ andthe Rate Per Hour in the internet cafe is greater than the Annual Turnover, update the resale value bydecreasingit by 100 c) When the resale value ofa gadget is greater than 550 and the suppliers name contains am“a”,update the RAM_Size to 3 d) When the gadget type is laptop the year foundedofthe supplier shouldbe before the year opened ofthe internet cafe Set Serveroutput On Size 1000000 Drop TriggerCUB_CU_Gadget; Create or Replace Trigger CUB_CU_Gadget Before Update on Gadget For Each Row Declare V_County Internet_Cafe.County%Type; V_Year_Founded Supplier.Year_Founded%Type; V_Name Supplier.Name%Type; V_Rate_Per_Hour Internet_Cafe.Rate_Per_Hour%Type; V_Annual_Turnover Supplier.Annual_Turnover%Type; V_Year_Opened Internet_Cafe.Year_Opened%Type; Begin Select County, Rate_Per_Hour, Year_OpenedInto V_County,V_Rate_Per_Hour,V_Year_Opened From Internet_Cafe Where Internet_Cafe_Id=:Old.Internet_Cafe_Id; Select Name, Year_Founded, Annual_Turnover Into V_Name, V_Year_Founded, V_Annual_Turnover From Supplier Where Supplier.Supplier_Id=:Old.Supplier_Id; /*(A)When the gadgets Name is Appleand the internet cafe countyis in Roscommon andthe year foundedis greater than 2007, update the cost price by50euro*/ If :Old.Name=’Apple’And V_County<>’Roscommon’Or V_Year_Founded < 2007Then Raise_Application_Error(-20101, ‘A)The gadget must be apple andinternet café in Roscommon’); EndIf; /*(B)When the gadget operating system is ‘Linux’and the Rate Per Hour in the internet cafe is greater than the Annual Turnover, update the resale value bydecreasingit by 100.*/ If :Old.Operating_System=’Linux’And V_Rate_Per_Hour > V_Annual_TurnoverThen Raise_Application_Error(-20101, ‘B) Theanuual turnover must be greater than the rate per hour’); EndIf; /*(C)When the resale value ofa gadget is greater than 550and the suppliers namecontains am “a”, updatethe RAM_Size to 3 .*/ If :Old.Resale_Value>550 And V_Name Like ‘%a%’Then Raise_Application_Error(-20101, ‘C) Thesuppliers name must not contain an a for the update to proceed’); EndIf; /*(D)When the gadget type is laptopthe year foundedofthe supplier should bebefore the year openedofthe internet cafe .*/ If :Old.Type=’Laptop’ And V_Year_Opened <V_Year_Founded Then Raise_Application_Error(-20101, ‘D)The year founded ofthe supplier shouldbe before the year openedofthe internet cafe’); EndIf; End; / Sho err
  • 8. AISLING FALLON-A00203593 INTERNET CAFE Page 7 /*(A)When the gadgets Name is Appleand the internet cafe countyis in Roscommon andthe year foundedis greater than 2007, update the cost price by 50euro*/ INVALID UPDATE Insert into Internet_Cafe values (196,'Cruise TheNet Cafe','MainStreet','Shercock','Galway','Cruise TheNet Cafe','cruisethenet@hotmail.com',2005,5,'Colddrinks'); Insert into Supplier values (37,'Tech Data Ltd.','Toronto','Canada','www.techdata.com', 2006,'PCs',55000); Insert into Gadget values (256,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,196,37); Update Gadget Set Cost_Price= Cost_Price+50 Where Gadget_Id=256; VALID UPDATE Insert into Internet_Cafe values (150,'Cruise TheNet Cafe','MainStreet','Shercock','Roscommon','Cruise The Net Cafe','cruisethenet@hotmail.com',2005,5,'Colddrinks'); Insert into Supplier values (09,'Tech Data Ltd.','Toronto','Canada','www.techdata.com',2009,'PCs',55000); Insert into Gadget values (1050,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,150,09); Update Gadget Set Cost_Price= Cost_Price+50 Where Gadget_Id=1050;
  • 9. AISLING FALLON-A00203593 INTERNET CAFE Page 8 /*(B)When the gadget operatingsystem is ‘Linux’and the Rate Per Hour in the internet cafe is greater than the Annual Turnover, update the resale value by decreasingit by 100.*/ INVALID UPDATE Insert into Internet_Cafe values (231,'Cruise TheNet Cafe','MainStreet','Shercock','Roscommon','Cruise TheNet Cafe','cruisethenet@hotmail.com',2005,9,'Colddrinks'); Insert into Supplier values (92,'Tech Data Ltd.','Toronto','Canada','www.techdata.com', 2009,'PCs',8); Insert into Gadget values (148,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,231,92); Update Gadget Set Resale_Value=Resale_Value-100 Where Gadget_Id=148; VALID UPDATE Insert into Internet_Cafe values (231,'Cruise TheNet Cafe','MainStreet','Shercock','Roscommon','Cruise TheNet Cafe','cruisethenet@hotmail.com',2005,9,'Colddrinks'); Insert into Supplier values (92,'Tech Data Ltd.','Toronto','Canada','www.techdata.com', 2009,'PCs',50000); Insert into Gadget values (148,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,231,92); Update Gadget Set Resale_Value=Resale_Value-100 Where Gadget_Id=148;
  • 10. AISLING FALLON-A00203593 INTERNET CAFE Page 9 /*(C)When the resale value ofa gadget is greater than 550and the suppliers namecontains am “a”, updatethe RAM_Size to 3 .*/ INVALID UPDATE Insert into Internet_Cafe values (231,'Cruise TheNet Cafe','MainStreet','Shercock','Roscommon','Cruise TheNet Cafe','cruisethenet@hotmail.com',2005,9,'Colddrinks'); Insert into Supplier values (92,'Teacha Ltd.','Toronto','Canada','www.techdata.com', 2009,'PCs',50000); Insert into Gadget values (148,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,560,231,92); Update Gadget Set RAM_Size=3 Where Gadget_Id=148; VALID UPDATE Insert into Internet_Cafe values (231,'Cruise TheNet Cafe','MainStreet','Shercock','Roscommon','Cruise TheNet Cafe','cruisethenet@hotmail.com',2005,9,'Colddrinks'); Insert into Supplier values (92,'TeeckeLtd.','Toronto','Canada','www.techdata.com', 2009,'PCs',50000); Insert into Gadget values (148,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,560,231,92); Update Gadget Set RAM_Size=3 Where Gadget_Id=148;
  • 11. AISLING FALLON-A00203593 INTERNET CAFE Page 10 /*(D)When the gadget type is laptopthe year foundedofthe supplier should bebefore the year openedofthe internet cafe .*/ VALID UPDATE Insert into Internet_Cafe values (231,'Cruise TheNet Cafe','MainStreet','Shercock','Roscommon','Cruise TheNet Cafe','cruisethenet@hotmail.com',2012,9,'Colddrinks'); Insert into Supplier values (92,'TeeckeLtd.','Toronto','Canada','www.techdata.com', 2009,'PCs',50000); Insert into Gadget values (148,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,560,231,92); Update Gadget Set RAM_Size=3 Where Gadget_Id=148; INVALID UPDATE Insert into Internet_Cafe values (231,'Cruise TheNet Cafe','MainStreet','Shercock','Roscommon','Cruise TheNet Cafe','cruisethenet@hotmail.com',2009,9,'Colddrinks'); Insert into Supplier values (92,'TeeckeLtd.','Toronto','Canada','www.techdata.com', 2012,'PCs',50000); Insert into Gadget values (148,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,560,231,92); Update Gadget Set RAM_Size=3 Where Gadget_Id=148;
  • 13. AISLING FALLON-A00203593 INTERNET CAFE Page 12 Question 3-BEFORE DELETE TRIGGER Before deletingcafé users bookingthe followingconditions need to bemet: A) The amount due should be between 5 and10, the county shouldnot be Galwayand the cost priceshould begreater than 300. B) A café user bookingwith payment methodcash, may not be deletedifthe user is a student or the ram size is less than 2. C) A café user bookingwhose activityis email, may not be deletedifthe length ofthe length of the type is less less than the length of the county in letters. Drop TriggerCafe_User_Booking_Gad_Cafe_U; Create or Replace Trigger Cafe_User_Booking_Gad_Cafe_U Before Delete on Cafe_User_Booking For Each Row Declare V_County Cafe_User.County%Type; V_Type Cafe_User.Type%Type; V_Cost_Price Gadget.Cost_Price%Type; V_RAM_Size Gadget.RAM_Size%Type; Begin Select County, Type Into V_County,V_Type From Cafe_User Where Cafe_User_Id=:Old.Cafe_User_Id; Select Cost_Price, RAM_Size Into V_Cost_Price,V_RAM_Size From Gadget Where Gadget_Id=:Old.Gadget_Id; /*(A) The amount due shouldbe between 5 and 10, the countyshould not beGalwayand the cost priceshould begreater than 300*/ If :Old.Amount_Due BETWEEN 5 AND 10And V_County<>’Galway’ And V_Cost_Price > 300Then Raise_Application_Error(-20101, ‘A)Theuser must be from Galway,gadget cost price less than 300 andamount due on bookingbetween 5 and10 ’); End If; /*(B) A café user bookingwith payment methodcash, may not bedeletedifthe user is a student or the ram size is less than 2*/ If :Old.Payment_Method=’Cash’ And( V_Type=’Student’ orV_RAM_Size < 2) Then Raise_Application_Error(-20101, ‘B)Café User must not be a student or a gadget with a ram size less than 2 ’); End If; /*(C) A café user bookingwhose activityis email, may not bedeletedifthe length ofthe length of the type is less less than the length ofthe county in letters, */ If :Old.Activity=’Email’ And(Length(V_Type) < Length(V_County))Then Raise_Application_Error(-20101, ‘C)Café User whose activity is email must have the type in letters greater than the county ’); End If; End; / Sho err
  • 14. AISLING FALLON-A00203593 INTERNET CAFE Page 13 /*(A) When a café user bookings amount due is between 5 and 10, the café user must not be from Galwaywith a gadget cost price less than 300 in order to delete a café user booking*/ INVALID DELETE Insertinto Cafe_User values (49,'Colm','Hannon','Abbey Street','Shercock','Galway','Male','23-Jan-90','Irish',086- 1234567,'colmh@gmail.com','Student','Novice'); Insertinto Gadgetvalues (7000,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',200,36,550,101,50); Insertinto Cafe_User_Booking values (49,5000,'01-Sep-13','09:30','10:30',7,'Email','Cash'); Delete From Cafe_User_Booking Where Cafe_User_Id=49; INVALID DELETE Insertinto Cafe_User values (99,'Colm','Hannon','AbbeyStreet','Shercock','Roscommon','Male','23-Jan-90','Irish',086- 1234567,'colmh@gmail.com','Student','Novice'); Insertinto Gadgetvalues (5000,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50); Insertinto Cafe_User_Booking values (99,5000,'01-Sep-13','09:30','10:30',7,'Email','Cash'); Delete From Cafe_User_Booking Where Cafe_User_Id=99;
  • 15. AISLING FALLON-A00203593 INTERNET CAFE Page 14 /*(B) When a café user bookingpayment method is cash, the type ofcafé user must not be student andthe ram size must be less than 2*/ INVALID DELETE Insertinto Cafe_User values (49,'Colm','Hannon','AbbeyStreet','Shercock','Galway','Male','23-Jan-90','Irish',086- 1234567,'colmh@gmail.com','Student','Novice'); Insertinto Gadget values (7000,'Acer','Laptop',17.3,3.5,1,'Linux','Yes','No','13-Nov-10',200,36,550,101,50); Insertinto Cafe_User_Booking values (49,5000,'01-Sep-13','09:30','10:30',7,'Email','Cash'); Delete From Cafe_User_Booking Where Cafe_User_Id=49; VALID DELETE Insertinto Cafe_User values (49,'Colm','Hannon','Abbey Street','Shercock','Galway','Male','23-Jan-90','Irish',086- 1234567,'colmh@gmail.com','Graduate','Novice'); Insertinto Gadgetvalues (7000,'Acer','Laptop',17.3,3.5,5,'Linux','Yes','No','13-Nov-10',200,36,550,101,50); Insertinto Cafe_User_Booking values (49,5000,'01-Sep-13','09:30','10:30',7,'Email','Cash'); Delete From Cafe_User_Booking Where Cafe_User_Id=49;
  • 16. AISLING FALLON-A00203593 INTERNET CAFE Page 15 /*(C) A café user bookingwhose activityis email, may not bedeletedifthe length ofthe type is less less than the length ofthe county in letters, */ INVALID DELETE Insertinto Cafe_User values (49,'Colm','Hannon','AbbeyStreet','Shercock','Galway','Male','23-Jan-90','Irish',086- 1234567,'colmh@gmail.com','OAP','Novice'); Insertinto Gadgetvalues (7000,'Acer','Laptop',17.3,3.5,5,'Linux','Yes','No','13-Nov-10',200,36,550,101,50); Insertinto Cafe_User_Booking values (49,5000,'01-Sep-13','09:30','10:30',7,'Email','Cash'); Delete From Cafe_User_Booking Where Cafe_User_Id=49; VALID DELETE Insertinto Cafe_User values (49,'Colm','Hannon','AbbeyStreet','Shercock','Galway','Male','23-Jan-90','Irish',086- 1234567,'colmh@gmail.com','Students','Novice'); Insertinto Gadgetvalues (7000,'Acer','Laptop',17.3,3.5,5,'Linux','Yes','No','13-Nov-10',200,36,550,101,50); Insertinto Cafe_User_Booking values (49,5000,'01-Sep-13','09:30','10:30',7,'Email','Cash'); Delete From Cafe_User_Booking Where Cafe_User_Id=49;
  • 18. AISLING FALLON-A00203593 INTERNET CAFE Page 17 Question 4-AFTER INSERT TRIGGER Create an AFTER insert trigger on the Gadget table, incorporating select statements to retrieve data, IF statements will evaluate/compare the data retrieved from the table, andtherefore influence which ofa number of update Statements to relatedtables will be executed after the successful Insert on the Child table. a) Increase the rate per hour in internet cafes by 10% when the gadget is oftype laptop b) Increase the year opened ofinternet cafes by 3 years ifthe gadget ifthe gadget name is Dell c) Increase the annual turnover by 1000 euro when the ram size ofa gadget is 2 d) Information relatingto the Insert must also be recordedin a LOG table Drop Trigger Gad_Int_Cafe_Supp_Updates; Drop Table New_Gadget_Audit_Log; Create Table New_Gadget_Audit_Log(User_Involved_NameVarchar2(15),Current_Date Date,Gadget_Id Number (4)); Create Or Replace TriggerGad_Int_Cafe_Supp_Updates After Insert On Gadget For Each Row Declare V_Rate_Per_Hour Internet_Cafe.Rate_Per_Hour%Type; V_Annual_Turnover Supplier.Annual_Turnover%Type; V_Year_Opened Internet_Cafe.Year_Opened%Type; Begin Select Rate_Per_Hour, Year_Opened IntoV_Rate_Per_Hour, V_Year_Opened From Internet_Cafe Where Internet_Cafe_Id=:New.Internet_Cafe_Id; Select Annual_TurnoverInto V_Annual_Turnover From Supplier Where Supplier_Id=:New.Supplier_Id; Insertinto New_Gadget_Audit_Log Values (User , Sysdate,:New.Gadget_Id); /*( A) Increase the rate per hour in internet cafes by 10% when the gadget is oftype laptop*/ If :New.Type= ‘Laptop’Then Update Internet_Cafe Set Rate_Per_Hour= Rate_Per_Hour*1.1 Where Internet_Cafe_Id=:New.Internet_Cafe_Id; Endif; /*( B) Increase the year opened ofinternet cafes by 3 years ifthe gadget ifthe gadget name is Dell*/ If :New.Name = ‘Dell’ Then Update Internet_Cafe Set Year_Opened= Year_Opened+3 Where Internet_Cafe_Id=:New.Internet_Cafe_Id; Endif; /*( C) Increase the annual turnover by 1000 eurowhen the ram size ofa gadget is 2*/ If :New.RAM_Size= 2 Then Update Supplier Set Annual_Turnover=Annual_Turnover+1000 Where Supplier_Id=:New.Supplier_Id; Endif; End; / Sho Err /*( A) Increase the rate per hour in internet cafes by 10% when the gadget is oftype laptop*/
  • 19. AISLING FALLON-A00203593 INTERNET CAFE Page 18 Select I.Rate_Per_Hour,I.Internet_Cafe_Id, G.Type From Internet_Cafe I, Gadget G Where I.Internet_Cafe_Id=G. Internet_Cafe_Id And G.Type=’Laptop’; Insert into Internet_Cafe values (197,'Cruise TheNet Cafe','MainStreet','Shercock','Cavan','Cruise The Net Cafe','cruisethenet@hotmail.com',2008,5,'Colddrinks'); Insert into Suppliervalues (91,'Taecht.','Toronto','UnitedStates','www.techdere.com',2009,'PCs',55000); Insert into Gadget values (7115,'Acer','Laptop',17.3,3.5,2,'Vista','Yes','No','13-Nov-10',20,36,550,197,91); Select I.Rate_Per_Hour,I.Internet_Cafe_Id, G.Type From Internet_Cafe I, Gadget G Where I.Internet_Cafe_Id=G. Internet_Cafe_Id And G.Type=’Laptop’;
  • 20. AISLING FALLON-A00203593 INTERNET CAFE Page 19 /*(B) Increase the year openedofinternet cafes by 3 years ifthe gadget if the gadget name is Dell*/ Select I.Year_Opened, I.Internet_Cafe_Id, G.Name From Internet_Cafe I, Gadget G Where I.Internet_Cafe_Id=G. Internet_Cafe_Id And G.Name=’Dell’ Insert into Internet_Cafe values (297,'Cruise TheNet Cafe','Main Street','Shercock','Cavan','Cruise The Net Cafe','cruisethenet@hotmail.com',2008,5,'Colddrinks'); Insert into Suppliervalues (72,'Taecht.','Toronto','UnitedStates','www.techdere.com',2009,'PCs',55000); Insert into Gadget values (7225,'Dell','Laptop',17.3,3.5,2,'Vista','Yes','No','13-Nov-10',20,36,550,297,72); Select I.Year_Opened, I.Internet_Cafe_Id, G.Name From Internet_Cafe I, Gadget G Where I.Internet_Cafe_Id=G. Internet_Cafe_Id And G.Name=’Dell’
  • 21. AISLING FALLON-A00203593 INTERNET CAFE Page 20 /*( C) Increase the annual turnover by 1000 eurowhen the ram size ofa gadget is 2*/ Select S.Annual_Turnover, S.Supplier_Id, G.RAM_Size From Supplier S, Gadget G Where S.Supplier_Id=G.Supplier_Id And G.Ram_Size=2; Insert into Internet_Cafe values (397,'Cruise TheNet Cafe','MainStreet','Shercock','Cavan','Cruise The Net Cafe','cruisethenet@hotmail.com',2008,5,'Colddrinks'); Insert into Suppliervalues (12,'Taecht.','Toronto','UnitedStates','www.techdere.com',2009,'PCs',55000); Insert into Gadget values (7215,'Dell','Laptop',17.3,3.5,2,'Vista','Yes','No','13-Nov-10',20,36,550,397,12); Select S.Annual_Turnover, S.Supplier_Id, G.RAM_Size From Supplier S, Gadget G Where S.Supplier_Id=G.Supplier_Id And G.Ram_Size=2;
  • 22. AISLING FALLON-A00203593 INTERNET CAFE Page 21 Select * From New_Gadget_Audit_Log;
  • 24. AISLING FALLON-A00203593 INTERNET CAFE Page 23 Question 5-AFTER UPDATE OF A COLUMN TRIGGER After updatingthe details ofgadget repair the folloeingupdates shouldoccur providedthe conditions are met: a) When the gadget repairs details are updatedto ‘fix system’,the hourly rate must be greater than the minimum hourly rate of all technicians for the cost ofthe repair to increase by 10% b) When the gadget repair details are updatedto ‘update window’, the technicians hourly rate must begreater than the maxram size ofany gadget in order for the resale value to decrease by100euro. c) When the gadget repair details are updatedto ‘Broken monitor’, the expected life ofthe gadget must be less than the average screen size in order to update the hourly rate by cuttingit in half Set Serveroutput On Size 1000000 Drop Table New_Gad_Rep_Audit_Log; Create Table New_Gad_Rep_Audit_Log(Details Varchar2(35), User_Involved_Name Varchar2(15),Current_Date Date, Gadget_Id Number (4)); Drop TriggerGadget_Rep_Tech_Gad; Create or Replace Trigger Gadget_Rep_Tech_Gad After Update of Details on Gadget_Repair For Each Row Declare V_Min_Hourly_Rate Technician.Hourly_Rate%Type; V_Max_RAM_Size Gadget.RAM_Size%Type; V_Hourly_Rate Technician.Hourly_Rate%Type; V_Avg_Screen_Size_Inches Gadget. Screen_Size_Inches%Type; V_Expected_Life Gadget.Expected_Life%Type; Begin Select Min(Hourly_Rate) Into V_Min_Hourly_Rate From Technician; Select Hourly_Rate Into V_Hourly_Rate From Technician Where Technician_Id=:New.Technician_Id; Select Max(RAM_Size) Into V_Max_RAM_Size From Gadget; Select Avg(Screen_Size_Inches)Into V_Avg_Screen_Size_Inches From Gadget; Select Expected_Life Into V_Expected_Life From Gadget Where Gadget_Id=:New.Gadget_Id; /*(A)When the gadget repairs details are updated to ‘fix system’, the hourly rate must begreater than the minimumhourly rate of all technicians for the cost of the repair to increase by 10%*/ If :Old.Details=’Remove Virus’ And:New.Details=’FixSystem’And V_Hourly_Rate > V_Min_Hourly_Rate Then Update Gadget Set Cost_Price= Cost_Price*1.1 Where Gadget.Gadget_Id= :New.Gadget_Id; Insertinto New_Gad_Rep_Audit_Log Values(‘Cost_Price updated’,User, Sysdate,:New.Gadget_Id); End If; /*(B)When the gadget repair details are updated to ‘updatewindow’,the technicians hourly rate must be greater than the max ram size of any gadget in order for the resale value to decrease by 100euro.*/ If :Old.Details=’Remove Virus’ And:New.Details=’Update Windows’ And V_Hourly_Rate > V_Max_RAM_Size Then Update Gadget Set Resale_Value=Resale_Value-100 Where Gadget.Gadget_Id= :New.Gadget_Id; Insertinto New_Gad_Rep_Audit_Log Values(‘Resale_Value updated’, User, Sysdate,:New.Gadget_Id); End If; /*(C)When the gadget repair details are updated to ‘Brokenmonitor’,the expectedlife ofthe gadget must be less than the average screen size in order to update the hourly rate by cuttingit in half*/ If :Old.Details=’Remove Virus’ And:New.Details=’Broken monitor’ And V_Expected_Life < V_Avg_Screen_Size_Inches Then Update Technician Set Hourly_Rate= Hourly_Rate /2 Where Technician.Technician_Id=:New.Technician_Id; Insertinto New_Gad_Rep_Audit_Log Values(‘Hourly_Rate updated’, User, Sysdate, :New.Technician_Id); End If; End;
  • 25. AISLING FALLON-A00203593 INTERNET CAFE Page 24 / Sho err /*(A)When the gadget repairs details are updated to ‘fix system’, the hourly rate must begreater than the minimumhourly rate of all technicians for the cost ofthe repair to increase by 10%*/ VALID UPDATE Insert into Gadget values (1127,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50); Insert into Technicianvalues (2089,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',25,'23-Oct-08',086-4536578); Insert into Gadget_Repair values (1127,'01-Nov-11',‘Remove Virus',30,120,2089); Select Cost_Price From Gadget Where Gadget_Id=1127; Update Gadget_Repair Set Details=’Fix System’ Where Gadget_Id=1127; Select Cost_Price From Gadget Where Gadget_Id=1127;
  • 26. AISLING FALLON-A00203593 INTERNET CAFE Page 25 INVALID UPDATE Insert into Gadget values (1127,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50); Insert into Technicianvalues (2089,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',14,'23-Oct-08',086-4536578); Insert into Gadget_Repair values (1127,'01-Nov-11',‘Remove Virus',30,120,2089); Select Cost_Price From Gadget Where Gadget_Id=1127; Update Gadget_Repair Set Details=’Fix System’ Where Gadget_Id=1127; Select Cost_Price From Gadget Where Gadget_Id=1127;
  • 27. AISLING FALLON-A00203593 INTERNET CAFE Page 26 /*(B)When the gadget repair details are updated to ‘updatewindow’,the technicians hourly rate must be greater than the max ramsize of any gadget in order for the resale value to decrease by 100euro.*/ VALID UPDATE Insert into Gadget values (1139,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50); Insert into Technicianvalues (2039,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',14,'23-Oct-08',086-4536578); Insert into Gadget_Repair values (1139,'01-Nov-11',‘Remove Virus',90,120,2039); Select Resale_Value From Gadget Where Gadget_Id=1139; Update Gadget_Repair Set Details=’Update Windows’ Where Gadget_Id=1139; Select Resale_Value From Gadget Where Gadget_Id=1139;
  • 28. AISLING FALLON-A00203593 INTERNET CAFE Page 27 INVALID UPDATE Insert into Gadget values (1139,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50); Insert into Technicianvalues (2039,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',1,'23-Oct-08',086-4536578); Insert into Gadget_Repair values (1139,'01-Nov-11',‘Remove Virus',10,120,2039); Select Resale_Value From Gadget Where Gadget_Id=1139; Update Gadget_Repair Set Details=’Update Windows’ Where Gadget_Id=1139; Select Resale_Value From Gadget Where Gadget_Id=1139;
  • 29. AISLING FALLON-A00203593 INTERNET CAFE Page 28 /*(C)When the gadget repair details are updated to ‘broken monitor’, the expectedlife ofthe gadget must be less than the average screen size in order to update the hourly rate by cuttingit in half.*/ INVALID UPDATE Insert into Gadget values (1189,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50); Insert into Technicianvalues (2079,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',14,'23-Oct-08',086-4536578); Insert into Gadget_Repair values (1189,'01-Nov-11',‘Remove Virus',30,120,2079); Select Hourly_Rate From Technician Where Technician_Id=2079; Update Gadget_Repair Set Details=’Brokenmonitor’ Where Technician_Id=2079; Select Hourly_Rate From Technician Where Technician_Id=2079;
  • 30. AISLING FALLON-A00203593 INTERNET CAFE Page 29 VALID UPDATE Insert into Gadget values (1189,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,5,550,101,50); Insert into Technicianvalues (2079,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',14,'23-Oct-08',086-4536578); Insert into Gadget_Repair values (1189,'01-Nov-11',‘Remove Virus',30,120,2079); Select Hourly_Rate From Technician Where Technician_Id=2079; Update Gadget_Repair Set Details=’Brokenmonitor’ Where Technician_Id=2079; Select Hourly_Rate From Technician Where Technician_Id=2079;
  • 32. AISLING FALLON-A00203593 INTERNET CAFE Page 31 Question 6-AFTER DELETE TRIGGER After deletinga gadget repair,amendments canbe made to othertables providedthefollowingconditions are met: A)When the gadget repairs details are updatedto ‘fix system’, the hourly rate must be greater than the minimumhourly rate of all technicians for the cost ofthe repair to increase by 10%, when a delete occurs. B)When the gadget repair details are ‘REMOVE VIRUS’, the technicians hourly rate must be greater than the max ramsize ofany gadget in order for the resale value to decrease by100euro. C)When the gadget repair details are updated to ‘Broken monitor’,the expectedlife ofthe gadget must be less than the average screen size in order to update the hourly rate by cuttingit in half Set Serveroutput On Size 1000000 Drop Table Old_G_Rep_Gad_Tech_Audit_Log; Create Table Old_G_Rep_Gad_Tech_Audit_Log(Details Varchar2(35),User_Involved_Name Varchar2(15), Current_Date Date, Gadget_IdNumber (4)); Drop TriggerGad_Rep_Tech_Gad; Create or Replace Trigger Gad_Rep_Tech_Gad After Delete on Gadget_Repair For Each Row Declare V_Min_Hourly_Rate Technician.Hourly_Rate%Type; V_Max_RAM_Size Gadget.RAM_Size%Type; V_Hourly_Rate Technician.Hourly_Rate%Type; V_Avg_Screen_Size_Inches Gadget. Screen_Size_Inches%Type; V_Expected_Life Gadget.Expected_Life%Type; Begin Select Min(Hourly_Rate) Into V_Min_Hourly_Rate From Technician; Select Hourly_Rate Into V_Hourly_Rate From Technician Where Technician_Id=:Old.Technician_Id; Select Max(RAM_Size) Into V_Max_RAM_Size From Gadget; Select Avg(Screen_Size_Inches)Into V_Avg_Screen_Size_Inches From Gadget; Select Expected_Life Into V_Expected_Life From Gadget Where Gadget_Id=:Old.Gadget_Id; /*(A)When the gadget repairs details are updated to ‘fix system’, the hourly rate must begreater than the minimumhourly rate of all technicians for the cost of the repair to increase by 10%, when a delete occurs.*/ If :Old.Details=’Remove Virus’ And V_Hourly_Rate > V_Min_Hourly_Rate Then Update Gadget Set Cost_Price= Cost_Price*1.1 Where Gadget.Gadget_Id= :Old.Gadget_Id; Insertinto Old_G_Rep_Gad_Tech_Audit_LogValues (‘Cost_Price updated’, User, Sysdate, :Old.Gadget_Id); End If; /*(B)When the gadget repair details are ‘REMOVE VIRUS’, the technicians hourly rate must be greater than the maxram size of any gadget in order for the resale value to decrease by 100euro.*/ If :Old.Details=’Remove Virus’ And V_Hourly_Rate > V_Max_RAM_Size Then Update Gadget Set Resale_Value=Resale_Value-100 Where Gadget.Gadget_Id= :Old.Gadget_Id; Insertinto Old_G_Rep_Gad_Tech_Audit_LogValues (‘Resale_Value updated’, User, Sysdate, :Old.Gadget_Id); End If; /*(C)When the gadget repair details are updated to ‘Brokenmonitor’,the expectedlife ofthe gadget must be less than the average screen size in order to update the hourly rate by cuttingit in half*/ If :Old.Details=’Remove Virus’ And V_Expected_Life< V_Avg_Screen_Size_Inches Then Update Technician Set Hourly_Rate= Hourly_Rate /2 Where Technician.Technician_Id=:Old.Technician_Id; Insertinto Old_G_Rep_Gad_Tech_Audit_Log Values(‘Hourly_Rate updated’, User, Sysdate, :Old.Technician_Id); End If; End; / Sho er /*(A)When the gadget repairs details are updated to ‘fix system’, the hourly rate must begreater than the minimumhourly rate of all technicians for the cost ofthe repair to increase by 10%*/
  • 33. AISLING FALLON-A00203593 INTERNET CAFE Page 32 VALID DELETE Insert into Gadget values (1397,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50); Insert into Technicianvalues (2059,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',25,'23-Oct-08',086-4536578); Insert into Gadget_Repair values (1397,'01-Nov-11',‘Remove Virus',30,120,2059); Select Cost_Price From Gadget Where Gadget_Id=1397; Delete From Gadget_Repair Where Repair_Date='01-Nov-11' And Gadget_Id=1397; Select Cost_Price From Gadget Where Gadget_Id=1397;
  • 34. AISLING FALLON-A00203593 INTERNET CAFE Page 33 INVALID DELETE Insert into Gadget values (2597,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50); Insert into Technicianvalues (7159,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',6,'23-Oct-08',086-4536578); Insert into Gadget_Repair values (2597,'01-Nov-11',‘Remove Virus',30,120,7159); Select Cost_Price From Gadget Where Gadget_Id=2597; Delete From Gadget_Repair Where Repair_Date='01-Nov-11'; And Gadget_Id=2597; Select Cost_Price From Gadget Where Gadget_Id=2597;
  • 35. AISLING FALLON-A00203593 INTERNET CAFE Page 34 /*(B)When the gadget repair details are remove virus, the technicians hourly rate must be greater than the max ramsize ofany gadget in order for the resale value to decrease by 100 euro.*/ VALID DELETE Insert into Gadget values (1141,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50); Insert into Technicianvalues (2039,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',14,'23-Oct-08',086-4536578); Insert into Gadget_Repair values (1141,'01-Nov-11',‘Remove Virus',90,120,2039); Select Resale_Value From Gadget Where Gadget_Id=1141; Delete From Gadget_Repair Where Repair_Date='01-Nov-11'; And Gadget_Id=1141; Select Resale_Value From Gadget Where Gadget_Id=1141; #
  • 36. AISLING FALLON-A00203593 INTERNET CAFE Page 35 INVALID DELETE Insert into Gadget values (7139,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50); Insert into Technicianvalues (7039,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',1,'23-Oct-08',086-4536578); Insert into Gadget_Repair values (7139,'03-Nov-11',‘Remove Virus',10,120,7039); Select Resale_Value From Gadget Where Gadget_Id=7139; Delete From Gadget_Repair Where Repair_Date='03-Nov-11' And Gadget_Id=7139; Select Resale_Value From Gadget Where Gadget_Id=7139;
  • 37. AISLING FALLON-A00203593 INTERNET CAFE Page 36 /*(C)When the gadget repair details are updated to ‘broken monitor’, the expectedlife ofthe gadget must be less than the average screen size in order to update the hourly rate by cuttingit in half .*/ INVALID DELETE Insert into Gadget values (1369,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50); Insert into Technicianvalues (2091,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',14,'23-Oct-08',086-4536578); Insert into Gadget_Repair values (1369,'05-Nov-11',‘Remove Virus',30,120,2091); Select Hourly_Rate From Technician Where Technician_Id=2091; Delete From Gadget_Repair Where Repair_Date='05-Nov-11' And Gadget_Id=1369; Select Hourly_Rate From Technician Where Technician_Id=2091;
  • 38. AISLING FALLON-A00203593 INTERNET CAFE Page 37 VALID DELETE Insert into Gadget values (1369,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,5,550,101,50); Insert into Technicianvalues (2091,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',14,'23-Oct-08',086-4536578); Insert into Gadget_Repair values (1369,'05-Nov-11',‘Remove Virus',30,120,2091); Select Hourly_Rate From Technician Where Technician_Id=2091; Delete From Gadget_Repair Where Repair_Date='05-Nov-11' And Gadget_Id=1369; Select Hourly_Rate From Technician Where Technician_Id=2091; Select * From Old_G_Rep_Gad_Tech_Audit_Log;
  • 40. AISLING FALLON-A00203593 INTERNET CAFE Page 39 Question 7-BEFOREINSERT/UPDATE/DELETE TRIGGER Before inserts, updates and deletes can onlybe performedprovidedthe followingconditions are met: A) When a gadgets cost price is less than 25, the café year openedmust be LESS than 2006*/ B)When the gadgets Name is Apple and the internet cafe county is in Roscommon andthe year foundedis greater than 2007, update the cost price by 50euro*/ C) When the type ofgadget is PC, the specialism msut be Servers and the length ofthe email contain an”a” Set Serveroutput On Size 1000000 Drop TriggerGadget_Yr_Opened; Create or Replace Trigger Gadget_Yr_Opened Before Insert or Update or Delete onGadget For Each Row Declare V_Year_Opened Internet_Cafe.Year_Opened%Type; V_County Internet_Cafe.County%Type; V_Specialism Supplier.Specialism%Type; V_Email Internet_Cafe.Email%Type; Begin /*(A) When a gadgets cost price is less than 25, the café year openedmust be LESS than 2006*/ If Inserting Then Select Year_OpenedInto V_Year_Opened From Internet_Cafe Where Internet_Cafe_Id=:New.Internet_Cafe_Id; If :New.Cost_Price< 25 And V_Year_Opened > 2006 Then Raise_Application_Error(-20101, ‘A)When a gadgets cost price is less than 25, the year openedmust be greater than 2006’); End If; /*(B)When the gadgets Name is Appleand the internet cafe countyis in Roscommon andthe year foundedis greater than 2007, update the cost price by50euro*/ Elsif UpdatingThen Select County IntoV_County From Internet_Cafe Where Internet_Cafe_Id=:New.Internet_Cafe_Id; If :Old.Name=’Apple’And V_County<>’Roscommon’Then Raise_Application_Error(-20101, ‘A)The gadget must be apple andinternet café in Roscommon’); End If; /*(C) When the type ofgadget is PC, the specialism msut be Servers and the length ofthe email contain an”a”*/ Elsif DeletingThen Select Specialism Into V_Specialism From Supplier Where Supplier.Supplier_Id=:Old.Supplier_Id; Select Email IntoV_Email From Internet_Cafe Where Internet_Cafe_Id=:Old.Internet_Cafe_Id; If :Old.Type=’PC’ And V_Specialism<>’Servers’Or V_Email LIKE‘%a%’ Then Raise_Application_Error(-20101, ‘A)When the gadget typeis PC, the specialsm must be Servers andemail must not contain an “a” ’); End If; End If; End; / Sho err
  • 41. AISLING FALLON-A00203593 INTERNET CAFE Page 40 INVALID INSERT Insert into Internet_Cafe values (129,'Cruise TheNet Cafe','MainStreet','Shercock','Cavan','Cruise The Net Cafe','cruisethenet@hotmail.com',2008,5,'Colddrinks'); Insert into Suppliervalues (71,'Taecht.','Toronto','UnitedStates','www.techdere.com',2009,'PCs',55000); Insert into Gadget values (8635,'Acer','PC',17.3,3.5,2,'Vista','Yes','No','13-Nov-10',20,36,550,129,71); VALID INSERT Insert into Internet_Cafe values (137,'Cruise TheNet Cafe','MainStreet','Shercock','Cavan','Cruise The Net Cafe','cruisethenet@hotmail.com',2005,5,'Colddrinks'); Insert into Supplier values (21,'Taecht.','Toronto','UnitedStates','www.techdere.com',2003,'PCs',55000); Insert into Gadget values (8117,'Acer','PC',17.3,3.5,2,'Vista','Yes','No','13-Nov-10',20,20,550,137,21);
  • 42. AISLING FALLON-A00203593 INTERNET CAFE Page 41 INVALID UPDATE Insert into Internet_Cafe values (196,'Cruise TheNet Cafe','MainStreet','Shercock','Galway','Cruise TheNet Cafe','cruisethenet@hotmail.com',2005,5,'Colddrinks'); Insert into Supplier values (37,'Tech Data Ltd.','Toronto','Canada','www.techdata.com', 2009,'PCs',55000); Insert into Gadget values (256,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,196,37); Update Gadget Set Cost_Price= Cost_Price+50 Where Gadget_Id=256; VALID UPDATE Insert into Internet_Cafe values (150,'Cruise TheNet Cafe','MainStreet','Shercock','Roscommon','Cruise The Net Cafe','cruisethenet@hotmail.com',2005,5,'Colddrinks'); Insert into Supplier values (09,'Tech Data Ltd.','Toronto','Canada','www.techdata.com',2009,'PCs',55000); Insert into Gadget values (1050,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,150,09); Update Gadget Set Cost_Price= Cost_Price+50 Where Gadget_Id=1050;
  • 43. AISLING FALLON-A00203593 INTERNET CAFE Page 42 INVALID DELETE Insert into Internet_Cafe values (150,'Cruise TheNet Cafe','MainStreet','Shercock','Roscommon','Cruise TheNet Cafe','cruas@hotmail.com',2005,5,'Colddrinks'); Insert into Supplier values (09,'Tech Data Ltd.','Toronto','Canada','www.techta.com',2009,'PC',55000); Insert into Gadget values (1050,'Apple','PC',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,150,09); Delete From Gadget Where Gadget_Id=1050; VALID DELETE Insert into Internet_Cafe values (150,'Cruise TheNet Cafe','MainStreet','Shercock','Roscommon','Cruise TheNet Cafe','cruse@hotmail.com',2005,5,'Colddrinks'); Insert into Supplier values (09,'Tech Data Ltd.','Toronto','Canada','www.techdata.com',2009,'Servers',55000); Insert into Gadget values (1050,'Apple','PC',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,150,09); Delete From Gadget Where Gadget_Id=1050;
  • 45. AISLING FALLON-A00203593 INTERNET CAFE Page 44 Question 8-AFTER INSERT/UPDATE/DELETE Amendments made to gadget repair can bemade after insert, update anddelete provided the folloeingconditions are met: A)When the gadget repair details are updated to ‘Broken monitor’,the expectedlife ofthe gadget must be less than the average screen size in order to update the hourly rate by cuttingit in half*/ B)When the gadget repairs details are updatedto ‘fix system’, the hourly rate must be greater than the minimumhourly rate of all technicians for the cost ofthe repair to increase by 10%*/ C)When the gadget repair details are REMOVE VIRUS the technicians hourly rate must be greater than the max ramsize ofany gadget in order for the resale value to decrease by 100euro Set Serveroutput On Size 1000000 Drop Trigger Gad_Tech_GR; Create or Replace Trigger Gad_Tech_GR After Insert or Update or Delete on Gadget_Repair For Each Row Declare V_Min_Hourly_Rate Technician.Hourly_Rate%Type; V_Hourly_Rate Technician.Hourly_Rate%Type; V_Max_RAM_Size Gadget.RAM_Size%Type; V_Avg_Screen_Size_Inches Gadget.Screen_Size_Inches%Type; V_Expected_Life Gadget.Expected_Life%Type; Begin /*(A)When the gadget repair details are updated to ‘Broken monitor’, the expected life of the gadget must be less than the average screen size in order to update the hourly rate by cutting it in half */ If Inserting Then Select Avg(Screen_Size_Inches) Into V_Avg_Screen_Size_Inches From Gadget; Select Expected_Life Into V_Expected_Life From Gadget Where Gadget_Id=:New.Gadget_Id; If :New.Details=’Remove Virus’ And V_Expected_Life < V_Avg_Screen_Size_Inches Then Update Technician Set Hourly_Rate= Hourly_Rate /2 Where Technician.Technician_Id=:New.Technician_Id; End If; /*(B)When the gadget repairs details are updated to ‘fix system’, the hourly rate must be greater than the minimum hourly rat e of all technicians for the cost of the repair to increase by 10%*/ Elsif Updating Then Select Min(Hourly_Rate) Into V_Min_Hourly_Rate From Technician; Select Hourly_Rate Into V_Hourly_Rate From Technician Where Technician_Id=:New.Technician_Id; If :Old.Details=’Remove Virus’ And :New.Details=’Fix System’ And V_Hourly_Rate > V_Min_Hourly_Rate Then Update Gadget Set Cost_Price= Cost_Price*1.1 Where Gadget.Gadget_Id= :New.Gadget_Id; End If; Elsif Deleting Then /*(C)When the gadget repair details are REMOVE VIRUS the technicians hourly rate must be greater than the max ram size of any gadget in order for the resale value to decrease by 100 euro.*/ Select Max(RAM_Size) Into V_Max_RAM_Size From Gadget; Select Hourly_Rate Into V_Hourly_Rate From Technician Where Technician_Id=:Old.Technician_Id; If :Old.Details=’Remove Virus’ And V_Hourly_Rate > V_Max_RAM_Size Then Update Gadget Set Resale_Value=Resale_Value-100 Where Gadget.Gadget_Id=:Old.Gadget_Id; End If; End If; End; / Sho Err
  • 46. AISLING FALLON-A00203593 INTERNET CAFE Page 45 /*(A)When the gadget repair details are updated to ‘Brokenmonitor’,the expectedlife ofthe gadget must be less than the average screen size in order to update the hourly rate by cuttingit in half*/ INVALID INSERT Select T.Hourly_Rate, GR.Details From TechnicianT,Gadget_Repair GR Where T.Technician_Id=GR.Technician_Id And GR.Details=’Remove Virus’; Insert into Gadget values (2300,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,3,550,101,50); Insert into Technicianvalues (2110,'Aidan','Cassidy','Market Square','Shercock','Cavan','Certificate','Software',15,'23-Oct-08',086- 4536578); Insert into Gadget_Repair values (2300,'13-Nov-11','Remove Virus',30,120,2110); Select T.Hourly_Rate, GR.Details From TechnicianT,Gadget_Repair GR Where T.Technician_Id=GR.Technician_Id And GR.Details=’Remove Virus’; VALID INSERT Select T.Hourly_Rate, GR.Details From TechnicianT,Gadget_Repair GR Where T.Technician_Id=GR.Technician_Id And GR.Details=’Remove Virus’; Insert into Gadget values (2300,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50); Insert into Technicianvalues (2110,'Aidan','Cassidy','Market Square','Shercock','Cavan','Certificate','Software',15,'23-Oct-08',086- 4536578); Insert into Gadget_Repair values (2300,'13-Nov-11','Remove Virus',30,120,2110); Select T.Hourly_Rate, GR.Details From TechnicianT,Gadget_Repair GR Where T.Technician_Id=GR.Technician_Id And GR.Details=’Remove Virus’;
  • 47. AISLING FALLON-A00203593 INTERNET CAFE Page 46 /*(B)When the gadget repairs details are updated to ‘fix system’, the hourly rate must begreater than the minimumhourly rate of all technicians for the cost ofthe repair to increase by 10%*/ VALID UPDATE Insert into Gadget values (1927,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50); Insert into Technicianvalues (2029,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',25,'23-Oct-08',086-4536578); Insert into Gadget_Repair values (1927,'01-Nov-11',‘Remove Virus',30,120,2029); Select Cost_Price From Gadget Where Gadget_Id=1927; Update Gadget_Repair Set Details=’Fix System’ Where Gadget_Id=1927; Select Cost_Price From Gadget Where Gadget_Id=1927;
  • 48. AISLING FALLON-A00203593 INTERNET CAFE Page 47 INVALID UPDATE Insert into Gadget values (1927,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50); Insert into Technicianvalues (2029,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',2,'23-Oct-08',086-4536578); Insert into Gadget_Repair values (1927,'01-Nov-11',‘Remove Virus',30,120,2029); Select Cost_Price From Gadget Where Gadget_Id=1927; Update Gadget_Repair Set Details=’Fix System’ Where Gadget_Id=1927; Select Cost_Price From Gadget Where Gadget_Id=1927;
  • 49. AISLING FALLON-A00203593 INTERNET CAFE Page 48 /*(C)When the gadget repair details remove virus, the technicians hourly rate must be greater than the max ramsize of any gadget in order for the resale value to decrease by 100 euro.*/ VALID DELETE Insert into Gadget values (7551,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50); Insert into Technicianvalues (8019,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',14,'23-Oct-08',086-4536578); Insert into Gadget_Repair values (7551,'08-Nov-11',‘Remove Virus',90,120,8019); Select Resale_Value From Gadget Where Gadget_Id=7551; Delete From Gadget_Repair Where Repair_Date='08-Nov-11' And Gadget_Id=7551; Select Resale_Value From Gadget Where Gadget_Id=7551;
  • 50. AISLING FALLON-A00203593 INTERNET CAFE Page 49 INVALID DELETE Insert into Gadget values (7139,'Acer','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,101,50); Insert into Technicianvalues (7039,'Aidan','Cassidy','MarketSquare','Shercock','Cavan','Certificate','Software',1,'23-Oct-08',086-4536578); Insert into Gadget_Repair values (7139,'03-Nov-11',‘Remove Virus',10,120,7039); Select Resale_Value From Gadget Where Gadget_Id=7139; Delete From Gadget_Repair Where Repair_Date='03-Nov-11' And Gadget_Id=7139; Select Resale_Value From Gadget Where Gadget_Id=7139;
  • 52. AISLING FALLON-A00203593 INTERNET CAFE Page 51 Question9-TABLE LEVEL TRIGGER Create a table level trigger to allow the followingto occur on the gadget table: A) You may onlydelete from the gadget table between8 and6, Mondayto Friday B) You may onlyinsert from the gadget table between8 and6, Mondayto Friday C) You may onlyupdate from the gadget tablebetween8 and 6,Monday to Friday VALID INSERTS, UPDATES, DELETES (Inserts updates anddeletes tookplacebetween 8am and6pm, MondaytoFriday) Drop Trigger Gagdet_Date_Cost; Create or ReplaceTrigger Gagdet_Date_Cost Before Insert or Update or Delete onGadget Begin IF(TO_CHAR(Sysdate, ‘DY’)IN(‘SAT’, ‘SUN’)) OR (TO_CHAR(Sysdate,‘HH24’) NOT BETWEEN ‘08’AND ‘18’)Then If DeletingThen Raise_Application_Error(-20502,‘Youmay onlydeletefromgadget between8 and6, MondaytoFriday’); Elsif InsertingThen Raise_Application_Error(-20500, ‘Youmayonlyinsert fromgadget between8 and6, MondaytoFriday’); Elsif Updating(‘RAM_Size’) Then Raise_Application_Error(-20503, ‘Youmayonlyupdate from gadget between 8 and6, Monday toFriday’); Else Raise_Application_Error(-20503, ‘Youmayonlyupdate from gadget between 8 and6, Monday toFriday’); EndIf; EndIf; End; / Sho Err VALID UPDATE Update Gadget Set RAM_Size= RAM_Size+2 Where RAM_Size=2; VALID INSERT Insert into Internet_Cafe values (196,'Cruise TheNet Cafe','MainStreet','Shercock','Galway','Cruise TheNet Cafe','cruisethenet@hotmail.com',2005,5,'Colddrinks'); Insert into Supplier values (37,'Tech Data Ltd.','Toronto','Canada','www.techdata.com', 2006,'PCs',55000); Insert into Gadget values (256,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,196,37);
  • 53. AISLING FALLON-A00203593 INTERNET CAFE Page 52 VALID DELETE Delete From Gadget Where Gadget_Id=1000;
  • 54. AISLING FALLON-A00203593 INTERNET CAFE Page 53 IINVALID INSERTS, UPDATES, DELETES (Inserts updates anddeletes tookplacebetween 2pmand6 pm,Wednesday orThursday) Drop Trigger Gagdet_Date_Cost_RAM; Create or ReplaceTrigger Gagdet_Date_Cost_RAM Before Insert or Update or Delete onGadget Begin IF(TO_CHAR(Sysdate, ‘DY’)IN(‘WED’,‘THURS’)) OR (TO_CHAR(Sysdate, ‘HH24’) NOT BETWEEN ‘14’ AND‘18’)Then If DeletingThen Raise_Application_Error(-20502,‘Youmay not delete onWednesday or Thursdayandbetween thehours of 2 and6’); Elsif InsertingThen Raise_Application_Error(-20500, ‘Youmayonlydelete on Wednesday or Thursdayandbetween thehours of 2 and6’); Elsif Updating(‘RAM_Size’) Then Raise_Application_Error(-20503, ‘Youmayonlyupdate onWednesday orThursdayandbetween thehours of 2 and6’); Else Raise_Application_Error(-20503, ‘Youmayonlyupdate from gadget between 8 and6, Monday toFriday’); EndIf; EndIf; End; / Sho Err INVALID INSERT Insert into Internet_Cafe values (196,'Cruise TheNet Cafe','MainStreet','Shercock','Galway','Cruise TheNet Cafe','cruisethenet@hotmail.com',2005,5,'Colddrinks'); Insert into Supplier values (37,'Tech Data Ltd.','Toronto','Canada','www.techdata.com', 2006,'PCs',55000); Insert into Gadget values (256,'Apple','Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,196,37); INVALID DELETE Delete From Gadget Where Gadget_Id=1000;
  • 55. AISLING FALLON-A00203593 INTERNET CAFE Page 54 INVALID UPDATE Update Gadget Set RAM_Size= RAM_Size+2 Where RAM_Size=2;
  • 56. AISLING FALLON-A00203593 INTERNET CAFE Page 55 Question 10-TABLE LEVEL TRIGGER(BEFORE DELETE) Create a Table level trigger on the Café User bookingto ensure you can only deletea bookingbetween 8am and6pm Mondayto Friday. Drop Trigger Cafe_User_Book_Date; Create or ReplaceTrigger Cafe_User_Booking_Date Before Delete onCafe_User_Booking Begin IF(TO_CHAR(Sysdate, ‘DY’)IN(‘SAT’, ‘SUN’)) OR (TO_CHAR(Sysdate,‘HH24’) NOT BETWEEN ‘08’AND ‘18’)Then If DeletingThen Raise_Application_Error(-20502,‘Youmay onlydelete from café user bookingbetween8 and6, MondaytoFriday’); EndIf; EndIf; End; / Sho Err VALID DELETE Delete From Cafe_User_Booking Where Payment_Method=’Cash’; INVALID DELETE You may only delete a café user booking ifit is not Wednesdayor Sunday between 3 pmand6pm. (This trigger was executedon a Wednesday) Drop Trigger Cafe_User_Book_Date; Create or ReplaceTrigger Cafe_User_Booking_Date Before Delete onCafe_User_Booking Begin IF(TO_CHAR(Sysdate, ‘DY’)IN(‘WED’, ‘SUN’)) OR (TO_CHAR(Sysdate, ‘HH24’)NOT BETWEEN ‘15’AND ‘18’)Then If DeletingThen Raise_Application_Error(-20502,‘Youmay onlydeletefromcaféuser bookingif it is not a wedor sun between 3 and 6’ ); EndIf; EndIf; End; / Sho Err
  • 58. AISLING FALLON-A00203593 INTERNET CAFE Page 57 Question 11-PROCEDUREWITHOUTA CURSOR Write a PLSQL procedure that will allow theuser to increase the annual turnover of a supplier, provided theannual turnover is not null, the minimum increase is 500and themaximum is 1500, in cases where these exceptions are not met store in a logtable. Usingprint and variable statements display theannual turnover for the supplier executed. Set Serveroutput On Size 1000000 Drop Procedure Raise_Supplier_Annual_Turn; Drop Table Supplier_Annual_Turnover_Audit; Create Table Supplier_Annual_Turnover_Audit(Supplier_Numb Number(4) Not Null, Supplier_Details Varchar2(50)); Variable A_Turnover Number Print A_Turnover Create or Replace Procedure Raise_Supplier_Annual_Turn(Supp_Num IN Integer, Increase IN Real, Ann_Turnover OUT Number)Is V_Current_Annual_Turnover Number(8); Annual_Turnover_Missing Exception; Increase_Too_Big Exception; Increase_Too_Small Exception; Others Exception; No_Data_Found Exception; Begin Dbms_Output.Put_Line(‘Viewthe data without update’); Select Annual_TurnoverInto V_Current_Annual_Turnover From Supplier Where Supplier_Id=Supp_Num; Dbms_Output.Put_Line(‘The original annual turnover for the supplier is: ‘ ||V_Current_Annual_Turnover); If Increase<=500Then Raise Increase_Too_Small; Endif; If Increase>1500 Then Raise Increase_Too_Big; Endif; If V_Current_Annual_Turnover Is NULLThen Raise Annual_Turnover_Missing; Else Update Supplier Set Annual_Turnover=Annual_Turnover+ Increase Where Supplier_Id=Supp_Num; EndIf; Dbms_Output.Put_Line(‘Viewdata afterchanges’); Select Annual_TurnoverInto V_Current_Annual_Turnover From Supplier Where Supplier_Id=Supp_Num; Dbms_Output.Put_Line(‘The original annual turnover for the supplier is: ‘ ||V_Current_Annual_Turnover); Ann_Turnover:= V_Current_Annual_Turnover; Exception When No_Data_Found Then Insert into Supplier_Annual_Turnover_Audit Values(Supp_Num, ‘Nosuch supplier number’); When Annual_Turnover_Missing Then Insert into Supplier_Annual_Turnover_Audit Values(Supp_Num, ‘Annual Turnover is NULL’); When Increase_Too_Big Then Insert into Supplier_Annual_Turnover_Audit Values(Supp_Num, ‘Excessive annual turnover increase’); When Increase_Too_Small Then Insert into Supplier_Annual_Turnover_Audit Values(Supp_Num, ‘Insufficient annual turnoverincrease’); When Others Then Insert into Supplier_Annual_Turnover_Audit Values(Supp_Num, ‘Unforseen Error’); End Raise_Supplier_Annual_Turn; / Sho Err
  • 59. AISLING FALLON-A00203593 INTERNET CAFE Page 58 VALID INCREASE Execute Raise_Supplier_Annual_Turn(54,600, :A_Turnover); INSUFFICIENTINCREASE Execute Raise_Supplier_Annual_Turn(54,200, :A_Turnover);
  • 60. AISLING FALLON-A00203593 INTERNET CAFE Page 59 EXCESSIVEINCREASE Execute Raise_Supplier_Annual_Turn(54,1900,:A_Turnover); UNFORSEEN ERROR Execute Raise_Supplier_Annual_Turn(94,900, :A_Turnover);
  • 61. AISLING FALLON-A00203593 INTERNET CAFE Page 60 NULL ANNUAL TURNOVER Insert into Supplier values (90,'Tech Data Ltd.','Toronto','Canada','www.techdata.com',2009,'PCs',NULL); Execute Raise_Supplier_Annual_Turn(90,900, :A_Turnover);
  • 63. AISLING FALLON-A00203593 INTERNET CAFE Page 62 Question 12-PROCEDUREWITH IN AND OUTPARAMETERS Create a procedure to give technicians an increasein hourly rate providedthe exceptions are met,otherwise store this information of the technicians (technicianid, details) in a logtable where theamount is too largeor small, where thetechnicianid does not exist or where you may have anerror at runtime, otherwise update the hourly rate where technicians earn €20 anhour.Usiing print statements andvariables display for the updated theincreased hourly rate for the technicianexecutedat runtime. Set Serveroutput On Size 1000000 Variable T_Hourly_Rate Number Print T_Hourly_Rate Drop Procedure Raise_Tech_Hourly_Rate; Drop Table Tech_Hourly_Rate_Audit; Create Table Tech_Hourly_Rate_Audit(Techn_NumbNumber(4)Not Null, Tech_Details Varchar2(50)); Create or Replace Procedure Raise_Tech_Hourly_Rate(Tech_Num IN Integer, Increase IN Real, Total_Hourly_Rate OUT Number) Is V_Current_Hourly_Rate Number(8); Hourly_Rate_Missing Exception; Increase_Too_Big Exception; Increase_Too_Small Exception; Others Exception; No_Data_Found Exception; Begin Dbms_Output.Put_Line(‘Viewthe data without update’); Select Hourly_Rate Into V_Current_Hourly_Rate From Technician Where Technician_Id=Tech_Num; Dbms_Output.Put_Line(‘The original hourlyrate fortechnicians is: ‘ ||V_Current_Hourly_Rate); If Increase<=5 Then Raise Increase_Too_Small; Endif; If Increase>30Then Raise Increase_Too_Big; Endif; If V_Current_Hourly_Rate Is NULL Then Raise Hourly_Rate_Missing; Else Update Technician Set Hourly_Rate= Hourly_Rate+ Increase Where Hourly_Rate=20 And Technician_Id=Tech_Num; EndIf; Dbms_Output.Put_Line(‘Viewdata after changes’); Select Hourly_Rate Into V_Current_Hourly_Rate From Technician Where Technician_Id=Tech_Num; Dbms_Output.Put_Line(‘The adjustedhourly ratefor technicians is: ‘ ||V_Current_Hourly_Rate); Total_Hourly_Rate := V_Current_Hourly_Rate; Exception When No_Data_FoundThen Insert into Tech_Hourly_Rate_Audit Values(Tech_Num,‘Nosuch techniciannumber’); When Hourly_Rate_MissingThen Insert into Tech_Hourly_Rate_Audit Values(Tech_Num,‘Hourly Rate is NULL’); When Increase_Too_BigThen Insert into Tech_Hourly_Rate_Audit Values(Tech_Num,‘Excessive hourly rateincrease’); When Increase_Too_Small Then Insert into Tech_Hourly_Rate_Audit Values(Tech_Num,‘Insufficient hourlyrate increase’); When Others Then Insert into Tech_Hourly_Rate_Audit Values(Tech_Num,‘UnforseenError’); End Raise_Tech_Hourly_Rate; / Sho Err
  • 64. AISLING FALLON-A00203593 INTERNET CAFE Page 63 INVALID UPDATE(HOURLY_RATENOT€20) Insert into Technicianvalues (2350,'Aidan','Cassidy','Market Square','Shercock','Cavan','Certificate','Software',15,'23-Oct-08',086- 4536578); Execute Raise_Tech_Hourly_Rate(2350,25, :T_Hourly_Rate); VALID INCREASE Insert into Technicianvalues (2350,'Aidan','Cassidy','Market Square','Shercock','Cavan','Certificate','Software',20,'23-Oct-08',086- 4536578); Execute Raise_Tech_Hourly_Rate(2350,25, :T_Hourly_Rate); Print T_Hourly_Rate
  • 65. AISLING FALLON-A00203593 INTERNET CAFE Page 64 EXCESSIVEHOURLY RATE Insert into Technicianvalues (2350,'Aidan','Cassidy','Market Square','Shercock','Cavan','Certificate','Software',15,'23-Oct-08',086- 4536578); Execute Raise_Tech_Hourly_Rate(2350,35, :T_Hourly_Rate); INSUFFICIENTHOURLY RATE Insert into Technicianvalues (2950,'Aidan','Cassidy','Market Square','Shercock','Cavan','Certificate','Software',3,'23-Oct-08',086-4536578); Execute Raise_Tech_Hourly_Rate(2950,3,:T_Hourly_Rate); NULL HOURLY RATE Insert into Technicianvalues (2950,'Aidan','Cassidy','Market Square','Shercock','Cavan','Certificate','Software',NULL,'23-Oct-08',086- 4536578); Execute Raise_Tech_Hourly_Rate(2950,15, :T_Hourly_Rate);
  • 66. AISLING FALLON-A00203593 INTERNET CAFE Page 65 UNFORSEEN ERROR Insert into Technicianvalues (2950,'Aidan','Cassidy','Market Square','Shercock','Cavan','Certificate','Software',NULL,'23-Oct-08',086- 4536578); Execute Raise_Tech_Hourly_Rate(2960,15, :T_Hourly_Rate);
  • 68. AISLING FALLON-A00203593 INTERNET CAFE Page 67 Question 13-PROCEDUREWITH CURSOR, LOOP AND IN AND OUTPARAMETERS Write a PLSQL procedure to allow a user to increase theannual turnover of suppliers each time theapplication is run. There is a minimum increase of €5 anda maximum of €30. Thenumber of suppliers youwish to updateis done so at execution, alongwith the amount youwish to increase by. The sum of all annual turnovers is shown before any updatetakes place.The sum of the updated annual turnover alongwith the number of suppliers is displayedusingprint andvariable statements. Set Serveroutput On Size 1000000 Variable S_Annual_Turnover Number Variable T_Suppliers_Updated Number Print S_Annual_Turnover Print T_Suppliers_Updated Drop Table DML_Supplier_Log; Create Table DML_Supplier_Log(Ora_User Varchar2(20), DML_Date Date, Details Varchar(40)); Drop Procedure Annual_Turn_Supplier; Create or ReplaceProcedure Annual_Turn_Supplier(Num_Suppliers IN Number, Increase IN Number,Sum_Annual_Turnover OUT Number, Num_Suppliers_Updated OUT Number) As Cursor SuppcurIs Select Name, Annual_Turnover From Supplier Order By Annual_Turnover For Update of Annual_Turnover; V_Name Supplier.Name%Type; V_Annual_Turnover Supplier.Annual_Turnover%Type; V_Sum_Annual_Turnover Number(9,2); V_Total_Suppliers_Updated Number(3) :=0; Increase_Too_Big Exception; Increase_Too_Small Exception; Begin If Increase<=5 Then Raise Increase_Too_Small; Endif; If Increase>30Then Raise Increase_Too_Big; EndIf; Open Suppcur; Dbms_Output.Put_Line(‘Here are the annual turnoverfigures for suppliers’); Select Sum(Annual_Turnover) Into V_Sum_Annual_Turnover From Supplier; Dbms_Output.Put_Line(‘The sum of the annual turnover is: ‘||V_Sum_Annual_Turnover); For V_Count in 1..Num_Suppliers Loop Fetch Suppcur Into V_Name, V_Annual_Turnover ; Exit WhenSuppcur% NotFound; Dbms_Output.Put_Line(‘The names are: ‘||V_Name); Dbms_Output.Put_Line(‘The annual turnovers are: ‘||V_Annual_Turnover); Dbms_Output.Put_Line(‘****************************************’); Update Supplier Set Annual_Turnover =Annual_Turnover + Increase Where Current Of Suppcur; V_Sum_Annual_Turnover :=V_Sum_Annual_Turnover+ Increase; V_Total_Suppliers_Updated := V_Total_Suppliers_Updated+1; End Loop; Sum_Annual_Turnover :=V_Sum_Annual_Turnover; Num_Suppliers_Updated :=V_Total_Suppliers_Updated; Close Suppcur; Exception When Increase_Too_Big Then Insert into DML_Supplier_LogValues(User, Sysdate,‘Excessive annual turnover increase’); When Increase_Too_Small Then Insert into DML_Supplier_LogValues(User, Sysdate,‘Insufficient annual turnover increase’); End; / Sho Err
  • 69. AISLING FALLON-A00203593 INTERNET CAFE Page 68 VALID UPDATE Execute Annual_Turn_Supplier(3,20, :S_Annual_Turnover, :T_Suppliers_Updated);
  • 70. AISLING FALLON-A00203593 INTERNET CAFE Page 69 EXCESSIVEINCREASE Execute Annual_Turn_Supplier(3,35, :S_Annual_Turnover, :T_Suppliers_Updated); INSUFFICIENTINCREASE Execute Annual_Turn_Supplier(3,2, :S_Annual_Turnover, :T_Suppliers_Updated);
  • 72. AISLING FALLON-A00203593 INTERNET CAFE Page 71 Question 14-PROCEDURE WITH TWO CURSORS, LOOP AND IN AND OUT PARAMTERS Create a procedure to find the highest and lowest rates per hour for internet cafes showing the rate and the name for each of them. Using variables and print statements display the sum of the rates per hour and the number of internet cafes selected. Let the number of internet cafés you wish to see be selected at execution time, and logging if too many or few rows executed in a log table. Set Serveroutput On Size 1000000 Variable S_Rate_Per_Hour Number Variable T_Internet_Cafe_UpdatedNumber Print S_Rate_Per_Hour Print T_Internet_Cafe_Updated Drop Table DML_Int_Cafe_Log; Create Table DML_Int_Cafe_Log(Ora_User Varchar2(20), DML_Date Date, Details Varchar(50)); Drop Procedure Int_Cafe_RatePHr; Create or Replace Procedure Int_Cafe_RatePHr(Num_Internet_C IN Number, Total_Rate_Per_Hour OUT Number, Sum_Internet_Cafe_Updated OUT Number) As Cursor Rate_P_Hour_Min Is Select Name, Rate_Per_Hour From Internet_Cafe Order By Rate_Per_Hour ASC; Cursor Rate_P_Hour_Max Is Select Name, Rate_Per_Hour From Internet_Cafe Order By Rate_Per_Hour DESC; V_Name Internet_Cafe.Name%Type; V_Rate_Per_Hour Internet_Cafe.Rate_Per_Hour%Type; V_Sum_Rate_Per_Hour Number(9,2):=0; V_Total_Internet_Cafe_Updated Number(3) :=0; Too_Few_Rows Exception; Too_many_Rows Exception; Begin If Num_Internet_C <5 Then Raise Too_Few_Rows; End If; If Num_Internet_C >7 Then Raise Too_Many_Rows; End If; Open Rate_P_Hour_Min; For V_Count in 1.. Num_Internet_C Loop Fetch Rate_P_Hour_Min Into V_Name, V_Rate_Per_Hour; Exit When Rate_P_Hour_Min %NotFound; Dbms_Output.Put_Line(‘The names of the internet cafe with min rate per hour is: ‘|| V_Name); Dbms_Output.Put_Line(‘The rate per hour is: ‘|| V_Rate_Per_Hour); Dbms_Output.Put_Line(‘****************************************’); V_Sum_Rate_Per_Hour :=V_Sum_Rate_Per_Hour + V_Rate_Per_Hour; V_Total_Internet_Cafe_Updated :=V_Total_Internet_Cafe_Updated+1; End Loop; Total_Rate_Per_Hour := V_Sum_Rate_Per_Hour; Sum_Internet_Cafe_Updated := V_Total_Internet_Cafe_Updated; Close Rate_P_Hour_Min; Open Rate_P_Hour_Max; For V_Count in 1.. Num_Internet_C Loop Fetch Rate_P_Hour_Max Into V_Name, V_Rate_Per_Hour; Exit When Rate_P_Hour_Max %NotFound; Dbms_Output.Put_Line(‘The name of the internet cafe with the max rate per hour is: ‘|| V_Name); Dbms_Output.Put_Line(‘The rate per hour is: ‘|| V_Rate_Per_Hour); Dbms_Output.Put_Line(‘****************************************’); V_Sum_Rate_Per_Hour :=V_Sum_Rate_Per_Hour + V_Rate_Per_Hour; V_Total_Internet_Cafe_Updated :=V_Total_Internet_Cafe_Updated+1; End Loop; Total_Rate_Per_Hour := V_Sum_Rate_Per_Hour; Sum_Internet_Cafe_Updated := V_Total_Internet_Cafe_Updated; Close Rate_P_Hour_Max; Exception When Too_Few_Rows Then Insert into DML_Int_Cafe_Log Values(User, Sysdate, ‘Not enough rows updated’); When Too_Many_Rows Then Insert into DML_Int_Cafe_Log Values(User, Sysdate, ‘Too many rows updated’); End; / Sho Err
  • 74. AISLING FALLON-A00203593 INTERNET CAFE Page 73 VALID EXECUTION Execute Int_Cafe_RatePHr (6, :S_Rate_Per_Hour, :T_Internet_Cafe_Updated);
  • 75. AISLING FALLON-A00203593 INTERNET CAFE Page 74 TOO FEW ROWS Execute Int_Cafe_RatePHr (3, :S_Rate_Per_Hour, :T_Internet_Cafe_Updated); TOO MANY ROWS Execute Int_Cafe_RatePHr (8, :S_Rate_Per_Hour, :T_Internet_Cafe_Updated);
  • 76. AISLING FALLON-A00203593 INTERNET CAFE Page 75 Question 15-PROCEDUREWITH CURSOR TO DELETE Create a procedure to count the number of suppliers, the max annual turnover andthe sum of the annual turnover for all these suppliers. Starting with the lowest annual turnover delete suppliers until the sum of eachof their annual turnovers exceeds 1200000. For eachsupplier deleted show their name and annual turnover andthe number of rows after deletingat the end. Usinga variable andprint statement display the number of suppliers deleted. Set Serveroutput On Size 1000000 Variable No_Deleted Number Print No_Deleted Drop Table DML_Supplier_Log; Create Table DML_Supplier_Log(Ora_User Varchar2(20), DML_Date Date, Details Varchar(30)); Drop Procedure Annual_Turn_Supplier; Create or ReplaceProcedure Annual_Turn_Supplier(Num_Deleted Out Number) As Cursor SuppcurIs Select Name, Annual_Turnover From Supplier Order By Annual_Turnover For Update of Annual_Turnover; V_Name Supplier.Name%Type; V_Annual_Turnover Supplier.Annual_Turnover%Type; V_Sum_Annual_Turnover Number(9,2); V_Max_Annual_Turnover Number(9) :=0; V_Number_Deleted Number(3) :=0; V_Tot_Annual_Turnover Number(3); Begin Open Suppcur; Select Sum(Annual_Turnover) Into V_Sum_Annual_Turnover From Supplier; Dbms_Output.Put_Line(‘The sum of the annual turnover is: ‘||V_Sum_Annual_Turnover); Select Count(*)into V_Tot_Annual_Turnover From Supplier; Dbms_Output.Put_Line(‘The numberof rows before delete: ‘||V_Tot_Annual_Turnover); V_Max_Annual_Turnover := 1200000; Dbms_Output.Put_Line(‘The maxannual turnover is: ‘||V_Max_Annual_Turnover); WhileV_Sum_Annual_Turnover> V_Max_Annual_Turnover Loop Fetch Suppcur Into V_Name, V_Annual_Turnover; Exit WhenSuppcur% NotFound; Dbms_Output.Put_Line(‘The names are: ‘||V_Name); Dbms_Output.Put_Line(‘The annual turnovers are: ‘||V_Annual_Turnover); Dbms_Output.Put_Line(‘****************************************’); Delete From Supplier Where Current ofSuppcur; V_Sum_Annual_Turnover := V_Sum_Annual_Turnover- V_Annual_Turnover; V_Number_Deleted :=V_Number_Deleted+1; End Loop; Dbms_Output.Put_Line(‘The sum of the annual turnover after delete is: ‘||V_Sum_Annual_Turnover); Select Count(*)into V_Tot_Annual_Turnover From Supplier; Dbms_Output.Put_Line(‘The numberof rows afterdelete: ‘||V_Tot_Annual_Turnover); Dbms_Output.Put_Line(‘The numberof rows deleted: ‘||V_Number_Deleted); Close Suppcur; Num_Deleted:= V_Number_Deleted; End; / Sho Err
  • 77. AISLING FALLON-A00203593 INTERNET CAFE Page 76 Execute Annual_Turn_Supplier(:No_Deleted); Print No_Deleted Rollback;
  • 79. AISLING FALLON-A00203593 INTERNET CAFE Page 78 Question 16-PACKAGEWITH 3 DIFFERENTPROCEDURES Create a packagethat contains procedures whichwill ALLOW: (A) A Gadget_Id and Name be insertedintothe Gadget table whenthe procedure within thepackage is executed. (B) The name of thegadget be updatedwhenthe procedurewithin thepackage is executed. (C) A record to be deleted whenthe Gadget_Idis executed withinthe package. Drop Package Gadget_Pack; Create or ReplacePackage Gadget_PackIs Procedure Add_Gadget (A_Gadget_Idin Gadget.Gadget_Id%Type,A_NameIn Gadget.Name%Type); Procedure Upd_Gadget (A_Gadget_Idin Gadget.Gadget_Id%Type,A_NameIn Gadget.Name%Type); Procedure Del_Gadget (A_Gadget_Idin Gadget.Gadget_Id%Type); End Gadget_Pack; / Sho Err Create or ReplacePackage BODY Gadget_PackIs Procedure Add_Gadget (A_Gadget_Idin Gadget.Gadget_Id%Type,A_NameIn Gadget.Name%Type) Is Begin Insert into Gadget(Gadget_Id, Name) Values(A_Gadget_Id, A_Name); EndAdd_Gadget; Procedure Upd_Gadget (A_Gadget_Idin Gadget.Gadget_Id%Type,A_NameIn Gadget.Name%Type) Is Begin Update Gadget Set Name=A_Name Where Gadget_Id=A_Gadget_Id; If Sql%NotFoundThen Raise_Application_Error(-20203,’Gadget Iddoes not exist, cannot not modify now’); EndIf; EndUpd_Gadget; Procedure Del_Gadget (A_Gadget_Idin Gadget.Gadget_Id%Type)Is Begin Delete From Gadget Where Gadget_Id=A_Gadget_Id; If Sql%NotFoundThen Raise_Application_Error(-20203,’Gadget iddoes not exist so cannot removeit’); EndIf; EndDel_Gadget; End Gadget_Pack; / Sho Err
  • 80. AISLING FALLON-A00203593 INTERNET CAFE Page 79 TEST INSERT Select Gadget_Id, Name From Gadget; Execute Gadget_Pack. Add_Gadget(‘1079’,’Sharp’);
  • 81. AISLING FALLON-A00203593 INTERNET CAFE Page 80 TEST UPDATE Execute Gadget_Pack. Upd_Gadget (‘1010’,’Dell’); TEST DELETE Execute Gadget_Pack. Del_Gadget (‘1010’);
  • 83. AISLING FALLON-A00203593 INTERNET CAFE Page 82 Question 17-PACKAGE WITH THREE DIFFERENT PROCEDURES WITH DIFFERENT TABLES Create a packagewhichwill makethe followingchanges to the necessary tables: (A)Updates the annual turnover ofa supplier, at runtime selectingthe supplier andthe amount ofthe increase, displayingthe amount before and after update. (B)Upodates the hourly rate oftechnicians who are on a rate of€20,at runtime selectingthe technician and the amount ofthe increase, displayingthe amount before and after update. (C)Update the annual turnover ofa supplier by specifyinghow many suppliers at runtime you want to update, also showingthe sum of annual turnovers before any update andthe nameand annual turnover ofthe supplier updated. Set serveroutput onsize 1000000 Drop Package Modify_Pack; Create Or Replace PACKAGE Modify_Pack Is Procedure Raise_Supplier_Annual_Turn(Supp_Num IN Integer, Increase IN Real, Ann_Turnover OUTNumber); Procedure Annual_Turn_Supplier(Num_Suppliers IN Number, Increase IN Number, Sum_Annual_Turnover OUT Number, Num_Suppliers_UpdatedOUT Number); Procedure Raise_Tech_Hourly_Rate(Tech_Num IN Integer, Increase IN Real, Total_Hourly_Rate OUT Number); EndModify_Pack; / Sho Err Create Or Replace Package Body Modify_Pack Is Procedure Raise_Supplier_Annual_Turn(Supp_Num IN Integer, Increase IN Real, Ann_Turnover OUTNumber)Is V_Current_Annual_Turnover Number(8); Begin Dbms_Output.Put_Line(‘Viewthe data without update’); Select Annual_TurnoverInto V_Current_Annual_Turnover From Supplier Where Supplier_Id=Supp_Num; Dbms_Output.Put_Line(‘The original annual turnover for the supplier is: ‘ ||V_Current_Annual_Turnover); Update Supplier Set Annual_Turnover=Annual_Turnover+ Increase Where Supplier_Id=Supp_Num; Dbms_Output.Put_Line(‘Viewdata afterchanges’); Select Annual_TurnoverInto V_Current_Annual_Turnover From Supplier Where Supplier_Id=Supp_Num; Dbms_Output.Put_Line(‘The original annual turnover for the supplier is: ‘ ||V_Current_Annual_Turnover); Ann_Turnover:= V_Current_Annual_Turnover; End Raise_Supplier_Annual_Turn;
  • 84. AISLING FALLON-A00203593 INTERNET CAFE Page 83 Procedure Raise_Tech_Hourly_Rate(Tech_Num IN Integer, Increase IN Real, Total_Hourly_Rate OUT Number)Is V_Current_Hourly_Rate Number(8); Begin Dbms_Output.Put_Line(‘Viewthe data without update’); Select Hourly_Rate Into V_Current_Hourly_Rate From Technician Where Technician_Id=Tech_Num; Dbms_Output.Put_Line(‘The original hourlyrate fortechnicians is: ‘ ||V_Current_Hourly_Rate); Update Technician Set Hourly_Rate= Hourly_Rate+ Increase Where Hourly_Rate=20 And Technician_Id=Tech_Num; Dbms_Output.Put_Line(‘Viewdata afterchanges’); Select Hourly_Rate Into V_Current_Hourly_Rate From Technician Where Technician_Id=Tech_Num; Dbms_Output.Put_Line(‘The adjustedhourly ratefor technicians is: ‘ ||V_Current_Hourly_Rate); End Raise_Tech_Hourly_Rate; Procedure Annual_Turn_Supplier(Num_Suppliers IN Number, Increase IN Number, Sum_Annual_Turnover OUT Number, Num_Suppliers_Updated OUT Number)As Cursor SuppcurIs Select Name, Annual_Turnover From Supplier Order By Annual_Turnover For Update of Annual_Turnover; V_Name Supplier.Name%Type; V_Annual_Turnover Supplier.Annual_Turnover%Type; V_Sum_Annual_Turnover Number(9,2); V_Total_Suppliers_Updated Number(3) :=0; Increase_Too_Big Exception; Increase_Too_Small Exception; Begin Open Suppcur; Dbms_Output.Put_Line(‘Here are the annual turnoverfigures for suppliers’); Select Sum(Annual_Turnover) Into V_Sum_Annual_Turnover From Supplier; Dbms_Output.Put_Line(‘The sum of the annual turnover is: ‘||V_Sum_Annual_Turnover); For V_Count in 1..Num_Suppliers Loop Fetch Suppcur Into V_Name, V_Annual_Turnover ; Exit WhenSuppcur% NotFound; Dbms_Output.Put_Line(‘The names are: ‘||V_Name); Dbms_Output.Put_Line(‘The annual turnovers are: ‘||V_Annual_Turnover); Dbms_Output.Put_Line(‘****************************************’); Update Supplier Set Annual_Turnover =Annual_Turnover + Increase Where Current Of Suppcur; V_Sum_Annual_Turnover :=V_Sum_Annual_Turnover+ Increase; V_Total_Suppliers_Updated := V_Total_Suppliers_Updated+1; End Loop; Sum_Annual_Turnover :=V_Sum_Annual_Turnover; Num_Suppliers_Updated :=V_Total_Suppliers_Updated; Close Suppcur; End Annual_Turn_Supplier; EndModify_Pack; / Sho Err
  • 85. AISLING FALLON-A00203593 INTERNET CAFE Page 84 Variable A_Turnover Number Print A_Turnover Variable T_Hourly_Rate Number Print T_Hourly_Rate Variable S_Annual_Turnover Number Variable T_Suppliers_Updated Number Print S_Annual_Turnover Print T_Suppliers_Updated Procedure Raise_Supplier_Annual_Turn Execute Modify_Pack.Raise_Supplier_Annual_Turn(54,600, :A_Turnover); Insertinto Supplier values (54,'Tech Data Ltd.','Toronto','Canada','www.techdata.com',2009,'PCs',59000); Procedure Annual_Turn_Supplier Insert into Technicianvalues (2350,'Aidan','Cassidy','Market Square','Shercock','Cavan','Certificate','Software',20,'23-Oct-08',086- 4536578); Execute Modify_Pack.Raise_Tech_Hourly_Rate(2350,25,:T_Hourly_Rate); Procedure Raise_Tech_Hourly_Rate Execute Modify_Pack.Annual_Turn_Supplier(3,2, :S_Annual_Turnover,:T_Suppliers_Updated);
  • 87. AISLING FALLON-A00203593 INTERNET CAFE Page 86 Question 18-CALLING PROCEDURE FROM A TRIGGER Create a procedure which will be called from a Trigger. The procedure should: (A) Increase annual turnover for suppliers by 10% where the type ofGadget is a Tablet andthe nameis Apple, otherwise increase the annual turnover by 20% ifnot namedApple. (B) Decrease annual turnover for suppliers by 10% where the type is Laptopand the name is Dell, otherwise decrease the annual turnover by 20% ifnot namedDell. Set Serveroutput On Size 1000000 Drop Procedure Raise_Supplier_Annual_Turn; Create or Replace Procedure Raise_Supp_A_Turn(Supp_Num IN Number, Increase IN Number) Is V_Current_Annual_Turnover Number(8); Begin Dbms_Output.Put_Line(‘Viewthe data without update’); Select Annual_TurnoverInto V_Current_Annual_Turnover From Supplier Where Supplier_Id=Supp_Num; Dbms_Output.Put_Line(‘The original annual turnover for the supplier is: ‘ ||V_Current_Annual_Turnover); Update Supplier Set Annual_Turnover=Annual_Turnover+ Increase Where Supplier_Id=Supp_Num; Dbms_Output.Put_Line(‘Viewdata after changes’); Select Annual_TurnoverInto V_Current_Annual_Turnover From Supplier Where Supplier_Id=Supp_Num; Dbms_Output.Put_Line(‘The updatedannual turnoverforthe supplieris: ‘ ||V_Current_Annual_Turnover); End; / Sho Err Create or Replace Procedure Decrease_Supp_A_Turn(Supp_Num IN Number, Increase IN Number) Is V_Current_Annual_Turnover Number(8); Begin Dbms_Output.Put_Line(‘Viewthe data without update’); Select Annual_TurnoverInto V_Current_Annual_Turnover From Supplier Where Supplier_Id=Supp_Num; Dbms_Output.Put_Line(‘The original annual turnover for the supplier is: ‘ ||V_Current_Annual_Turnover); Update Supplier Set Annual_Turnover=Annual_Turnover - Increase Where Supplier_Id=Supp_Num; Dbms_Output.Put_Line(‘Viewdata afterchanges’); Select Annual_TurnoverInto V_Current_Annual_Turnover From Supplier Where Supplier_Id=Supp_Num; Dbms_Output.Put_Line(‘The updatedannual turnoverforthe supplieris: ‘ || V_Current_Annual_Turnover); End; / Sho Err
  • 88. AISLING FALLON-A00203593 INTERNET CAFE Page 87 Drop Trigger Modify_A_Turnover_Supp; Create or ReplaceTrigger Modify_A_Turnover_Supp After Update onGadget For Each Row Declare V_Annual_Turnover Supplier.Annual_Turnover%Type; Begin Select Annual_TurnoverInto V_Annual_Turnover From Supplier Where Supplier.Supplier_Id= :New.Supplier_Id; If :New.Type=’Tablet’Then If :New.Name=’Apple’ Then Raise_Supp_A_Turn(:New.Supplier_Id, V_Annual_Turnover*0.10); Else Raise_Supp_A_Turn(:New.Supplier_Id, V_Annual_Turnover*0.20); End If; End If; If :New.Type=’Laptop’Then If :New.Name=’Dell’ Then Decrease_Supp_A_Turn(:New.Supplier_Id, V_Annual_Turnover*0.10); Else Decrease_Supp_A_Turn(:New.Supplier_Id, V_Annual_Turnover*0.20); End If; End If; End; / Sho Err
  • 89. AISLING FALLON-A00203593 INTERNET CAFE Page 88 (A) Increase annual turnover for suppliers by 10% where the type ofGadget is Tablet andthe nameis Apple, otherwise increase the annual turnover by 20% ifnot namedApple. Insert into Supplier values (98,'Tech Data Ltd.','Toronto','Ireland','www.techdata.com',2009,'Tablets',5000); Insert into Gadget values (1326,'Apple’,’Tablet',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,196,98); Select Annual_Turnover From Supplier Where Supplier_Id=98; Update Gadget Set Cost_Price=60 Where Supplier_Id=98; Select Annual_Turnover From Supplier Where Supplier_Id=98; Insert into Supplier values (98,'Tech Data Ltd.','Toronto','Ireland','www.techdata.com',2009,'Tablets',5000); Insert into Gadget values (1326,'Dell’,’Tablet',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,196,98); Select Annual_Turnover From Supplier Where Supplier_Id=98; Update Gadget Set Cost_Price=60 Where Supplier_Id=98; Select Annual_Turnover From Supplier Where Supplier_Id=98;
  • 90. AISLING FALLON-A00203593 INTERNET CAFE Page 89 (B) Decrease annual turnover for suppliers by 10% where the type is Laptopand the name is Dell, otherwise decrease the annual turnover by 20% ifnot namedDell. Insert into Supplier values (98,'Tech Data Ltd.','Toronto','Ireland','www.techdata.com',2009,'Tablets',5000); Insert into Gadget values (1326,'Dell’,’Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,196,98); Select Annual_Turnover From Supplier Where Supplier_Id=98; Update Gadget Set Cost_Price=60 Where Supplier_Id=98; Select Annual_Turnover From Supplier Where Supplier_Id=98; Insert into Supplier values (98,'Tech Data Ltd.','Toronto','Ireland','www.techdata.com',2009,'Tablets',5000); Insert into Gadget values (1326,'Asus’,’Laptop',17.3,3.5,2,'Linux','Yes','No','13-Nov-10',650,36,550,196,98); Select Annual_Turnover From Supplier Where Supplier_Id=98; Update Gadget Set Cost_Price=60 Where Supplier_Id=98; Select Annual_Turnover From Supplier Where Supplier_Id=98;
  • 94. AISLING FALLON-A00203593 INTERNET CAFE Page 93 Question 19-TRIGGER USING A CURSOR Create a trigger that uses a cursor to give thetwo lowest paidtechnicians an increaseof €10whenthe details of gadget repair are updated. Set Serverout ON Size 1000000 Drop TriggerGadget_Rep_Tech_Gad; Create or Replace Trigger Gadget_Rep_Tech_Gad After Update of Details on Gadget_Repair For Each Row Declare Cursor GadCur Is Select FName, Hourly_Rate From Technician Where Technician_Id=:New.Technician_Id Order By Hourly_RateASC For Update Of Hourly_Rate; V_Hourly_Rate Technician.Hourly_Rate%Type; V_Fname Technician.FName%Type; V_Total_Hourly_Rate_Before Number(9,2); V_Total_Hourly_Rate_After Number(9,2); Begin Open GadCur; DBMS_Output.Put_Line(‘Total hourlyrate before increase is: ‘); Select Sum(Hourly_Rate) Into V_Total_Hourly_Rate_Before From TechnicianT Where Technician_Id=:New.Technician_Id; DBMS_Output.Put_Line(‘Sum of hourly ratebefore‘ || V_Total_Hourly_Rate_Before); DBMS_Output.Put_Line(‘***********************************************************’); For V_Count In 1..2 Loop Fetch GadCur Into V_Fname,V_Hourly_Rate; Exit WhenGadCur%NotFound; DBMS_Output.Put_Line(‘Technician Name is: ‘ || V_FName); DBMS_Output.Put_Line(‘VTechnician Hourly rate is: ‘ || V_Hourly_Rate); Update Technician Set Hourly_Rate=Hourly_Rate+10 Where Current of Gadcur; End Loop; Close GadCur; Select Sum (Hourly_Rate)Into V_Total_Hourly_Rate_After From TechnicianT Where Technician_Id=:New.Technician_Id; DBMS_Output.Put_Line(‘Sum of hourly ratebefore‘ || V_Total_Hourly_Rate_After); DBMS_Output.Put_Line(‘***********************************************************’); End; / Sho err
  • 96. AISLING FALLON-A00203593 INTERNET CAFE Page 95 Select Sum(Hourly_Rate) From Technician; Update Gadget_Repair Set Details=’Fix Monitor’ Where Technician_Id=2000; Select Sum(Hourly_Rate) From Technician;