FK on a single column referencing a column from composite PK

后端 未结 2 1875
你的背包
你的背包 2020-12-22 09:15

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_i         


        
相关标签:
2条回答
  • 2020-12-22 09:53

    I suspect that this is not unique to Oracle. Considering you have a composite primary key in the referenced table, that implies that only one of the columns comprising the composite key is not enough to uniquely identify the record in that table. Therefore, it's impossible to reference only a single column of the primary key in a foreign key relationship that's one-to-many (e.g. one record in the referenced table can have many records in the referencing table--the one with the FK). However, if the relationship to be established is many-to-many, this may be possible.

    HTH.

    0 讨论(0)
  • 2020-12-22 10:11

    You can't.

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

    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.

    2. Add PROD_NAME to the PURCHASE table.

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

    0 讨论(0)
提交回复
热议问题