Simple clear advice in plain English

Hands on: Organisation in databases

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

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.

Article tags

Reader Comments

   

Add your comment

All fields must be completed. Your email address will not be displayed or used to send marketing messages.

All messages will be checked by moderators before appearing on the site.

See our Privacy Policy for more information.

Related articles

Tips for Word illustration

Get the best out of Microsoft Word

Unless you've had a training course on Word, you probably know and use only a small percentage of its capabilities. Here are 10 top features for you to try out

Extensions for Open Office Writer

Use Open Office Writer as a free alternative to Microsoft Word

Open Office Writer is a great piece of software that has hundreds of features. Here we show you 10 lesser-known features that you might not have discovered yet

Make animated stick images with Pivot

Make your debut movie using animated matchstick characters

Create stick figure animations that can be viewed by anyone in any web browser. We show you how to make matchstick characters using Pivot, a free program

Question & Answer

Q.Why are some of the keys on my keyboard doing strange...

> Read the answer

Q.Is my phone’s Bluetooth any use?

> Read the answer

Q.Can I switch boot drives so that I can work on older...

> Read the answer

Best deals on the web

img

Samsung RV520-A07

£359.98- Buy it now

img

Acer Aspire 5750G (LX.RXP02.019)

£399.99- Buy it now

img

Apple MacBook Pro (MD313B/A)

£904.37- Buy it now

Latest issue & subscription deals

Poll

Are you concerned about viruses that target mobile phones?

Jargon Buster

Computing terms explained in plain English

Restore point

A Windows backup of system files and settings.

Great shopping deals from Computeractive