我想从主键创建外键并收到此错误“#1005 - 无法创建表”
这是使用的 DDL...
CREATE TABLE Invoice
(
InvoiceID SMALLINT,
TaskID SMALLINT,
FOREIGN KEY Invoice(InvoiceID) REFERENCES XTABLE(InvoiceID),
PRIMARY KEY (InvoiceID, TaskID)
);
XTABLE
确保和中的键列Invoice
被标记为PRIMARY KEY
和NOT 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