Information Technology Topic

Relationships and Joins

  1. Read notes
  2. Flashcards
  3. Quiz
  4. Confident

Study focus - 3 things

  1. A primary key identifies each record in its table, and a foreign key in another table holds matching values, which links the two tables in a relationship (usually one-to-many)
  2. A join combines records from two tables by matching the linked fields, side by side, and a record with no match in the other table does not appear in the result
  3. Joining adds fields side by side by matching a key, while merging adds the records of one table under another table with the same fields, so the two give different results to count

Visual notes

Two linked tables and the row a join makes
Two linked tables. The CUSTOMER table has the fields CustID (primary key), Name and Parish, and 6 records: 101 Andrea Reid Manchester; 102 Marcus Bailey St Andrew; 103 Shanice Clarke Clarendon; 104 Devon Gordon St Catherine; 105 Kerry-Ann Lewis Manchester; 106 Omar Francis Portland. The ORDERS table has the fields OrderNo, CustID (foreign key), Item and Amount, and 7 records: 5001 102 Backpack 4500; 5002 101 Notebook 250; 5003 104 Calculator 3800; 5004 102 Ruler 120; 5005 101 Pen box 600; 5006 105 Flash drive 2200; 5007 104 Glue stick 180. A line joins the CustID of order 5004 (many side) to the CustID 102 in CUSTOMER (one side). Underneath, the joined row shows 5004, Marcus Bailey, St Andrew, Ruler, 120.

CUSTOMER (one side) is linked to ORDERS (many side) through CustID. The join puts the matching records side by side.

Section 1Why we link tables

A database keeps one table for each kind of thing. A small shop in Mandeville keeps a CUSTOMER table (who the customers are) and an ORDERS table (what they bought). Why not one big table? Then Marcus Bailey's name and parish would be typed again on every order he makes. That wastes space, and one wrong spelling makes the same customer look like two different people. The fix is to store each customer's details once, and store only the customer's number on every order. That number acts as a link between the two tables. We use these two tables all through this topic. CUSTOMER (CustID, Name, Parish): 101, Andrea Reid, Manchester; 102, Marcus Bailey, St Andrew; 103, Shanice Clarke, Clarendon; 104, Devon Gordon, St Catherine; 105, Kerry-Ann Lewis, Manchester; 106, Omar Francis, Portland. ORDERS (OrderNo, CustID, Item, Amount in J$): 5001, 102, Backpack, 4500; 5002, 101, Notebook, 250; 5003, 104, Calculator, 3800; 5004, 102, Ruler, 120; 5005, 101, Pen box, 600; 5006, 105, Flash drive, 2200; 5007, 104, Glue stick, 180.

Section 2Primary keys and foreign keys

The link is made with keys. A primary key is a field that identifies each record in its table uniquely, and it can never be empty. In CUSTOMER the primary key is CustID. Names and parishes repeat (Andrea Reid and Kerry-Ann Lewis are both in Manchester), so they would not do. A foreign key is a field in one table whose values must match values of the primary key in another table. In ORDERS the field CustID is a foreign key. Order 5004 holds the value 102, and 102 is a CustID in CUSTOMER, so that order belongs to Marcus Bailey. The foreign key field must hold the same type of data as the key it points to. It can repeat, because Marcus has two orders, but it cannot hold a number with no matching customer. An order for customer 150 should be refused, because there is no customer 150. The link between the two tables is called a relationship.

Section 3Types of relationship

One-to-many: one record in the first table is linked to many records in the second. One customer can place many orders, so CUSTOMER to ORDERS is one-to-many. The primary key of the 'one' table is stored as a foreign key in the 'many' table. Most relationships are of this type. One-to-one: a record in the first table is linked to at most one record in the second, and the other way round. A staff table and a table of locker numbers is an example when every person has one locker. Many-to-many: many records in the first table link to many records in the second. A student can join several clubs and a club has several students. One foreign key cannot store this, so a third, intermediate table is used. STUDENT (StudentID, Name): 1, Andre; 2, Shanice; 3, Kemar. CLUB (ClubID, Club): C1, Chess; C2, Drama; C3, Cadets. ENROL (StudentID, ClubID): 1, C1; 1, C2; 2, C1; 3, C1; 3, C3. Each row of ENROL is one membership. Student 1 appears twice and club C1 appears three times, so neither field alone is unique. The pair StudentID and ClubID together forms the primary key of ENROL.

Section 4Joining tables

A join is a query that combines records from two tables by matching the values in the linked fields. For our tables the matching condition is ORDERS.CustID = CUSTOMER.CustID. The computer takes each order, finds the customer with the same CustID, and puts the two records side by side as one wider row. For order 5004 the joined row shows Marcus Bailey, St Andrew, Ruler and 120. The full join gives 7 rows, listed here by order number: 5001, Marcus Bailey, Backpack, 4500 5002, Andrea Reid, Notebook, 250 5003, Devon Gordon, Calculator, 3800 5004, Marcus Bailey, Ruler, 120 5005, Andrea Reid, Pen box, 600 5006, Kerry-Ann Lewis, Flash drive, 2200 5007, Devon Gordon, Glue stick, 180 A join is only a view of the data, so the two tables are not changed. You can put criteria on a join too. Parish = "Manchester" gives 3 rows (orders 5002, 5005 and 5006). Parish = "Manchester" AND Amount > 300 gives 2 rows (orders 5005 and 5006).

Section 5Records with no match

The usual join keeps only the records that have a match in both tables. This is called an inner join. Customers 103 (Shanice Clarke) and 106 (Omar Francis) have no orders, so they do not appear in the 7 rows. So a join is not a way to list every customer. If you want to know which customers have never ordered, look down the CustID values in CUSTOMER and tick off the ones that appear in ORDERS. The two left over, 103 and 106, are the customers with no orders.

Section 6Merging tables

Joining and merging are different jobs. A join links tables through a key and adds fields side by side. Merging puts the records of one table under the records of another, so the result has more records. To merge, the two tables must have the same fields with the same data types. FORM5A (StudentID, Name): 5A01, Andre Reid; 5A02, Shanice Clarke; 5A03, Kemar Lewis; 5A04, Tanya Gordon. FORM5B (StudentID, Name): 5B01, Omar Brown; 5B02, Marcia Francis; 5B03, Devon Campbell. Merging FORM5B into FORM5A gives one table of 7 records. The primary key values must stay unique, so no StudentID can appear twice. Easy to mix up: a join makes the result wider (more fields), and a merge makes it longer (more records). Merging is often done by importing records from another source into an existing table.

Section 7Checking a join by hand

Predict a join before you trust it. Step 1: name the key in each table. Step 2: take each record of the 'many' table and find its match. Step 3: write out the combined rows. Step 4: count them. A quick check: when every foreign key has a match, an inner join from a one-to-many link has as many rows as the 'many' table. ORDERS has 7 records and every CustID matches, so the join has 7 rows, not 6 (the customers) and not 13 (6 + 7). Watch out: an order whose CustID has no matching customer drops out of an inner join. Suppose order 5008 (CustID 107) were saved by mistake. ORDERS would hold 8 records but the inner join would still show 7. The relationship rule is there to stop that happening.

Examples

A shop in Mandeville stores its customers and orders in two linked tables. CUSTOMER (CustID, Name, Parish): 101, Andrea Reid, Manchester; 102, Marcus Bailey, St Andrew; 103, Shanice Clarke, Clarendon; 104, Devon Gordon, St Catherine; 105, Kerry-Ann Lewis, Manchester; 106, Omar Francis, Portland. ORDERS (OrderNo, CustID, Item, Amount in J$): 5001, 102, Backpack, 4500; 5002, 101, Notebook, 250; 5003, 104, Calculator, 3800; 5004, 102, Ruler, 120; 5005, 101, Pen box, 600; 5006, 105, Flash drive, 2200; 5007, 104, Glue stick, 180. (a) Identify the primary key of CUSTOMER and the foreign key in ORDERS. (b) State the type of relationship between CUSTOMER and ORDERS. (c) Use the link to state the name and parish of the customer who placed order 5003.

(a) The primary key of CUSTOMER is CustID, because each customer has a different number. The foreign key in ORDERS is also CustID. It holds the number of the customer who placed the order. (b) It is a one-to-many relationship. One customer can place many orders, for example customer 102 placed orders 5001 and 5004. The key of the 'one' table, CUSTOMER, is stored in the 'many' table, ORDERS. (c) Order 5003 has CustID 104. Customer 104 in CUSTOMER is Devon Gordon, from St Catherine.

Use the same tables. CUSTOMER (CustID, Name, Parish): 101, Andrea Reid, Manchester; 102, Marcus Bailey, St Andrew; 103, Shanice Clarke, Clarendon; 104, Devon Gordon, St Catherine; 105, Kerry-Ann Lewis, Manchester; 106, Omar Francis, Portland. ORDERS (OrderNo, CustID, Item, Amount in J$): 5001, 102, Backpack, 4500; 5002, 101, Notebook, 250; 5003, 104, Calculator, 3800; 5004, 102, Ruler, 120; 5005, 101, Pen box, 600; 5006, 105, Flash drive, 2200; 5007, 104, Glue stick, 180. (a) A join links ORDERS to CUSTOMER on CustID. State how many rows the inner join gives, and explain why the answer is not 6 or 13. (b) A criterion Parish = "Manchester" AND Amount > 300 is added to the join. List the order numbers that remain. (c) Name the customers who do not appear in the join, and explain why. (d) Work out the total amount of Marcus Bailey's orders.

(a) Every order has a customer that matches, and each order makes one joined row. ORDERS has 7 records, so the inner join gives 7 rows. The answer is not 6, which is the number of customers, and not 13, which wrongly adds the two table sizes. (b) The Manchester customers are 101 (Andrea Reid) and 105 (Kerry-Ann Lewis). Their orders are 5002 (250), 5005 (600) and 5006 (2200). Only 5005 and 5006 are over 300, so the remaining orders are 5005 and 5006. (c) The customers who appear in the join are 101, 102, 104 and 105, because only their CustID values are in ORDERS. Shanice Clarke (103) and Omar Francis (106) have no orders, so they have no match and do not appear in the join. (d) Marcus Bailey is customer 102, with orders 5001 (4500) and 5004 (120). The total is 4500 + 120 = J$4620.

Now let's water it

Session runs notes to flashcards to quiz back-to-back.

Where this comes from