2015-11-01 85 views
1

我不斷收到如下所示的我的代碼「缺少關鍵字」當Oracle SQL Developer中創建一個表

"SQL Error: ORA - 00905: missing keyword" in the "CREATE TABLE"

部分:

CREATE TABLE Orders (
    Received CHAR(9), 
    Shipped CHAR(9), 
    FOREIGN KEY (Ono), REFERENCES Odetails (Ono), 
    FOREIGN KEY (Cno), REFERENCES customers (Cno), 
    FOREIGN KEY (Eno), REFERENCES employee (Eno), 
    PRIMARY KEY (Ono, Cno, Eno) 
    ); 

    CREATE TABLE ZIPCODES (
    ZIP CHAR(5) NOT NULL, 
    City CHAR(20), 
    FOREIGN KEY (ZIP), REFERENCES customers (ZIP), 
    PRIMARY KEY (ZIP) 
    ); 

但是,我在同一個工作表中寫了這個代碼和沒有得到任何錯誤是如此如此:

CREATE TABLE Odetails (
    Ono CHAR(4) NOT NULL, 
    Pno CHAR(5) NOT NULL, 
    Qty CHAR(1), 
    FOREIGN KEY (Pno), REFERENCES parts (Pno), 
    PRIMARY KEY (Ono) 
    ); 

任何人都可以幫助我確定是什麼導致錯誤?

回答

2

1)不要使用逗號

2)定義列

CREATE TABLE Orders (
    Received CHAR(9), 
    Shipped CHAR(9), 
    Ono INT NOT NULL , -- your missing columns (add datatype you need) 
    Cno INT NOT NULL, 
    Eno INT NOT NULL, 
    FOREIGN KEY (Ono) REFERENCES Odetails (Ono), -- here no comma inside 
    FOREIGN KEY (Cno) REFERENCES customers (Cno), 
    FOREIGN KEY (Eno) REFERENCES employee (Eno), 
    PRIMARY KEY (Ono, Cno, Eno) 
    ); 

CREATE TABLE ZIPCODES (
    ZIP CHAR(5) NOT NULL, 
    City CHAR(20), 
    FOREIGN KEY (ZIP) REFERENCES customers (ZIP), 
    PRIMARY KEY (ZIP) 
    ); 

考慮使用VARCHAR2數據類型,而不是CHAR

您也可以使用內嵌的語法,如:

CREATE TABLE Orders (
    ... 
    Ono INT NOT NULL REFERENCES Odetails (Ono) 
    ... 
); 

或添加明確的約束名稱:

CREATE TABLE Orders (
     ... 
     Ono INT NOT NULL, 
     ... 
     CONSTRAINT FK_Orders_Odetails_Ono FOREIGN KEY (Ono) REFERENCES Odetails (Ono), 
     ... 
); 
+0

謝謝!有效! – kagehana

+0

@ kagehana不客氣。勾選答案http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235 :) – lad2025

+0

此外,最好使用'VARCHAR2'而不是'CHAR',填充空白空間達到所定義的大小,並且是存儲的浪費。 –

0

外鍵後刪除comma所以它應該是這樣的:

CREATE TABLE Orders (
Received CHAR(9), 
Shipped CHAR(9), 
FOREIGN KEY (Ono) REFERENCES Odetails (Ono),--are you defining this Ono? 
FOREIGN KEY (Cno) REFERENCES customers (Cno), 
FOREIGN KEY (Eno) REFERENCES employee (Eno), 
PRIMARY KEY (Ono, Cno, Eno) 
);