Professional Documents
Culture Documents
Trigger
Trigger
Aim:
To implement trigger concept in student database in insertion, updation and deletion
Triggers
BEFORE INSERT:
BEFORE DELETE
Output
After Insert
BEGIN
dbms_output.put_line('Student is male');
ELSE
dbms_output.put_line('Student is female');
END IF;
END;
Output
After update
Output
After delete
CREATE TRIGGER d1
AFTER DELETE
ON employee
BEGIN
dbms_output.put_line('Trigger is activated');
END;
Output
QUERIES:
1. Write a code in PL/SQL to create a trigger that restricts the insertion of new rows if the
total of column value exceed a certain threshold.
BEFORE INSERT
ON studentmaster1
DECLARE
count1 int(10);
BEGIN
END IF;
END;
OUTPUT:
CREATE TRIGGER u
BEFORE INSERT
ON Employeemaster
FOR EACH ROW
DECLARE
total_rows NUMBER;
BEGIN
SELECT COUNT(*) INTO total_rows FROM Employeemaster;
dbms_output.put_line('New rows added. Total rows: ' || TO_CHAR(total_rows + 1));
END;
/
insert into employeemaster3 values(1001,'yeager','29-feb-
2000','maths','regular',21,'shioganshina',9734543,'b-ve');
OUTPUT:
3. Write a pl/sql code to create a trigger that automatically updates the last modified time whenever a
new row is updated
4. Write a code in pl/sql to create a trigger that check for duplicate values in a specific
column and raises an exception if found
Output:
5. Write a code in pl/sql to create a trigger that prevents update on a certain column during
specific hour of the day
Output: