MySQL
  Home arrow MySQL arrow Page 5 - Database Design Using Key-Value Tables
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

Database Design Using Key-Value Tables
By: Tal Olier
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 25
    2006-02-13

    Table of Contents:
  • Database Design Using Key-Value Tables
  • Traditional database design vs. key-value table
  • Drawback no. 1: No real use of database data types
  • Drawback no. 2: Awkward use of database constraints
  • Drawback no. 3: Problematic use of AND operator when relating to more than one attribute in the query criteria

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Database Design Using Key-Value Tables - Drawback no. 3: Problematic use of AND operator when relating to more than one attribute in the query criteria


    (Page 5 of 5 )

    This is the most problematic issue that needs to be taken into consideration. Since it is complex, let’s start with an example. Consider the following report (report 1):

    Fetch all employee names and their start working date, that had started working in 2004. The result of course should look like the following table:

    NAME

    START_WORK_YEAR

    Dany

    2004

    Brit

    2004

    In scenario 1, the following query will provide the required results:

    SELECT NAME, START_WORK_YEAR
    FROM EMPLOYEES
    WHERE START_WORK_YEAR = 2004;

    In scenario 2, the following query will provide the required results:

    SELECT E.NAME, EA.VALUE1 AS START_WORK_YEAR
    FROM EMPLOYEES E INNER JOIN EMPLOYEE_ATTRIBUTES EA
        ON (E.ID = EA.EMPLOYEE_ID)
    WHERE KEY1 = 'START_WORK_YEAR' AND VALUE1 = '2004';

    But in case we want to check more than one attribute in the criteria, and the relation between the two attributes would need to be defined by an AND operator, we need to be careful not to fall into the following trap. Consider the following report (report 2):

    Fetch all employee names, and their start working date that are males AND had started working in 2004. The result of course should look like the following table:

    In scenario 1, the following query will provide the required results:

    SELECT NAME, START_WORK_YEAR
    FROM EMPLOYEES
    WHERE START_WORK_YEAR = 2004 AND SEX = 'MALE';

    NAME

    START_WORK_YEAR

    Dany

    2004

    In scenario 2, many developers fall into the trap and introduce the following query as the report implementation:

    SELECT E.NAME, EA.VALUE1 AS START_WORK_YEAR
    FROM EMPLOYEES E INNER JOIN EMPLOYEE_ATTRIBUTES EA 
        ON (E.ID = EA.EMPLOYEE_ID)
    WHERE (EA.KEY1 = 'START_WORK_YEAR' AND EA.VALUE1 = '2004') 
        AND (EA.KEY1 = 'SEX' AND EA.VALUE1 = 'MALE');

    The above query will provide the following results:

    NAME

    START_WORK_YEAR

    We get an empty result-set!

    The reason for it is that no record in the Cartesian product of EMPLOYEES and EMPLOYEE_ATTRIBUTES fulfill both conditions of

    (EA.KEY1 = ‘START_WORK_YEAR’ AND EA.VALUE1 = ‘2004’)
    AND (EA.KEY1 = ‘SEX’ AND EA.VALUE1 = ‘MALE’)

    In order to better understand this issue, let's look at what the database engine is doing:

    First the engine creates the following Cartesian product of EMPLOYEES and EMPLOYEE_ATTRIBUTES:

    E.ID

    E.NAME

    E.SALARY

    EA.EMPL-
    OYEE_ID

    EA.KEY

    EA.VALUE

    1

    Dany

    4000

    1

    HAS_COMPANY_VEHICLE

    1

    1

    Dany

    4000

    1

    START_WORK_YEAR

    2004

    1

    Dany

    4000

    1

    SEX

    MALE

    2

    Brit

    5000

    2

    HAS_COMPANY_VEHICLE

    0

    2

    Brit

    5000

    2

    START_WORK_YEAR

    2004

    2

    Brit

    5000

    2

    SEX

    FEMALE

    3

    Sharon

    1000

    3

    HAS_COMPANY_VEHICLE

    1

    3

    Sharon

    1000

    3

    START_WORK_YEAR

    2005

    3

    Sharon

    1000

    3

    SEX

    FEMALE

    Then the database engine removes records which do not comply with the where criteria. As we can see, there is no record which complies with both:

    (EA.KEY1 = ‘START_WORK_YEAR’ AND EA.VALUE1 = ‘2004’)
    AND (EA.KEY1 = ‘SEX’ AND EA.VALUE1 = ‘MALE’)

    The above pitfall can be workaround using the following query:

    SELECT E.NAME, EA1.VALUE1 AS START_WORK_YEAR
    FROM EMPLOYEES E INNER JOIN EMPLOYEE_ATTRIBUTES EA1 
        ON (E.ID = EA1.EMPLOYEE_ID) 
        INNER JOIN EMPLOYEE_ATTRIBUTES EA2 
            ON (EA1.EMPLOYEE_ID = EA2.EMPLOYEE_ID)
    WHERE (EA1.KEY1 = 'START_WORK_YEAR' AND EA1.VALUE1 = '2004') 
        AND (EA2.KEY1 = 'SEX' AND EA2.VALUE1 = 'MALE');

    In this case the database engine will create the following Cartesian product and select the marked record using the criteria clause:

    E.
    ID

    E.
    NAME

    E.
    SALARY

    EA1/2.
    EMPL-
    OYEE_ID

    EA1.
    KEY

    EA1.
    VALUE

    EA2.
    KEY

    EA2.
    VALUE

    1

    Dany

    4000

    1

    SEX

    MALE

    SEX

    MALE

    1

    Dany

    4000

    1

    SEX

    MALE

    START_WORK_YEAR

    2004

    1

    Dany

    4000

    1

    START_WORK_YEAR

    2004

    SEX

    MALE

    1

    Dany

    4000

    1

    START_WORK_YEAR

    2004

    START_WORK_YEAR

    2004

    2

    Brit

    5000

    2

    SEX

    FEMALE

    SEX

    FEMALE

    2

    Brit

    5000

    2

    SEX

    FEMALE

    START_WORK_YEAR

    2004

    2

    Brit

    5000

    2

    START_WORK_YEAR

    2004

    SEX

    FEMALE

    2

    Brit

    5000

    2

    START_WORK_YEAR

    2004

    START_WORK_YEAR

    2004

    3

    Sharon

    1000

    3

    SEX

    FEMALE

    SEX

    FEMALE

    3

    Sharon

    1000

    3

    SEX

    FEMALE

    START_WORK_YEAR

    2005

    3

    Sharon

    1000

    3

    START_WORK_YEAR

    2005

    SEX

    FEMALE

    3

    Sharon

    1000

    3

    START_WORK_YEAR

    2005

    START_WORK_YEAR

    2005

    Conclusion

    We can use key-value tables in a database design, but we should keep in mind that the same flexibility which has been enabling us to add attributes in a quick and easy way can also cause data integrity errors.

    Furthermore, using key-value tables is recommended mostly with attributes which are required for long descriptions, rather than attributes which are used for data filtering. 


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · i rated this article 4 out of 5 because it does an excellent job of describing and...
       · This design flaw is called EAV (Entity-Attribute-Value) and it does not work. It...
       · But I have to echo the sentiments listed here. If you do not exercise iron-clad...
       · In my previous comment, I stated that I have NEVER seen meta-data done right. Take...
       · This design is a wonderful thing for holding global data of multiple types or for...
       · Thanks.Did you read it all?I’ve mentioned all drawbacks I know, do you have...
       · Having been a dba over the past 36 years for DB2, Oracle, SAS, MySQL, and a few...
       · There are many systems that work on EAV for recording observations and...
     

       

    MYSQL ARTICLES

    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL
    - Getting PHP to Talk to MySQL
    - Creating an RSS Reader: the Reader
    - MySQL Security Overview
    - Creating the Admin Script for a PHP/MySQL Bl...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway