新手程序员在这里。
我有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
发布评论