SQL Error: ORA-02298: cannot validate (SYSTEM.AEROPUERTO_FK) - parent keys not found - sql

I'm getting the following error in on Oracle SQL Developer:
Error starting at line 1 in command:
ALTER TABLE AEROPUERTO ADD CONSTRAINT AEROPUERTO_FK FOREIGN KEY (CODIGO_CIUDAD) REFERENCES CIUDAD(CODIGO)
Error report:
SQL Error: ORA-02298: cannot validate (SYSTEM.AEROPUERTO_FK) - parent keys not found
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
Why?

There are records in AEROPUERTO that point to records that do not exist in CIUDAD.
To find out which records of AEROPUERTO have that kind of issue:
select * from AEROPUERTO where CODIGO_CIUDAD not in (select CODIGO from CIUDAD)
If the result set is not empty, you do have orphanaged records. You'll need to add the missing CIUDAD records in order to create the AEROPUERTO_FK foreign key, or update all the erroneous AEROPUERTO.CODIGO_CIUDAD to null (if this is a nullable field, but you will lose the city information for those airport records).

remove primary key constraint from the column in which you want to add the foreign key

Related

Can't modify a table to add a foreign key

I have 2 tables
Songs (SONGID, SONGNAME, ALBUM, ARRANGER, VOCALISTS, LYRCISTS)
Source_songs (Source_Song_Name, ID_ACTUALSONGNAME)
and I can't modify Songs to add a foreign key
ALTER TABLE SONGS
ADD CONSTRAINT Original_Song_Name
ADD FOREIGN KEY (Source_song_id)
REFERENCES Source_SongName (SOURCE_SONGNAME);
And I get this error. I know I'm spelling the column name right idk what to do.
Error:
SQL Error: ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
Alter table Songs ADD CONSTRAINT FK_ID FOREIGN KEY(source_song_id) references ...
Use ADD keyword only once
What is so hard to understand? The syntax is references <table_name> ( <column_name> ). Your table name is not Source_SongName, it is Source_songs, and it doesn't have any column named SOURCE_SONGNAME. You are missing the second underscore _ in Source_Song_Name.
Besides, it is strange that you would have a foreign key on a column (Source_song_id) that doesn't exist in your table.
Or is all the information you provided made up?

Parent Keys Not Found?

One of the classic questions...
But I really can't find where the issue is in the parent table. I am currently working on a mini-version of the IMDB database. Our teacher tasked us with taking the original database we were given and make it into a smaller, and smarter version. It should follow this diagram:
I have so far successfully made the COMPANY_NAME, MOVIE_COMPANY, MOVIE, GENRE and PLOT tables and is currently making KEYWORD. The table itself now exists, but I cannot place the Foreign constraint which indicates that the MOVIE_ID present in keyword is from the parent table MOVIE. I keep getting the following error:
ALTER TABLE KEYWORD
ADD CONSTRAINT FK_MOVIE_ID_KEYWORD
FOREIGN KEY (MOVIE_ID)
REFERENCES MOVIE (ID)
Error report -
SQL Error: ORA-02298: cannot validate (DB_031.FK_MOVIE_ID_KEYWORD) - parent keys not found
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 <-- especially this part is annoying
I have looked around at similar questions and then checked my statement a few times, but I have so far been unable to see why this fails. The GENRE and PLOT tables are both empty in terms of data, as they are made for future expansion of the database. But KEYWORD already have a collection of KEYWORD, MOVIE_ID pairs.
EDIT: Request Schemas
You are referencing to wrong col for parent reference....it should be MOVIE_ID rather than ID ( which u have done )
try it this way :
ALTER TABLE KEYWORD
ADD CONSTRAINT FK_MOVIE_ID_KEYWORD
FOREIGN KEY (MOVIE_ID)
REFERENCES MOVIE (MOVIE_ID) /* <- notice this */
EDIT
Only possible reason i can think of is that you table contains non-matching data....that's why you are having an error due to mismatch!!
To simplyfy....your MOVIE_ID contains value which do not exist in the ID column of the other table....so because of mismatch at the time of setting up constraint,its popping the error!!
Solution : Validate the data in your tables ( updating / deleting non-matching columns in either tables ) in relation to the id's you need to set up...then apply the constraint and it should work!!

Error report: SQL Error: A foreign key value has no matching primary key value. Completely baffled

I can safely say that I am new to SQL and therefore upon writing the code to insert data into a table, I received an error report, that I can't seem to understand what it means, therefore I am hoping someone out there may be able to tell me what silly mistake I am making and remove a lot of stress ^.^
This is the error code I got:
Error report:
SQL Error: ORA-02291: integrity constraint (H.VENDOR_ID_FK) 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.
Thanks in advance!
If I run all of that I get several errors before this point, which I'll skip over for now... the first one against this FK seems to be:
INSERT INTO parts_order VALUES
(2
,2
,95115995
,'Delivered'
,'04/dec/2012'
,'01/jan/2013'
,'20/dec/2012'
,'Handler Pro'
);
It's better to put the columns in the insert clause so you can see what lines up (i.e. INSERT INTO parts_order (order_id, job_id, vendor_id, ...) VALUES (4, 4, 95115995, ...), and also so you (and we) don't have to refer back to the table definition, and to avoid failures if the definition changes in the future. It's also better not to rely on implicit date conversions (i.e. use to_char('05/jan/2013', 'DD/mon/YYYY').
Anyway... the constraint it's complaining about is VENDOR_ID_FK, which we can see in the table definition:
CREATE TABLE parts_order
( order_id NUMBER(11)
CONSTRAINT order_id_pk PRIMARY KEY
,job_id NUMBER(11)
CONSTRAINT job_id_fk REFERENCES maintenance(job_id)
,vendor_id NUMBER(11)
CONSTRAINT vendor_id_fk REFERENCES parts_vendor(part_vendor_id)
,parts_status VARCHAR2(20)
,date_ordered DATE
,date_arrived DATE
,date_delivery_due DATE
,part_name VARCHAR2(20)
CONSTRAINT part_name_nn NOT NULL);
... is against parts_vendor(part_vendor_id). What the error is saying is that the vendor_id you're inserting, 95115995, doesn't exist in the parent parts_vendor table - which is true, you only insert records with part_vendor_id values 1, 2, 3 and 4.
The constraint is working as intended - it's stopping you putting a 'child' record in without its 'parent' existing. You either need to create a parts_vendor record for ID 95115995, or change the vendor_id value you're trying to insert into parts_order to one that already exists.

PostgreSQL Database - Error Assistance

I'm trying to create a query that will update the value of rgn_no and chp_cd within the table bue to the values of rgn_no and chp_cd within the table chapterassociation. I don't think the WHERE clause is any problems, but I'm getting the following error when I run it in:
SQL error:
ERROR: insert or update on table "bue" violates foreign key constraint "bue_chp_cd_fkey"
DETAIL: Key (chp_cd)=(CA3) is not present in table "chapter".
Any assistance is greatly appreciated!
SQL Query:
UPDATE bue SET
rgn_no = chapterassociation.rgn_no,
chp_cd = chapterassociation.chp_cd
FROM
chapterassociation
WHERE
bue.mbr_no IS NULL AND bue.chp_cd IS NULL
AND bue.work_state = chapterassociation.work_state
AND bue.bgu_cd = chapterassociation.bgu_cd
Read the error message:
ERROR: insert or update on table "bue" violates foreign key constraint
"bue_chp_cd_fkey" DETAIL: Key (chp_cd)=(CA3) is not present in table "chapter".
It says that one of your updates wants to set chp_cd to 'CA3', but that CA3 is not an allowed value, because a foreign key constraint wants the value 'CA3" to be present in the table "chapter". That's all.
There is nothing wrong with your query's syntax, it is just the fact that your query would cause the data to conflict with a constraint in the data model.

How to add cascade delete in oracle when the foreign key is on the same table

I have a table that contains a tree in oracle.
MY_TABLE
node_id
parent_id
How do I add a cascade delete when the root of the tree is not going to have a parent?
Right now the parent id is set to -1 for the root. When I try this I get the following error:
Error starting at line 1 in command:
ALTER TABLE regional_defaults_working
add CONSTRAINT regional_defaults_wk_delete
FOREIGN KEY (parent_id)
REFERENCES regional_defaults_working(node_id)
ON DELETE CASCADE
Error report:
SQL Error: ORA-02298: cannot validate (XVTEST.REGIONAL_DEFAULTS_WK_DELETE) - parent keys not found
02298. 00000 - "cannot validate (%s.%s) - parent keys not found"
*Cause: an alter table validating constraint failed because the table has
child records.
I can't recall if this will work or not, but my first impulse is to say set parent_id=NULL for the root.
Figured it out.
The key (no pun intended) is to make your parent_id nullable and then set your root to be null.