image: DOB query
By sleight of hand we can see the OrderEmployee DOB query and the answer table
R E L A T E D   C O N T E N T
ADVERTISEMENT

Hands on: Relationships in databases

We explain some of the basics of creating a database and planning relationships

Mark Whitehorn, Personal Computer World 14 Feb 2008
ADVERTISEMENT

One of the joys of writing about databases is the number of places you get to visit; the downside is the travelling.

But occasionally it yields lighter moments. I recently stayed at the Renaissance Hotel at Gatwick and while I was settling the bill I noticed that a tip I had added for a meal had been increased.

“Oh,” I was told. “That’ll be the new computer system. We had a lot of work done and the new server sometimes just adds random numbers to the bills.”

Right, excellent. I must remember that explanation next time I have to account for errors in one of my databases.

Tables
One of the central tenets of relational databases is that we split data into multiple tables. There is a formal process (called normalisation) for working out what data should go into each table, but there is also a good rule of thumb. Simply identify each real-world type of object that you want to store data about (Customer, Order or Employee), give each one a table and include in each table only the fields that are relevant for that object. For example, in an Employee table we might make fields such as FName, LName and DOB (Date Of Birth).

The table must also have a primary key. This is usually one field and must contain a unique value for each record. So, our three tables might look like the image (above, left): the primary keys in each case are the first column in the table.

Primary keys are significant because we use them to link the data in the separate tables together. If you look at the Order table you can see that Order 1 was placed by customer 2 (Sally Jones) and taken by employee 3 (Harry). The column that points to the primary key (for example, the CustID field in Orders) is called a Foreign key.

The good news about splitting the data into separate tables is that we never store the same data more than once. So, for example, the date of birth of each employee is only ever stored once. We always know where to find it: it has to be in the Employee table. But how can we get to it? For example, how do we list all of the orders, together with the DOB of the associated employee?

Simple, we use the primary and foreign keys to pull the data back together. In this case we could create a query (You’ll find the OrderEmployeeDOB in DBCFeb08. mdb here) that links them like this and it would return the correct information.

In these examples I’ve used a number field to make the joins, which is common, but there is no reason why you cannot make a join between two text fields. If you do so, then the join is case insensitive, so it will happily join “Harry” with “harry”. Most of the time this is what we want, but occasionally people want a case-sensitive join.


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
London, United Kingdom | MI5
Programme Managers - Project Managers - Project Support Staff   Getting the best out of technology is critical to helping us protect the UK. Join MI5 and use your skills and experience to help us ... more >
London, United Kingdom | Barts and The London NHS
 Information Manager - £28,924 - £38,591 pa inc - London   Applications are invited for the post of Information Manager in the Head Office of the Central and East London Comprehensive Local Research Network. The ... more >
London, City of London, United Kingdom | Mulvaney Capital
 Senior SQL Developer - Hedge Fund - London  Experienced SQL based database developer sought to join systematic trading group. The role will focus on all aspects of automated data collection and database design, programming and ... more >
Reading, Berkshire, United Kingdom | EDS
Job Title - Applications Build and Test engineer Short Description: DII The DII project is contracted to supply both hardware and software infrastructure solutions to support the MoD transition to a common base solution, based ... more >
More job opportunities