问题描述
我必须在数据库中存储一组相关的关键字。截至目前,我正在考虑使用以下内容:存储关键字本身:
CREATE TABLE关键字(
id int(11)AUTO_INCREMENT PRIMARY KEY,
word VARCHAR(255)
);
存储关系(存储相关关键字的ids):
CREATE TABLE relatedkeywords(
id int(11)AUTO_INCREMENT PRIMARY KEY,
keyword1 int(11),
keyword2 int (11),
FOREIGN KEY(keyword1)参考关键字(id),
FOREIGN KEY(keyword2)参考关键词(id)
);
这是惯例还是有更好的方法?我唯一遇到的问题是我需要检查这两列,以便能够有时得到相关的关键字...我可能会在这里缺少一些东西。
如果关联性是一对关键字的属性,则该模式是可以的(不要忘了添加UNIQUE(keyword1,keyword2))
如果相关性可以传播一组关键字,并且一组相关的关键字可能有其他的属性,则可能需要在关键字之间添加一个新表Related_Set和M:N关系Keyword_Set并设置。
如果一个集合没有任何其他属性,您可能只能使用Keyword_Set表
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.
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
这篇关于任何关于用于存储相关关键字的数据库模式的建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!