728 Assignment 05
728 Assignment 05
728 Assignment 05
NAME-ROHIT KUMAR
REG.NO.- 728
ROLL NO.-CSE/21068
SUBJECT CODE- CSC613
EMAIL:- cse21068@iiitkalyani.ac.in
Prob 1:
schemas:
person (SSN, Name, Department)
works_under (SSN, MGR_SSN)
wants_to_work_under (SSN, MGR_SSN)
Tables:
Queries:
a) Write trigger(s) to check if (A, B) is there in works_under then (A, C) cannot be the added to
DECLARE
mgr_count NUMBER;
BEGIN
SELECT COUNT(*) INTO mgr_count FROM works_under WHERE SSN = :new.SSN;
IF mgr_count > 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'One employee can work under one manager only');
END IF;
END;
/
INSERT INTO works_under VALUES (10005,10006);
Result:-
b) Write a trigger that automatically changes the department name of A by the name of the department
END;
/
INSERT INTO works_under VALUES (10007,10003);
SELECT * FROM person;
SELECT * FROM works_under;
Result:-
c) Write a trigger to enforce the following: Assume that currently A’s manager is C. If A wants to work
under B and (A, B) is inserted to wants_to_work_under then immediately (A, C) will be deleted from
works_under. Department names will be updated accordingly.
CREATE OR REPLACE TRIGGER trigger3
BEFORE INSERT ON wants_to_work_under
FOR EACH ROW
DECLARE
old_mgr_ssn NUMBER;
BEGIN
SELECT MGR_SSN INTO old_mgr_ssn FROM works_under WHERE SSN = :new.SSN;
IF old_mgr_ssn IS NOT NULL AND old_mgr_ssn <> :new.MGR_SSN THEN
END;
/
Result:-
Prob 2:
schemas:
CUSTOMER (SSN, Name, Surname, PhoneNum, Plan_Code)
PLAN (Plan_Code, ConnectionFee, PricePerSecond ) /* The connection fee is onetime payable when
the customer is registered. */
PHONE_CALL (SSN, Date, Time, CalledNum, Seconds)
BILL (SSN, Month, Year, amount ) /* Monthly bills */
Tables:-
Queries :
a) Write a trigger to update the customer's bill after each phone call. Assume that every customer has
a bill amount at any point of time. You can create another trigger that creates a bill with Rs 0.0 for
initialization for each registered customer. This can happen when the customer's details are entered
into the customer table OR for existing customers when the month begins, i.e., on the first day of
every month at 12:01 AM.
b) Write a trigger so that when a customer’s plan is changed (i.e., edited Plan_Code in the Customer
table), it should create a message showing the extra cost to be added in the bill against the
ConnectionFee and the current bill should be displayed.
CREATE OR REPLACE TRIGGER plan_change
AFTER UPDATE OF Plan_Code ON CUSTOMER
FOR EACH ROW
DECLARE
extra_cost NUMBER(10,2);
current_bill NUMBER(10,2);
BEGIN
IF :new.Plan_Code <> :old.Plan_Code THEN
UPDATE BILL
SET Amount = Amount + extra_cost
WHERE SSN = :new.SSN AND Month = EXTRACT(MONTH FROM SYSDATE) AND Year =
EXTRACT(YEAR FROM SYSDATE);
SELECT Amount INTO current_bill FROM BILL WHERE SSN = :new.SSN AND Month =
EXTRACT(MONTH FROM SYSDATE) AND Year = EXTRACT(YEAR FROM SYSDATE);
Result:-
c) Write a trigger to check the bill amount after every call. If the amount crosses Rs. 2000 then
PricePerSecond drops by 50% in the same plan for that customer.
ELSE
DBMS_OUTPUT.PUT_LINE('CURRENT BILL AMT LESS THAN 2000');
UPDATE BILL
SET Amount = Amount + (SELECT :new.Seconds*p.PricePerSecond FROM PLAN p,CUSTOMER c
WHERE c.SSN = :new.SSN AND p.Plan_Code = c.Plan_Code)
WHERE SSN = :new.SSN AND Month = EXTRACT(MONTH FROM SYSDATE) AND Year =
EXTRACT(YEAR FROM SYSDATE);
END IF;
END;
/
INSERT INTO PHONE_CALL VALUES('106', TO_DATE('2023-03-30', 'YYYY-MM-DD'),
TO_TIMESTAMP('2022-03-30 12:30:00', 'YYYY-MM-DD HH24:MI:SS'), '55-568', 100);
SELECT * FROM BILL;
Result:-
d) Write a trigger to check: Assume that five-digit phone numbers are being used, and the first two
digits will identify the service provider. If the customer calls the numbers of the same company, i.e., if
the first two digits of PhoneNum are the same as the first two digits of CalledNum then the
PricePerSecond will be applicable with a 50% discount.
Result:-