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,Nodes有Content。 SQL表如下所示:
One of the fundamental classes is called Node, and Nodes have Content. The SQL tables look like this:
TABLE节点(NodeId int,.... etc)
TABLE Node ( NodeId int, .... etc )
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.
推荐答案
为什么不能设置 NoteContentRelationship.ContentId
作为外键?您可以很容易地使用一个表格 Content
表示抽象基类的表格和各种表格 AnimalContent
code> 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屋!