当存在与每条信息相关联的可变数量的标签时,在关系数据库中存储标签的良好方式 [英] good way to store tags in relational database when there are variable numbers of tags associated with each piece of information
问题描述
我想在SQL关系数据库中存储可以用变量标签标记的句子。所以,我可能有一个句子,如
'太阳慢慢地在西方,我们向你提出了一个很好的告别。 '
标记以下标记:
'farewell','goodbye','amiable'
句子的标签数量可以不同。在这里给出的例子中,有三个标签。
在关系数据库中组织这种类型的信息是一种明智的方法?
你有一个多对多的关系。创建三个表:
句
,标签
和 sentence_tag
。 句
和标签
每个都有一个主键 id
字段和 sentence_tag
将有 sentence_id
字段和 tag_id
字段。要查找 id = $ sID
的句子标签, SELECT tag_name FROM tags WHERE id IN(SELECT tag_id FROM sentence_tag WHERE sentence_id = $ sID);
I want to store sentences that can be tagged with variable numbers of tags in an SQL relational database. So, I may have a sentence such as
'As the sun sets slowly in the west, we bid you a fine farewell.'
tagged with the following tags:
'farewell', 'goodbye', 'amiable'
The number of tags for a sentence can vary. In the example given here, there are three tags.
What would be a sensible way to organise this type of information in a relational database? What tables etc. would be reasonable?
Thanks!
You have a many-to-many relationship. Make three tables: sentence
, tag
and sentence_tag
. The sentence
and tag
tables will each have a primary key id
field and sentence_tag
will have a sentence_id
field and a tag_id
field. To find the tags for a sentence with id = $sID
,
SELECT tag_name FROM tags WHERE id IN (SELECT tag_id FROM sentence_tag WHERE sentence_id = $sID);
这篇关于当存在与每条信息相关联的可变数量的标签时,在关系数据库中存储标签的良好方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!