如何创建member_follower表结构?

时间:2013-12-23 17:04:00

标签: sql sql-server

我在SQL Server中有一个成员表结构:

member_table

memberid name address email
111      aaa  IND     a@a.com
222      bbb  UK      b@b.com

现在我想提供一个成员可以跟随其他成员的设施。我怎么能这样做?

我应该创建一个新表吗?我应该使用任何旗帜吗?

我很想创建一个新表并给会员表

2 个答案:

答案 0 :(得分:4)

您可以创建另一个包含两个属性的表(member_id,follows_member_id) 此表保留每个成员跟随其他成员的记录。两个属性都引用您的成员表。这是在标准化时创建多对多关系的标准方法。 read more

答案 1 :(得分:1)

我会在两个member_ids上创建一个带有键的联结表,以确保最多有一行。

我试图不满足YGWIT(您会想要它),但似乎订阅表会调用一些额外的元数据,例如订阅开始的日期或最后的访问日期,以了解是否有新活动。< / p>

我最终会得到这样的结果:

CREATE TABLE [dbo].[member_subscription](
    [member_id] [int] NOT NULL,
    [target_member_id] [int] NOT NULL,
    [date_created] [datetime] NOT NULL,
    [date_last_visit] [datetime] NULL,
    CONSTRAINT [PK_member_subscription] PRIMARY KEY CLUSTERED 
    (   -- key on both member_id fields
        [member_id] ASC,
        [target_member_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

-- Foreign key contrain for member_id
ALTER TABLE [dbo].[member_subscription]  WITH CHECK ADD  CONSTRAINT [FK_member_subscription_member] FOREIGN KEY([member_id])
REFERENCES [dbo].[member] ([member_id])
GO
ALTER TABLE [dbo].[member_subscription] CHECK CONSTRAINT [FK_member_subscription_member]
GO


-- Foreign key contrain for target_member_id
ALTER TABLE [dbo].[member_subscription]  WITH CHECK ADD  CONSTRAINT [FK_member_subscription_target_member] FOREIGN KEY([target_member_id])
REFERENCES [dbo].[member] ([member_id])
GO
ALTER TABLE [dbo].[member_subscription] CHECK CONSTRAINT [FK_member_subscription_target_member]
GO

-- Default value for date_created
ALTER TABLE [dbo].[member_subscription] ADD  CONSTRAINT [DF_member_subscription_datecreated]  DEFAULT (getdate()) FOR [date_created]
GO