Data Modeling: Supertype / Subtype

前端 未结 2 983
迷失自我
迷失自我 2021-01-31 23:45

Looking to figure out the proper way to model the below requirements.

  1. There are 3 types of “parties” to be concerned with, a Fan, a Band, and a BandMember.
2条回答
  •  暖寄归人
    2021-02-01 00:19

    I think this is simpler than you think. You've got two objects - Band and Person, and they can be connected in two different ways, either as a fan or a member. Here is a quickie db script with no foreign keys or anything:

    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
    

    As for the relationship specific attributes, you can place them in the XREF tables, e.g., FanClubMembershipNumber goes in XREFBandFans.

提交回复
热议问题