多对多关系、逐位比较与链接器表

发布于 2024-09-12 19:54:49 字数 366 浏览 1 评论 0原文

数据库表(例如汉堡)包含一个将位存储为整数值的字段,因此它的值为 1,2,4,8,16 ...

辅助表包含这些值 1 = 奶酪,2 = 番茄,4 = 鸡蛋,8 = 生菜,16 = 蛋黄酱

按位比较可以使任何汉堡具有任意馅料组合(对于新手来说,val 24 的汉堡将包含蛋黄酱和生菜(16 + 8 ),而 val 5 的汉堡将包含奶酪和鸡蛋等)。

据我所知,这不是一个特别好的主意,你只能使用相对较小范围的不同汉堡馅料(64?)。唯一的优点是它节省了数据库空间,因为不需要链接器表。

然而,我接替的人总体来说技术非常出色。那么我还缺少其他什么好处吗?在我看来,有两个缺点:它限制了不同填充物的数量,而且它很奇怪/不寻常(当记忆是主要考虑因素时,挂断了?)。

A database table, lets say burger, contains a field that stores bits as an integer value, so it has value of either 1,2,4,8,16 ...

A secondary table contains the values
1 = cheese, 2 = tomato, 4=egg, 8 = lettuce, 16 = mayo

Bit wise comparison then enables any burger to have any combination of fillings (for the uninitiated a burger with val 24 would have mayo and lettuce(16 + 8), and a burger with val 5 would have cheese and egg, etc).

As far I can see this is not a particularly good idea, you are limited to a relatively small range of different burger fillings (64?). The only merit is it saves space on the database as no need for a linker table.

However, I am taking over from a someone who was, generally, very good technically; so are there are any other benefits I am missing? As it seems to me there are two negatives: it limits the number of different fillings, and its weird/unusual (a hang up from the when memory was the main consideration ?).

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

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

发布评论

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

评论(3

七七 2024-09-19 19:54:49

我看到的唯一好处是,它可以相对容易地返回一个以逗号分隔的字段,其中包含一行中的所有汉堡配料和其他数据。顺便说一句,我不知道如何做到这一点。如果是这样的话,我很想看看 SQL。

如果前端程序缓存辅助表中的数据并在返回后解析按位字段,那么也将消除对数据库的调用。

但是,我没有看到任何其他好处。我只会使用链接器表并完成它,因为我认为位打包只会使数据库设计变得复杂。

The only benefit I see, is that it might make it relatively easy to return a comma-delimited field containing all the burger toppings and other data all in one row. Offhand, I don't know how that would be done though. I would be interested to see that SQL if that's the case.

If the front-end program cached data from the secondary table and parsed the bitwise field after being returned it would eliminate a call to the database as well.

However, I don't see any other benefit. I would just use a linker table and be done with it, because the bit-packing, I think, just complicates the database design.

挽容 2024-09-19 19:54:49

以这种方式使用位掩码,您将无法利用 SQL 引用完整性检查的强大功能。如果我输入值 128 会发生什么?您的查找表中没有这样的条目。正确实现外键约束后,如果我尝试加载查找表中不存在的值,我会收到错误,并且该值将(正确)不会加载到数据库中。

实现按位运算符可能更快、更容易(也更有趣),但随着时间的推移,正确规范化的数据库可以更容易支持和维护。

Using bitmasks this way, you will not be able to leverage the power of SQLs referential integrity checks. What happens if somehow I enter a value of 128? There is no such entry in your lookup table. With foreign key constraints properly imlemented, if I attempted to load a value that did not exist in the lookup table, I'd get an error and the value would (properly) not be loaded in the database.

It may be quicker and easier (and more interesting) to implement bitwise operators, but properly normalized databases can be a lot easier to support and maintain over time.

起风了 2024-09-19 19:54:49

您可以通过添加一个表来解决该解决方案
看起来像:

Table: Fillings
Id  Cheese Tomato Egg Lettuce Mayo
 0     N      N     N     N     N
 1     Y      N     N     N     N
 2     N      Y     N     N     N
 3     Y      Y     N     N     N
...
31     Y      Y     Y     Y     Y

那么你可以说:

select
    count(1) as WithCheeseButNoTomato
from Burger as b
join Fillings as f on f.Id = b.FillingId
where Cheese = 'Y'
  and Tomato = 'N' ;

任何所谓的好处都在前端填充选择或应用程序层。

You could get around that solution by adding a table that would
look like:

Table: Fillings
Id  Cheese Tomato Egg Lettuce Mayo
 0     N      N     N     N     N
 1     Y      N     N     N     N
 2     N      Y     N     N     N
 3     Y      Y     N     N     N
...
31     Y      Y     Y     Y     Y

then you can say:

select
    count(1) as WithCheeseButNoTomato
from Burger as b
join Fillings as f on f.Id = b.FillingId
where Cheese = 'Y'
  and Tomato = 'N' ;

Any supposed benefits are in the front end filling selection or in the application layer.

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