如何将构词规范化地存储在关系数据库中?

发布于 2024-08-11 11:34:03 字数 581 浏览 5 评论 0原文

我正在尝试找到一种存储以下形式的单词组合的好方法:

exhaustcleaningsystem
exhaust cleaning system
exhaustcleaning system
exhaust cleaningsystem

组合按每种情况默认给出。组合中的每个单词都作为唯一的行存储在表“标签”中。

labels
id   value
--------------------------
1    exhaustcleaningsystem
2    exhaust
3    cleaning
4    system
5    exhaustcleaning
6    cleaningsystem

我考虑了一个名为“组合”的新表:

compositions
id   domain_id   range
----------------------
1    1           2,3,4
2    1           5,4
etc...

但是在列中存储多个分隔值并不是标准化设计。有什么想法吗?

顺便说一句:我正在使用 MySQL 和 ActiveRecord/Rails。

I'm trying to find a nice way to store word compositions of the following form:

exhaustcleaningsystem
exhaust cleaning system
exhaustcleaning system
exhaust cleaningsystem

The combinations are given by a default per case. Every word in a composition is stored as a unique row in table 'labels'.

labels
id   value
--------------------------
1    exhaustcleaningsystem
2    exhaust
3    cleaning
4    system
5    exhaustcleaning
6    cleaningsystem

I thought about a new table called 'compositions':

compositions
id   domain_id   range
----------------------
1    1           2,3,4
2    1           5,4
etc...

But storing multiple separated values in a column isn't normalized design. Any ideas for that?

BTW: I'm using MySQL und ActiveRecord/Rails.

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

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

发布评论

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

评论(2

转身泪倾城 2024-08-18 11:34:03

您提出的设计甚至不是第一范式,因为范围不是原子的

我在这里使用的模式将是

compositions
id   domain_id
-------------
1    1
2    1

compositions-content
composition_id        rank        label_id
------------------------------------------
1                     1           2
1                     2           3
1                     3           4
2                     1           5
2                     2           4

与composition_id引用composition.id和label_id引用label.id

排名列是可选的,应该在这里如果和仅当您在此处定义的范围对顺序敏感时。

通过这种设计,您可以在数据库级别获得一些引用完整性。

The design you propose is not even in first normal form, since range is not atomic

The schema I'd use here would be

compositions
id   domain_id
-------------
1    1
2    1

compositions-content
composition_id        rank        label_id
------------------------------------------
1                     1           2
1                     2           3
1                     3           4
2                     1           5
2                     2           4

with composition_id referencing an composition.id and label_id referencing label.id

The rank column is optional and should be here if and only if the range you define here is order-sensitive.

With this design, you have some referential integrity at DB level.

烂人 2024-08-18 11:34:03

嗯,这是我在标准化方面所能想到的:

sets
id   domain_id
--------------
1    1
2    1
etc...
compositions
id  set_id  label_id  order
---------------------------
1   1       2         1
2   1       3         2
3   1       4         3
4   2       5         1
5   2       4         2
etc...

Well, this is as far as I can think of in terms of normalisation:

sets
id   domain_id
--------------
1    1
2    1
etc...
compositions
id  set_id  label_id  order
---------------------------
1   1       2         1
2   1       3         2
3   1       4         3
4   2       5         1
5   2       4         2
etc...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文