空外键(Null Foreign Key)

编程入门 行业动态 更新时间:2024-10-26 17:22:21
空外键(Null Foreign Key)

新手程序员在这里。

我有3个表,即产品,类别和子类别。 我以这种方式配置他们的关系:

产品到类别:多对多 产品到子类别:一对一 分类的子类别:多对一

我添加了一个subcategory_id列,它是产品表中的外键(用于映射产品和子类别表)。 如果产品具有子类别,则此方法有效。 现在的问题是我的产品没有子类别。 假设是subcategory_id列将为null,但不允许。 这有解决方法吗? 我也认为关系配置与此有关。 有什么想法吗?

newbie programmer here.

I have 3 tables namely product, category, and subcategory. I configured their relationships this way:

Product to Category: Many-to-many Product to Subcategory: One-to-one Subcategory to Category: Many-to-one

I added a subcategory_id column which is a foreign key in the product table (for mapping the product and subcategory tables). This works if a product has a subcategory. Now the problem is I have products which doesn't have subcategory. Supposed to be the subcategory_id column will be null, but it's not allowed. Is there a workaround for this? I also think that the relationship configuration has something to do with this. Any thoughts?

最满意答案

只要subcategory_id可以为空,您应该能够添加一个外键,该外键将在列不为null的所有情况下强制执行关系的完整性。 这是一个相当常见的用例。

As long as the subcategory_id is nullable, you should be able to add a foreign key that will enforce the integrity of the relationship in all cases where the column is not null. This is a fairly common use case.

更多推荐

subcategory_id,产品,类别,电脑培训,计算机培训,IT培训"/> <meta name="descript

本文发布于:2023-07-08 00:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1070187.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Null   空外键   Key   Foreign

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!