0% found this document useful (0 votes)
19 views4 pages

Triggers

Uploaded by

sunil.babu101
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)
19 views4 pages

Triggers

Uploaded by

sunil.babu101
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/ 4

Guru99 method::

select * from emp;

select * from dept;

CREATE TABLE emp(


emp_no NUMBER(10),
emp_name VARCHAR2(50),
salary NUMBER(10),
manager VARCHAR2(50),
dept_no NUMBER(10));
/

CREATE TABLE dept(


Dept_no NUMBER(10),
Dept_name VARCHAR2(50),
LOCATION VARCHAR2(50));
/

BEGIN
INSERT INTO DEPT VALUES (10, 'HR', 'USA');
INSERT INTO DEPT VALUES (20, 'SALES', 'UK');
INSERT INTO DEPT VALUES (30, 'FINANCIAL', 'JAPAN');
COMMIT;
END;
/

BEGIN
INSERT INTO EMP VALUES (1000, 'XXX5', 15000, 'AAA', 30);
INSERT INTO EMP VALUES (1001, 'YYY5', 18000, 'AAA', 20);
INSERT INTO EMP VALUES (1002, 'ZZZ5', 20000, 'AAA', 10);
COMMIT;
END;
/

CREATE VIEW guru99_emp_view


(Employee_name, dept_name, location) AS
SELECT emp.emp_name, dept.dept_name, dept.location
FROM emp, dept
WHERE emp.dept_no = dept.dept_no;
/

SELECT * FROM guru99_emp_view;

BEGIN
UPDATE guru99_emp_view SET location = 'FRANCE' WHERE employee_name = :'XXX';
COMMIT;
END;
/
CREATE TRIGGER guru99_view_modify_trg
INSTEAD OF UPDATE
ON guru99_emp_view
FOR EACH ROW
BEGIN
UPDATE dept
SET location=:new.location
WHERE dept_name=:old.dept_name;
END;
/

BEGIN
UPDATE guru99_emp_view SET location='FRANCE' WHERE employee_name='XXX5';
COMMIT;
END;
/

select * from emp;

select * from dept;

CREATE TRIGGER emp_trig


FOR INSERT
ON emp
COMPOUND TRIGGER
BEFORE EACH ROW IS
BEGIN
:new.salary:=5000;
END BEFORE EACH ROW;
END emp_trig;
/

BEGIN
INSERT INTO EMP VALUES (1004,'CCC', 15000, 'AAA', 30);
COMMIT;
END;
/

SELECT * FROM emp WHERE emp_no=1004;

ALTER TRIGGER emp_trig DISABLE;

ALTER TRIGGER emp_trig ENABLE;

ALTER TABLE emp DISABLE ALL TRIGGERS;


ALTER TABLE emp ENABLE ALL TRIGGERS;

BEGIN
INSERT INTO EMP VALUES (1006,'ABC', 15000, 'AAA', 30);
COMMIT;
END;
/

------------------------------------------------------------

StudyTonight method:

Triggers in oracle are blocks of PL/SQL code which oracle engine can execute
automatically based on some action or event.

These events can be:

DDL statements (CREATE, ALTER, DROP, TRUNCATE)


DML statements (INSERT, SELECT, UPDATE, DELETE)
Database operation like connecting or disconnecting to oracle (LOGON, LOGOFF,
SHUTDOWN)

select * from testing;

CREATE OR REPLACE TRIGGER CheckAge


BEFORE
INSERT OR UPDATE ON testing
FOR EACH ROW
BEGIN
IF :new.Age<18 THEN
raise_application_error(-20001, 'Age should not be lesser than 18');
END IF;
END;

INSERT INTO testing VALUES (15, 'Roger Milk', 'Khi', 17, 16000, 'Internee',
'testii');

INSERT into testing VALUES(16, 'Saina', 'Lahore', 17, 17500, 'HR intern', 'HR');

INSERT into testing VALUES(16, 'Aisha', 'Lahore', 20, 18000, 'HR intern', 'HR');

update testing set age=14 where id=11;

update testing set age=19 where id=12;

You might also like