链接到父表有时 - 参考完整性 [英] Linking to Parent Table SOMETIMES - Referential Integrity

查看:54
本文介绍了链接到父表有时 - 参考完整性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。


有人能告诉我一个很好的方法只从子表的某些记录中引用父表吗?我知道这打破了参照完整性,但我不确定如何解决这个问题。


我有一个代表灯泡实体的表格,我希望它只在某些时候链接到产品表(对于电子商务网站)。所以有一个灯泡。具有名为product_id的外键列的表,该列也是产品的主键的名称。表。如果灯泡不是我们目录中的产品,那么我想给外键值NULL。问题是products表的主键定义为NOT NULL。产品表是购物车解决方案的一部分,所以我不想搞砸它。


我是否应该在名为product_id的灯泡表中创建一个未定义为外键的列?它将映射到products表而不是技术上的外键。还是有另外一种完全不同的方式可以解决这个问题?我想继续前进,但我不想做出决定,以后会用脚射击我。任何建议或意见将不胜感激。谢谢。

解决方案

您是否考虑过在产品表格中创建一个条目,您可以在其中引用任何不属于此类的灯泡你的目录?


我必须承认,我不完全理解你的桌子结构。您是否可以发布他们的CREATE语法?



您是否考虑过在产品表中创建一个可以参考的条目任何不属于你目录的灯泡?


我必须承认,我不完全理解你的桌面结构。你可以发布他们的CREATE语法吗?



你好。我不想触摸产品表,因为它是购物车应用程序的一部分,我担心我可能会无意中破坏某些东西。


是的,抱歉。我应该在原帖中提供这个。

展开 | 选择 | Wrap | 行号


您可以创建一个中间表来链接您想要的灯泡产品清单。


如下:

展开 | 选择 | Wrap | 行号< /跨度>

Hello.

Can someone tell me a good way to reference a parent table from only some of the records of the child table? I know that this is breaking referential integrity, but I''m not sure how to go about this problem.

I have a table representing a light bulb entity, and I want it to link to a products table (for an ecommerce site) only some of the time. So there''s a "bulb" table that has a foreign key column named "product_id", which is also the name of the primary key of the "products" table. If the bulb is not a product in our catalog, then I want to give the foreign key a value of NULL. The problem is that the primary key of the products table is defined as NOT NULL. The products table is part of a shopping cart solution, so I don''t want to mess with it.

Should I just create a column in the bulb table called product_id that''s not defined as a foreign key? It would map to the products table without technically being a foreign key. Or is there another completely different way that I could go about solving this problem? I would like to move along, but I don''t want to make a decision that will shoot me in the foot later. Any suggestions or advice would be appreciated. Thanks.

解决方案

Have you considered creating an entry in the ''product'' table to which you can refer any ''light bulb'' that is not a part of you catalog?

I must admit, I don''t fully understand your table structures. Could you perhaps post their CREATE syntax?


Have you considered creating an entry in the ''product'' table to which you can refer any ''light bulb'' that is not a part of you catalog?

I must admit, I don''t fully understand your table structures. Could you perhaps post their CREATE syntax?

Hello. I don''t want to touch the products table, because it''s part of a shopping cart application, and I''m afraid I might inadvertently break something.

Yeah, sorry. I should have provided this in the original post.

Expand|Select|Wrap|Line Numbers


You could create an intermediary table to link the bulbs you want to the product list.

Like so:

Expand|Select|Wrap|Line Numbers


这篇关于链接到父表有时 - 参考完整性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆