您留下了很多未说明的要求,因此很难具体说明。
但是,如果我在 MySQL 中工作,这就是我会做的事情。
发布表:
post_id int primary key an id number
field int not null which of the 20 or so text fields this goes with
user_id int not null fk the user making the post
active tinyint 1 = active 2 = invisible to users (deleted)
(other identifying data as needed)
posttime datetime not null when the post was first created
edittime datetime not null when the post was most recently changed
(if never changed, the same as posttime)
title varchar text of the post's title
POSTTEXT 表
post_id int part of primary key
posttext_id int part of primary key
posttext text part or all of the post's text
active tinyint 1 = active 2 = invisible to users (deleted)
因此,每个帖子在 POST 中将有一行,在 POSTTEXT 中将有零或多行。大多数帖子在 POSTTEXT 中只有一行,但超长帖子可能有几行。POSTTEXT.post_id 是指向 POST.post_id 的外键。posttext_id 是每个不同帖子从零开始计数的数字。
为什么,你问,不只是把 posttext 列放在 POST 表中?好吧,如果你确定你永远不需要让你的用户浏览很长的帖子,你可以这样做。您的架构会简单得多。但是如果系统中有很多十万字的帖子,用户可能想查看第n页的文本,那么两表解决方案会更好。
这种事情会比具有多个字段的多个文本列的表格执行得更好。在 RDMS 工作中,处理多个相似的行几乎总是比处理具有大量列的真正宽行要好。