ORA-02298 - Parent keys not found when enable constraint - oracle

I have a migration script in between 2 different schema database. The script does 3 things:
Disable constraint
Insert record from old schema to new schema
Enable constraint
During enable constraint, it encouter ORA-02298 - Parent keys not found: at the following 2 tables:
ALTER TABLE COUNTRY ENABLE CONSTRAINT COUNTRY_FK1;
ALTER TABLE EMPLOYEE ENABLE CONSTRAINT EMPLOYEE_FK7;
Anything went wrong in the table structure definition of these 2 tables?

It appears you are migrating detail records without ensuring that all the foreign key values are present in the referenced tables. If this is the case then you need to migrate records from REGION#SOURCE_DB into REGION#TARGET_DB before you migrate the COUNTRY records.

Related

Unable to delete a record when created foreign-key constraints manually in mysql without using migration.

I have created database tables manually and i have also created foreign key constraints in tables manually without using migration. now when i am going to delete a record it giving me following error-
Integrity constraint violation: Cannot delete or update a parent row:
a foreign key constraint fails
Note: i can't use migration because database was already created.
Use ON DELETE CASCADE and ON UPDATE CASCADE on foreign keys, like:
...(create/alter Children table query)...
CONSTRAINT FK_ParentChild
FOREIGN KEY (parent_id) REFERENCES Parents(id)
ON UPDATE CASCADE ON DELETE CASCADE;
So you don't have to manually delete children before deleting the parent element. All children records will automatically delete along with the deletion of parent record.
I would suggest investigating this further by doing the follow:
On your development environment, write migrations to create the
tables from scratch 2)
Look at the generated schema and compare it
to the schema of your existing production tables
Note the
differences between the two schema and write migrations to correct
the production tables.
Note:
If you need to correct the data in the meantime and you are confident that the data integrity of your database is not at risk. You can use the following statements to drop the foreign key checks whilst you correct the data.
SET FOREIGN_KEY_CHECKS = 0;
SET FOREIGN_KEY_CHECKS = 1;
I can't stress enough, remember to turn on your foreign key checks once you are finished and do not do this on a production database. Take a copy of the database and try things locally until you are confident that your corrections are 100% safe ;)

Oracle Materialized View in Entitiy Framework issue

I have the following issue.
1. I created an Oracle Materialized View which contains the "WITH PRIMARY KEY" clause.
2. when I am trying to add that View to my EDMX, I encounter the following error:
"The table/view 'XXX' does not have a primary key defined and no valid primary key could
be inferred..."
Does anyone know how to add a primary key to Materialized View that can be added to EDMX?
Is this issue solvable ?
thanks,
Hagai
This clause refers to the materialised view including a primary key column from the master table, not that it possesses a primary key itself.
A materialised view is simply metadata on a regular (base) table, so you can perform an alter table command using the name of the the materialised view to create a primary key on the base table.

Oracle Database with very few foreign key constraints

I've just started a new project and I am confronted with a production application Oracle 10g database that has just 3 foreign key constraints. I am not used to seeing databases with no foreign key constraints. I am guessing that there may be some performance/concurrency considerations to not using FKs. The reason is that in the logical database schema the architect has specified all the relationships, but these relationships are not implemented in the database as Foreign Key constraints.
Question: I read that I can define a Foreign Key Constraint with RELY NOVALIDATE that will not impact performance. Is it worth while to define RELY FK constraints on this database just so that the relationship can be easily seen? this application is not built using ORM, is it really worth while to do without foreign keys?
The database is denormalised with example below
Table 1 : FINProduct(ID (number), Description(varchar(5)), FINproductCode(varchar(10))...)
Table 2: FINProductCode(ID (number, FINproductCode(varchar(10)) , LastUpdated(datetime)...)
So instead of having a relationship between Tables 1 and 2 the FINproductCode column is just replicated in table 1.
It's too early to drink but I think i need one!
I would be very wary about assuming that the absence of foreign key constraints was a reasoned response to performance issues. There is an overhead to enforcing a foreign key constraint (particularly where appropriate indexes are missing) but it is incredibly unlikely that your application can validate the constraint more efficiently than Oracle can. So the question really is whether you want the small overhead of foreign key constraints or the near certainty that you will get invalid data inserted into the database. It would be extremely unlikely that this is a trade-off that you want to make-- I've yet to meet a business user that would be happy to capture incorrect and incomprehensible data even if doing so was a bit faster than capturing correct data.
Unless there is substantially more background, I would tend to create all the missing foreign key constraints. Creating RELY NOVALIDATE constraints is possible but it defeats the major benefit of foreign key constraints-- preventing invalid data from entering the database in the first place.
It depends on whether you want to add the FK only for documentation purposes or whether you want to prevent future INSERTs/UPDATEs with an invalid FK value.
If you want it only for documentation purposes, I'd create the FK constraint with RELY NOVALIDATE and DISABLE it afterwards - otherwise, Oracle will check it for future INSERTs / UPDATEs.
However: DON'T DO THIS UNLESS YOU ABSOLUTELY NEED IT!
I agree with Justin Cave: In most cases, you should just add "plain" FK constraints - this way, you can ensure that your existing data is correct.
I would try to create the constraints and report violations into a exception table. Fix the data and enable the constraint.
Create some test data
create table parent (pk integer
,data varchar2(1)
,CONSTRAINT PARENT_PK PRIMARY KEY (PK) ENABLE );
create table child (pk integer
,pk_parent integer
,data varchar2(1)
,CONSTRAINT CHILD_PK PRIMARY KEY (PK) ENABLE );
insert into parent values (1,'a');
insert into parent values (2,'b');
insert into child values (1,1,'a');
insert into child values (2,2,'b');
insert into child values (3,3,'c');
Create a foreign key constraint:
alter table child add constraint fk_parent foreign key(pk_parent) references parent(pk);
SQL Error: ORA-02298: Kan (ROB.FK_PARENT) niet valideren - bovenliggende sleutels zijn niet gevonden.
02298. 00000 - "cannot validate (%s.%s) - parent keys not found"
*Cause: an alter table validating constraint failed because the table has
child records.
*Action: Obvious
Create the foreign key with 'enable novalidate' option
alter table child add constraint fk_parent foreign key(pk_parent) references parent(pk) enable novalidate;
table CHILD altered.
insert into child values (4,4,'c');
SQL Error: ORA-02291: Integriteitsbeperking (ROB.FK_PARENT) is geschonden - bovenliggende sleutel is niet gevonden.
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.
No new data violating the FK can be inserted.
Now let's fix the data already in the table that violates the FK constraint
Create an exceptions table and try to enable the constraint:
create table exceptions(row_id rowid,
owner varchar2(30),
table_name varchar2(30),
constraint varchar2(30));
ALTER TABLE child ENABLE constraint fk_parent EXCEPTIONS INTO EXCEPTIONS;
Error report:
SQL Error: ORA-02298: Kan (ROB.FK_PARENT) niet valideren - bovenliggende sleutels zijn niet gevonden.
02298. 00000 - "cannot validate (%s.%s) - parent keys not found"
*Cause: an alter table validating constraint failed because the table has
child records.
*Action: Obvious
Check the exceptions table for problems:
select * from exceptions;
ROW_ID OWNER TABLE_NAME CONSTRAINT
------ ------------------------------ ------------------------------ ------------------------------
AABA78 ROB CHILD FK_PARENT
AAFAAA
Ow9AAC
select * from child where rowid = 'AABA78AAFAAAOw9AAC';
Fix the problem
delete from child where pk = 3;
1 rows deleted.
ALTER TABLE child ENABLE constraint fk_parent EXCEPTIONS INTO EXCEPTIONS;
table CHILD altered.
Constraint enabled and data correct

how to drop and add multi constraints by 1 sql statement for H2 database

I'm working on H2 database, and I meet this problem -
to drop one constraint is fine, I can use this statement
alter table customer drop constraint if exists fk_customer_order ;
for add one constraint is fine too, I can use this statement.
alter table customer add constraint fk_customer_order foreign key (order_id) references order (id) on delete cascade on update cascade;
but the problems is, in customer table I have more foreign key and I want delete them in one query statement.
Something like this
alter table customer drop constraint fk_customer_order
drop constraint fk_customer_information
drop constraint ....
but this seem can not be done in h2 database, anyone can tell me can or not add or drop multi constraint by 1 sql statment? Any answer are welcome and I appreciate much.
I think it can not be done. Why don't you use multiple statements?

Create constraint in alter table without checking existing data

I'm trying to create a constraint on the OE.PRODUCT_INFORMATION table which is delivered with Oracle 11g R2.
The constraint should make the PRODUCT_NAME unique.
I've tried it with the following statement:
ALTER TABLE PRODUCT_INFORMATION
ADD CONSTRAINT PRINF_NAME_UNIQUE UNIQUE (PRODUCT_NAME);
The problem is, that in the OE.PRODUCT_INFORMATION there are already product names which currently exist more than twice.
Executing the code above throws the following error:
an alter table validating constraint failed because the table has
duplicate key values.
Is there a possibility that a new created constraint won't be used on existing table data?
I've already tried the DISABLED keyword. But when I enable the constraint then I receive the same error message.
You can certainly create a constraint which will validate any newly inserted or updated records, but which will not be validated against old existing data, using the NOVALIDATE keyword, e.g.:
ALTER TABLE PRODUCT_INFORMATION
ADD CONSTRAINT PRINF_NAME_UNIQUE UNIQUE (PRODUCT_NAME)
NOVALIDATE;
If there is no index on the column, this command will create a non-unique index on the column.
If you are looking to enforce some sort of uniqueness for all future entries whilst keeping your current duplicates you cannot use a UNIQUE constraint.
You could use a trigger on the table to check the value to be inserted against the current table values and if it already exists, prevent the insert.
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14251/adfns_triggers.htm
or you could just remove the duplicate values and then enfoce your UNIQUE constraint.
EDIT: After Jonearles and Jeffrey Kemp's comments, I'll add that you can actually enable a unique constraint on a table with duplicate values present using the NOVALIDATE clause but you'd not be able to have a unique index on that constrained column.
See Tom Kyte's explanation here.
However, I would still worry about how obvious the intent was to future people who have to support the database. From a support perspective, it'd be more obvious to either remove the duplicates or use the trigger to make your intent clear.
YMMV
You can use deferrable .
ALTER TABLE PRODUCT_INFORMATION
ADD CONSTRAINT PRINF_NAME_UNIQUE UNIQUE (PRODUCT_NAME)
deferrable initially deferred NOVALIDATE;

Resources