SQL:一个外键引用几个表之一中的主键 [英] SQL : one foreign key references primary key in one of several tables
问题描述
我正在开发一个应用程序,该应用程序将用作其他应用程序的可扩展框架.
I am working on an application that will be used as an extensible framework for other applications.
其中一个基本类称为 Node,Node 具有 Content.SQL 表如下所示:
One of the fundamental classes is called Node, and Nodes have Content. The SQL tables look like this:
TABLE 节点(NodeId int, .... 等)
TABLE Node ( NodeId int, .... etc )
TABLE NodeContentRelationship (NodeId int, ContentType string, ContentId int)
TABLE NodeContentRelationship ( NodeId int, ContentType string, ContentId int)
这将取决于扩展应用程序以创建自己的内容类型的开发人员.
It will be up to the developers extending the application to create their own content types.
显然,从关系数据库的角度来看,这很糟糕,因为无法将外键关系添加到 NodeContentRelationship.ContentId,即使它是一个外键列.
Clearly this is bad from a relationship-database point of view as it is not possible to add a foreign key relationship to NodeContentRelationship.ContentId, even though it is a foreign key column.
但是,解决方案非常简单且功能强大,因此我不愿意更改它.
However, the solution is quite simple and powerful so I am reluctant to change it.
你怎么看 - 我会在赛道上陷入痛苦的世界吗?
What do you think - am I in for a world of pain down the track?
推荐答案
为什么不能设置NoteContentRelationship.ContentId
为外键?您可以轻松地使用关系继承模型,其中表 Content
表示抽象基类,各种表 AnimalContent
、CarContent
等表示派生类.
Why is it impossible to set NoteContentRelationship.ContentId
as a foreign key? You can easily use a relational inheritance model with a table Content
representing an abstract base class, and various tables AnimalContent
, CarContent
, etc. representing derived classes.
这篇关于SQL:一个外键引用几个表之一中的主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!