ORA-04098: trigger 'SYSTEM.TEXT_TRIG' is invalid and failed re-validation - sql

I am using the oracle database 10g express edition to execute triggers. My table structure is as shown below:
Table text:
create table text
(
tid number Not Null,
tname varchar2(5)
);
Table book:
create table book
(
bid number Not Null,
bname varchar2(5),
foreign key(bid) references text(tid)
);
Later inserted one row in text tabe: 1,abc
My question is when i am about to insert a value to text table, it should automatically insert a value in book table.
I had created a trigger named text_trig: structure is a s follows:
create trigger text_trig after insert on text
for each row
begin
insert into book set bid=NEW.tid;
end;
/
It gave me an warning message saying: Trigger created with compilation errors.
when I inserted a new value in text table, it is showing an error message as
trigger 'SYSTEM.TEXT_TRIG' is invalid and failed re-validation.

You missed a colon.
set bid = :NEW.tid

Related

How to automatically update second table with some same information after insert into first table

When I enter a new record in one table, I need to have some of the information from the first table be automatically added to the second table. I unsuccessfully tried triggers to do this.
My primary table looks like this:
CREATE TABLE demographics (
person_local_id BIGSERIAL UNIQUE PRIMARY KEY,
first_name VARCHAR(50)...[other lines]
);
I set up the child table like this:
CREATE TABLE pedigree (
pedigree_id BIGSERIAL PRIMARY KEY NOT NULL,
person_local_id BIGSERIAL NOT NULL,
person_sex VARCHAR(10),
father VARCHAR(10) DEFAULT 0,
mother VARCHAR(10) DEFAULT 0,
FOREIGN KEY (person_local_id) REFERENCES demographics (person_local_id)
ON DELETE CASCADE ON UPDATE CASCADE
);
My approach was to create a trigger on the demographics primary table such that any time a record was added to it, a corresponding record would be added to the pedigree table consisting of just the person_local_id. I added a foreign key on the pedigree table that referenced the column in the demographics that I need to copy over to the pedigree table in that column.
Then I created a trigger, but it doesn't work. I tried this with and without the word "EXECUTE".
CREATE TRIGGER into_pedigree AFTER INSERT OR UPDATE ON demographics
FOR EACH ROW EXECUTE INSERT INTO pedigree (person_local_id) SELECT (NEW.person_local_id) FROM NEW;
I keep getting syntax errors but I can't identify the error:
ERROR: syntax error at or near "INSERT"
LINE 2: FOR EACH ROW EXECUTE INSERT INTO pedigree (person_local_id) ...
^
I also tried this, adding the name:
CREATE TRIGGER into_pedigree ON identify_relatives_database.demographics
AFTER INSERT
AS
BEGIN
INSERT INTO pedigree (person_local_id) VALUES (INSERTED.person_local_id)
END;
But I get the error message:
ERROR: syntax error at or near "ON"
LINE 1: CREATE TRIGGER into_pedigree ON demographics
^
I appreciate your assistance.
You may try this.
CREATE TRIGGER [dbo].[Customer_INSERT]
ON [dbo].[demographics]
AFTER INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON;
DECLARE #CustomerId INT
SELECT #CustomerId = INSERTED.person_local_id FROM INSERTED
IF EXISTS ( SELECT * FROM PEDIGREE WHERE person_local_id = #CustomerId)
BEGIN
--- here col is the required column name need to be modified,
--- since you are inserting person_local_id from base table which is auto generated and not suppose to be change in any condition
UPDATE PEDIGREE SET COL = INSERTED.COL WHERE person_local_id = #CustomerId
END
ELSE
BEGIN
INSERT INTO pedigree (person_local_id)
VALUES(#CustomerId)
END
END
Although I don't find anything related to your update part. Since you are inserting primary key from base table as foreign key in child table, so for normalization it is not going to changed in any condition. So i don't think you need update part in your trigger Hence your required trigger will be:
CREATE TRIGGER [dbo].[Customer_INSERT]
ON [dbo].[demographics]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO pedigree (person_local_id)
SELECT INSERTED.person_local_id FROM INSERTED
END
You should try this :
CREATE TRIGGER into_pedigree ON demographics
FOR INSERT
AS
insert into pedigree (person_local_id)
values(inserted.person_local_id);
PRINT 'AFTER INSERT trigger fired.'
GO

how to insert data using trigger

I got question about using trigger to insert data, for instance, I do have two tables, and second table has attributes and records with table, except additional two attributes, like below:
CREATE TABLE dept
(
DEPTNO NUMBER(3) PRIMARY KEY,
DNAME VARCHAR2(16),
LOC VARCHAR2(16)
);
CREATE TABLE dept_shadow
(
DEPTNO NUMBER(3) PRIMARY KEY,
DNAME VARCHAR2(16),
LOC VARCHAR2(16),
USER VARCHAR2(32),
MODTIME CHAR(17)
);
and I want create a trigger to track all inserts into a table.
surprisedly, I got error about creating table:
Error starting at line : 11 in command -
CREATE TABLE dept_shadow
(
DEPTNO NUMBER(3) PRIMARY KEY,
DNAME VARCHAR2(16),
LOC VARCHAR2(16),
USER VARCHAR2(32),
MODTIME CHAR(17)
)
Error report -
ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
I have no idea about this error and does anyone can tell me how to do this job by create trigger? Since there is no actual records to insert! Any suggestions are appreciated
Okay, so the error you are getting is because oracle (like all databases) has some reserved words. Now I'm not 100% sure because I don't tend to work with Oracle DB all that often, but I would assume that you cannot use the word USER for that reason. Try using USERNAME or USERDESCRIPTION or something like that instead.
Now for the trigger:
CREATE OR REPLACE TRIGGER trg_shadow
BEFORE INSERT OR UPDATE OR DELETE
ON dept_shadow
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
MODTIME char (17);
BEGIN
IF INSERTING
THEN
-- do something
ELSIF UPDATING
THEN
-- do something
ELSIF DELETING
THEN
-- do something
END IF;
From there, you can access "new" data through :NEW and "old" data through :OLD.
EDIT:
The difference of a BEFORE and AFTER trigger is when they are executed and both have a valid use.
BEFORE triggers may be used to validate data BEFORE inserting or updating. So for instance if you don't want to update the rows that would otherwise have the value 0 in column x.
AFTER triggers may be used to validate the new data AFTER inserting. So for instance if you want to delete all rows that now have the value 0 in column x.
It doesn't really matter in your case though.
Hope that helps!
The error is because of the column name(user) you are using in the dept_shadow table.
USER is predefined that's why it won't work as column name in table. Rename it to 'audit_user' or anything you want, which is not keyword. It will work seamlessly.
And for trigger
CREATE OR REPLACE TRIGGER dept_trigger
AFTER INSERT
ON dept
FOR EACH ROW
DECLARE
v_username varchar2(10);
BEGIN
-- Find username of person performing the INSERT into the table
SELECT user INTO v_username
FROM dual;
-- Insert record into shadow table
INSERT INTO dept_shadow
( DEPTNO,
DNAME,
LOC,
AUDIT_USER,
MODTIME )
VALUES
( :new.DEPTNO,
:new.DNAME,
:new.LOC,
v_username,
:new.MODTIME
);
END;
/
Hope this will work for you.

Oracle: Trigger to INSERT into one table changes from separate table

I have two tables, item and item_price_history. I want to create a trigger that inserts records of the old prices after an item's price is changed in the price table. Here are the two tables:
CREATE TABLE item(
item_id DECIMAL(10) NOT NULL,
description VARCHAR(30),
price DECIMAL(10),
PRIMARY KEY (item_id));
CREATE TABLE item_price_history(
item_id DECIMAL(10) NOT NULL,
old_price DECIMAL(10) NOT NULL,
new_price DECIMAL(10) NOT NULL,
date_of_change DATE NOT NULL,
FOREIGN KEY (item_id) references ITEM);
I have tried numerous ways just to get it to compile but this is what I have as the trigger:
CREATE OR REPLACE TRIGGER price_hist
AFTER UPDATE ON item
FOR EACH ROW
WHEN (new.price <> old.price)
BEGIN
INSERT INTO item_price_history(item_id, old_price, new_price, date_of_change)
VALUES (:NEW.item_id, :OLD.price, :NEW.price, SYSDATE);
END;
I receive the following error after updating the price on item:
Error starting at line : 1 in command -
UPDATE item
SET price = 11
WHERE item_id = 1
Error at Command Line : 1 Column : 8
Error report -
SQL Error: ORA-04098: trigger 'KEVIN.PRICE_UPDATE' is invalid and failed re-validation
04098. 00000 - "trigger '%s.%s' is invalid and failed re-validation"
*Cause: A trigger was attempted to be retrieved for execution and was
found to be invalid. This also means that compilation/authorization
failed for the trigger.
*Action: Options are to resolve the compilation/authorization errors,
disable the trigger, or drop the trigger.
Please let me know what you suggest, I've searched high and low but am not able to find a solution.
This error only indicates a problem with the trigger.
You will need to view the USER_ERROR table
select *
from
user_errors
where
type = 'TRIGGER'
and
name = 'price_hist';
Check also SHOW ERRORS TRIGGER price_hist.
You should receive more information from this source. Reasons can be many, largely due to errors on the object.
I checked on my schemat and everything went perfectly, so probably something is wrong on your database schema.
Try creating the same tables on another user and see if the problem repeats itself.

Why is trigger not fired on every single row when using "insert select" or "merge"

I defined a BEFORE INSERT trigger for a table and it works as expected for single INSERTstatements, but not for INSERT ... SELECT nor MERGE statements.
These are my database objects (simplified):
CREATE TABLE "COMPANY" (
"ID" NUMBER NOT NULL,
"NAME" VARCHAR(100)
);
CREATE TABLE "EMPLOYEE" (
"ID" NUMBER NOT NULL,
"COMPANY_ID" NUMBER NOT NULL
);
CREATE UNIQUE INDEX "EMPLOYEE_PK" ON "EMPLOYEE" ("ID");
CREATE SEQUENCE "EMPLOYEE_SEQUENCE";
CREATE TRIGGER "BI_EMPLOYEE" BEFORE INSERT ON "EMPLOYEE"
REFERENCING NEW AS newrow FOR EACH ROW BEGIN ATOMIC
IF newrow.id IS NULL THEN
SET newrow.id = NEXT VALUE FOR employee_sequence;
END IF;
END;
If single INSERTstatements are executed, everything works as expected, the ÌD is fetched from the sequence. But if I execute something like
INSERT INTO employee (company_id) SELECT id FROM company;
the I get an error:
integrity constraint violation: unique constraint or index violation: "EMPLOYEE_PK"
which could propably mean that it tries to insert the same key from the sequence twice.
I'm using the latests version 2.3.2 of HSQLDB.
Because triggers are set based, not row based.
See details here

sql triggers - want to compare existing row value with value being inserted

I am creating a trigger that should compare of the values being inserted with one that already exists in the table. The old referencer doesn't work here because I am inserting, but how can I reference something that already exists?
Here is my tables and trigger:
create table events(eid char(2) primary key, cid char(2));
create table activities(mid char(2), eid char(2),
primary key (mid, eid),
constraint activities_fk foreign key (eid) references events(eid));
create or replace trigger check_valid
before insert or update on activities
for each row when (old.mid=new.mid)
declare
v_eid char(2);
v_cid char(2);
n_cid char(2);
begin
select eid into v_eid from activities
where mid=:new.mid;
select cid into v_cid from events
where eid=v_eid;
select cid into n_cid from events
where eid=:new.eid;
if v_cid=n_cid then
raise_application_error(-20000, 'Error');
end if;
end check_valid;
/
show errors;
You can't generally select from the table you're inserting into in a trigger. This is the mutating table problem, or as I often call it, the "damn mutating table problem".
Basically, don't do this. It's a bad idea. What happens if you have two sessions operating on the table at once? The trigger fires and neither session sees what the other has done until the commit, which is after the trigger. Then you've got unexpected data in your database.
Tom Kyte says, "when I hit a mutating table error, I've got a serious fatal flaw
in my logic."