• <bdo id='F4vaY'></bdo><ul id='F4vaY'></ul>

      1. <tfoot id='F4vaY'></tfoot>

        <small id='F4vaY'></small><noframes id='F4vaY'>

      2. <legend id='F4vaY'><style id='F4vaY'><dir id='F4vaY'><q id='F4vaY'></q></dir></style></legend>

        <i id='F4vaY'><tr id='F4vaY'><dt id='F4vaY'><q id='F4vaY'><span id='F4vaY'><b id='F4vaY'><form id='F4vaY'><ins id='F4vaY'></ins><ul id='F4vaY'></ul><sub id='F4vaY'></sub></form><legend id='F4vaY'></legend><bdo id='F4vaY'><pre id='F4vaY'><center id='F4vaY'></center></pre></bdo></b><th id='F4vaY'></th></span></q></dt></tr></i><div id='F4vaY'><tfoot id='F4vaY'></tfoot><dl id='F4vaY'><fieldset id='F4vaY'></fieldset></dl></div>

        两种表结构的区别

        时间:2023-11-27

          <small id='Rj7t0'></small><noframes id='Rj7t0'>

            <tbody id='Rj7t0'></tbody>
        1. <tfoot id='Rj7t0'></tfoot>
            <i id='Rj7t0'><tr id='Rj7t0'><dt id='Rj7t0'><q id='Rj7t0'><span id='Rj7t0'><b id='Rj7t0'><form id='Rj7t0'><ins id='Rj7t0'></ins><ul id='Rj7t0'></ul><sub id='Rj7t0'></sub></form><legend id='Rj7t0'></legend><bdo id='Rj7t0'><pre id='Rj7t0'><center id='Rj7t0'></center></pre></bdo></b><th id='Rj7t0'></th></span></q></dt></tr></i><div id='Rj7t0'><tfoot id='Rj7t0'></tfoot><dl id='Rj7t0'><fieldset id='Rj7t0'></fieldset></dl></div>

                <legend id='Rj7t0'><style id='Rj7t0'><dir id='Rj7t0'><q id='Rj7t0'></q></dir></style></legend>

                • <bdo id='Rj7t0'></bdo><ul id='Rj7t0'></ul>

                  本文介绍了两种表结构的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对这两种结构很困惑.这两个表的优缺点是什么?哪个更好,为什么?

                  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).在某些情况下,使用这种设计是合理的,但这种情况很少见 - 即使在这种情况下也可以避免.

                  Antipattern?

                  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.

                  • 无法制作强制性属性.您不能强制某些属性,因为属性现在存储为一行 - 并且该属性未设置的唯一标志是表中不存在相应的行.SQL 不允许您在本地构建此类约束 - 因此,您必须在应用程序中进行检查 - 并且,是的,每次都查询您的表
                  • 混合数据类型.您将无法使用 SQL 标准数据类型.因为您的值列必须是超类型"对于其中的所有存储值.这意味着 - 通常您必须将所有数据存储为原始字符串.然后你会看到处理日期和处理字符串一样痛苦,每次都转换数据类型,检查数据完整性,等等
                  • 无法强制执行参照完整性.在正常情况下,您可以使用外键来限制您的值,这些值在父表中定义.但不是在这种情况下 - 那是因为参照完整性应用于表中的每一行,但不适用于行值.所以 - 你会失去这个优势 - 它是关系数据库中的一个基本
                  • 无法设置属性名称.这意味着 - 您不能在 DB 级别正确限制属性名称.例如,您将在第一种情况下将 "customer_name" 写为属性名称 - 另一个开发人员会忘记这一点并使用 "name_of_customer".而且.. 没关系,DB 会通过的,您将花费数小时来调试这个案例.
                  • Impossible to make mandatory attributes. You can not make some attribute mandatory, since attribute is now stored as a row - and the only sign that attribute is not set - is that the corresponding row absent in table. SQL will not allow you to build such constraint natively - thus, you'll have to check that in application - and, yes, query your table each time
                  • Mixing of data types. You will not be able to use SQL standard data types. Because your value column must be a "super-type" for all stored values in it. That means - you'll have in general to store all data as raw strings. Then you'll see how painful is to work with dates as with strings, casting data types each time, checking data integrity, e t.c.
                  • Impossible to enforce referential intregrity. In normal situation, you can use foreign key to restrict your values by those, which are defined in parent table. But not in this case - that's because referential integrity is applied to each row in table, but not for row values. So - you'll loose this advantage - and it's one of fundamental in relation DB
                  • Impossible to set attributes names. That means - you can't restrict attribute name on DB level properly. For example, you'll write "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.

                  这篇关于两种表结构的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我应该使用 SQL_Variant 数据类型吗? 下一篇:用户反馈系统的正确数据库模型(一个有趣的案例)

                  相关文章

                • <i id='HapKM'><tr id='HapKM'><dt id='HapKM'><q id='HapKM'><span id='HapKM'><b id='HapKM'><form id='HapKM'><ins id='HapKM'></ins><ul id='HapKM'></ul><sub id='HapKM'></sub></form><legend id='HapKM'></legend><bdo id='HapKM'><pre id='HapKM'><center id='HapKM'></center></pre></bdo></b><th id='HapKM'></th></span></q></dt></tr></i><div id='HapKM'><tfoot id='HapKM'></tfoot><dl id='HapKM'><fieldset id='HapKM'></fieldset></dl></div>

                    <legend id='HapKM'><style id='HapKM'><dir id='HapKM'><q id='HapKM'></q></dir></style></legend>
                      • <bdo id='HapKM'></bdo><ul id='HapKM'></ul>
                    1. <small id='HapKM'></small><noframes id='HapKM'>

                      <tfoot id='HapKM'></tfoot>