0

我想从主键创建外键并收到此错误“#1005 - 无法创建表”

这是使用的 DDL...

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

1 回答 1

0

XTABLE确保和中的键列Invoice被标记为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

于 2013-06-27T09:31:38.273 回答