想要找出模拟以下要求的正确方法。
这是一个更大思想的一小部分,但它在扩展模型时产生了混乱。我相信它必须是图2或其他一些选项,因为我没有看到BandMember如何与第一个模型中的Band相关联。
我感谢任何意见。
答案 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中。