Presentation is loading. Please wait.

Presentation is loading. Please wait.

19 – Databases: Multiple Tables

Similar presentations


Presentation on theme: "19 – Databases: Multiple Tables"— Presentation transcript:

1 19 – Databases: Multiple Tables

2 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

3 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

4 Problem: Data Duplication
takes up lots of space can become inconsistent (misspellings) difficult to change (need to change each instance) difficult to search (misspellings)

5 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

6 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

7 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

8 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

9 People Database (with Hobbies)
Person ID Surname Forenames Phone 1 Dixon Mark 2 Smith John 3 Jones Sally 4 Bloggs Fred 5 Anderson Genny Hobby HobbyID Description PersonID 1 Archery 2 Herpetology 3 Music 4 Football 5 Rugby 6 Hitting people with swords

10 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)

11 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

12 Question: Which relationship type?
Person ID Surname Forenames Phone 1 Dixon Mark 2 Smith John 3 Jones Sally 4 Bloggs Fred 5 Anderson Genny Hobby HobbyID Description PersonID 1 Archery 2 Herpetology 3 Music 4 Football 5 Rugby 6 Hitting people with swords Person Hobby

13 SQL: Joining tables SELECT * FROM [Person], [Hobby]; Two tables
Cartesian set (all record combinations):

14 SQL: Joining tables Two tables SELECT * FROM [Person], [Hobby] WHERE [Person].[ID] = [Hobby].[PersonID]; Matching records ID Surname Forenames Phone HobbyID Description PersonID 1 Dixon Mark Archery 2 Herpetology 3 Music 6 Hitting people with swords Smith John 4 Football 5 Rugby

15 SQL: Joining tables SELECT [ID], [Surname] FROM [Person], [Hobby] WHERE [Person].[ID] = [Hobby].[PersonID]; ID Surname 1 Dixon 2 Smith

16 SQL: DISTINCT records SELECT DISTINCT [ID], [Surname] FROM [Person], [Hobby] WHERE [Person].[ID] = [Hobby].[PersonID]; ID Surname 1 Dixon 2 Smith

17 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:

18 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.

19 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.


Download ppt "19 – Databases: Multiple Tables"

Similar presentations


Ads by Google