1

次の 2 つのエラーが発生しています。

エラー: 参照されるテーブル "flight" の指定されたキーに一致する一意の制約がありません

***エラー** *

エラー: 参照されるテーブル "flight" の指定されたキーに一致する一意の制約がありません

これは私のコードです:

CREATE TABLE Staff (
 EmployeeNumber int NOT NULL,
 FirstName char(15) NOT NULL,
 LastName char(15) NOT NULL,
 SocialSecurity int NOT NULL,
 Sex  char(1) NOT NULL,
 Address  char(20) NOT NULL,
 City  char(20) NOT NULL,
 Province char(15) NOT NULL,
 Country  char(20) NOT NULL,
 Primary Key (EmployeeNumber)
 );

CREATE TABLE FlightAttendent (
 FALN  int,
 StaffRole char (20) NOT NULL,
 EmployeeNumber int NOT NULL,
 Foreign Key (EmployeeNumber) References Staff(EmployeeNumber),
 Primary Key (FALN)
 );

Create TABLE AircraftType (
 ACType  char (10),
 Instrument char(1) NOT NULL,
 Engines  int NOT NULL,
 CrewCount int NOT NULL,
 PassengerCount int NOT NULL,
 Primary Key (ACType)
 );


CREATE TABLE Pilot (
 PILN  int,
 MedicalValid date NOT NULL,
 StaffRole char (20) NOT NULL,
 EmployeeNumber  int NOT NULL,
 AircraftType char (10) NOT NULL,
 Foreign Key (EmployeeNumber) references Staff(EmployeeNumber),
 Foreign Key (AircraftType) References AircraftType(ACType),
 Primary Key (PILN)
 );

Create TABLE Aircraft (
 AircraftID  char(6) NOT NULL,
 AircraftManufacturer char(10) NOT NULL,
 AircraftType  char(10) NOT NULL,
 Foreign Key (AircraftType) References AircraftType(ACType),
 Primary Key (AircraftID)
 );

CREATE Table Airport (
 AirportCode char(4) NOT NULL,
 AirportName char(40) NOT NULL,
 City  char(20) NOT NULL,
 Country  char(20) NOT NULL,
 Continent char(20) NOT NULL,
 Primary Key (AirportCode)
 );

Create TABLE Flight (
 FlightID  char (20),
 FlightDate  date,
 AircraftID  char(6) NOT NULL,
 ArrivalAirport  char(4) NOT NULL,
 DepartureAirport char(4) NOT NULL,
 PRIMARY KEY (FlightID, FlightDate),
 FOREIGN Key (ArrivalAirport) references Airport(AirportCode),
 FOREIGN Key (DepartureAirport) references Airport(AirportCode),
 FOREIGN KEY (AircraftID) references Aircraft(AircraftID)
 );


Create TABLE FlightCrew (
 FlightID char (20) REFERENCES Flight(FlightID) ON DELETE CASCADE,
 FlightDate date REFERENCES Flight(FlightDate) ON DELETE CASCADE,
 EmployeeNumber int NOT NULL,
 StaffRole char(20) NOT NULL,
 PRIMARY KEY(FlightID, FlightDate),
 Foreign Key (EmployeeNumber) references Staff(EmployeeNumber)
 );

CREATE Table Passenger (
 PassengerNumber int,
 PassportNumber int NOT NULL,
 Citizenship char (20) NOT NULL,
 FirstName char (20) NOT NULL,
 LastName char (20) NOT NULL,
 Primary Key (PassengerNumber)
 );

CREATE Table PassengerManifest (
 FlightID char(20),
 FlightDate date,
 PassengerNumber int NOT NULL,
 Foreign Key (FlightDate) References Flight(FlightDate),
 Foreign Key (PassengerNumber) References Passenger(PassengerNumber),
 Primary Key (FlightID, FlightDate)
 );

私は何を間違えましたか?ありがとう!

4

1 に答える 1

5

主キーに複数の値がある場合は、それを外部キーとして別の方法で参照する必要があります。

基本的に言うと

FlightID char (20) REFERENCES Flight(FlightID) ON DELETE CASCADE,

PostgreSQL は、存在しない主キーをチェックします (そのテーブルの主キーは (flightid, flightdate) であるため)。

REFERENCESそのため、フライト テーブルを参照するときに句を削除し、追加します

FOREIGN KEY (FlightID, FlightDate) REFERENCES Flight (FlightID, FlightDate)

他のいくつかのテーブル定義にある方法で。

于 2012-06-05T18:07:23.753 に答える