我假设StackOverflow的问题和答案(甚至FB的留言信息)可以通过以下模型进行模仿:
TABLE: message
==================================================================
| message_id | parent_message_id | message | timestamp |
==================================================================
| 1 | 0 | Hi | 100 |
| 2 | 1 | hello | 200 |
| 3 | 1 | hello back to you | 300 |
| 4 | 0 | How are you? | 150 |
| 5 | 4 | Good. You? | 200 |
| 6 | 4 | Good, too. | 250 |
------------------------------------------------------------------
问题:
如果数据库设计概念本质上是错误的,请告诉我。
答案 0 :(得分:1)
以下是SO的数据库布局
正如您在第一个表posts
中所看到的,您的想法正是SO所做的。
获得问题及其答案的查询将是:
SELECT q.body, q.otherfields
FROM posts q
WHERE q.id = 1234
UNION ALL
SELECT a.body, a.otherfields
FROM posts a
WHERE a.ParentId = 1234
这会将问题置于顶部,答案位于其下方。
Stackoverflow数据结构:
Posts
Id
PostTypeId
AcceptedAnswerId
ParentId
CreationDate
Score
ViewCount
Body
OwnerUserId
OwnerDisplayName
LastEditorUserId
LastEditorDisplayName
LastEditDate
LastActivityDate
Title
Tags
AnswerCount
CommentCount
FavoriteCount
ClosedDate
CommunityOwnedDate
Users
Id
Reputation
CreationDate
DisplayName
LastAccessDate
WebsiteUrl
Location
AboutMe
Views
UpVotes
DownVotes
EmailHash
Age
Comments
Id
PostId
Score
Text
CreationDate
UserDisplayName
UserId
Badges
Id
UserId
Name
Date
PostFeedback
Id
PostId
IsAnonymous
VoteTypeId
CreationDate
PostHistory
Id
PostHistoryTypeId
PostId
RevisionGUID
CreationDate
UserId
UserDisplayName
Comment
Text
PostHistoryTypes
Id
Name
PostTags
PostId
TagId
PostTypes
Id
Name
SuggestedEdits
Id
PostId
CreationDate
ApprovalDate
RejectionDate
OwnerUserId
Comment
Text
Title
Tags
RevisionGUID
SuggestedEditVotes
Id
SuggestedEditId
UserId
VoteTypeId
CreationDate
TargetUserId
TargetRepChange
Tags
Id
TagName
Count
TagSynonyms
Id
SourceTagName
TargetTagName
CreationDate
OwnerUserId
AutoRenameCount
LastAutoRename
Score
ApprovedByUserId
ApprovalDate
Votes
Id
PostId
VoteTypeId
UserId
CreationDate
BountyAmount
VoteTypes
Id
Name