Hudzilla.org - the homepage of Paul Hudson
Contents > Databases > Normalisation Wish List | Report Bug | About Me ]

9.8.2     So, what is the solution here?

This is NOT the latest copy of this book; click here for the latest version.

Normalisation is the process of producing a set of tables that have the same properties we would have had in our large table, except split neatly up into grouped elements. This is how our master table looks right now:

  • ID

  • Name

  • Address 1

  • Address 2

  • Company Name

  • Company Address 1

  • Company Address 2

  • Company City

When you think about it, the city a company is in really has no relation to the city a person lives in - the company and the person are different things entirely, linked merely by the fact that a person works for a company. So, we could split our table into two smaller tables like this:



Our two tables, People and Companies, look something like this:

People:

  • ID

  • Name

  • Address1

  • Address2

  • CompanyNum (new)

Companies

  • ID

  • Name

  • Address1

  • Address2

  • City

Structured like this, companies are stored separately to people, with only an ID number linking the two - each person has a CompanyNum, which should be set to the ID of the company (in the Companies table) that they work for.

Now if we change the address of Microsoft, we only need to update it in one place. Or if we're adding a new person to BloggsCo, we just need to enter "1" into the CompanyNum box rather than copy all the information across. This eliminates both the data duplication and the chance of error - operators can be restricted to selecting a company by name from the Companies table.





<< 9.8.1 Why separate data?   9.8.3 Why not separate data? >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Reau - 29 Aug 2008

If it was my call I'd make 4 tables:

Addresses

Persons

Contacts

Organizations

You might think that an address table is not really helpful. But if you think about it: First of all you can relate this table to some kind of (national) address database so that when creating a new organization, searching for zip code and street number should be enough to make a relation to the right address. Second, this way it's much easier when you create a new person, which is part of an organization to fill is the right (work)address.

Persons and contacts might seem the same thing, but actually they're not. What about people who have different relationships with different organizations? should they be entered twice in the person-table? It's probably better to enter them once (with their name and birthdate for example) and then put other information, like the relationship to an organization and the jobtitle in the contacts table. This way persons are unique, but a person can have multiple contacts (for example manager at company A, Board of directors at company B and a private contact with their home address). It's all about object relationships!

A PHP User - 29 Aug 2008

no image

A PHP User - 29 Aug 2008

picture doesn't show

nielubie@yahoo.com - 29 Aug 2008

The image on this page is not loading. I get an X instead of the image. I'm talking about the image for the normalized database.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is six plus nine?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow