I am writing a database for practical purposes which involves some triggers and constraints for the moment.
One of my triggers must set a column value to 0 before deleting a speciffied row , but the deletion cannot work with the constraint on ... so I decided that BEFORE DELETE I remove the constraint then after I update my column value from the other table I will ADD it again using my 2nd trigger.
However I can get it to work :
ALTER TABLE teams ADD CONSTRAINT teams_to_trainer FOREIGN KEY (coda)
REFERENCES trainer(coda);
--insert values
--...
--Procedure in which I execute the alter table commands
CREATE OR REPLACE PROCEDURE constraint_operation (p_hardcod IN VARCHAR2)
IS
PRAGMA AUTONOMOUS_TRANSACTION;
resource_busy EXCEPTION;
PRAGMA EXCEPTION_INIT(resource_busy,-54);
BEGIN
EXECUTE IMMEDIATE p_hardcod;
EXCEPTION
WHEN resource_busy
THEN
DBMS_LOCK.SLEEP(5);
COMMIT;
END;
/
--My Two Triggers
CREATE OR REPLACE TRIGGER delete_trainer
BEFORE DELETE
ON trainer
FOR EACH ROW
BEGIN
constraint_operation('ALTER TABLE teams DROP CONSTRAINT teams_to_trainer');
UPDATE teams SET coda = 0 WHERE :old.coda = coda;
END;
/
CREATE OR REPLACE TRIGGER delete_trainer_add_constraint
AFTER DELETE
ON trainer
FOR EACH ROW
BEGIN
constraint_operation('ALTER TABLE teams ADD CONSTRAINT teams_to_trainer
FOREIGN KEY (coda)REFERENCES trainer(coda)');
END;
/
And when calling :
DELETE FROM trainer WHERE coda = 14;
I get this :
SQL Error: ORA-02291: integrity constraint (USER.TEAMS_TO_TRAINER)
violated - parent key not found
02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found"
*Cause: A foreign key value has no matching primary key value.
*Action: Delete the foreign key or add a matching primary key.
I can't seem to find the problem , I thought calling the procedure will remove the constraint.
What am I doing wrong ?
Thank you.
You are updating a foreign key column in TEAMS to a value that doesn't exist in the referenced primary key in TRAINER.
If the FK constraint is required, a simple fix would be to add a default/dummy row in TRAINER where the value in CODA is 0.
Related
I'm on Oracle Database 18c Express Edition and APEX 19.1.
The requirement is to be able to link between items with same or different types:
logs with logs,
events with logs
...and so on.
e.g.
logs.item_id = 1
logs.item_id = 2
events.item_id = 5
logs.item_id = 2
logs.item_id = 1
events.item_id = 5
logs.item_id = 1
My idea is to have a shared ID sequence, that would be populated with every insert on - for example - logs table.
Having unique IDs for logs, events and other items, I could create links in separate links table
https://imgur.com/Dchz7De.jpg
After executing DDL script, my first
INSERT INTO events (dummy) VALUES ('D');
results with an error.
ORA-02291: integrity constraint (C##TEST.EVE_ITE_FK_1) violated - parent key not found
Following inserts assign sequence values starting from 3.
https://imgur.com/ru2aewG.jpg
DROP TABLE items CASCADE CONSTRAINTS;
DROP TABLE logs CASCADE CONSTRAINTS;
DROP TABLE events CASCADE CONSTRAINTS;
DROP TABLE links CASCADE CONSTRAINTS;
DROP SEQUENCE items_seq;
DROP TRIGGER logs_trg;
DROP TRIGGER events_trg;
/
CREATE TABLE logs (
item_id NUMBER(*,0) PRIMARY KEY
,dummy CHAR(1));
/
CREATE TABLE events (
item_id NUMBER (*,0) PRIMARY KEY
,dummy CHAR(1));
/
CREATE TABLE links (
id NUMBER(*,0) GENERATED ALWAYS AS IDENTITY PRIMARY KEY
,item_id_1 NUMBER(*,0)
,item_id_2 NUMBER(*,0));
/
CREATE TABLE items (
id NUMBER(*,0) PRIMARY KEY
,type CHAR(1 CHAR));
/
ALTER TABLE logs ADD CONSTRAINT log_ite_fk_1 FOREIGN KEY (item_id) REFERENCES items (id);
/
ALTER TABLE events ADD CONSTRAINT eve_ite_fk_1 FOREIGN KEY (item_id) REFERENCES items (id);
/
ALTER TABLE links ADD CONSTRAINT lin_ite_fk_1 FOREIGN KEY (item_id_1) REFERENCES items (id);
/
ALTER TABLE links ADD CONSTRAINT lin_ite_fk_2 FOREIGN KEY (item_id_2) REFERENCES items (id);
/
CREATE SEQUENCE items_seq START WITH 1;
/
CREATE OR REPLACE TRIGGER logs_trg FOR INSERT ON logs
COMPOUND TRIGGER
l_item_id PLS_INTEGER := items_seq.nextval;
co_item_type CONSTANT CHAR(1) := 'L';
BEFORE STATEMENT
IS
BEGIN
INSERT INTO items(id
,type)
VALUES (l_item_id
,co_item_type);
END BEFORE STATEMENT;
BEFORE EACH ROW
IS
BEGIN
SELECT l_item_id
INTO :NEW.item_id
FROM dual;
END BEFORE EACH ROW;
END;
/
CREATE OR REPLACE TRIGGER events_trg FOR INSERT ON events
COMPOUND TRIGGER
l_item_id PLS_INTEGER := items_seq.nextval;
co_item_type CONSTANT CHAR(1) := 'E';
BEFORE STATEMENT
IS
BEGIN
INSERT INTO items(id
,type)
VALUES (l_item_id
,co_item_type);
END BEFORE STATEMENT;
BEFORE EACH ROW
IS
BEGIN
SELECT l_item_id
INTO :NEW.item_id
FROM dual;
END BEFORE EACH ROW;
END;
/
Do you have any suggestions on what could I do to make it work with the 1st insert?
I expect
INSERT INTO events (dummy) VALUES ('D');
to produce items.id = 1 and events.item_id = 1.
EDIT:
Following Enrique's advice, I switched to non-compound trigger with returning clause - as below.
DROP TABLE items CASCADE CONSTRAINTS;
DROP TABLE logs CASCADE CONSTRAINTS;
DROP TABLE events CASCADE CONSTRAINTS;
DROP TABLE links CASCADE CONSTRAINTS;
DROP SEQUENCE items_seq;
/
CREATE TABLE logs (
item_id NUMBER(*,0) PRIMARY KEY
,dummy CHAR(1));
/
CREATE TABLE events (
item_id NUMBER (*,0) PRIMARY KEY
,dummy CHAR(1));
/
CREATE TABLE links (
id NUMBER(*,0) GENERATED ALWAYS AS IDENTITY PRIMARY KEY
,item_id_1 NUMBER(*,0)
,item_id_2 NUMBER(*,0));
/
CREATE TABLE items (
id NUMBER(*,0) PRIMARY KEY
,type CHAR(1 CHAR));
/
ALTER TABLE logs ADD CONSTRAINT log_ite_fk_1 FOREIGN KEY (item_id) REFERENCES items (id);
/
ALTER TABLE events ADD CONSTRAINT eve_ite_fk_1 FOREIGN KEY (item_id) REFERENCES items (id);
/
ALTER TABLE links ADD CONSTRAINT lin_ite_fk_1 FOREIGN KEY (item_id_1) REFERENCES items (id);
/
ALTER TABLE links ADD CONSTRAINT lin_ite_fk_2 FOREIGN KEY (item_id_2) REFERENCES items (id);
/
CREATE SEQUENCE items_seq START WITH 1;
/
CREATE OR REPLACE TRIGGER logs_trg FOR INSERT ON logs
COMPOUND TRIGGER
l_item_id PLS_INTEGER := items_seq.nextval;
co_item_type CONSTANT CHAR(1) := 'L';
BEFORE STATEMENT
IS
BEGIN
INSERT INTO items(id
,type)
VALUES (l_item_id
,co_item_type);
END BEFORE STATEMENT;
BEFORE EACH ROW
IS
BEGIN
SELECT l_item_id
INTO :NEW.item_id
FROM dual;
END BEFORE EACH ROW;
END;
/
CREATE OR REPLACE TRIGGER events_trg BEFORE INSERT ON events
FOR EACH ROW
DECLARE
co_item_type CONSTANT CHAR(1) := 'M';
BEGIN
INSERT INTO items(id
,type)
VALUES (items_seq.nextval
,co_item_type)
RETURNING id INTO :NEW.item_id;
DBMS_OUTPUT.PUT_LINE(systimestamp);
END;
/
INSERT INTO events (dummy) values ('D');
INSERT INTO events (dummy) values ('D');
Now the problem is different. The 1st insert on events table generates 2 values on items_seq
INSERT INTO events (dummy) VALUES ('D');
DBMS_OUTPUT:
08-MAY-19 11.08.29.301000000 +02:00
08-MAY-19 11.08.29.303000000 +02:00
Ongoing inserts behave as expected - 1 seq number for each insert. So 2 inserts generate 3 sequence values.
My desired outcome is to have items.id = 1 for first insert on events table.
Just use this
v_myid number;
insert into table(col1, col2)
values(value1, value2)
returning id into v_myid;
I'm working on a project in which I'm writing queries from this database that I am attempting to create in PLSQL. However, before I can do that I must create the db, and some of the table-create statemens I'm trying to run are returning errors. Specifically, the errors seem to indicate that some of the primary keys I'm referencing don't exist. When I check to see if that is true I see the likely issue. For example, I try to do
create table group_disforum ( df_id int,
ig_id int,
constraint gdf_FK foreign key(ig_id) references Course_Interest_group(interest_gid),
constraint gdf_PK primary key(df_id, ig_id),
comments varchar(150)
)
setting the foreign key to the primary key(interest_gid) in Course_Interest_group.
However, the Course_Interest_group is this:
create table Course_Interest_group( interest_gid int,
gname varchar(20),
courseid int,
facultyid int,
past_gpa float,
constraint IG_PK primary key(interest_gid, courseid, facultyid),
constraint IG_FK1 foreign key(courseid) references course(courseid),
constraint IG_FK2 foreign key(facultyid) references User_Faculty(userid)
);
The primary key for Course_Interest_group is not just interest_gid, but interest_gid, courseid, and facultyid.
There are several other instances of the same issue.
My question is, when referencing Course_Interest_group how can I reference just interest_gid? Is it possible? Should I change something?
I would like to keep the current Course_Interest_group pk of (interest_gid, courseid, facultyid) intact but I will modify it if needed.
my code
my output
If in the course_interest_group table interest_gid uniquely identifies each row in the table then interest_gid should be the primary key. Similarly, in the group_disforum if df_id uniquely identifies each row in the table then df_id should be the primary key. Modifying the primary keys of both these tables should solve the issue.
I think the way around for your question will be a TRIGGER to customize the data Integrity. You can create a before INSERT Trigger which will check first whether the data in your case (Course_Interest_group(interest_gid)). So basic structure of your Trigger will be like. Hope this may be a workwround.
SET SQLBL ON;
SET DEFINE OFF;
CREATE OR REPLACE TRIGGER group_disforum_tg BEFORE
INSERT OR
UPDATE
/*OF ig_id*/
ON group_disforum FOR EACH ROW
DECLARE lv_chck PLS_INTEGER;
BEGIN
SELECT COUNT(1)
INTO lv_chck
FROM Course_Interest_group
WHERE interest_gid = :new.interest_gid;
IF lv_chck = 0 THEN
RAISE_APPLICATION_ERROR(-20001,'Parent key (interest_gid) not found in group_disforum_tg',TRUE);
END IF;
END;
/
I have two tables (master-detail) I use to record orders, I need to create a trigger that allows me to update the "TOTAL_GENERAL" field that is in the master table with the sum of subtotals in the "SUBTOTAL" field the detail table that are related to the foreign key "ID_ORDEN" but I get an error with the trigger.
tables:
CREATE TABLE "ENCABEZADO_ORDEN"
("ID_ENCABEZADO" NUMBER(10,0),
"NUMERO_ORDEN" NUMBER(10,0),
"FECHA" DATE,
"NOMBRE_CLIENTE" VARCHAR2(50),
"DIRECCION" VARCHAR2(50),
"TOTAL_GENERAL" NUMBER(10,0),
"LUGAR_VENTA" VARCHAR2(50),
CONSTRAINT "ENCABEZADO_ORDEN_PK" PRIMARY KEY ("ID_ENCABEZADO")
USING INDEX ENABLE
)
CREATE TABLE "DETALLE_ORDEN"
("ID_DETALLE" NUMBER(10,0),
"PRODUCTO" VARCHAR2(50),
"PRECIO_UNITARIO" NUMBER(10,2),
"CANTIDAD" NUMBER(10,0),
"SUBTOTAL" NUMBER(10,2),
"ID_ENCABEZADO" NUMBER(10,0),
CONSTRAINT "DETALLE_ORDEN_PK" PRIMARY KEY ("ID_DETALLE")
USING INDEX ENABLE
)
/
ALTER TABLE "DETALLE_ORDEN" ADD CONSTRAINT "DETALLE_ORDEN_FK" FOREIGN KEY ("ID_ENCABEZADO")
REFERENCES "ENCABEZADO_ORDEN" ("ID_ENCABEZADO") ENABLE
/
trigger:
create or replace TRIGGER "CALCULAR_TOTAL_GENERAL"
BEFORE INSERT OR UPDATE ON "DETALLE_ORDEN"
FOR EACH ROW
DECLARE
V_ID_ENCABEZADO NUMBER(10,0);
BEGIN
SELECT "ID_ENCABEZADO"
INTO V_ID_ENCABEZADO
FROM "ENCABEZADO_ORDEN"
WHERE "ID_ENCABEZADO" = :NEW."ID_ENCABEZADO";
UPDATE "ENCABEZADO_ORDEN"
SET "TOTAL_GENERAL" = (SELECT SUM("SUBTOTAL") FROM "DETALLE_ORDEN"
WHERE "ID_ENCABEZADO" = V_ID_ENCABEZADO)
WHERE "ID_ENCABEZADO" = V_ID_ENCABEZADO;
END;
This is the error message I get when I insert or update the table "DETALLE_ORDEN":
1 error has occurred
ORA-04091: table CARLOSM.DETALLE_ORDEN is mutating, trigger/function may not see it
ORA-06512: at "CARLOSM.CALCULAR_TOTAL_GENERAL", line 9
ORA-04088: error during execution of trigger 'CARLOSM.CALCULAR_TOTAL_GENERAL'
Don't use triggers for this kind of logic (for that matter, don't use triggers ever; there's almost always a better way). Also, avoid storing redundant information in base tables whenever possible.
Far better design, with minimal impact to existing code is to
1) rename table "ENCABEZADO_ORDEN" (i.e. to "ENCABEZADO_ORDEN_TAB") and 2) disable/drop "TOTAL_GENERAL" field, and then 3) create a view with original name "ENCABEZADO_ORDEN" as:
CREATE OR REPLACE VIEW ENCABEZADO_ORDEN AS
SELECT O.*, (SELECT SUM(D.SUBTOTAL) FROM DETALLE_ORDEN D
WHERE D.ID_ENCABEZADO = O.ID_ENCABEZADO) TOTAL_GENERAL
FROM ENCABEZADO_ORDEN_TAB O;
This will ensure TOTAL_GENERAL is always correct (in fact, any efforts to set it directly to some other value via update of ENCABEZADO_ORDEN will result in immediate syntax error).
If performance is an issue (i.e. users frequently query TOTAL_GENERAL field in ENCABEZADO_ORDEN table for orders with large numbers of detail records in DETALLE_ORDEN, causing Oracle to repeatedly fetch&sum multitudes of SUBTOTALS) then use a materialized view instead of a basic view.
I have the following tables:
create table emp_test_lucian as select employee_id,last_name,first_name,department_id from employees;
ALTER TABLE emp_test_lucian
ADD PRIMARY KEY (employee_id);
create table dept_test_lucian as select department_id,department_name from departments_copy;
ALTER TABLE dept_test_lucian
ADD PRIMARY KEY (department_id);
On this tables I want to perform different operations for example: If a department gets deleted (from dept_test_lucian) I will delete all the rows in emp_test_lucian that have that department id with a trigger. This works fine when no fk between the 2 is declared with the following code :
CREATE OR REPLACE TRIGGER triger5
BEFORE UPDATE or DELETE on dept_test_lucian
FOR EACH ROW
BEGIN
IF DELETING then
delete
from emp_test_lucian
where department_id = :OLD.department_id;
else if UPDATING('department_id') then
UPDATE emp_test_lucian
set department_id = :NEW.department_id
where department_id = :OLD.department_id;
END IF;
END IF;
END;
/
What can I add to the code above to work even if I have a fk between the 2 tables like so:
ALTER TABLE emp_test_lucian
ADD CONSTRAINT fk_dep_id FOREIGN KEY(department_id) REFERENCES dept_test_lucian(department_id);
the current code returns :
Error report:
ORA-00001: unique constraint (C##LABORATOR.SYS_C009994) violated
ORA-06512: at line 2
00001. 00000 - "unique constraint (%s.%s) violated"
*Cause: An UPDATE or INSERT statement attempted to insert a duplicate key.
For Trusted Oracle configured in DBMS MAC mode, you may see
this message if a duplicate entry exists at a different level.
*Action: Either remove the unique restriction or do not insert the key.
You need to make clear what table is the 'parent' and what table is the 'child'.
In your example:
- Parent: dept_test_lucian
- Child: emp_test_lucian
Lets call 'dept_test_lucian': TableA
Lets call 'emp_test_lucian': TableB
I come to this conclusion since there is a CONSTRAINT on TableB.department_id" that can only have a value that exists
in "TableA.department_id"
The error message tells you that there is a 'primary key being vialated'.
Primary keys on you tables are:
- "TableA.employee_id"
- "TableB.department_id"
Apparently you are trying to insert a value in one of these columns where that value already exists in.
If '1' is already existing in "TableA.employee_id" you would get such an error.
What I also see in your trigger is:
You have a BEFORE UPDATE Trigger.
So the Trigger looks if there is an UPDATE comming on "TableA" (Parent).
Then you try to UPDATE "TableB" (child) first.
This could be tricky, since "TableB.department_id" can only have values that exist in "TableA.department_id".
If the new UPDATE value doesn't exist in "TableA.department_id", you can not UPDATE that value in "TableB.department_id"
I am trying to create a trigger which will enter values into a table terminated_employees when we delete values from the nm_employees table. I have written the trigger but it does not work. Is my trigger format right? Any ideas?
CREATE TABLE nm_departments(
dept2 varchar(20),
CONSTRAINT empPK PRIMARY KEY (dept2)
);
CREATE TABLE nm_employees(
name varchar(20),
dept varchar(20),
CONSTRAINT departments FOREIGN KEY (dept) REFERENCES nm_departments (dept2)ON DELETE CASCADE
);
CREATE TABLE terminated_employees(
te_name varchar(20),
te_dept varchar(20)
);
CREATE TRIGGER term_employee AFTER DELETE ON nm_employee
FOR EACH ROW
BEGIN
INSERT INTO terminated_employees (NEW.te_name, NEW.te_dept) VALUES (OLD.name,OLD.dept)
END;
You should not be specifying the NEW. on the column names of your INSERT statement. These are the columns in the terminated_employees table, NOT the new values. i.e.
INSERT INTO terminated_employees (te_name, te_dept)
VALUES (OLD.name,OLD.dept)
You can use show errors (or show err) in SQL*Plus to see the exact error.
You have a number of problems:
Wrong table name on create trigger (missing the s)
Missing ; after instert statement
The OLD. need to have : prefix. i.e. :OLD.name