Friday 20 January 2012

Many to many data table connection handling

There are some other ways to create connnections but as a programmer you should allways consider the future. An ideal program should work when it's grow or forcing the borders without changing any field lengths.

So you have to plan the future for the worst scenerios. Many to many connections are the gaps that new developers fall into it.

This is the ideal way of handling this situation.

When will you need many to many connections ?
Consider there are two tables Profile and Hobbies.
There are many profiles and many hobbies. How will you keep which profile has which hobbies or finding profiles who play chess ?
In this case you'll need another table to keep connections. Which we call "Cross Reference Table"

Here is the table structure

ProfileHobbyProfileHobbyCross
IDIDID
........ProfileID
........HobbyID

You may say that there is no need to keep ID field in cross reference table. I find it useful but it's up to you.

Keeping a cross reference table expands the horizons of your program. Whatever the record numbers will be you can handle it. Also updates can be done easily.

No comments:

Post a Comment