2

假设我想要一个需要存储用户信息、图像等以及存储状态更新或帖子/评论的 Web 应用程序,我想要分隔表吗?例如,如果我有一个“用户”表,其中包含用户信息,如密码、电子邮件和典型的社交网络信息,如年龄、位置等。创建处理用户内容的第二个表(“帖子”)是否是个好主意例如评论和/或帖子?

表一:“用户”

  • 用户身份
  • 用户名
  • 年龄
  • 等等

表二:“帖子”

  • 邮政编号
  • 发布内容
  • 邮政作者
  • 发布日期
  • ETC

这是一个有效的组织吗?此外,如果我想跟踪媒体,我应该在另一个表中执行此操作吗?

表三:“媒体”

  • ID
  • 类型
  • 上传者
  • 等等

任何帮助深表感谢。我很想知道我是走在正确的轨道上还是完全迷路了。我主要想知道我是否应该有很多表,或者我是否应该有更大的较少隔离的表。到目前为止,还值得注意的是,我计划将关注者(或朋友)等信息保留在“用户”表中,但我不确定回想起来这是一个好主意。提前致谢,

4

2 回答 2

6

Generally speaking to design a database you create a table for each object you will be dealing with. In you example you have Users, Posts, Comments and Media. From that you can flesh out what it is you want to store for each object. Each item you want to store is a field in the table:

[Users]
ID
Username
PasswordHash
Age
Birthdate
Email
JoinDate
LastLogin

[Posts]
ID
UserID
Title
Content
CreateDate
PostedDate

[Comments]
ID
PostID
UserID
Content

[Media]
ID
Title
Description
FileURI

Taking a look above you can see a basic structure for holding the information for each object. By the field names you can even tell the relationships between the objects. That is a post has a UserID so the post was created by that user. the comments have a PostID and a UserID so you can see that a comment was written by a person for a specific post.

Once you have the general fields identified you can look at some other aspects of the design. For example right now the Email field under the Users table means that a user can have one (1) email address, no more. You can solve this one of two ways... add more email fields (EmailA, EmailB, EmailC) this generally works if you know there are specific types of emails you are dealing with, for example EmailWork or EmailHome. This doesn't work if you do not know how many emails in total there will be. To solve this you can pull the emails out into its own table:

[Users]
ID
Username
PasswordHash
Age
Birthdate
JoinDate
LastLogin

[Emails]
ID
UserID
Email

Now you can have any number of emails for a single user. You can do this for just about any database you are trying to design. Take it in small steps and break your bigger objects into smaller ones as needed.

Update

To deal with friends you should think about the relationship you are dealing with. There is one (1) person with many friends. In relation to the tables above its one User to many Users. This can be done with a special table that hold no information other than the relationship you are looking for.

[Friends]
[UserA]
[UserB]

So if the current user's ID is in A his friend's ID is in B and visa-verse. This sets up the friendship so that if you are my friend, then I am your friend. There is no way for me to be your friend without you being mine. If you want to setup the ability for one way friendships you can setup the table like this:

[Friends]
[UserID]
[FriendID]

So If we are both friends with each other there would have to be 2 records, one for my friendship to you and one for your freindship to me.

于 2012-07-20T01:21:31.857 回答
1

您需要使用多个表。

表格的数量取决于您希望交互式站点的复杂程度。根据您发布的内容,您将需要一个存储用户信息的表、一个用于评论的表等,例如一个存储状态类型的表。

例如 tbl_Users 应该存储: 1. UserID 2. First Name 3. Last name 4. Email 5. Password(加密) 6. Address 7. City 8. State 9. Country 10. Date of Birth 11. UserStatus 12. 等等

该项目听起来应该使用关系数据库,该数据库将通过相关用户 ID 提取记录,例如评论。

这意味着您将需要一个存储以下内容的表: 1. CommentID(主键,int,自动增量) 2. Comment(文本) 3. UserID(外键,int)

注释通过外键附加到用户,外键本质上是 tbl_Users 表中的 userId。您需要将 SQL 语句中的这些表与您的脚本结合起来,以将信息作为一条信息进行查询。查看示例代码

$sql_userWall = "SELECT tbl_Users.*, tbl_Comments.*, tbl_userStatus FROM tbl_Users
INNER JOIN tbl_Comments ON tbl_Users.userID = tbl_Comments.userID
INNER JOIN tbl_UserStatus ON tbl_Users.userID = tbl.UserStatus
WHERE tbl_Users.userID = $userID";

该语句本质上是说从用户表中获取所提供用户的信息,并获取所有具有相同用户ID的评论,并从用户状态表中获取用户状态。

因此,您需要一个名为 tbl_userStatus 的表,其中包含唯一的 statusID(主键、int、自动递增)以及确定长度的文本(varchar),例如“在线”或“离线”。当您开始使用 php、asp 或类似语言从 e 记录中写入信息时,该表将自动为您检索 tbl_userStatus 中的信息,只需使用简单的一行

<?php echo $_REQUEST['userStatus']; ?>

无需额外的工作。您的大部分项目时间将花费在开发数据库结构和编写 SQL 语句以正确检索每个页面所需的信息。

有许多很棒的 YouTube 视频系列描述了关系 DBS 和绘制实体关系图。这是您应该研究的内容,以了解有关创建您所描述的项目类型的更多信息。

最后一点,如果您希望评论对组的所有成员可见,这将描述所谓的多对多关系,这将需要额外的表以允许多个用户“拥有”与单个的关系桌子。您可以存储一个引用组表的 groupID。

tbl_groups 1. GroupID 2. GroupName 3. 更多组信息等

以及为组 Tbl_groupMembers 注册的用户表 1.membershipCountID(主键,int,自增) 2.GroupID(外键,int) 3.UserID(外键,int)

这允许用户注册一个组并在内部将他们加入到基于组的评论中。这些关系需要更多时间来理解,这些视频将有很大帮助。

我希望这会有所帮助,稍后我会回来发布一些 YouTube 链接,我发现这些链接对学习这些内容很有帮助。

于 2012-07-20T01:20:04.887 回答