如何制作像 stackoverflow.com 这样的标签系统?

发布于 2025-01-04 20:09:13 字数 164 浏览 1 评论 0原文

我要做一个网站来发布带有标签的内容。标签最多可以有五个标签,如 stackoverflow.com 中的标签。

谁能告诉我StackOverflow的标签系统吗?带有post和tag的关系数据库系统。

我应该在帖子表中添加一列还是应该为其创建单独的标签表?标签可以用空格或逗号分隔。

I'm going to make a website to post with tags. Tags can be up to five number of tags like at stackoverflow.com.

Can anyone tell the tag system of StackOverflow? The relational database system with post and tag.

Should I add a column in post table or should I create separate tag table for it? Tags can be separated by spaces or comma.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

场罚期间 2025-01-11 20:09:13

一定要创建一个包含可用标签列表的表格。

您还应该创建一个单独的应用标签表,其中包含:

  • 帖子的外键。
  • 您的标签的外键。
  • 序号
    显示顺序。
  • 您可能感兴趣的其他任何事情,例如谁
    添加标签或添加标签时。

您想要使用规范化设计,因为如果您想要更改业务规则以允许更少或更多标签,则使用非规范化设计(添加 5 列)将会中断。此外,如果您需要保留其他信息(例如标签的添加时间和添加者),这对您也没有帮助。


编辑:DDL

应OP的要求:

CREATE TABLE post (
  id            INTEGER       IDENTITY 
, title         VARCHAR(1000) NOT NULL
, added_date    DATETIME      NOT NULL
, posting_user_id INTEGER     NOT NULL
, ... (and so forth) ...
, PRIMARY KEY (id)
, FOREIGN KEY (posting_user_id) REFERENCES posting_user (id)
);

CREATE TABLE tag (
  id            INTEGER       IDENTITY
, term          VARCHAR(20)   NOT NULL
, description   VARCHAR(1000) NULL
, ... (and so forth) ....
, PRIMARY KEY (id)
);

CREATE TABLE applied_tag (
  post_id       INTEGER       NOT NULL
, tag_id        INTEGER       NOT NULL
, display_order INTEGER       NOT NULL
, tagging_user  INTEGER       NOT NULL
, applied_date  DATETIME      NOT NULL
, ... (anything else you want)....
, PRIMARY KEY (post_id, tag_id_, display_order) -- Or use an auto-increment, but this is unique.
, FOREIGN KEY (post_id) REFERENCES post (id)
, FOREIGN KEY (tag_id) REFERENCES tag (id)
, FOREIGN KEY (tagging_user) REFERENCES posting_user (id)
);

Definitely create a table containin the list of available tags.

You should also definitely create a separate applied tag table containing:

  • Foreign key to your post.
  • Foreign key to your tag.
  • Sequence number
    showing the order.
  • Anything else that might interest you, like who
    added the tag or when it was added.

You want to use a normalized design because using an denormalized design (adding 5 columns) will break if you ever want to change your business rules to allow fewer or more tags. Also, it doesn't help you if you have other information to keep, such as when the tag was added and by whom.


EDIT: DDL

At the OP's request:

CREATE TABLE post (
  id            INTEGER       IDENTITY 
, title         VARCHAR(1000) NOT NULL
, added_date    DATETIME      NOT NULL
, posting_user_id INTEGER     NOT NULL
, ... (and so forth) ...
, PRIMARY KEY (id)
, FOREIGN KEY (posting_user_id) REFERENCES posting_user (id)
);

CREATE TABLE tag (
  id            INTEGER       IDENTITY
, term          VARCHAR(20)   NOT NULL
, description   VARCHAR(1000) NULL
, ... (and so forth) ....
, PRIMARY KEY (id)
);

CREATE TABLE applied_tag (
  post_id       INTEGER       NOT NULL
, tag_id        INTEGER       NOT NULL
, display_order INTEGER       NOT NULL
, tagging_user  INTEGER       NOT NULL
, applied_date  DATETIME      NOT NULL
, ... (anything else you want)....
, PRIMARY KEY (post_id, tag_id_, display_order) -- Or use an auto-increment, but this is unique.
, FOREIGN KEY (post_id) REFERENCES post (id)
, FOREIGN KEY (tag_id) REFERENCES tag (id)
, FOREIGN KEY (tagging_user) REFERENCES posting_user (id)
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文