我对这两种结构很困惑.这两个表的优缺点是什么?哪个更好,为什么?
I am very confuse about the two structure. What are the advantage and disadvantage of this two table? Which one is better and why?
TABLE1
id, name, age, birthdate, address
somedata1 somedata1 somedata1 somedata1 somedata1
somedata2 somedata2 somedata2 somedata2 somedata2
somedata3 somedata3 somedata3 somedata3 somedata3
TABLE2
id, col_name, col_value
somedata name somedata
somedata age somedata
somedata birthdate somedata
somedata address somedata
somedata2 name somedata2
somedata2 age somedata2
somedata2 birthdate somedata2
somedata2 address somedata2
somedata3 name somedata3
somedata3 age somedata3
somedata3 birthdate somedata3
somedata3 address somedata3
通常情况下,在数据库设计的上下文中,第二个表是反模式.而且,更重要的是,它有特定的名称:Entity-Attribute-Value (EAV).在某些情况下,使用这种设计是合理的,但这种情况很少见 - 即使在这种情况下也可以避免.
In common case, second table is anti-pattern in context of database design. And, even more, it has specific name: Entity-Attribute-Value (EAV). There are some cases, when using this design is justified, but that are rare cases - and even there it can be avoided.
数据完整性支持
尽管如此,这种结构似乎更灵活";或高级",这种设计有弱点.
Despite the fact, that such structure seems to be more "flexible" or "advanced", this design has weakness.
"customer_name"
写为属性名称 - 另一个开发人员会忘记这一点并使用 "name_of_customer"
.而且.. 没关系,DB 会通过的,您将花费数小时来调试这个案例."customer_name"
as attribute name in first case - and another developer will forget that and use "name_of_customer"
. And.. it's ok, DB will pass that and you'll end with hours spent on debugging this case.行重建
此外,行重建在普通情况下会很糟糕.例如,如果您有 5 个属性 - 这将是 5 个自表 JOIN
-s.对于这种简单的——乍一看——情况来说太糟糕了.所以我什至不想想象你将如何维护 20 个属性.
In addition, row reconstruction will be awful in common case. If you have, for example, 5 attributes - that will be 5 self-table JOIN
-s. Too bad for such simple - at first glance - case. So I don't want even imagine how you'll maintain 20 attributes.
我的观点是 - 不.在 RDBMS 中,总会有办法避免这种情况.这太糟糕了.如果打算使用 EAV,那么最好的选择可能是非关系数据库.
My point is - no. In RDBMS there will always be a way to avoid this. It's horrible. And if EAV is intended to be used, then best choice may be non-relational databases.
这篇关于两种表结构的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!