16
Mark Dixon, SoCCE SOFT 131 Page 1 14 – Databases: Multiple Tables

Mark Dixon, SoCCE SOFT 131Page 1 14 Databases: Multiple Tables

Embed Size (px)

DESCRIPTION

Mark Dixon, SoCCE SOFT 131Page 3 Record Field Flat files: Data Duplication Track TitleArtist NameCountry ParanoidBlack SabbathUK Falling in LoveAerosmithUS PinkAerosmithUS Love in an ElevatorAerosmithUS Smooth CriminalAlien Ant FarmUS Meaning of LifeDisturbedUS The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track

Citation preview

Page 1: Mark Dixon, SoCCE SOFT 131Page 1 14  Databases: Multiple Tables

Mark Dixon, SoCCE SOFT 131 Page 1

14 – Databases: Multiple Tables

Page 2: Mark Dixon, SoCCE SOFT 131Page 1 14  Databases: Multiple Tables

Mark Dixon, SoCCE SOFT 131 Page 2

Session Aims & Objectives• Aims

– 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

Page 3: Mark Dixon, SoCCE SOFT 131Page 1 14  Databases: Multiple Tables

Mark Dixon, SoCCE SOFT 131 Page 3

Record

Field

Flat files: Data Duplication

Track Title Artist Name CountryParanoid Black Sabbath UKFalling in Love Aerosmith USPink Aerosmith USLove in an Elevator Aerosmith USSmooth Criminal Alien Ant Farm USMeaning of Life Disturbed USThe Game Disturbed USVoices Disturbed USDown with the Sickness Disturbed US

Track

Page 4: Mark Dixon, SoCCE SOFT 131Page 1 14  Databases: Multiple Tables

Mark Dixon, SoCCE SOFT 131 Page 4

Relations (tables)Track Title Artist

IDParanoid 1Falling in Love 2Pink 2Love in an Elevator 2Smooth Criminal 3Meaning of Life 4The Game 4Voices 4Down with the Sickness 4

Artist ID

Artist Name Country

1 Black Sabbath UK2 Aerosmith US3 Alien Ant Farm US4 Disturbed US

Track

Artist

Primary Key

Foreign Key

Page 5: Mark Dixon, SoCCE SOFT 131Page 1 14  Databases: Multiple Tables

Mark Dixon, SoCCE SOFT 131 Page 5

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

Page 6: Mark Dixon, SoCCE SOFT 131Page 1 14  Databases: Multiple Tables

Mark Dixon, SoCCE SOFT 131 Page 6

Question: Prescriptions• Identify duplication and separate:

Date Surname Forenames Drug Name6 Jan 04 Jones Alison Co-codamol11 Jan 04 Smith Bob Tegretol18 Jan 04 Hope John Co-codamol5 Feb 04 Johnson Sally Co-codamol8 Feb 04 Smith Bob Tegretol10 Feb 04 Smith Bob Sorbitol

Prescription

Page 7: Mark Dixon, SoCCE SOFT 131Page 1 14  Databases: Multiple Tables

Mark Dixon, SoCCE SOFT 131 Page 7

Question: Solution

Date PatientID DrugID6 Jan 04 1 111 Jan 04 2 218 Jan 04 3 15 Feb 04 4 18 Feb 04 2 210 Feb 04 2 3

Prescription

PatientID Surname Forenames1 Jones Alison2 Smith Bob3 Hope John4 Johnson Sally

Patient

DrugID Drug Name1 Co-codamol2 Tegretol3 Sorbitol

Drug

Page 8: Mark Dixon, SoCCE SOFT 131Page 1 14  Databases: Multiple Tables

Mark Dixon, SoCCE SOFT 131 Page 8

People Database (with Hobbies)ID Surname Forenames Phone email1 Dixon Mark 01752 232556 [email protected] Smith John 01752 111111 [email protected] Jones Sally 01752 888888 [email protected] Bloggs Fred 01752 123123 [email protected] Anderson Genny 01752 987987 [email protected]

HobbyID Description PersonID1 Archery 12 Herpetology 13 Music 14 Football 25 Rugby 26 Hitting people with swords 1

Hobby

Person

Page 9: Mark Dixon, SoCCE SOFT 131Page 1 14  Databases: Multiple Tables

Mark Dixon, SoCCE SOFT 131 Page 9

Entity-relationship diagrams• Each table in db

– stores details of entity• shown as rectangular box

•Relationships between tables–represent relationships between entities

•shown as line between entities (boxes)

Person Hobby

Page 10: Mark Dixon, SoCCE SOFT 131Page 1 14  Databases: Multiple Tables

Mark Dixon, SoCCE SOFT 131 Page 10

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

Page 11: Mark Dixon, SoCCE SOFT 131Page 1 14  Databases: Multiple Tables

Mark Dixon, SoCCE SOFT 131 Page 11

Question: Which relationship type?ID Surname Forenames Phone email1 Dixon Mark 01752 232556 [email protected] Smith John 01752 111111 [email protected] Jones Sally 01752 888888 [email protected] Bloggs Fred 01752 123123 [email protected] Anderson Genny 01752 987987 [email protected]

HobbyID Description PersonID1 Archery 12 Herpetology 13 Music 14 Football 25 Rugby 26 Hitting people with swords 1

Hobby

Person

Person

Hobby

Page 12: Mark Dixon, SoCCE SOFT 131Page 1 14  Databases: Multiple Tables

Mark Dixon, SoCCE SOFT 131 Page 12

SQL: Joining tables

SELECT *FROM [Person], [Hobby]WHERE [Person].[ID] = [Hobby].[PersonID];

Two tables

Matching recordsID Surname Forenames Phone email HobbyID Description PersonID

1 Dixon Mark 01752 232556 [email protected] 1 Archery 1

1 Dixon Mark 01752 232556 [email protected] 2 Herpetology 1

1 Dixon Mark 01752 232556 [email protected] 3 Music 1

1 Dixon Mark 01752 232556 [email protected] 6 Hitting people with swords 1

2 Smith John 01752 111111 [email protected] 4 Football 2

2 Smith John 01752 111111 [email protected] 5 Rugby 2

Page 13: Mark Dixon, SoCCE SOFT 131Page 1 14  Databases: Multiple Tables

Mark Dixon, SoCCE SOFT 131 Page 13

SQL: Joining tables

ID Surname

1 Dixon

1 Dixon

1 Dixon

1 Dixon

2 Smith

2 Smith

SELECT [ID], [Surname]FROM [Person], [Hobby]WHERE [Person].[ID] = [Hobby].[PersonID];

Page 14: Mark Dixon, SoCCE SOFT 131Page 1 14  Databases: Multiple Tables

Mark Dixon, SoCCE SOFT 131 Page 14

SQL: DISTINCT records

SELECT DISTINCT [ID], [Surname]FROM [Person], [Hobby]WHERE [Person].[ID] = [Hobby].[PersonID];

ID Surname

1 Dixon

2 Smith

Page 15: Mark Dixon, SoCCE SOFT 131Page 1 14  Databases: Multiple Tables

Mark Dixon, SoCCE SOFT 131 Page 15

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. Use query strings to pass the artist ID.

Page 16: Mark Dixon, SoCCE SOFT 131Page 1 14  Databases: Multiple Tables

Mark Dixon, SoCCE SOFT 131 Page 16

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 4: Change that web page, so that each drug name is a link to another page, which displays all the people using that drug.