2015-11-08 49 views
0

是的,這個不太確定。 SQL新手和我認爲一切都做對了,有什麼想法?如果這不是添加外鍵的方式,請有人向我解釋如何正確的方式來做這件事嗎?將不勝感激,謝謝。不能創建表,sql錯誤ORA-02270

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

錯誤信息出現此錯誤的每個表是相當清楚的。外鍵的引用需要是另一個表中的唯一鍵或主鍵。所以這在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,在這個例子。

換句話說,引用進入其他表,而不是主鍵定義的位置。

此外,我會建議與外鍵的名稱保持一致。在可能的情況下,它們通常應該與主鍵具有相同的名稱。

0

trip_id,vehicle_type_id和employee_no都需要標記爲UNIQUE。