Image: sample tables from Project database
These are the three sample tables from the Project database
R E L A T E D   C O N T E N T
ADVERTISEMENT

Hands on: Solving a storage and usabililty problem

How one-to-one relationships can help optimise storage without causing problems for users

Mark Whitehorn, Personal Computer World 25 Jan 2007
ADVERTISEMENT

One-to-one relationships are relatively rarely implemented and they get little airtime, so I was pleased to receive a question from Wayne Sutherland that can be answered using this kind of relationship.

Wayne has a set of data about projects and he wants a user-friendly way of entering data. Each project has 50 pieces of information associated with it: a project has an ID, start and end dates and so on (Wayne’s data is more complex, but the principle is the same).

Normally we’d place all this data into a single table because it all pertains to a single project ‘object’ in the real world. (The complete database application, of course, would be likely to have multiple tables but here we’re solely concerned with one entity – a project – and its 50 attributes).

Why would we ever use more than one table for the project data? Imagine a situation where 30 of the fields contain core information that is collected about each project without fail. The remaining fields are rarely used.

Ten of them, for example, are for data about the project architect. Some projects require the services of an architect, even though most don’t. The other 10 fields are for external finance data; again most projects don’t need external finance but a few do.

These occasional eventualities are covered perfectly by the single table solution: the only problem is that it wastes space in the table. Can we devise a solution where storage is optimised without making life difficult for the users?

Yes, we can (I wouldn’t have started this otherwise…) We’ll begin with three simple sample tables like the ones shown in the screen, where the ID field in the Project, Architect and ExtFinance tables are of type Number.

The Default Value attribute needs to be blank for all three tables, rather than the default Default Value, which is zero. We then put in place two one-to-one joins using Access’ Relationships editor. Both joins come from the ProjectID field in the Projects table. One goes to Architect.ProjectID and the other ExtFinance.ProjectID.

As I noted earlier, one-to-one relationships are rare because the implication is that all the data could go into one table. If there were always an exact one-to-one relationship between two pieces of data, there would be no point in splitting the data into two tables.

What we have here, if we specify it more precisely, is a one-to-zero-or-one relationship. For each record in the Project table there can be one entry in the Architect table (or in the ExtFinance table) or none. All the records with no (zero) entries in those tables would be wasting space were the data to be held in a single table.

That’s the basic data structure: now we need to build an easy-to-use interface for users. First we need a query to pull together the complete data for each project. Start by creating a query and add the three tables. We want the answer table to contain data for every project, regardless of whether it has an entry in the other tables or not.

For this we can use a left outer join. Right-click on the join line between the Project and ExtFinance tables and select Join Properties. Choose the option that gives all records from the Project table and only those that match from the ExtFinance table. Use the same technique to turn the other join into a left outer join that includes all records from Project.

The SQL for the complete ProjectData query reads:

SELECT Project.ProjectID, Project.StartDate, Architect.Company, ExtFinance.TotalAmount
FROM (Project LEFT JOIN Architect ON Project.ProjectID = Architect.ProjectID) LEFT JOIN ExtFinance ON Project. ProjectID = ExtFinance.ProjectID;

Running the query lets you see all the project records with all permutations of project, architect and finance displayed.

Finally we need a form. It’s easiest to create an AutoForm: Columnar, based on the ProjectData query. Labels can be edited or added later to improve re adability. Check how ProjectDataForm is working by entering a new record. Enter an ID and a start date then post the record and check out the base tables.

A new record has appeared in the Project table but no entry has been made in the other tables. Return to the form, add an architect and post the change. The Architect table now has a new row, but the ExtFinance table still hasn’t changed. It will only do so once an entry has been added from the form.

Behind the scenes Access handles the data input very elegantly: if data is entered, Access creates the necessary records and if it isn’t, it doesn’t.


All Software Developer
Tags: Databases

Like this story? Spread the news by clicking below:

Post this to Delicious del.icio.us    Post this to Digg Digg this    Post this to reddit reddit!

Permalink for this story
R E A D E R   C O M M E N T S
M A R K E T P L A C E
Get your free demo of Numara Track-It! 8 - the leading help desk solution for IT related issues.
Make presentations, review documents & share your entire desktop. 30-day free trial! (cc required).
Discover how remote support can fuel your IT business in ways you've never thought of before.
Apply ITIL best practices at your service desk while eliminating integration cost. Learn more here.
WAN based, automated, daily vulnerability assessments. Click here to try and request our whitepapers.
Have your product or service listed here >   
Sponsored links
F E A T U R E D   J O B S
Hertfordshire, United Kingdom | Tesco.com
Database Operations Team Leader - Hertfordshire Who's behind the world's most successful online retailer?Just over 10 years ago we started Tesco.com (aka Dotcom). Today, we've an incredible 750,000 active customers and sales at just under £1 ... more >
Solihull, United Kingdom | Enzen Global Limited
  Business Consultant - £35,000 - £40,000 - Solihull We are in need of a Business Consultant with strong analytical skills and a penchant for learning the domain knowledge of the Utilities sector (Gas industry ... more >
Welwyn Garden City, Hertfordshire, United Kingdom | Tesco.com
IT Development Manager - Welwyn Garden City Who's behind the world's most successful online retailer? Just over 10 years ago we started Tesco.com (aka Dotcom). Today, we've an incredible 750,000 active customers and sales at ... more >
Newcastle upon Tyne, United Kingdom | NCFE
Information Services Manager - £37,626 - £50,633 - Newcastle Upon Tyne   Information Services Manager, (IT Manager) Newcastle Upon Tyne, Times Top 100 company, City Centre Location.  We're looking for an experienced IT Manager/professional who ... more >
More job opportunities