是否可以在另一个表中设置唯一约束作为外键?

发布于 2024-08-02 21:17:56 字数 535 浏览 3 评论 0原文

是否可以在另一个表中设置唯一约束作为外键? 如果是,您将如何声明?

您将如何分配候选键? 是否可以?

例子: 我有一个产品表,其中包含:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

饮惑 2024-08-09 21:17:56

完全有可能在 Oracle FOREIGN KEY 中引用 UNIQUE 约束:

SQL> create table products (
  2      prod_id number not null
  3      , prod_name varchar2 (30) not null
  4      , constraint prod_pk primary key ( prod_id )
  5      , constraint prod_uk unique ( prod_name )
  6      )
  7  /

Table created.

SQL> create table despatch (
  2      desp_id number not null
  3      , prod_name
  4      , constraint desp_pk primary key ( desp_id )
  5      , constraint desp_prod_pk foreign key ( prod_name )
  6          references products ( prod_name )
  7      )
  8  /

Table created.

SQL>

但是这是不好的做法。将主键与唯一键一起使用的主要原因是提供一个用于外键的合成键。我是你,我会担心你的老师给你一个作业充满了不好的做法

It is perfectly possible to reference a UNIQUE constraint in an Oracle FOREIGN KEY:

SQL> create table products (
  2      prod_id number not null
  3      , prod_name varchar2 (30) not null
  4      , constraint prod_pk primary key ( prod_id )
  5      , constraint prod_uk unique ( prod_name )
  6      )
  7  /

Table created.

SQL> create table despatch (
  2      desp_id number not null
  3      , prod_name
  4      , constraint desp_pk primary key ( desp_id )
  5      , constraint desp_prod_pk foreign key ( prod_name )
  6          references products ( prod_name )
  7      )
  8  /

Table created.

SQL>

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.

缱绻入梦 2024-08-09 21:17:56

这必然依赖于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文