是的,对此不太确定。新的sql和我认为一切都做得对,任何想法?如果这不是添加外键的方式,有人可以向我解释一下如何正确地做到这一点是好的吗?非常感谢,谢谢。
CREATE TABLE customer (
reference NUMBER(5) PRIMARY KEY,
company_name VARCHAR2(30),
address VARCHAR2(30),
post_code VARCHAR2(10),
telephone VARCHAR(20),
contact_fname VARCHAR2(20),
contact_sname VARCHAR2(20),
contact_email VARCHAR2(30)
);
CREATE TABLE manifest (
barcode NUMBER(10) PRIMARY KEY,
trip_id NUMBER(10),
pickup_customer_ref VARCHAR2(30),
delivery_customer_ref VARCHAR2(30),
category NUMBER(1),
weight NUMBER(10)
);
CREATE TABLE category (
category NUMBER(1) PRIMARY KEY,
description VARCHAR2(15),
requirements VARCHAR2(30),
FOREIGN KEY (category) REFERENCES manifest(category)
);
CREATE TABLE trip (
trip_id NUMBER(10) PRIMARY KEY,
departure_date DATE,
return_date DATE,
vehicle_id VARCHAR2(10),
employee_no NUMBER(10),
FOREIGN KEY (trip_id) REFERENCES manifest(trip_id)
);
CREATE TABLE vehicle (
registration VARCHAR2(10) PRIMARY KEY,
vehicle_type_id VARCHAR2(10),
model VARCHAR2(15),
make VARCHAR2(15),
body VARCHAR2(15),
year NUMBER(4),
FOREIGN KEY (registration) REFERENCES trip(registration)
);
CREATE TABLE model (
vehicle_type_id VARCHAR(10) PRIMARY KEY,
make VARCHAR2(15),
model VARCHAR2(15),
FOREIGN KEY (vehicle_type_id) REFERENCES vehicle(vehicle_type_id)
);
CREATE TABLE driver (
employee_no NUMBER(10) PRIMARY KEY,
first_name VARCHAR2(20),
last_name VARCHAR(20),
ni_no VARCHAR2(15),
telephone VARCHAR2(20),
mobile VARCHAR2(12),
hazardous_goods VARCHAR2(1),
FOREIGN KEY (employee_no) REFERENCES trip(employee_no)
);
,我得到的错误信息是
SQL Error: ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view
我在清单btw
之后为每个表收到此错误答案 0 :(得分:0)
错误信息非常清楚。外键的引用需要是另一个表中的唯一键或主键。所以这在category
表中是错误的。
FOREIGN KEY (category) REFERENCES manifest(category)
我假设你打算让清单看起来像这样:
CREATE TABLE manifest (
barcode NUMBER(10) PRIMARY KEY,
trip_id NUMBER(10) REFERENCES trip(trip_id),
pickup_customer_ref VARCHAR2(30),
delivery_customer_ref VARCHAR2(30),
category NUMBER(1) REFERENCES category(category),
weight NUMBER(10)
);
对于使用外键的其他地方等等。 (这对外键引用使用了简写符号; CREATE TABLE
中的单独子句也没问题。)
当然,在此示例中,manifest
的定义必须在类别和trip
之后。
换句话说,引用位于 other 表中,而不是主键定义的位置。
另外,我建议与外键的名称保持一致。至少,它们通常应尽可能与主键同名。
答案 1 :(得分:0)
trip_id,vehicle_type_id和employee_no都需要标记为UNIQUE。