database SQL 错误:ORA-02298:无法验证 (SYSTEM.AEROPUERTO_FK) - 未找到父键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4702310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:00:10  来源:igfitidea点击:

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

databaseoracleoracle10g

提问by andandandand

I'm getting the following error in on Oracle SQL Developer:

我在 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?

为什么?

回答by rsenna

There are records in AEROPUERTOthat point to records that do not exist in CIUDAD.

有记录在AEROPUERTO那个点到不存在的记录CIUDAD

To find out which records of AEROPUERTOhave that kind of issue:

要找出哪些记录AEROPUERTO有这种问题:

select * from AEROPUERTO where CODIGO_CIUDAD not in (select CODIGO from CIUDAD)

If the result set is not empty, you dohave orphanaged records. You'll need to add the missing CIUDADrecords in order to create the AEROPUERTO_FKforeign key, orupdate all the erroneous AEROPUERTO.CODIGO_CIUDADto null (if this is a nullable field, but you will lose the city information for those airport records).

如果结果集不为空,则您确实有孤立记录。您需要添加缺失的CIUDAD记录以创建AEROPUERTO_FK外键,或将所有错误更新AEROPUERTO.CODIGO_CIUDAD为空(如果这是一个可为空的字段,但您将丢失这些机场记录的城市信息)。

回答by Amit Kumar

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

从要添加外键的列中删除主键约束