数据库表建模 - 列继承

时间:2013-01-17 18:37:22

标签: mysql database inheritance rdbms modeling

我想要两张桌子:

[Table1]         [Table2]
id1              id2
createdon        createdon
createdby        createdby
column1a         column2
column1b                 

可以看出,这两张桌子确实已经“创造了”。并且'由'创建列。类似的事情是我的其他表。有没有办法建模和实现具有列继承的数据库表结构(就像我在OOP中使用类属性一样)。从概念上讲我的意思是:我想创建一个超级表:

[Supertable]
createdon
createdby

并使Table10和Table20扩展为Supertable。这样我就不必创建' createdon'并且'由'创建每个特定表的列,因为它们将从超级表继承。

[Table10]  extends  [Supertable]   results in wanted   [Table1]
id1                 createdon                          id1
column1a            createdby                          createdon
column1b                                               createdby
                                                       column1a
                                                       column1b

[Table20]  extends  [Supertable]   results in wanted   [Table2]
id2                 createdon                          id2
column2             createdby                          createdon
                                                       createdby
                                                       column2

因此Table10,Table20和Supertabe只是RDBMS内部的结构,Table1和Table2是数据库用户在访问数据库时看到的最终表。在MySQL(或任何其他RDBMS)中是否可以这样?

2 个答案:

答案 0 :(得分:0)

从纯粹的建模角度来看......为什么不可能呢?

您的表结构将如下所示(我已经调整了您的语法):

[Table1]       [Table2]
id1            id2
column1a       column2
column1b

...和...

[Table0]
table   <- either Table1 or Table2
id      <- either the value id1 or id2
createdon
createdby

但最重要的问题是......你会在申请中受益吗?如果不深入了解你将要做的事情,我就无法回答这个问题。

答案 1 :(得分:0)

您还可以将表与关联键链接在一起:

[supertable]
id
createdon
createdby

[Table1]  
id1  
supertable_id      
column1a    
column1b    

这不是您正在寻找的,但可能有用。