查询不适用于外键作为另一个 table oracle SQL 的复合主键

query not working for foreign key as a composite primary key of another table oracle SQL

我正在尝试为 SQL Oracle 中的关联实体添加两个外键。我从另一个 table 引用的主键是复合主键。当我尝试输入 SQL 时,它显示

no matching unique or primary key for this column-list

我用来创建 tbl_customer

的代码
 CREATE TABLE tbl_Customer(
customer_id     NUMBER(4)
    CONSTRAINT pk_customer PRIMARY KEY,
CustomerName    VARCHAR2(50) NOT NULL,
Telephone       VARCHAR2(10),
CusEmail        VARCHAR2(20),
    CONSTRAINT cus_email UNIQUE(CusEmail),

location_id     NUMBER(4)
    CONSTRAINT fk_location_id references tbl_Location(location_id));

SQL 创建 tbl_Vehicle

CREATE TABLE tbl_Vehicle(
   vehicle_id   NUMBER(4),
   PlateNo      VARCHAR2(10),
    CONSTRAINT pk_v PRIMARY KEY(vehicle_id,PlateNo),
   Brand        VARCHAR2(20),
   Model        VARCHAR2(10),
   TotalNoSeats     NUMBER(2),
   Class        VARCHAR2(4) NOT NULL,
        CONSTRAINT no_seats CHECK (TotalNoSeats<100),
     driver_id      NUMBER(4)
        CONSTRAINT fk_driveridV references tbl_Driver(driver_id),
     location_id    NUMBER(4)
        CONSTRAINT fk_locationV references tbl_Location(location_id));

联想table是

CREATE TABLE tbl_Customer_Vehicle(
customer_id NUMBER(4)
CONSTRAINT fk_customer_id references tbl_Customer(customer_id),
vehicle_id NUMBER(4)
CONSTRAINT fk_vehicle_id references tbl_Vehicle(vehicle_id)
);

错误在这一行

 CONSTRAINT fk_vehicle_id references tbl_Vehicle(vehicle_id)
                                             *

这个错误是因为vehicle_id是复合主键吗? 请帮忙!!

您需要在 tbl_Customer_Vehicle table 中添加 PlateNo 列并在其上定义外键。

CREATE TABLE tbl_Customer_Vehicle
(
customer_id NUMBER(4)
CONSTRAINT fk_customer_id references tbl_Customer(customer_id),
vehicle_id NUMBER(4),
PlateNo      VARCHAR2(10),
CONSTRAINT fk_vehicle_id_PlateNo FOREIGN KEY(vehicle_id,PlateNo)
            references tbl_Vehicle(vehicle_id,PlateNo)
);