对于存储相关关键字的数据库模式有什么建议吗?

发布于 2024-08-12 02:34:18 字数 520 浏览 2 评论 0原文

我必须在数据库中存储一组相关的关键字。截至目前,我正在考虑使用以下内容:

存储关键字本身:

CREATE TABLE keywords(
   id int(11) AUTO_INCREMENT PRIMARY KEY,
   word VARCHAR(255)
);

存储关系(存储相关关键字的 id):

CREATE TABLE relatedkeywords(
   id int(11) AUTO_INCREMENT PRIMARY KEY,
   keyword1 int(11),
   keyword2 int(11),
   FOREIGN KEY (keyword1) REFERENCES keywords(id),
   FOREIGN KEY (keyword2) REFERENCES keywords(id)
);

这是惯例还是有更好的方法?我看到的唯一问题是我需要检查两个列,以便有时能够获取相关的关键字......我可能在这里遗漏了一些东西。

I have to store a set of related keywords inside a database. As of now, I am thinking of using the following:

To store the keywords themselves:

CREATE TABLE keywords(
   id int(11) AUTO_INCREMENT PRIMARY KEY,
   word VARCHAR(255)
);

To store the relations (stores the ids of the related keywords):

CREATE TABLE relatedkeywords(
   id int(11) AUTO_INCREMENT PRIMARY KEY,
   keyword1 int(11),
   keyword2 int(11),
   FOREIGN KEY (keyword1) REFERENCES keywords(id),
   FOREIGN KEY (keyword2) REFERENCES keywords(id)
);

Is this the convention or is there a better way of doing this? The only problem I am seeing is that I need to check both the column in order to be able to get the related keywords sometimes... I might be missing something here.

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

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

发布评论

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

评论(3

甜是你 2024-08-19 02:34:18

如果“相关性”是一对关键字的属性,则此模式是可以的(不要忘记添加 UNIQUE(keyword1, keywords2))

如果“相关性”可以传播一组关键字,并且一组相关关键字可能有附加的在属性中,您可能需要添加一个新表“Related_Set”以及关键字和集合之间的 M:N 关系“Keyword_Set”。

如果集合没有任何附加属性,您可能只使用“Keyword_Set”表

If "relatedness" is a property of a pair of keywords, this schema is OK (don't forget to add UNIQUE(keyword1, keyword2))

If "relatedness" can spread a set of keywords and a set of related keywords may have additional propertirs, you may want to add a new table "Related_Set" and a M:N relationship "Keyword_Set" between keywords and sets.

If a set doesn't have any additional properties, you may just live with "Keyword_Set" table

驱逐舰岛风号 2024-08-19 02:34:18

将第二个表简化为:

CREATE TABLE relatedkeywords(
   keyword1 int(11),
   keyword2 int(11),
   FOREIGN KEY (keyword1) REFERENCES keywords(id),
   FOREIGN KEY (keyword2) REFERENCES keywords(id),
   PRIMARY KEY (keyword1, keyword2)
)

因为这是“人造主键”没有什么意义并且没有实际用处的情况之一。

Simplify the second table to:

CREATE TABLE relatedkeywords(
   keyword1 int(11),
   keyword2 int(11),
   FOREIGN KEY (keyword1) REFERENCES keywords(id),
   FOREIGN KEY (keyword2) REFERENCES keywords(id),
   PRIMARY KEY (keyword1, keyword2)
)

as this is one of the cases where an "artificial primary key" just makes little sense and offers no practical usefulness.

千仐 2024-08-19 02:34:18

有没有只有一张桌子的解决方案 -

create table keywords (
   keywrd varchar (40) not null primary key,
   related_keys_csv varchar(400)  
)

Is there a solution with just one table -

create table keywords (
   keywrd varchar (40) not null primary key,
   related_keys_csv varchar(400)  
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文