FK在单列上引用复合PK中的列 [英] FK on a single column referencing a column from composite PK

查看:95
本文介绍了FK在单列上引用复合PK中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法创建/查找逻辑,以在子表中的列上应用FK,引用父表格的复合PK中的列。

Not able to create /find the logic to apply FK on a column in child table referencing a column from composite PK of parent table.

create table product(prod_id number,
    prod_name varchar2(20),
    price number,
    constraint PK12 primary key(prod_id,prod_name));

创建表。

create table purchase(prod_id number,
    purchase_price number,
    constraint FK12 foreign key(prod_id) references product(prod_id));
create table purchase(prod_id number,
    purchase_price number,
    constraint FK12 foreign key(prod_id) references product(prod_id))


ERROR at line 1:
ORA-02270: no matching unique or primary key for this column-list

Kinldy建议我如何

Kinldy suggest how i can incorporate this logic.

谢谢。

推荐答案

你不能。

由于错误表示该列列表中没有匹配的主键;你必须有一个。您有三个选项:

As the error says there's no matching primary key for that column list; you must have one. You have three options:


  1. 从PRODUCT的主键中删除PROD_NAME。从表面上看,这似乎是逻辑解决方案,如果这不是必需的,以使主键是唯一的。

  1. Remove PROD_NAME from the primary key of PRODUCT. On the face of it this seems like the logical solution, if this is not required in order to make the primary key unique.

将PROD_NAME添加到PUR​​CHASE表。

Add PROD_NAME to the PURCHASE table.

在PURCHASE.PROD_ID上创建唯一的索引。如果这是一个主要的候选人,这似乎是过度的。

Create a unique index on PURCHASE.PROD_ID. This seems excessive if it would be a primary key candidate anyway.

这篇关于FK在单列上引用复合PK中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆