是否可以在另一个表中设置唯一约束作为外键?
是否可以在另一个表中设置唯一约束作为外键? 如果是,您将如何声明?
您将如何分配候选键? 是否可以?
例子: 我有一个产品表,其中包含:
prod_id, prod_name, prod_price, QOH
我希望 prod_name 链接到调度表的位置:
desp_id, prod_name, shelfLoc, quantity
我的想法是我可能需要创建一个如下所示的唯一约束:
ALTER TABLE product
ADD CONSTRAINT prod_nameID_uc
UNIQUE (prod_id,prod_name)
我想知道的是,如果它是可以将唯一键引用为调度表中的外键。我必须在调度表中使用 prod_name
而不是 prod_id
,以便用户在阅读信息时更有意义,而不是看到 ID 号。 我在 oracle 上使用 iSQL plus。
Is it possible to set a unique constraint as a foreign key in another table?
If yes, how would you go about declaring it?
How would you go about assigning a candidate key?
Is it possible?
Example:
I have a product table that consists of:
prod_id, prod_name, prod_price, QOH
Where I want prod_name to link to the despatch table:
desp_id, prod_name, shelfLoc, quantity
What I was thinking is that I may need to create a unique constraint which will look like this:
ALTER TABLE product
ADD CONSTRAINT prod_nameID_uc
UNIQUE (prod_id,prod_name)
What I'm wondering is, if it is possible to refer to a unique key as a foreign key in the despatch table. I have to have prod_name
rather than prod_id
in the despatch table so that the information is more meaningful to the user when reading it, rather than seeing an id number.
I am using iSQL plus on oracle.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
完全有可能在 Oracle FOREIGN KEY 中引用 UNIQUE 约束:
但是这是不好的做法。将主键与唯一键一起使用的主要原因是提供一个用于外键的合成键。我是你,我会担心你的老师给你一个作业充满了不好的做法。
It is perfectly possible to reference a UNIQUE constraint in an Oracle FOREIGN KEY:
It is however bad practice. The main reason for using a primary key alongside a unique key is to provide a synthetic key for use in foreign keys. I were you I would be concerned that your teachers are giving you an assignment riddled with bad practice.
这必然依赖于 DBMS。在我熟悉的 DBMS 中,唯一约束和外键约束是单独考虑的,您可以同时拥有两者,并且它们在组合时都可以正常运行。
This is necessarily DBMS dependent. In the DBMSes I'm familiar with, the unique constraint and the foreign key constraint are separate considerations, you can have both, and they both act normally when combined.