我已经创建了一些SQL代码来创建基于ERD的7个表。
它似乎非常接近工作,除了它说
外键约束中引用的列“inv_no”不存在
但是,创建了inv_no所在的表发票,并且列为inv_no,因此我很困惑如何创建另一个表invoiceLine,它需要inv_no作为外键。
这是我的代码:
CREATE TABLE invoice(
inv_no INTEGER NOT NULL,
cust_id INTEGER NOT NULL,
inv_date DATE NOT NULL,
CONSTRAINT PK_invoice_inv_no PRIMARY KEY(inv_no),
CONSTRAINT FK_customer_cust_id FOREIGN KEY(cust_id) REFERENCES customer(cust_id)
);
CREATE TABLE invoiceLine(
inv_line_no INTEGER NOT NULL,
inv_line_qty INTEGER NOT NULL,
CONSTRAINT PK_invoiceLine_inv_line_no PRIMARY KEY(inv_line_no),
CONSTRAINT FK_invoice_inv_no FOREIGN KEY(inv_no) REFERENCES invoice(inv_no)
);
同样,有问题的表是invoiceLine,它似乎依赖于发票表中的inv_no。
如果有人能发现我的错误会很棒!
答案 0 :(得分:5)
错误消息是正确的,在invoiceline中没有列inv_no
。您还需要声明该列:
CREATE TABLE invoiceLine( inv_no INTEGER NOT NULL, inv_line_no INTEGER NOT NULL, inv_line_qty INTEGER NOT NULL, CONSTRAINT PK_invoiceLine_inv_line_no PRIMARY KEY(inv_line_no), CONSTRAINT FK_invoice_inv_no FOREIGN KEY(inv_no) REFERENCES invoice(inv_no) );
答案 1 :(得分:0)
你的invoiceline表不应该读;
CREATE TABLE invoiceLine(
inv_line_no INTEGER NOT NULL,
inv_line_qty INTEGER NOT NULL,
CONSTRAINT PK_invoiceLine_inv_line_no PRIMARY KEY(inv_line_no),
CONSTRAINT FK_invoice_inv_no FOREIGN KEY(inv_line_no) REFERENCES invoice(inv_no)
);
inv_line_no
代替inv_no
答案 2 :(得分:0)
在添加外键约束之前,您可能希望将inv_no列添加到invoiceLine表。
CREATE TABLE invoiceLine(
inv_line_no INTEGER NOT NULL,
inv_line_qty INTEGER NOT NULL,
inv_no Integer Not Null,
CONSTRAINT PK_invoiceLine_inv_line_no PRIMARY KEY(inv_line_no),
CONSTRAINT FK_invoice_inv_no FOREIGN KEY(inv_no) REFERENCES invoice(inv_no)
);