2013-06-27 42 views
0

我想要從一個主鍵的外鍵和得到這個錯誤「#1005 - 無法創建表」如何從組合鍵中創建外鍵?

這裏使用了DDL ...

CREATE TABLE Invoice 
(
InvoiceID SMALLINT, 
TaskID SMALLINT, 
FOREIGN KEY Invoice(InvoiceID) REFERENCES XTABLE(InvoiceID), 
PRIMARY KEY (InvoiceID, TaskID) 
); 
+0

這是整個錯誤消息?您是否嘗試使用FOREIGN KEY InoviceID而不是FOREIGN KEY發票(InvoiceID)?或者可能直接在InvoiceID SMALLINT之後寫入REFERENCES XTABLE(InvoiceID)? –

+1

顯示'CREATE TABLE XTABLE ...' – peterm

+0

問題標題提到了一個複合鍵,但您的外鍵僅使用一列:'InvoiceID': - ? –

回答

0

確保關鍵XTABLEInvoice中的列標記爲PRIMARY KEYNOT NULL

CREATE TABLE XTABLE(InvoiceID SMALLINT NOT NULL, PRIMARY KEY(InvoiceID)); 

CREATE TABLE Invoice ( 
    InvoiceID SMALLINT NOT NULL, 
    TaskID SMALLINT NOT NULL, 
    PRIMARY KEY (InvoiceID, TaskID), 
    CONSTRAINT FOREIGN KEY Invoice(InvoiceID) REFERENCES XTABLE(InvoiceID) 
); 

SQL FIDDLE:http://sqlfiddle.com/#!2/5a56f