如何修复“ ORA-02270:此列列表没有匹配的唯一键或主键”在SQL中的错误

时间:2019-04-26 14:30:22

标签: sql oracle sqlplus

此问题发生在FLIGHT表上,

create table LOCATION (  
  airportCode   VARCHAR2(10) not null,  
  country       VARCHAR2(15) not null,  
  address       VARCHAR2(50) not null,  
  PRIMARY KEY(airportCode));

create table ROUTE (  
  routeID           VARCHAR2(10) not null,  
  airportCode       VARCHAR2(10) not null,  
  description       VARCHAR2(100) not null,  
  PRIMARY KEY(routeID, airportCode),
  FOREIGN KEY(airportCode) REFERENCES LOCATION(airportCode));

create table NEACC (  
  memberID          VARCHAR2(20),  
  flightGoldpoints  NUMBER,  
  PRIMARY KEY(memberID));

create table STAFF (  
  staffID           VARCHAR2(20) not null,  
  memberID          VARCHAR2(20) not null,
  name              VARCHAR2(20) not null,
  address           VARCHAR2(50) not null,
  email             VARCHAR2(30) not null,  
  phone             NUMBER not null,
  passportNum       NUMBER not null,
  PRIMARY KEY(staffID),
  FOREIGN KEY(memberID) REFERENCES NEACC(memberID));

create table FLIGHT (   
  flightID          VARCHAR2(20) not null,   
  routeID           VARCHAR2(20) not null,   
  staffID           VARCHAR2(20) not null, 
  captainOrFirstOfficer VARCHAR2(15) CHECK (captainOrFirstOfficer IN ('captain', 'first officer')) not null,
  estDepartDateTime VARCHAR2(20) not null,   
  actDepartDateTime VARCHAR2(20) not null,   
  actArriveDateTime VARCHAR2(20) not null, 
  avgSpeed          NUMBER not null, 
  avgHeight         NUMBER not null, 
  estDuration       NUMBER not null, 
  estFuel           NUMBER not null, 
  PRIMARY KEY(flightID, routeID), 
  FOREIGN KEY(routeID) REFERENCES ROUTE(routeID), 
  FOREIGN KEY(staffID) REFERENCES STAFF(staffID));

我得到的错误是“ ORA-02270:此列列表没有匹配的唯一键或主键”。飞行表中正确引用了外键和主键,但我不明白为什么会出现此错误。

1 个答案:

答案 0 :(得分:2)

Route表中的主键是:

PRIMARY KEY(routeID, airportCode),

正确的外键引用需要依次引用两者。那应该是:

FOREIGN KEY(routeID, airportCode) REFERENCES ROUTE(routeID, airportCode), 

但是,FLIGHT没有airportCode,因此无法正常工作。

我不确定您要朝哪个方向走-从主键中删除airportCode或将其添加到Flight表中。但这是你的问题。