dshmvqnl98119 2017-12-11 02:26
浏览 63

在主数据库中插入记录;从另一个表中插入外键列

EDIT: It's seemingly the term 'bridging tables' got what I need

I'm trying to insert a record in a table produ_tag with a primary and foreign key as columns.

produ_tag

link to the table structure

products

products structure

tags

tags structure

The first column refHe is the code of the product from products table, the second contains the tag for the product from tags table. The modeling software made this additional table so it can saves which product contains which tag which is necessary.

The problem is they're primary, so if I have a product with 2 tags, the record won't be inserted, as it has already the same product inserted previously.

Should I delete the primary thing for both? Or there is another way to handle this, as the modeling software rendered it that way.

  • 写回答

1条回答 默认 最新

  • douzhuiqing1151 2017-12-11 05:29
    关注

    The produ_tag table is a "bridging table", and the the composite primary key will allow any number of tags (namCh) to be associated with a product (refHe), provided that the tag exists in the tags table and that the (refHe, nameCh) pair is unique (i.e. you can't associate the same product and tag more than once).

    If you get an error trying to insert a tag, it's not due to a faulty PK setup. You're either trying to reference a tag that doesn't exist, or you're inserting a combination of values that are already present in the table.

    Don't delete your primary keys. The only obvious problem I see is that there's no FK constraint from produ_tag.refHe to products.refHe.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度