简体   繁体   English

在另一个表的主键;外键列中插入记录

[英]Inserting records in a primary;foreign key columns from another table

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插入一条记录, produ_tag主键和外键作为列。

produ_tag produ_tag

链接到表结构

products 制品

产品结构

tags 标签

标签结构

The first column refHe is the code of the product from products table, the second contains the tag for the product from tags table. 第一列refHeproducts表中产品的代码,第二列包含tags表中产品的tags 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. 问题是它们是主要的,所以如果我有一个带有2个标签的产品,则不会插入该记录,因为它已经插入了与以前相同的产品。

Should I delete the primary thing for both? 我应该同时删除两者的主要内容吗? Or there is another way to handle this, as the modeling software rendered it that way. 还是有另一种方法可以处理此问题,因为建模软件以这种方式进行了渲染。

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 (ie you can't associate the same product and tag more than once). produ_tag表是一个“桥接表”,并且复合主键将允许将任意数量的标签( namCh )与产品( refHe )关联,只要该标签存在于tags表中并且(refHe, nameCh)对是唯一的(即,您不能将同一产品关联并标记多次)。

If you get an error trying to insert a tag, it's not due to a faulty PK setup. 如果您在尝试插入标签时遇到错误,则不是由于PK设置错误所致。 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 . 我看到的唯一明显的问题是,从produ_tag.refHeproducts.refHe没有FK约束。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL-显示表中的记录,其中外键值与另一个表中的主键值匹配 - SQL - Displaying records from a table where the foreign key value matches the primary key value in another table 如何在 php 中同时从单个表单插入记录时将数据库中表的主键用作表 2 中的外键 - how to use the primary key of a table in database as a foreign key in table 2 while inserting the records from a single form simultaneously in php 如何从另一个表主键添加mysql外键? - How to Add mysql foreign key from another table primary key? 来自外键的PHP主键表 - PHP primary key table from foreign key 如何从两个表中查询匹配表中的外键和另一个表中的主键 - How do I query from two tables matching the foreign key from table with the primary key from another 将表的主键存储到另一个表的外键中— Laravel 5.8 - Storing Primary Key of table to Foreign key of another table — Laravel 5.8 将数据插入依赖于新记录主键的表中 - Inserting data into a table that relies on the new records primary key for insertion 如何使用php和mysql将外键字段链接到另一个表的主键字段? - How to link the foreign key field to a primary key field from another table using php and mysql? 如何从下拉列表中捕获主键并插入为另一个表的外键? - How to capture primary key from drop down and insert as foreign key of another table? 如何将主键作为外键插入另一个表? - How do I insert the primary key to another table as foreign key?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM