Database design 数据库设计中的封装或抽象

Database design 数据库设计中的封装或抽象,database-design,Database Design,在我的初级语言(C#)中,抽象数据类非常容易。例如: public interface IFruit { public TimeSpan GrowthTime { get; set; } public string Color { get; set; } } public class Apple : IFruit { public TimeSpan GrowthTime { get { throw ne

在我的初级语言(C#)中,抽象数据类非常容易。例如:

public interface IFruit
{
    public TimeSpan GrowthTime { get; set; }
    public string Color { get; set; }

}

public class Apple : IFruit
{

    public TimeSpan GrowthTime
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    public string Color
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }
}  
您将如何在数据库设计中实现这种类型的功能?假设我有一个名为
公告
的表,它可以保存许多不同类型的公告,无论它们是
公告
广告
,还是
事件

我考虑为上述每个类创建一个
公告
表和一个单独的表,并为
公告
表中包含的每个类创建一个
FK
列;但是,这将不起作用,因为FK是强制性的(如果取消FK,则是不好的做法),并且一个
公告
帖子不能超过上面列出的一个类

我可以为它们中的每一个创建单独的表,但是用户的外部模型视图只是
公告的视图。对他们来说,他们看到的公告可以是不同的类型,而不是单独的实体。有什么建议吗

FK是强制性的(如果取消FK,则是相当糟糕的做法)

事实并非如此。为“可选”父级建模时使用空外键是完全合法的。虽然你不一定需要继承

今天的关系数据库不支持Inheritance1,因此OOP和数据库之间存在需要克服的“阻抗不匹配”,而且要做到这一点,没有一个是理想的

我的默认方法可能是“每表一类”,在应用程序级别强制执行独占性和子级的存在,并且只有在有特定原因的情况下才考虑其他方法


1在数据库建模中也称为“类别”或“泛化层次结构”或“子类化”