oracle: deferring Foreign Key checks not working - oracle

oracle: deferring Foreign Key checks not working. e.g.,
create table Foo (id number(20,0), name varchar(20),
primary key(id));
create table Bar (id number(20,0), name varchar(20),
primary key(id),
constraint FK1 foreign key (id) references Foo (id));
insert into Foo(id,name) values(1, 'foo');
insert into Bar(id,name) values(1, 'bar');
delete data:
SET CONSTRAINTS ALL DEFERRED;
delete from Foo;
delete from Bar;
SET CONSTRAINTS ALL IMMEDIATE;
ERROR:
ORA-02292: integrity constraint violated - child record found

You can't defer a constraint if it is NOT DEFERRABLE. A constraint can be either deferrable or not, but the default (if you don't explicitly specify either way) is NOT DEFERRABLE.
In your sample code, add the keyword deferrable right after the foreign key constraint definition, and then run everything again. It will work as expected.
That is: Edit the following line of code
constraint FK1 foreign key (id) references Foo (id));
to
constraint FK1 foreign key (id) references Foo (id) deferrable);

Related

I'm trying to create foreign key between two tables but got: ORA-02270

Maybe I'm burnout, but I don't understand this. I have two tables in Oracle: TBL_a and TBL_x. I'm trying to create a foreign key between thos two tables as follows and get error
ORA-02270: no matching unique or primary key.
CREATE TABLE tbl_a (
cod_op integer,
cod_dni char(8),
cod_correl integer,
varchar2(50)
);
CREATE TABLE tbl_x (
cod_op integer,
cod_dni char(8),
blabla varchar2(50)
);
CREATE UNIQUE INDEX TBL_A_PK ON TBL_A (COD_OP);
CREATE UNIQUE INDEX TBL_x_PK ON TBL_x (COD_OP);
ALTER TABLE TBL_a ADD CONSTRAINT TBL_a_R01
FOREIGN KEY (COD_OP) REFERENCES TBL_x (COD_OP);
The problem is that you've created unique INDEXES on your tables, but you didn't create a unique or primary key CONSTRAINT. Oracle requires that the constraints exist in order to establish a foreign key relationship.
If you drop your existing indexes and add the appropriate constraints you can establish your foreign key relationship:
DROP INDEX TBL_A_PK;
DROP INDEX TBL_x_PK;
ALTER TABLE TBL_A
ADD CONSTRAINT UQ_A
UNIQUE(COD_OP)
USING INDEX;
ALTER TABLE TBL_X
ADD CONSTRAINT UQ_X
UNIQUE(COD_OP)
USING INDEX;
dbfiddle here
The table that is referred by the foreign key (here, tbl_x) must have a primary key or a unique constraint.
In your use case, as you are declaring a unique index on cod_op, you could simply make cod_op the primary key of tbl_x instead: that would make the error disappear.
Demo on DB Fiddle
In general, it is a good practice to have a primary key on any table. Extending the principe of turning your unique indexes to primary keys, your DDL statements could be simplified as follows:
CREATE TABLE tbl_x (
cod_op INTEGER PRIMARY KEY,
cod_dni CHAR(8),
blabla VARCHAR2(50)
);
CREATE TABLE tbl_a (
cod_op INTEGER PRIMARY KEY,
cod_dni CHAR(8),
cod_correl INTEGER,
blabla VARCHAR2(50),
CONSTRAINT TBL_a_R01 FOREIGN KEY (COD_OP) REFERENCES TBL_x (COD_OP)
);
Demo on DB Fiddle

Oracle foreign key constraints - check constraint syntax?

I have a child table in oracle that has two foreign key columns, relating to two different parent tables. I want to create a constraint that says the child must have at least one of those parents - e.g.
ALTER TABLE table_name
ADD CONSTRAINT constraint_name
FOREIGN KEY (column1)
REFERENCES parent_table (column1)
OR
FOREIGN KEY (column2)
REFERENCES parent_table_2 (column1)
This won't work with a foreign key constraint because that can only relate to one parent table - is it possible to do this with a check constraint instead?
Foreign key constraints ensure the referential integrity, not mandatory values.
I think you have to have to separate FK contraints and additional check constraint like this:
alter table table_name
add constraint c_check_cols
check(column1 is not null or column2 is not null);
You can do it with constraints:
ALTER TABLE table_name
ADD CONSTRAINT constraint_name_1
FOREIGN KEY (column1)
REFERENCES parent_table (column1);
ALTER TABLE table_name
ADD CONSTRAINT constraint_name_2
FOREIGN KEY (column2)
REFERENCES parent_table_2 (column1);
ALTER TABLE table_name
ADD CONSTRAINT constraint_name_3
check (COALESCE(column1, column2) IS NOT NULL);
Of course for column1 and column2 column you must permit NULL values.

Oracle foreign key allow one extra specific value

I have one simple table:
-- Create table
create table FAVOURITE_RULES
( rule_id NUMBER(9) not null,
user_id NUMBER(9) not null);
-- Create/Recreate primary, unique and foreign key constraints
alter table FAVOURITE_RULES
add constraint FAV_RULES_PK primary key (RULE_ID, USER_ID)
alter table FAVOURITE_RULES
add constraint FAV_RULES_RULE_ID_FK foreign key (RULE_ID)
references RULES (RULE_ID) on delete cascade;
alter table FAVOURITE_RULES
add constraint FAV_RULES_USER_ID foreign key (USER_ID)
references USER_AUTHENTICATION (USER_ID) on delete cascade;
I have a rule (from .Net code) that doesn't exist in the original table RULES. It has the Id=-999.
When I try to insert into the FAVOURITE_RULES I get an error about integrity constraint violation (as expected) (FAV_RULES_RULE_ID_FK) violated - parent key not found.
Can I keep the foreign key (FAV_RULES_RULE_ID_FK ) and allow extra only this value (-999) to be inserted?
May be this can help.
Step 1: drop fk constraint
Step 2: insert your violating row
Step 3: again create fk constraint with ENABLE NOVALIDATE

Creating table in Oracle 11g with multiple foreign keys with - ORA-00922: missing or invalid option

I followed the previous instruction of placing commas after each of the CONSTRAINTS. However, on this table, it's giving me the following error message:
ORA-02264: name already used by an existing constraint
All the foreign key tables that are associated with this table are created successfully. What is missing here?
CREATE TABLE FIELD (
ENCT_ID VARCHAR2(25) NOT NULL,
FLD_NUM NUMBER NOT NULL,
FLD_DESC VARCHAR2(50) NOT NULL,
SYMPT_CODE VARCHAR2(25),
DIAG_CODE VARCHAR2(25),
TEST_ID VARCHAR2(25),
RM_ID VARCHAR2(10) NOT NULL,
AX_CODE VARCHAR2(25) NOT NULL,
PROV_ID VARCHAR2(25) NOT NULL,
MED_NDC VARCHAR2(25),
PRIMARY KEY (ENCT_ID, FLD_NUM),
CONSTRAINT FK_ENCOUNTER FOREIGN KEY (ENCT_ID) REFERENCES ENCOUNTER(ENCT_ID),
CONSTRAINT FK_SYMPTOM FOREIGN KEY (SYMPT_CODE) REFERENCES SYMPTOM(SYMPT_CODE),
CONSTRAINT FK_DIAGNOSIS FOREIGN KEY (DIAG_CODE) REFERENCES DIAGNOSIS(DIAG_CODE),
CONSTRAINT FK_TEST FOREIGN KEY (TEST_ID) REFERENCES TEST(TEST_ID),
CONSTRAINT FK_ROOM FOREIGN KEY (RM_ID) REFERENCES ROOM(RM_ID),
CONSTRAINT FK_ASSESSMENT FOREIGN KEY (AX_CODE) REFERENCES ASSESSMENT(AX_CODE),
CONSTRAINT FK_PROVIDER FOREIGN KEY (PROV_ID) REFERENCES PROVIDER(PROV_ID),
CONSTRAINT FK_MEDICATION FOREIGN KEY (MED_NDC) REFERENCES MEDICATION(MED_NDC));
CREATE TABLE FIELD (
ENCT_ID VARCHAR2(25) PRIMARY KEY,
FLD_NUM NUMBER PRIMARY KEY,
FLD_DESC VARCHAR2(50) NOT NULL,...
Remove any one primary key. It will be the index for the table. Like a School text books have only one index, same way, every table will have only one primary key.

ORA-02256: referencing foreign key

I'm a SQL newbie and having some troubles with referencing a foreign KEY
My Event Table:
create table Event
(
Bookid number(5),
edate date,
FacID int,
GuestID int,
CONSTRAINT pk1edate PRIMARY KEY (edate,Bookid),
CONSTRAINT fk1Bookid FOREIGN KEY (Bookid) references BOOK (Bookid),
CONSTRAINT fk2FacID FOREIGN KEY (FacID) references CUSTOMER (SponsorID),
CONSTRAINT fk3GuestID FOREIGN KEY (GuestID) references CUSTOMER (SponsorID)
);
and my event_register table
create table EVENT_REGISTER
(
CID number(6),
Bookid number(5),
edate date,
CONSTRAINT pk1Edate PRIMARY KEY (edate,Bookid),
CONSTRAINT fk2Bookid FOREIGN KEY (Bookid) references BOOK,
CONSTRAINT fk3edate FOREIGN KEY (edate) references Event (edate,Bookid)
);
I get this error when I try to run the Event_Register:
ERROR at line 9:
ORA-02256: number of referencing columns must match referenced columns
You need to specify all the columns in both parent and child tables while referring.
create table EVENT_REGISTER
(
CID number(6),
Bookid number(5),
edate date,
CONSTRAINT pk1Edate PRIMARY KEY (edate,Bookid),
CONSTRAINT fk2Bookid FOREIGN KEY (Bookid) references BOOK(Bookid),
CONSTRAINT fk3edate FOREIGN KEY (edate,Bookid) references Event (edate,Bookid)
);

Resources