0% found this document useful (0 votes)
2K views3 pages

Ass 9

The document describes SQL triggers to automatically update database tables when new rows are inserted. It includes triggers to: 1) Calculate the line total when a new line is added 2) Update product quantities when a new line is added 3) Update aircraft flight hours when a new charter is added 4) Update pilot flight hours when a new crew is added for a pilot 5) Update customer balance when a new charter is added

Uploaded by

Mai Duy Khánh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views3 pages

Ass 9

The document describes SQL triggers to automatically update database tables when new rows are inserted. It includes triggers to: 1) Calculate the line total when a new line is added 2) Update product quantities when a new line is added 3) Update aircraft flight hours when a new charter is added 4) Update pilot flight hours when a new crew is added for a pilot 5) Update customer balance when a new charter is added

Uploaded by

Mai Duy Khánh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

ASSIGMENT 9

Mai Duy Khánh


ICE2017A
BDSM
Problem 28,29, 41,42,43 Chapter 8

28. Create a trigger named trg_line_total to write the LINE_TOTAL value in the
LINE table every time you add a new LINE row. (The LINE_TOTAL value is the
product of the LINE_UNITS and LINE_PRICE values.)
CREATE OR REPLACE TRIGGER TRG_LINE_TOTAL
BEFORE INSERT ON LINE
FOR EACH ROW
BEGIN
:NEW.LINE_TOTAL := :NEW.LINE_UNITS * :NEW.LINE_PRICE;
END;
29. Create a trigger named trg_line_prod that will automatically update the
product quantity on hand for each product sold after a new LINE row is added.
CREATE OR REPLACE TRIGGER TRG_LINE_PROD
AFTER INSERT ON LINE
FOR EACH ROW
BEGIN
UPDATE PRODUCT
SET P_QOH = P_QOH - :NEW.LINE_UNITS
WHERE PRODUCT.P_CODE = :NEW.P_CODE;
END;
41. Create a trigger named trg_char_hours that will automatically update the
AIRCRAFT table when a new CHARTER row is added. Use the CHARTER table’s
CHAR_HOURS_FLOWN to update the AIRCRAFT table’s AC_TTAF, AC_TTEL,
and AC_TTER values.
CREATE OR REPLACE TRIGGER TRG_CHAR_HOURS
AFTER INSERT ON CHARTER
FOR EACH ROW
BEGIN
UPDATE AIRCRAFT
SET
AC_TTAF = AC_TTAF + :NEW.CHAR_HOURS_FLOWN,
AC_TTEL = AC_TTEL + :NEW.CHAR_HOURS_FLOWN,
AC_TTER = AC_TTER + :NEW.CHAR_HOURS_FLOWN
WHERE AIRCRAFT.AC_NUMBER = :NEW.AC_NUMBER;
END;

42. Create a trigger named trg_pic_hoursthat will automatically update the


PILOT table when a new CREW row is added and the CREW table uses a ‘pilot’
CREW_JOB entry. Use the CHARTER table’s CHAR_HOURS_FLOWN to update
the PILOT table’s PIL_PIC_HRS only when the CREW table uses a ‘pilot’
CREW_JOB entry.
CREATE OR REPLACE TRIGGER TRG_PIC_HOURS
AFTER INSERT ON CREW
FOR EACH ROW
BEGIN
IF :NEW.CREW_JOB = 'Pilot' THEN
UPDATE PILOT
SET PIL_PIC_HRS = PIL_PIC_HRS +
(SELECT CHAR_HOURS_FLOWN FROM CHARTER
WHERE CHAR_TRIP = :NEW.CHAR_TRIP)
WHERE EMP_NUM = :NEW.EMP_NUM;
END IF;
END;

43. Create a trigger named trg_cust_balancethat will automatically update


the CUSTOMER table’s CUST_BALANCE when a new CHARTER row is added.
Use the CHARTER table’s CHAR_TOT_CHG as the update source (Assume
that all charter charges are charged to the customer balance.)
CREATE OR REPLACE TRIGGER TRG_CUST_BALANCE
AFTER INSERT ON CHARTER
FOR EACH ROW
BEGIN
UPDATE CUSTOMER
SET CUS_BALANCE = CUS_BALANCE + :NEW.CHAR_TOT_CHG
WHERE CUSTOMER.CUS_CODE = :NEW.CUS_CODE;
END;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy