19 – Databases: Multiple Tables
Session Aims & Objectives To add dealing with multiple tables to your understanding of databases Objectives, by end of this week’s sessions, you should be able to: identify duplicated data in a single table split that table to reduce data redundancy generate SQL statements to (temporarily) join tables, and use these in your code
Flat files: Data Duplication Field Track Track Title Artist Name Country Paranoid Black Sabbath UK Falling in Love Aerosmith US Pink Love in an Elevator Smooth Criminal Alien Ant Farm Meaning of Life Disturbed The Game Voices Down with the Sickness Record
Problem: Data Duplication takes up lots of space can become inconsistent (misspellings) difficult to change (need to change each instance) difficult to search (misspellings)
Relations (tables) Primary Key Foreign Key Track Track Title Artist ID Paranoid 1 Falling in Love 2 Pink Love in an Elevator Smooth Criminal 3 Meaning of Life 4 The Game Voices Down with the Sickness Artist Artist ID Artist Name Country 1 Black Sabbath UK 2 Aerosmith US 3 Alien Ant Farm 4 Disturbed Primary Key Foreign Key
Normalisation Part of database design Process of breaking data down Codd 7 stages of normalisation Mathematical Difficult to apply stages Most professionals do it instinctively
Question: Prescriptions Identify duplication and separate: Prescription Date Surname Forenames Drug Name 6 Jan 04 Jones Alison Co-codamol 11 Jan 04 Smith Bob Tegretol 18 Jan 04 Hope John 5 Feb 04 Johnson Sally 8 Feb 04 10 Feb 04 Sorbitol
Question: Solution Patient PatientID Surname Forenames 1 Jones Alison 2 Smith Bob 3 Hope John 4 Johnson Sally Prescription Date PatientID DrugID 6 Jan 04 1 11 Jan 04 2 18 Jan 04 3 5 Feb 04 4 8 Feb 04 10 Feb 04 Drug DrugID Drug Name 1 Co-codamol 2 Tegretol 3 Sorbitol
People Database (with Hobbies) Person ID Surname Forenames Phone email 1 Dixon Mark 01752 232556 mark.dixon@plymouth.ac.uk 2 Smith John 01752 111111 john.smith@john.smith.ac.uk 3 Jones Sally 01752 888888 sally.jones@sally.jones.com 4 Bloggs Fred 01752 123123 fred.bloggs@aaaaaa.com 5 Anderson Genny 01752 987987 genny@bbbb.cccc.com Hobby HobbyID Description PersonID 1 Archery 2 Herpetology 3 Music 4 Football 5 Rugby 6 Hitting people with swords
Entity-relationship diagrams Each table in db stores details of entity shown as rectangular box Person Hobby Relationships between tables represent relationships between entities shown as line between entities (boxes)
Relationship Types One-to-one One-to-many Many-to-one Many-to-many (can't be implemented in relational database) A B A B A B A B
Question: Which relationship type? Person ID Surname Forenames Phone email 1 Dixon Mark 01752 232556 mark.dixon@plymouth.ac.uk 2 Smith John 01752 111111 john.smith@john.smith.ac.uk 3 Jones Sally 01752 888888 sally.jones@sally.jones.com 4 Bloggs Fred 01752 123123 fred.bloggs@aaaaaa.com 5 Anderson Genny 01752 987987 genny@bbbb.cccc.com Hobby HobbyID Description PersonID 1 Archery 2 Herpetology 3 Music 4 Football 5 Rugby 6 Hitting people with swords Person Hobby
SQL: Joining tables SELECT * FROM [Person], [Hobby]; Two tables Cartesian set (all record combinations):
SQL: Joining tables Two tables SELECT * FROM [Person], [Hobby] WHERE [Person].[ID] = [Hobby].[PersonID]; Matching records ID Surname Forenames Phone email HobbyID Description PersonID 1 Dixon Mark 01752 232556 mark.dixon@plymouth.ac.uk Archery 2 Herpetology 3 Music 6 Hitting people with swords Smith John 01752 111111 john.smith@john.smith.ac.uk 4 Football 5 Rugby
SQL: Joining tables SELECT [ID], [Surname] FROM [Person], [Hobby] WHERE [Person].[ID] = [Hobby].[PersonID]; ID Surname 1 Dixon 2 Smith
SQL: DISTINCT records SELECT DISTINCT [ID], [Surname] FROM [Person], [Hobby] WHERE [Person].[ID] = [Hobby].[PersonID]; ID Surname 1 Dixon 2 Smith
SQL: More Loads more: Have a look at: group by aggregate functions: average, count inner joins outer joins (left and right) Have a look at: http://www.w3schools.com/sql/sql_join.asp
Tutorial Exercise: Music Task 1: Create the Music database (from the lecture) with the Track and Artist tables. Task 2: Create a web page to display a list of Artists. Task 4: Change that web page, so that each artist name is a link to another page, which displays all the tracks by that artist. Hint: Use query strings to pass the artist ID between pages.
Tutorial Exercise: Prescriptions Task 1: Create the Prescription database (from the lecture) with the Prescription, Patient, and Drug tables. Task 2: Create a web page to display a list of Drugs. Task 3: Change that web page, so that each drug name is a link to another page, which displays all the people using that drug.