2012-01-03 46 views
2

我在一个社交网站上工作,它有一个类似facebook的墙,用户可以发布状态,图片和内容。为用户建造一堵墙并不困难。我有这样的结构。facebook壁式数据库结构要求

Wall 
    { 
    Id, 
    ToUserId (BigInt) (Owner of wall) 
    ByUserId (The person who is posting) 
    Content (message, pic or something else) 
    more fields.... 
    } 

这堵墙只是一个用户,我不能再使用它,比如,我想网站将有其他 对象太像网页,论坛等,因此,所有的物体都会有墙为他们。我不想为每个墙壁创建单独的表格。

然后我想,我可以有ObjectId而不是ToUserId,我的结构将会是这样的。

Wall 
    { 
    Id, 
    ObjectId (BigInt)(PageId/UserId/GroupId (one of them)) 
    ByUserId (The person who is posting) 
    Content (message, pic or something else) 
    more fields.... 
    } 

正如我在我的表中使用增量字段,一个页面可以有一个用户具有相同的ID。所以这又是一个问题。

接下来,我想,我的对象应该是字符串类型

Wall 
    { 
    Id, 
    ObjectId (string(10))(PageId/UserId/GroupId (one of them)) 
    ByUserId (The person who is posting) 
    Content (message, pic or something else) 
    more fields.... 
    } 

现在对于一个用户,我会追加“U”的数量,以便对象ID的将是“1U”,“2U”和他们将是“1P”,“2P”和“1G”,“2G”组。

但我还是不服气,想听听专家对此的意见。

更新

谢谢你们,这个问题,我有是,我怎么能保持一个表的所有类型的墙。

A wall for a User 
A Wall for a Page 
A Wall for a Group 

如果你看看Facebook的,它的一面墙或至少,我要建立它,所以它应该被附加到任何对象(用户,到一个页面或一组或任何东西)。

我希望这现在更有意义。

回答

0

我不明白你为什么要创建某种“字符串”引用结构。这似乎不是“关系”。

这个怎么样。

一个用户许多帖子

一个用户具有一个

一个具有一个

一个具有一个用户

一个许多帖子

一个具有一个用户

POCO的(因为我也懒得写SQL)

public class User 
{ 
    public int UserId { get; set; } 
    public ICollection<Post> Posts { get; set; } // the Posts this user has written. Could be against different Wall's 
    public Wall Wall { get; set; } // the User's wall. 
} 

public class Post 
{ 
    public int PostId { get; set; } 
    public User User { get; set; } // the User who wrote the post. 
    public Wall Wall { get; set; } // the Wall this Post belongs to. 
} 

public class Wall 
{ 
    public int WallId { get; set; } 
    public ICollection<Post> Posts { get; set; } // all the posts on this Wall. 
} 

假设:

  • 一个只能针对一个完成。
+0

感谢RPM,我试图避免字符串这就是为什么我这里问这个问题。 – Parminder 2012-01-04 01:20:42

+0

说,我还有一个对象页面。现在页面也有一堵墙。怎么做。 ?? – Parminder 2012-01-04 02:24:00

+0

我认为你需要一种区分用户类型的“UserType”标志(TINYINT)。 1 =用户,2 =组,3 =页面等 – RPM1984 2012-01-04 02:49:07

0

我会去为每个可能的引用单独的可空字段。这也允许你使用外键,如果你想要的话,你甚至可以检查限制,所以所有的行必须有一个定义的参考。我绝对不会推荐去找那个字符串ID的想法 - 这样做会是无效的。

create table [Posts] 
(
    [ID] int not null identity(1, 1), 
    [ID_FromUser] int not null, 
    [ID_ToUser] int null, 
    [ID_ToPage] int null, 
    [ID_ToGroup] int null, 
    [Content] nvarchar(max) not null, 
    -- more stuff 

    constraint [FK_Posts_FromUser] foreign key ([ID_FromUser]) references [Users]([ID]), 
    constraint [FK_Posts_ToUser] foreign key ([ID_ToUser]) references [Users]([ID]), 
    constraint [FK_Posts_ToPage] foreign key ([ID_ToPage]) references [Pages]([ID]), 
    constraint [FK_Posts_ToGroup] foreign key ([ID_ToGroup]) references [Groups]([ID]), 
    constraint [PK_Posts] primary key ([ID]) 
) 
+0

感谢卢卡斯,但采用这种方法,每次两列都将是空白的。此外,如果你想添加一个对象,你将需要改变数据库结构。 – Parminder 2012-01-03 23:31:39