Trigger in oracle SQL - Validation? - oracle

Hello guys I'm trying to create a trigger to validate some data, I've got a table called 'Events' and what I need to do create a trigger that will give me an error when you try to add an event in July.
CREATE OR REPLACE TRIGGER concert_trigger
AFTER INSERT ON CONCERT
WHEN (event_date = 'July')
BEGIN
ALTER TABLE CONCERT
ADD CONSTRAINT chk_CONCERT CHECK (event.date ='July')
END;
Here is the trigger that I have started that I think is the way to go but I'm not sure if this is right.
Thanks, Leprejohn

Since your classwork requires the use of a trigger you'd want to do something like
CREATE OR REPLACE TRIGGER CONCERT_BI
BEFORE INSERT ON CONCERT
FOR EACH ROW
BEGIN
IF TO_CHAR(EVENT_DATE, 'MON') = 'JUL' THEN
RAISE_APPLICATION_ERROR(-20666, 'No concerts can be scheduled in July');
END IF;
END CONCERT_BI;
Here I'm assuming that EVENT_DATE is an actual DATE instead of a character string; if that was a bad guess then change the comparison appropriately.

Related

Using trigger to check the data if already exist in the table and block the insertion

I am trying to use trigger to check whether whether the plate number that user is inserting already exist or not.If found then display message insertion blocked. There were some errors but i cant solve it. Below is the script:
CREATE OR REPLACE TRIGGER trig_check_plate_no
BEFORE INSERT OR UPDATE OF plate_number ON cars
FOR EACH ROW
WHEN(NEW.plate_number IS NOT NULL)
DECLARE
vn_count NUMBER(10);
BEGIN
SELECT COUNT(*)
INTO vn_count
FROM cars
WHERE plate_number = NEW.plate_number
IF(:vn_count>0)
THEN DBMS_OUTPUT.PUT_LINE('Insertion blocked.');
ELSE DBMS_OUTPUT.PUT_LINE('Data inserted.');
END IF;
END trig_check_plate_no;
/
How can i solve it?
In fact, you can't force a trigger to abort the operation that triggered its execution other than providing invalid values on purpose which is not a good style at all.
To achieve what you want to do, the easiest way would be to create a unique index on that table:
ALTER TABLE cars ADD CONSTRAINT c_plate_no UNIQUE ( plate_number );
Your DBMS should throw an error when you try to insert a value that already exists.
After doing so, you can omit your trigger entirely.

Oracle_Trigger: Auto Update Of a Column Based Upon INSERT/UPDATE

This might appear a simple query for most of you, but I am a beginner in Oracle DB.
A table has been created with below script-
CREATE TABLE PLAN_TABLE
(
PL_ID DECIMAL(10,0) PRIMARY KEY NOT NULL
,PL_NAME VARCHAR2(300) DEFAULT NULL
,UPDATED_TS TIMESTAMP DEFAULT SYSDATE NOT NULL
,DELETE_FLAG DECIMAL(10,0) DEFAULT 0 NOT NULL
);
The requirement is to have SYSDATE for UPDATED_TS for any new record inserted into the table and also in case when the DELETE_FLAG is updated to 1. Can it be done by trigger?
The below trigger was created-
CREATE OR REPLACE TRIGGER PT_BEFORE_INSERT_TR
BEFORE INSERT ON PLAN_TABLE
FOR EACH ROW
BEGIN
SELECT SYSDATE INTO :new.UPDATED_TS FROM DUAL;
dbms_output.put_line('Inserted');
END;
/
Below error was encountered while inserting record into the table-
error: ORA-04091: table I60_SCH04.PLAN_TABLE is mutating, trigger/function may not see it
Can you please help in letting me know that where am I committing the mistake? Is there any better way to achieve the requirement based upon INSERT/UPDATE?
The actual error you get is due to the fact that you try to select from a table that you actually are changing. To prevent the issue there are a couple of methods, but in you case things are really simple.
SYSDATE is a function, that you could call directly inside PL/SQL block (which a trigger actually is) and use the value returned to update the set the column value
CREATE OR REPLACE TRIGGER PT_BEFORE_INSERT_TR
BEFORE INSERT ON PLAN_TABLE
FOR EACH ROW
BEGIN
:new.UPDATED_TS := sysdate;
dbms_output.put_line('Inserted');
END;
/
OK, this covers the insert part.
For updating - once again, many options. One could be - change your trigger to BEFORE INSERT OR UPDATE ON PLAN_TABLE.
In this case whenever you issue update or insert - this trigger is fired for each row and updates the date column accordingly.
And of course you could use particular checks available in triggers, something like
IF INSERTING OR UPDATING('DELETE_FLAG') THEN
...
END IF;
and code in the logic you need.

PLSQL Trigger: before insert

Am trying to write a trigger which has to restrict for zero salary before insertion.
I have written but its not working. I am getting an error as trigger is invalid and failed re validation.
Code:
create or replace trigger emp_sal
before insert or update on employee
referencing new as new old as old
for each row
begin
if :new.salary<=0 then
raise_application_error (-20999,’salary is zero’);
end if;
end;
/
Please let me know where the problem is. Thank you
Your code is okay. The only problem I can imagine is that you were using the wrong character for the single quotes in ’salary is zero’.
Run show errors right after you created the trigger (assuming you're uning sqlplus)
But I agree with zerkms. This is a case for a check constraint.

how to get value on other table in trigger on oracle

declare
tgl RENC_DITSEK.TGL_INPUT%TYPE;
thn varchar(10) := :new.tahun + 1;
target RENC_DITSEK.TARGET_2%TYPE;
BEGIN
select TARGET_2 into target from RENC_DITSEK where KODE_RENCANA = :NEW.KODE_RENCANA;
SELECT TO_CHAR(SYSDATE, 'DD/MM/YYYY HH24:MI:SS') INTO tgl FROM DUAL;
IF UPDATING ('TARGET_2') THEN
INSERT INTO NOTE_LOG VALUES(:NEW.PKP,'',:NEW.INDIKATOR,'Target Tahun '||thn,'',:new.target_2,**target**,tgl,'','0');
END IF;
END;
hi, there is my trigger body. i need a value of TARGET_2 before it updated, but i have no ide to do that. so this is my code, but it error "mutating table". is there a way to do that? please help.
thanks for helping.
it would help if you provide the table and the trigger definitions.
e.g. what table are you creating the trigger on? Is KODE_RENCANA the primary key of the table?
If the answer to the above questions is Yes, then you don't need to execute a SELECT statement, since you could get the value of TARGET_2 using :OLD.TARGET_2 for the record you're updating.
If KODE_RENCANA is not the primary key, and it is actually a field that could be updated, and you want to retrieve another record in the same table given the new KODE_RENCANA value, then the problem is more complicated, which I would not recommend using a trigger to handle, instead, try creating a stored procedure and call the stored procedure to perform the update.
cheers.

How to avoid Getting ORACLE Mutating trigger error

I created the trigger to update the oracle data base table after insert.
CREATE OR REPLACE TRIGGER Update_ACU
AFTER INSERT ON TBL_ACU
FOR EACH ROW
BEGIN
UPDATE TBL_ACU
SET CURRENCY = 'XXX'
WHERE ACCOUNT like '%1568';
END ;
I inserted record as
insert into TBL_ACU values('23','USD','1231568');
I am getting table ORACLE Mutating trigger error.
Please help me how to resolve this.
It would be better to use BEFORE INSERT trigger to do this.
Try like this,
CREATE OR REPLACE
TRIGGER update_acu
BEFORE INSERT ON tbl_acu
FOR EACH ROW
WHEN (NEW.ACCOUNT LIKE '%1568')
BEGIN
:NEW.currency := 'XXX';
END ;
Well, you cannot modify the table from the trigger if the trigger is called upon modification of that table. There are various solutions to this problem including an AFTER STATEMENT trigger and caching modifications in some collection defined in PL/SQL PACKAGE, howewer in your situation I'd rather change the body of your trigger to this:
BEGIN
IF :NEW.ACCOUNT LIKE '%1568' THEN
:NEW.CURRENCY := 'XXX';
END IF;
END;
You can use the :NEW and :OLD variables inside the trigger, which identify the new and old values of the record accordingly. Modifying values of the :NEW record will cause changes in data actually inserted to the database.

Resources