外键和主键错误
根据我们上次关于重复发票号的讨论。并且您建议的代码如下:
create table Invoice
(
companyID int,
invno varchar(50),
primary key (companyID,Invno)
)
工作正常。
但另一个表是 invcarat 相同的字段如下:
companyID(int) invno(varchar)
------------------------------------------------------
现在我必须在 invcarat(invno) 上创建外键,但在创建外键时发生错误“参考表发票上的主键不存在” ”。虽然我已经在发票上创建了主键,如上所述。
请回复如何在两个表之间生成关系。
提前致谢。
as per our last discussion regarding duplicate invoice no. and you have suggetsed code like:
create table Invoice
(
companyID int,
invno varchar(50),
primary key (companyID,Invno)
)
worked fine.
but another table is invcarat same has field like:
companyID(int) invno(varchar)
------------------------------------------------------
now i have to create foreign key on invcarat(invno) but error has occured while creating of foreign key that "primary key on reference table invoice doesn't exist". while i have created primary key on invoice as above.
kindly reply how to generate relationship bewteen two tables.
thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
外键应位于
invcarat (companyID, invno)
上,并应引用invoice (companyID, invno)
。invno
列本身不是主键,因此您不能将其用作外键目标。您可以在其上创建一个唯一的键,但您最好只更改主键。The foreign key should be on
invcarat (companyID, invno)
and should referenceinvoice (companyID, invno)
. Theinvno
columns itself is not a primary key, so you can't use it as a foreign key target. You could create a unique key on it, but they you might as well just change the primary key.