Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

TRIGGERS

1)To write a trigger to ensure that department table does not contain duplicate of null values in deptno column.

SQL> CREATE table Department(Name varchar(20),salary int,deptno int);


Table created.

SQL> CREATE OR REPLACE TRIGGER PreventDuplicateNulls


2 BEFORE INSERT OR UPDATE ON Department
3 FOR EACH ROW
4 DECLARE
5 v_count NUMBER;
6 BEGIN
7 SELECT COUNT(*)
8 INTO v_count
9 FROM Department
10 WHERE deptno IS NULL;
11 IF v_count > 1 THEN
12 dbms_output.put_line( 'Cannot insert or update: Duplicate null values in deptno column');
13 END IF;
14 END;
15 /

Trigger created.

2) Write a trigger to allow operation on student table on Sunday

SQL> CREATE table Student(Name varchar(20),Day varchar(20),dept varchar(20));

Table created.

SQL> insert into Student values('vijay','sunday','cse');


1 row created.

SQL> CREATE OR REPLACE TRIGGER Student_after_delete


2 BEFORE DELETE ON Student
3 FOR EACH ROW
4 DECLARE
5 v_count NUMBER;
6 BEGIN
7 SELECT COUNT(*) INTO v_count
8 FROM Student
9 WHERE day = 'sunday';
10 IF v_count > 0 THEN
11 DBMS_OUTPUT.PUT_LINE('SUCCESSFULLY TRIGGERED');
12 END IF;
13 END;
14 /

Trigger created.
3) Write a set of triggers to illustrate cascading of trigger
SQL> create table sports(
2 sportcode varchar(30));

Table created.

SQL> insert into sports values(‘7’);

1 row created.
SQL> CREATE OR REPLACE TRIGGER Sportcode_Upd_Cas
2 BEFORE UPDATE OF Sportcode ON sports
3 FOR EACH ROW
4 BEGIN
5 UPDATE sports
6 SET Sportcode =:new.Sportcode
7 WHERE Sportcode =:old.Sportcode;
8 DBMS_OUTPUT.PUT_LINE(‘Corresponding Sportcode in the Sports table has also been
9 updated’);
10 END;
11 /

Trigger created.

4)Write a database trigger BEFORE DELETE FOR EACH ROW now allowing
deletions and give appropriate message on the instructor a table

SQL> CREATE OR REPLACE TRIGGER Department_before_delete


2 BEFORE DELETE ON Department
3 FOR EACH ROW
4 DECLARE
5 BEGIN
6 DBMS_OUTPUT.PUT_LINE('DELETED BEFORE SUCCESSFULLY');
7 END;
8 /

Trigger created.

5).Write a database trigger AFTER DELETE FOR EACH ROW now allowing
deletions and give appropriate message on the instructor a table

SQL> CREATE OR REPLACE TRIGGER Department_after_delete


2 AFTER DELETE ON Department
3 FOR EACH ROW
4 DECLARE
5 BEGIN
6 DBMS_OUTPUT.PUT_LINE('DELETED AFTER SUCCESSFULLY');
7 END;
8 /

Trigger created.
6)Write a database trigger BEFORE INSERT/UPDATE/DELETE FOR EACH ROW
now allowing any of these operations on the student table during the month of
August/September

SQL> CREATE table Student1(Name varchar(20),Month varchar(20),dept varchar(20));

Table created.

SQL> insert into Student1 values('deva','August','cse');

1 row created.

SQL> insert into Student1 values('dharan','november','cse');

1 row created.

SQL> insert into Student1 values('ramesh','december','cse');

1 row created.

SQL> CREATE OR REPLACE TRIGGER student1_restrict_operations


2 BEFORE INSERT OR UPDATE OR DELETE ON student1
3 FOR EACH ROW
4 DECLARE
5 current_month VARCHAR(20);
6 BEGIN
7 current_month := TO_CHAR(SYSDATE, 'Month');
8 IF current_month IN ('August', 'September') THEN
9 dbms_output.put_line('Operation not allowed during August and September');
10 END IF;
11 END;
12 /

Trigger created.

7)Create a row level trigger for the customers table that would fire for INSERT or UPDATE or DELETE operations
performed on the CUSTOMERS table. This trigger will display the salary difference between the old values and new
values:

SQL> create table customers(


2 id int,
3 name varchar(25),
4 age int,
5 address varchar(25),
6 salary int);

Table created.

SQL> insert into customers values(1,'alive',24,'khammam',2000);

1 row created.
SQL> insert into customers values(2,'bob',27,'kadappa',3000);

1 row created.

SQL> insert into customers values(3,'catri',25,'guntur',4000);

1 row created.
SQL> insert into customers values(4,'dena',28,'hyderabad',5000);

1 row created.

SQL> insert into customers values(5,'Eeshwar',27,'kurnnol',6000);


1 row created.

SQL> insert into customers values(6,'farooq',28,'nellur',7000);

1 row created.

SQL> CREATE OR REPLACE TRIGGER display_salary_changes


2 BEFORE DELETE OR INSERT OR UPDATE ON customers
3 FOR EACH ROW
4 WHEN (new.id > 0)
5 DECLARE
6 sal_diff NUMBER;
7 BEGIN
8 sal_diff := :new.salary - :old.salary;
9 dbms_output.put_line('old salary: ' || :old.salary);
10 dbms_output.put_line('new salary: ' || :new.salary);
11 dbms_output.put_line('salary difference: ' || sal_diff);
12 END;
13 /

Trigger created.

8) Convert employee name into uppercase whenever an employee record is inserted or updated. Trigger to fire before
the insert or update.

SQL> create table employee1(


2 id varchar2(4 byte)not null,
3 first_name varchar2(10 byte),
4 last_name varchar2(10 byte),
5 startdate date,
6 end_date date,
7 salary number(8,2),
8 city varchar2(10 byte),
9 description varchar2(15 byte)
10 );

Table created.
SQL> CREATE OR REPLACE TRIGGER employee_insert_update
2 BEFORE INSERT OR UPDATE ON employee1
3 FOR EACH ROW
4 DECLARE
5 modified_first_name VARCHAR2(100);
6 BEGIN
7 modified_first_name := UPPER(:new.first_name);
8 :new.first_name := modified_first_name;
9 END;
10 /

Trigger created.

You might also like