gpt4 book ai didi

mysql - 如何从组合键创建外键?

转载 作者:行者123 更新时间:2023-11-30 01:32:01 25 4
gpt4 key购买 nike

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

这是使用的 DDL...

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

最佳答案

确保 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

关于mysql - 如何从组合键创建外键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17339229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com