0% found this document useful (0 votes)
31 views6 pages

Assignment 7

The document discusses creating triggers in PL/SQL. It provides examples of creating row-level and statement-level triggers for auditing updates and deletes from a table. It also shows examples of creating triggers to reject inserts or updates with salaries less than Rs. 50,000 and to calculate averages.

Uploaded by

wacih58121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views6 pages

Assignment 7

The document discusses creating triggers in PL/SQL. It provides examples of creating row-level and statement-level triggers for auditing updates and deletes from a table. It also shows examples of creating triggers to reject inserts or updates with salaries less than Rs. 50,000 and to calculate averages.

Uploaded by

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

Assignment 7

Title : PL/SQL code block using Trigger Trigger (All Types: Rowlevel and Statement
level
triggers, Before and After Triggers).

1. Write a update, delete trigger on clientmstr table. The System should keep track
of the records
that ARE BEING updated or deleted. The old value of updated or deleted records
should be added
in audit_trade table. (separate implementation using both row and statement
triggers)

CREATE TABLE clientmstr(


id int primary key,
name varchar(10),
amt int
);
.

select * from clientmstr;

ID NAME AMT
---------- ---------- ----------
255 SUJIT 255
258 OMKAR 564
256 SANKENT 545
249 KRUSHANA 979
251 SAMEER 479

CREATE TABLE audit_trade255(


time TIMESTAMP,
id INT,
oldAmt INT,
action VARCHAR2(10)
);

CREATE OR REPLACE TRIGGER auditTrigger


AFTER UPDATE OR DELETE ON clientmstr
FOR EACH ROW
DECLARE
BEGIN
IF UPDATING THEN
INSERT INTO audit_trade255 (time, id, oldAmt, action) VALUES
(SYSTIMESTAMP, :OLD.id, :OLD.amt, 'UPDATING');
ELSE
INSERT INTO audit_trade255 (time, id, oldAmt, action) VALUES
(SYSTIMESTAMP, :OLD.id, :OLD.amt, 'DELETING');
END IF;
END;
/

update clientmstr set amt = 1000 where id = 255;


1 row updated.

select * from audit_trade255;

TIME
---------------------------------------------------------------------------
ID OLDAMT ACTION
---------- ---------- ----------
26-MAR-24 11.59.08.068000 AM
255 255 UPDATING

update clientmstr set amt = 1125 where id = 256;

1 row updated.

delete from clientmstr;

5 rows deleted.

select * from audit_trade255;

TIME
---------------------------------------------------------------------------
ID OLDAMT ACTION
---------- ---------- ----------
26-MAR-24 11.59.08.068000 AM
255 255 UPDATING

26-MAR-24 12.00.32.487000 PM
256 545 UPDATING

26-MAR-24 12.01.03.276000 PM
255 1000 DELETING

TIME
---------------------------------------------------------------------------
ID OLDAMT ACTION
---------- ---------- ----------
26-MAR-24 12.01.03.276000 PM
258 564 DELETING

26-MAR-24 12.01.03.276000 PM
256 1125 DELETING

26-MAR-24 12.01.03.276000 PM
249 979 DELETING

TIME
---------------------------------------------------------------------------
ID OLDAMT ACTION
---------- ---------- ----------
26-MAR-24 12.01.03.276000 PM
251 479 DELETING

7 rows selected.
—----------------------------------------------------------------------------------
----------------------------------
STATMENT TRIGGER
CREATE OR REPLACE TRIGGER auditTriggerstat
AFTER UPDATE OR DELETE ON clientmstr255
DECLARE
BEGIN
IF UPDATING THEN
INSERT INTO audit255 VALUES ('UPDATING');
ELSE
INSERT INTO audit255 VALUES ('DELETING');
END IF;
END;
/

Trigger created.

SQL> update clientmstr set amt = 0;

5 rows updated.

SQL> delete from clientmstr;

5 rows deleted.

SQL> select * from audit255;

ACTION
----------
UPDATING
DELETING

SQL>

2. Write a before trigger for Insert, update event considering following


requirement:
Emp(e_no, e_name, salary)

I) Trigger action should be initiated when salary is tried to be inserted is less


than Rs. 50,000/-
II) Trigger action should be initiated when salary is tried to be updated for value
less than Rs.
50,000/-
Action should be rejection of update or Insert operation by displaying appropriate
error message.
Also the new values expected to be inserted will be stored in new table
Tracking(e_no, salary).

CREATE TABLE Emp255 (


e_no INT PRIMARY KEY,
e_name VARCHAR(50),
salary INT
);

CREATE TABLE Tracking255 (


e_no INT,
salary INT
);

Table created.

CREATE OR REPLACE TRIGGER emp_salary


BEFORE UPDATE OR INSERT ON Emp255
FOR EACH ROW
BEGIN
IF :NEW.salary < 50000 THEN
raise_application_error(-20003, 'Salary should be greater than 50000');
END IF;

INSERT INTO Tracking255 (e_no, salary)


VALUES (:NEW.e_no, :NEW.salary);
END;
/

Trigger created.

INSERT INTO EMP255 (e_no, e_name, salary) VALUES (251, 'SAMEER', 32001);
INSERT INTO EMP255 (e_no, e_name, salary) VALUES (251, 'SAMEER', 32001)
*
ERROR at line 1:
ORA-20003: Salary should be greater than 50000
ORA-06512: at "SYSTEM.EMP_SALARY", line 3
ORA-04088: error during execution of trigger 'SYSTEM.EMP_SALARY'

INSERT INTO EMP255 (e_no, e_name, salary) VALUES (255, 'SUJIT', 210005);

1 row created.

UPDATE EMP255 SET SALARY = 40000 WHERE e_no = 255;


UPDATE EMP255 SET SALARY = 40000 WHERE e_no = 255
*
ERROR at line 1:
ORA-20003: Salary should be greater than 50000
ORA-06512: at "SYSTEM.EMP_SALARY", line 3
ORA-04088: error during execution of trigger 'SYSTEM.EMP_SALARY'

SQL> UPDATE EMP255 SET SALARY = 400000 WHERE e_no = 255;

1 row updated.

select * from Tracking255;


E_NO SALARY
---------- ----------
255 210005
255 400000

3. Write a Database trigger for following requirements:


Employee salary of last three month is stored in the emp_sal table.
emp_sal(emp_no, sal1,sal2,sal3)
before inserting salary into emp_sal table, if salary of employee in any of the
last three month is
greater than Rs. 50,000/- then entry of average salary along with emp_no needs to
be inserted into
new table emp_new(emp_no, avg_sal).

CREATE TABLE emp_sal255 (


emp_no INT,
sal1 INT,
sal2 INT,
sal3 INT
);

CREATE TABLE emp_new (


emp_no INT,
avg_sal INT
);

SELECT * FROM emp_sal_b255;


no rows selected
SELECT * FROM emp_avg_b255;
no rows selected

CREATE OR REPLACE TRIGGER emp_sal_track


BEFORE INSERT
ON emp_sal_b255
FOR EACH ROW

BEGIN
IF (:new.sal_one > 50000 OR :new.sal_two > 50000 OR :new.sal_three > 50000) THEN
INSERT INTO emp_avg_b255 VALUES(:new.e_no,((:new.sal_one + :new.sal_two +
:new.sal_three)/3));
DBMS_OUTPUT.PUT_LINE('Record added to emp_sal and emp_avg successfully');
ELSE
DBMS_OUTPUT.PUT_LINE('Record added to emp_sal successfully');
END IF;
END;
/
Trigger created.

INSERT INTO emp_sal_b255 VALUES(255,100000,100000,100000);


Record added to emp_sal and emp_avg successfully
1 row created.
INSERT INTO emp_sal_b255 VALUES(258,100,100,100);
Record added to emp_sal successfully
1 row created.

SELECT * FROM emp_sal_b255;


E_NO SAL_ONE SAL_TWO SAL_THREE
---------- ---------- ---------- ----------
255 100000 100000 100000
258 100 100 100
SELECT * FROM emp_avg_b255;
E_NO AVG_SAL
---------- ----------
255 100000

You might also like