image: hands on database organise
Three table of data; the central one is the joining table to facilitate the many-to-many relationship
R E L A T E D   C O N T E N T
ADVERTISEMENT

Hands on: Organisation in databases

Organise your data by creating many-to-many relationships between multiple fields

Mark Whitehorn, Personal Computer World 20 Dec 2006
ADVERTISEMENT

A recent email from Ian Park provides the starting point for a ‘back to basics’ theme, which covers the art of building many-to-many relationships.

Ian keeps his church’s book catalogue in an Access database. He classifies each book into one or several subjects, for instance, ‘suffering’, ‘testimony’ and ‘the cross’. This is a classic example of a many-to-many relationship: each book can have multiple subjects and each subject can be covered by many books.

In the current database, each book occupies one row of a table and Ian uses one text field to store the classification(s) of the book. He can search the text field to find books dealing with a particular subject, but the process is unwieldy. Ian asks how he should redesign his database, so here we go.

Our sample data is held in two simple tables, Book and Subject. In order to model a many-to-many relationship between these, we introduce another table called a joining (or intersection) table, giving three tables. The joining table is BookSubject and is the central one in the screen.

Can we tell, by looking at the data, what subject is covered in which book? Yes. The first row in the BookSubject table has a 1 in the BookID column and a 2 in the SubjectID column. Glancing at the Book table tells us the book with the BookID of 1 is Damascus Road, and shifting our gaze to the Subject table tells us that the subject identified by SubjectID 2 is ‘epiphany’. The second and third rows in the joining table tell us that book 2 (Stations of the Cross) deals with two subjects, 1 (‘the cross’) and 3 (‘suffering’).

Database design description
It’s easy to visualise how the three tables work together, but let’s add some detail in database design terms. In the Book table, BookID field is the primary key field: each value in this field uniquely identifies one book. The same is true for SubjectID field in the Subject table: each value within the field is unique.

In BookSubject, BookID is a foreign key that ‘points’ to the primary key in the table Book; just as SubjectID is a foreign key pointing to the primary key of Subject. In addition, the primary key of BookSubject is made up of the two fields: BookID and SubjectID.

This means that we cannot have two identical rows in BookSubject. For example, we can have one row with a 1 in the BookID column and a 2 in the SubjectID column; but not two rows. It may not be completely clear, at this stage, why this is important, but all will be revealed shortly.

This is the classic way to model a many-to-many relationship in a relational database. It actually consists of two one-to-many relationships.

The first is that one book can cover many subjects – the Book table is at the ‘one’ end of the relationship and the BookSubject table is at the ‘many’ end.

The second is that one subject can occur in many books. The Subject table is at the ‘one’ end and the BookSubject table at the ‘many’ end.

The result of making these two one-to-many relationships with their ‘many’ ends in a joining table, is a many-to-many relationship between books and subjects.


All Software Applications
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
Aylesbury, Buckinghamshire, United Kingdom | Grass Roots
Business Analyst - £35,000 - £50,000 + benefits - Aylesbury    Grass Roots are one of the Sunday Times Top 100 companies to work for (2007 and 2008). Established in 1980, we're part of the ... more >
London, United Kingdom | The Crown Estate
 EDM Administrator - London - £22,300 to £24,200pa The Crown Estate is a unique organisation that manages a vast and varied property portfolio, comprising commercial, agricultural and marine interests throughout Britain. We are looking for an ... more >
London, United Kingdom | City of London
ICT Project Officer - Guildhall, London EC2 18-month fixed-term contract Bring your project management expertise to one of the country's most prestigious institutions. The City of London is the local authority for the Square Mile, ... more >
Central London, United Kingdom | MI5 Security Service
Communications Centre Engineer - Competitive salaries + excellent benefits - Central London Getting the best out of technology is critical to helping us protect the UK. Join MI5 and use your skills and experience to ... more >
More job opportunities