0

Recently I had this discussion with some other developers about how too many columns in a table , or too many attributes on a model is a code smell . Some argue that a Model with too many Attribute is doing too many things , and should be split . But what If the Model actually requires those attributes ?

Let me take the example of a users table .

A user can have first_name , last_name , street_name , city , state , age , etc . According to the argument , I assume street_name , city and state should be moved into a different table . I agree that related data are grouped together this way , but if the application is querying for the user with his address as well , wont that be a more expensive operation, since they're in 2 tables now ?

So what is the right way to model tables with a lot of attributes ? ( Should we also consider these cases : when 1. the number of rows are going to be less 2. the number of rows are going to be huge )

1
  • You probably shouldn't store age in your database. Your data will all become inaccurate
    – podiluska
    Aug 24, 2012 at 20:59

3 Answers 3

2

It's not a question of "too many attributes in one table". It's a question of "binding the wrong attributes together in one table". The key to a table should be related to some entity or relationship in the subject matter. Non key attributes should be dependent on (determined by) the key, the whole key, and nothing but the key.

This is an oversimplified view of what is called "data normalization". Data normalization helps prevent the necessity for storing the same fact in multiple places in the database. This harmful redundancy is not only wasteful, but it can also lead to a database that contradicts itself. This is a real pain.

Converting an unnormalized design into a normalized design often involves splitting tables. But don't just split tables at random. Learn the normalization rules. Follow them until you become expert enough to know when to disregard them.

3
  • 'Binding wrong attributes together in a table' is definitely not good . I usually spot them from a lot of null values across rows . But what if the attributes are actually dependent on the key of the table , but can also be split and included in another table using a foreign key ? Where do you draw the line in these cases ? Please do share any sources that can explain the normalizing rules more , if you happen to have them . Thanks !
    – Emil
    Aug 24, 2012 at 12:42
  • WRT nulls and normalization, look up sixth normal form. I normally don't worry about sixth normal form. Any table with multiple rows can be split into two related tables. Don't worry about nulls and wasted space. Do worry about nulls and 3 valued logic. Aug 24, 2012 at 13:05
  • If you want sources on explaining normalization, you can search on the "[normalization]" tag here in SO, or you can go to the wikipedia article en.wikipedia.org/wiki/Data_normalization and follow the external links or the further reading sections. For an in-depth treatment, it's hard to beat CJ Date. You can probably get away with a much lighter treatment. Aug 24, 2012 at 13:13
1

Using your address scenario specifically, you'll find it highly beneficial if your design is supposed to cater for multiple addresses per user or to track/trap multiple registrations using the same address.

Alternatively, you could consider a more generic address table implementation where you have a generic description field and a type column that tags the row as a specific type of address (e.g. email, house, office, spouse, etc.).

The moral of the story is the moral of this story is if there could be more than one of it, have a separate table. Over normalization only sets in when there's no benefit in jumping the extra table or two for info that:

  1. Doesn't change much,
  2. Doesn't occur more than once or
  3. Every primary key entity must have it.
1

This is quite a academical question. When designing a database model, you have often only one thing in mind: performance. You won't split a table just because it looks better. You'll do it for instance

  • when you can reduce redundancy
  • or enhance concurrency.

There is also a limit how large a record can be on most, when not all databases. So you may split a table to make the database be able to store it efficiently.

It is completely different when designing classes. Splitting classes doesn't have a big performance impact, but a big maintenance impact. Maintainability should be the main concern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.