Is storing a delimited list in a database column really that bad? -


imagine web form set of check boxes (any or of them can selected). chose save them in comma separated list of values stored in 1 column of database table.

now, know correct solution create second table , normalize database. quicker implement easy solution, , wanted have proof-of-concept of application , without having spend time on it.

i thought saved time , simpler code worth in situation, defensible design choice, or should have normalized start?

some more context, small internal application replaces excel file stored on shared folder. i'm asking because i'm thinking cleaning program , make more maintainable. there things in there i'm not entirely happy with, 1 of them topic of question.

in addition violating first normal form because of repeating group of values stored in single column, comma-separated lists have lot of other more practical problems:

  • can’t ensure each value right data type: no way prevent 1,2,3,banana,5
  • can’t use foreign key constraints link values lookup table; no way enforce referential integrity.
  • can’t enforce uniqueness: no way prevent 1,2,3,3,3,5
  • can’t delete value list without fetching whole list.
  • can't store list longer fits in string column.
  • hard search entities given value in list; have use inefficient table-scan. may have resort regular expressions, example in mysql:
    idlist regexp '[[:<:]]2[[:>:]]'
  • hard count elements in list, or other aggregate queries.
  • hard join values lookup table reference.
  • hard fetch list in sorted order.
  • storing integers strings takes twice space storing binary integers. not mention space taken comma characters.

to solve these problems, have write tons of application code, reinventing functionality rdbms already provides more efficiently.

comma-separated lists wrong enough made first chapter in book: sql antipatterns: avoiding pitfalls of database programming.

there times when need employ denormalization, @omg ponies mentions, these exception cases. non-relational “optimization” benefits 1 type of query @ expense of other uses of data, sure know of queries need treated specially deserve denormalization.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -