数据建模:超类型/子类型

时间:2011-01-21 19:51:51

标签: database database-design relational-database database-schema datamodel

想要找出模拟以下要求的正确方法。

  1. 有三种类型的“派对”,一个粉丝,一个乐队和一个BandMember。
  2. BandMember将始终与乐队相关联,也可以是任何乐队的粉丝。
  3. Fan,Band和BandMember之间有共同的属性,但这3个中的每一个也都有自己独特的属性。
  4. 一个粉丝可以是任何乐队的粉丝,或者根本没有粉丝
  5. 这是一个更大思想的一小部分,但它在扩展模型时产生了混乱。我相信它必须是图2或其他一些选项,因为我没有看到BandMember如何与第一个模型中的Band相关联。

    我感谢任何意见。

    alt text

    alt text

2 个答案:

答案 0 :(得分:10)

答案 1 :(得分:1)

我认为这比你想象的要简单。你有两个对象 - 乐队和人物,他们可以通过两种不同的方式连接,无论是作为粉丝还是作为成员。这是一个没有外键或任何东西的快速db脚本:

CREATE TABLE [dbo].[XREFBandMembers](
    [MemberID] [int] NOT NULL,
    [BandId] [int] NOT NULL,
 CONSTRAINT [PK_XREFBandMembers] PRIMARY KEY CLUSTERED 
(
    [MemberID] ASC,
    [BandId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[XREFBandFans](
    [FanId] [int] NOT NULL,
    [BandId] [int] NOT NULL,
 CONSTRAINT [PK_XREFBandFans] PRIMARY KEY CLUSTERED 
(
    [FanId] ASC,
    [BandId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[People](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](100) NOT NULL,
 CONSTRAINT [PK_People] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[Bands](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_Bands] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

对于特定于关系的属性,您可以将它们放在XREF表中,例如,FanClubMembershipNumber放在XREFBandFans中。