views:

385

answers:

3

Is using one database column for each attribute in an ER-model considered best practice?

I do maintenance on legacy code, and I see a lot of multiple attributes, as well as business logic squeezed into single database columns, and I wonder if there is ever any good reason for doing so.

Example is using prefixes for integer values, say a customer ID where the first two digits represent the customer state, and the following digits, the actual id of the customer. Thus storing two attributes in one field. Another example is using negative customer ID:s for customers that have ended their business, thus storing also information on whether the customer is active or not. Etc etc.

+1  A: 

What you have there sounds like an application for a new entry in The Daily WTF, not a sensible database design. Before considering such a thing, read up on database normalisation, and save yourself the headaches.

1800 INFORMATION
+1  A: 

There are many problems that occur in storing multiple fields in a column. For example suppose you need to update such a column, first you were using 'ab' as customer state but then decide to update it to 'cd'. This will get very messy when multiple fields are stored in one column.

You should checkout Database Normalization to better understand this. http://en.wikipedia.org/wiki/1NF

Babar
+3  A: 

Storing multiple pieces of information in a column is a violation of the first normal form, and therefore considered very bad in a relational database.

Rik
I upvoted, but in the case of Customer Id maybe that's how the business identifies their customers and TX123 is different to CA123. I could live with putting it in one column in that case. Would depend on the semantics of the update when a customer moved state, etc.
WW