PL/SQL Triggers - oracle

I am using these tables:
flights (flno, origin, destination, distance, departs, arrives, price)
aircraft (aid, aname, crusingrange)
employees (eid, ename, salary)
certified (eid,aid)
and I need to create a trigger that displays a warning when inserting an employee with "666" anywhere in his/her name.
This is what I came up with so far; I am lost with the rest of it.
set serveroutput on
create or replace trigger emp_warning
before insert
on employees
for each row
declare
v_name;
begin
select e.ename into v_ename
from employees e

A trigger cannot "display a warning"; a trigger can raise an exception.
In the context of the body of a before insert for each row trigger, the value being supplied for the column is available from :NEW.columname
For example:
BEGIN
IF :NEW.ename LIKE '%666%' THEN
RAISE_APPLICATION_ERROR(-20000, 'ename contains ''666''.');
END IF;
END;
It's not mandatory that you use the RAISE_APPLICATION_ERROR. You could emit some line(s) using DBMS_OUTPUT.PUT_LINE... the line could include whatever text you wanted, including the word "warning". But this isn't really a display of a warning.

Use a check constraint instead of a trigger:
alter table empoloyees modify ename check (ename not like '%666%');

I finally figured it out thanks for you help. This my answer to the trigger question.
set serveroutput on
create or replace trigger name_warning
before insert on employees
for each row
begin
if :new.ename like '%666%' then
dbms_output.put_line('Warning employees name contains 666');
end if;
end;
/

Related

Trying to create a trigger to check if there's more than 1 president in my database

I'm trying to find if there's more than 1 president in my database with a trigger and if yes, raise an error, I'm using hr, the table employees and I have to use the job_id to find it. Here's what my code looks like. Thanks!
CREATE OR REPLACE TRIGGER check_pres
BEFORE INSERT OR DELETE OR UPDATE ON employees
FOR EACH ROW
BEGIN
IF ((employees.job_id = 'AD_PRES') > 1)
THEN RAISE_APPLICATION_ERROR(-12345, 'More than one President in database.');
END IF;
END;
You should use Statement Level Trigger instead of Row Level Trigger by removing FOR EACH ROW expression
CREATE OR REPLACE TRIGGER check_pres
BEFORE INSERT OR DELETE OR UPDATE ON employees
DECLARE
v_cnt int;
BEGIN
SELECT COUNT(*) INTO v_cnt FROM employees WHERE job_id = 'AD_PRES';
IF ( v_cnt > 1 ) THEN
RAISE_APPLICATION_ERROR(-20345, 'More than one President in database.');
END IF;
END;
otherwise you'd get mutating error while getting the count value. Btw, the first argument value for RAISE_APPLICATION_ERROR should be between -20999 and -20000

Addition of values in two columns isn't working in PL/SQL ORACLE

So I am trying to add age with price and store the result on another table with the ID of the person who did this. I am able to set the business rule by making the trigger but when I check my second (END) table, there is nothing there.. Here is my code for the trigger:
CREATE OR REPLACE TRIGGER JIM
BEFORE INSERT ON END
FOR EACH ROW ENABLE
DECLARE
V_AGE JIM.AGE%TYPE;
V_PRICE JIM.PRICE%TYPE;
v_prices NUMBER(20);
BEGIN
SELECT AGE,PRICE INTO V_AGE,V_PRICE FROM JIM WHERE ID=:NEW.ID;
v_prices:=V_AGE+V_PRICE;
INSERT INTO END VALUES(:new.ID,v_prices);
END;
However, When I insert values onto the JIM table using the following code:
insert into jim values(4,'Sim',45,100);
nothing actually gets stored on the END table. i am sort of new to triggers and its so confusing. Please let me know what to do. thanls
Don't use a keyword end as a table name, this causes problem during
creation of trigger. I've presumed the table's name as t_end.
I think you are confused on which table to define trigger. It seems
you should define on table jim instead of t_end.
I've presumed you have a sequence named seq_end to populate the id
column of the table t_end
So , your trigger creation statement will be as follows :
create or replace trigger trg_ins_jim before insert on jim for each row
declare
v_id_end t_end.id%type;
v_prices t_end.prices%type;
begin
v_prices := :new.age + :new.price;
v_id_end := seq_end.nextval; insert into t_end values(v_id_end, v_prices);
/* if you have defined sequence for t_end as default value of id column, you may change the upper row as "insert into t_end(prices) values(v_prices);" and there would be no need for "v_id_end" */
end;
and when you issue insert into jim values(4,'Sim',45,100); command, you'll also have values inserted into t_end.
If there is only one table, then the SELECT and the INSERT are redundant.
CREATE OR REPLACE TRIGGER trigger_name
BEFORE INSERT ON table_name
FOR EACH ROW
BEGIN
:NEW.PRICES := :NEW.AGE + :NEW.PRICE;
END;
Possible duplicate

What is the proper way to create an update trigger with a variable getting value from the same table?

I'm creating a update trigger where an employee can never have a salary that is greater than the president's. However I need to subquery the president's salary for comparison and the "new" updated employee's salary.
I originally had the the subquery using from the from employees table but had to make a new table because of the mutating table problem. I don't think creating a new table is plausible solution for a real implementation.
Is there a way I can save the president's salary without creating a new table?
CREATE OR REPLACE TRIGGER prevent_salary
BEFORE UPDATE ON employees
FOR EACH ROW
declare pres_sal number(8,2);
BEGIN
select salary into pres_sal from employees_salary where job_id='AD_PRES';--employees_salary was employees but that gives mutating error
IF (:new.salary > pres_sal)
THEN UPDATE employees
SET salary = :old.salary
WHERE employee_id = :old.employee_id;
END IF;
END;
One way to do it is to save off the president's salary in a BEFORE STATEMENT trigger and then use that in the FOR EACH ROW trigger.
"Compound Triggers", which have been around at least since version 11.1, offer a nice way to do that all in one place.
Here is an example:
CREATE OR REPLACE TRIGGER prevent_salary
FOR UPDATE OF salary ON employees
COMPOUND TRIGGER
pres_sal NUMBER;
BEFORE STATEMENT IS
BEGIN
select salary
into pres_sal
from employees
where job_id='AD_PRES';
END BEFORE STATEMENT;
BEFORE EACH ROW IS
BEGIN
:new.salary := least(:new.salary, pres_sal);
END BEFORE EACH ROW;
END prevent_salary;
You may try this :
CREATE OR REPLACE TRIGGER prevent_salary
BEFORE UPDATE ON employees
FOR EACH ROW
declare pres_sal number(8,2);
BEGIN
select salary into pres_sal from employees_salary where job_id='AD_PRES';--employees_salary was employees but that gives mutating error
IF (:new.salary > pres_sal)
Raise_Application_Error (-20101, 'An employee''s salary couldn''t exceed president''s !');
END IF;
END;

Trigger to enforce constraint that employee never change department

Suppose I have following schema :
DEPARTMENT (DepartmentName, BudgetCode, OfficeNumber, Phone)
EMPLOYEE (EmployeeNumber, FirstName, LastName, Department, Phone, Email)
Now I need to write a trigger that enforce the constraint that an employee can never change his or her department.
CREATE OR REPLACE TRIGGER department_fixed
BEFORE UPDATE ON EMPLOYEE
FOR EACH ROW
WHEN (old.Department is not null)
BEGIN
dbms_output.put('You can not change department');
END;
/
Is this right way to write this oracle trigger or am missing something ?
Also please help me to write a trigger to allow the deletion of a department if it only has one employee. Assign the last employee to the Human Resources department.
You're close. What this trigger is going to do is for every update of the EMPLOYEE table, it's going to check the original department and if it's not null, it will print something to the screen. I'm assuming you're allowing updates for other fields, right? So my trigger would look like this:
CREATE OR REPLACE TRIGGER EMPLOYEE_BUR
BEFORE UPDATE ON EMPLOYEE
FOR EACH ROW
DECLARE
DEPT_EXCEPTION EXCEPTION; --Declare the exception
PRAGMA EXCEPTION_INIT(DEPT_EXCEPTION, -20002);
L_COUNT NUMBER
BEGIN
SELECT COUNT(*) INTO L_COUNT FROM DEPARTMENT D
WHERE D.DEPARTMENT=:OLD.DEPARTMENT;
IF (:OLD.DEPARTMENT <> :NEW.DEPARTMENT) AND (L_COUNT > 0) THEN
RAISE DEPT_EXCEPTION;
END IF;
END;
.. and then I handle the DEPT_EXCEPTION in my calling procedure. You must either raise an exception -OR- do something with :NEW.DEPARTMENT or else the trigger will finish and the update statement will complete. The "EMPLOYEE_BUR" name is more for best practices, this way you know what it is. "BUR" - "Before Update for each Row"
For the second part, you could have a trigger on DEPARTMENT that fires before the delete to confirm deletion, then one after to reassign the employee:
CREATE OR REPLACE TRIGGER DEPARTMENT_BDR
BEFORE DELETE ON DEPARTMENT
FOR EACH ROW
DECLARE
DEPT_COUNT_EXCEPTION EXCEPTION;
PRAGMA EXCEPTION_INIT(DEPT_COUNT_EXCEPTION, -20003);
L_COUNT NUMBER;
BEGIN
SELECT COUNT(*) INTO L_COUNT FROM EMPLOYEE E
WHERE E.DEPARTMENT=:OLD.DEPARTMENT;
IF (L_COUNT > 1 ) THEN
RAISE DEPT_COUNT_EXCEPTION;
END IF;
END;
Then...
CREATE OR REPLACE TRIGGER DEPARTMENT_ADR
AFTER DELETE ON DEPARTMENT
FOR EACH ROW
BEGIN
UPDATE EMPLOYEE
SET DEPARTMENT='HR'
WHERE EMPLOYEE=:OLD.EMPLOYEE;
END;
So this way, if you're deleting a Department with more than one employee, trigger 2 fires and raises an exception so trigger 3 will not. If trigger 2 completes fine, then trigger 3 will fire which re-assigns the employee. If the delete statement completed, then the update will be allowed since there is no former department.

How can i fix this Mutating table from a procedure an trigger

This trigger will pass on inserted values to a procedure which will insert those values in another table. I'm getting a mutating table error. How can i fix this?
CREATE OR REPLACE TRIGGER ADD_INVOICE
BEFORE INSERT OR UPDATE OF APP_NO,C_NO ON APPOINTMENT
FOR EACH ROW
DECLARE
BEGIN
POP_INVOICE(:NEW.APP_NO,:NEW.C_NO,:NEW.APP_DATE);
END;
/
CREATE OR REPLACE PROCEDURE POP_INVOICE(
I_APP_NO IN INVOICE.APP_NO%TYPE,
I_C_NO IN INVOICE.C_NO%TYPE,
I_INV_DATE IN INVOICE.INV_DATE%TYPE)
AS
CURSOR C_POP IS SELECT PRICE FROM TREATMENT T,APPOINTMENT A
WHERE T.TRT_NO=A.TRT_NO
AND A.APP_NO=I_APP_NO;
V_BILL INVOICE.BILL%TYPE;
BEGIN
OPEN C_POP;
FETCH C_POP INTO V_BILL;
UPDATE INVOICE
SET INV_NO=INV_IDSEQ.NEXTVAL,
APP_NO=I_APP_NO,
C_NO=I_C_NO,
BILL=V_BILL,
INV_DATE=I_INV_DATE;
END;
/
The problem is caused by referencing the table with the trigger on it within the trigger itself. Changing the procedure to accept the TRT_NO as a parameter removes the need to include APPOINTMENT in the query, and so will avoid the mutating table exception. Depending on how many records there are for each treatment, you could even incorporate the cursor into your UPDATE statement.
I think this should do it, although I haven't been able to check against a database.
CREATE OR REPLACE TRIGGER ADD_INVOICE
BEFORE INSERT OR UPDATE OF APP_NO,C_NO ON APPOINTMENT
FOR EACH ROW
DECLARE
BEGIN
POP_INVOICE(:NEW.APP_NO,:NEW.C_NO,:NEW.APP_DATE,:NEW.TRT_NO);
END;
/
The revised procedure:
CREATE OR REPLACE PROCEDURE POP_INVOICE(
I_APP_NO IN INVOICE.APP_NO%TYPE,
I_C_NO IN INVOICE.C_NO%TYPE,
I_INV_DATE IN INVOICE.INV_DATE%TYPE,
I_TRT_NO IN APPOINTMENT.TRT_NO%TYPE
)
AS
CURSOR C_POP IS SELECT PRICE
FROM TREATMENT T
WHERE T.TRT_NO = I_TRT_NO;
V_BILL INVOICE.BILL%TYPE;
BEGIN
OPEN C_POP;
FETCH C_POP INTO V_BILL;
CLOSE C_POP;
INSERT INVOICE
(inv_no, app_no, c_no, bill, inv_date)
VALUES
(INV_IDSEQ.NEXTVAL, I_APP_NO, I_C_NO, V_BILL, I_INV_DATE);
END;
/

Resources