假设我们有这种情况:
Artist ==< Album ==< Track
//ie, One Artist can have many albums, and one album can have many tracks
在这种情况下,所有 3 个实体都具有基本相同的字段:
- ID
- 姓名
- 与相应子代(Artist to Album 和 Album to Track)的一对多关系的外来物
所提供解决方案的典型解决方案是三个表,在一对多关系字段中具有相同的字段(ArtistID、AlbumID 等)和外键约束。
但是,在这种情况下,我们可以合并一种继承形式来避免相同字段的重复吗?我说的是这样的事情:
Table: EntityType(EntityTypeID, EntityName)
This table would hold 3 entities (1. Artist, 2. Album, 3. Track)
Table: Entities(EntityID, Name, RelField, EntityTypeID)
This table will hold the name of the entity (like the name of
an artist for example), the one-many field (foreign-key
of EntityID) and EntityTypeID holding 1 for Artist, 2 for Album
and so on.
你觉得上面的设计怎么样?在这个数据库场景中加入“OOP 概念”是否有意义?
最后,您更喜欢第一种情况的外键约束还是更通用的(例如,存在将艺术家与 Track 链接的风险,因为没有检查输入器外键值是否真的是一张专辑)的方法?
..btw,想想看,我想你实际上可以检查一个艺术家的 RelField 的输入值是否对应一个专辑,也许有触发器?