简体   繁体   English

需要帮助在MySQL DB中设置外键关系

[英]Need help setting up Foreign Key relationship in MySQL DB

So I have to InnoDB tables (Products) and (Categories). 所以我要InnoDB表(产品)和(类别)。 I wanted to setup a relationship between the "Category" field on the Products table, and the "CategoryId" of the Categories table. 我想在“产品”表上的“类别”字段与“类别”表的“ CategoryId”之间建立一种关系。

But when attempting to create a FK it will only let me select the Primary Key "ProductsId" on the Products table and map to the PK "CategoryId" on the Categories table. 但是,当尝试创建FK时,只会让我在“产品”表上选择主键“ ProductsId”,然后映射到“类别”表上的PK“ CategoryId”。

Maybe I am missing the way/reason to setup a Foreign key. 也许我错过了设置外键的方式/原因。 My thoughts and tell me if I am wrong: 我的想法,并告诉我我是否错:

1) Was to require a product be added to a category when added, the category must exist or you must create it first. 1)是在添加产品时要求将产品添加到类别中,该类别必须存在或必须首先创建它。 You can't remove a category unless you perform some task (programmically or on the DB backend) to remove the products from a category you are wanting to remove. 除非您执行某些任务(以编程方式或在DB后端上)从要删除的类别中删除产品,否则您不能删除该类别。

2) I was expecting the CategoryId value to be stored in the "Category" field of the Products table. 2)我期望CategoryId值存储在Products表的“ Category”字段中。 Then when displaying in my view, would need to look up the Categories.Name field by the CategoriesId value. 然后在我的视图中显示时,需要通过CategoriesId值查找Categories.Name字段。

EDIT: So I understand that two fields involved in the Foreign keys must be the same, size, types...etc. 编辑:所以我知道外键涉及的两个字段必须相同,大小,类型...等。 However, how does linking up the ProductId and CategoryId work in context to what I mentioned above I am wanting to do. 但是,将ProductId和CategoryId链接起来如何在上下文中与我上面想做的事情联系起来。 When I did create a FK between ProductId and CategoryId, I won't let me add a product record. 当我确实在ProductId和CategoryId之间创建FK时,不会让我添加产品记录。

Also, the Category Name field and the Product Category field are the same type, size..etc, yet I don't get the option to select those in the foreign key tab? 另外,“类别名称”字段和“产品类别”字段是相同的类型,大小..etc,但是我无法在外键选项卡中选择那些选项吗?

How should I be setting it up so that the categories table will know what products are part of each category. 我应该如何设置它,以便类别表知道每个类别属于哪些产品。

Ok, unfortunately, I must answer my own questions. 好的,很遗憾,我必须回答自己的问题。 The reason for most of my technical problems is because the field you are trying to make "must be indexed". 我大多数技术问题的原因是因为您要使“必须索引”的字段。

The comprehesive issue I was having was that I needed to get rid of the actual "Category" varchar field on the Products table, and create a CategoryId field that would have only a value that exists in the Category table CategoryId field. 我遇到的综合问题是,我需要摆脱Products表上的实际“ Category” varchar字段,并创建一个CategoryId字段,该字段只有一个值存在于Category表的CategoryId字段中。

Now I will just have to reference the Categories.Name field through the Products.CategoryId value. 现在,我只需要通过Products.CategoryId值引用Categories.Name字段。

At least this is what I have come to understand. 至少这是我所了解的。

如果您在工作台上,请验证FK中涉及的两列的类型,长度和属性是否相同。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM