Mark Dixon 1 21 – Databases: Multiple Tables. Mark Dixon 2 Questions: Databases How many records are in the following table? How many fields does the.

Post on 04-Jan-2016

219 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

Transcript

Mark Dixon 1

21 – Databases: Multiple Tables

Mark Dixon 2

Questions: Databases• How many records are in the following table?• How many fields does the following table have?

64

CountryName Population Land Mass Continent

UK 60776238 241590 EuropeSpain 40448191 499542 EuropeGermany 82400996 349223 EuropeEgypt 80335036 995450 AfricaKenya 36913721 569250 AfricaChina 1321851888 9326410 Asia

Mark Dixon 3

Questions: SQL• Write an SQL statement to display the name and

land mass of all countries in Africa.

SELECT Name, Land MassFROM Country WHERE Continent = 'Africa';

CountryName Population Land Mass Continent

UK 60776238 241590 EuropeSpain 40448191 499542 EuropeGermany 82400996 349223 EuropeEgypt 80335036 995450 AfricaKenya 36913721 569250 AfricaChina 1321851888 9326410 Asia

Mark Dixon 4

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 a suitable primary key for a table– identify duplicated data in a single table– split that table to reduce data redundancy,

using a suitable foreign key– generate SQL statements to (temporarily)

join tables, and use these in your code

Mark Dixon 5

Data Duplication• Look for repeating data:

Track Title Artist Name Country

Paranoid Black Sabbath UK

Falling in Love Aerosmith US

Pink Aerosmith US

Love in an Elevator Aerosmith US

Smooth Criminal Alien Ant Farm US

Meaning of Life Disturbed US

The Game Disturbed US

Voices Disturbed US

Down with the Sickness Disturbed US

Track

Mark Dixon 6

Problem: Data Duplication• takes up lots of space

• can become inconsistent (misspellings)

• difficult to change (need to change each instance)

• difficult to search (misspellings)

Mark Dixon 7

Solution: Normalisation• Part of database design

• Process of breaking data down (splitting)

• Codd– 7 stages of normalisation

• Mathematical• Difficult to apply stages• Most professionals do it instinctively

Mark Dixon 8

Relations (tables)

Track Title

Paranoid

Falling in Love Aerosmith US

Pink Aerosmith US

Love in an Elevator Aerosmith US

Smooth Criminal Alien Ant Farm US

Meaning of Life Disturbed US

The Game Disturbed US

Voices Disturbed US

Down with the Sickness Disturbed US

Track

Artist Name Country

Black Sabbath UK

Mark Dixon 9

Relations (tables)

Track Title

Paranoid

Falling in Love Aerosmith US

Pink Aerosmith US

Love in an Elevator Aerosmith US

Smooth Criminal Alien Ant Farm US

Meaning of Life Disturbed US

The Game Disturbed US

Voices Disturbed US

Down with the Sickness Disturbed US

Track

Artist ID

1

Artist

Artist Name Country

Black Sabbath UK

ID Artist Name Country

1 Black Sabbath UK

Mark Dixon 10

Relations (tables)

Track Title

Paranoid

Falling in Love

Pink Aerosmith US

Love in an Elevator Aerosmith US

Smooth Criminal Alien Ant Farm US

Meaning of Life Disturbed US

The Game Disturbed US

Voices Disturbed US

Down with the Sickness Disturbed US

Track

Artist ID

1

Artist

ID Artist Name Country

1 Black Sabbath UKAerosmith US

ID Artist Name Country

1 Black Sabbath UK

2 Aerosmith US2

Mark Dixon 11

Relations (tables)

Track Title

Paranoid

Falling in Love

Pink

Love in an Elevator Aerosmith US

Smooth Criminal Alien Ant Farm US

Meaning of Life Disturbed US

The Game Disturbed US

Voices Disturbed US

Down with the Sickness Disturbed US

Track

Artist ID

1

2

2

Artist

ID Artist Name Country

1 Black Sabbath UK

2 Aerosmith US

Mark Dixon 12

Relations (tables)

Track Title

Paranoid

Falling in Love

Pink

Love in an Elevator

Smooth Criminal Alien Ant Farm US

Meaning of Life Disturbed US

The Game Disturbed US

Voices Disturbed US

Down with the Sickness Disturbed US

Track

Artist ID

1

2

2

2

Artist

ID Artist Name Country

1 Black Sabbath UK

2 Aerosmith US

Mark Dixon 13

Relations (tables)

Track Title

Paranoid

Falling in Love

Pink

Love in an Elevator

Smooth Criminal

Meaning of Life Disturbed US

The Game Disturbed US

Voices Disturbed US

Down with the Sickness Disturbed US

Track

Artist ID

1

2

2

2

3

Artist

ID Artist Name Country

1 Black Sabbath UK

2 Aerosmith US

3 Alien Ant Farm US

Mark Dixon 14

Relations (tables)

Track Title

Paranoid

Falling in Love

Pink

Love in an Elevator

Smooth Criminal

Meaning of Life

The Game Disturbed US

Voices Disturbed US

Down with the Sickness Disturbed US

Track

Artist ID

1

2

2

2

3

4

Artist

ID Artist Name Country

1 Black Sabbath UK

2 Aerosmith US

3 Alien Ant Farm US

4 Disturbed US

Mark Dixon 15

Relations (tables)

Track Title

Paranoid

Falling in Love

Pink

Love in an Elevator

Smooth Criminal

Meaning of Life

The Game

Voices Disturbed US

Down with the Sickness Disturbed US

Track

Artist ID

1

2

2

2

3

4

4

Artist

ID Artist Name Country

1 Black Sabbath UK

2 Aerosmith US

3 Alien Ant Farm US

4 Disturbed US

Mark Dixon 16

Relations (tables)

Track Title

Paranoid

Falling in Love

Pink

Love in an Elevator

Smooth Criminal

Meaning of Life

The Game

Voices

Down with the Sickness Disturbed US

Track

Artist ID

1

2

2

2

3

4

4

4

Artist

ID Artist Name Country

1 Black Sabbath UK

2 Aerosmith US

3 Alien Ant Farm US

4 Disturbed US

Mark Dixon 17

Relations (tables)

Track Title Artist ID

Paranoid 1

Falling in Love 2

Pink 2

Love in an Elevator 2

Smooth Criminal 3

Meaning of Life 4

The Game 4

Voices 4

Down with the Sickness 4

Track Artist

ID Artist Name Country

1 Black Sabbath UK

2 Aerosmith US

3 Alien Ant Farm US

4 Disturbed US

PrimaryKey

ForeignKey

Mark Dixon 18

Question: Prescriptions• Identify duplication and separate:

Date Surname Forenames Drug Name

6 Jan 04 Jones Alison Co-codamol

11 Jan 04 Smith Bob Tegretol

18 Jan 04 Hope John Co-codamol

5 Feb 04 Johnson Sally Co-codamol

8 Feb 04 Smith Bob Tegretol

10 Feb 04 Smith Bob Sorbitol

Prescription

Mark Dixon 19

Question: Solution

Date PatientID DrugID

6 Jan 04 1 1

11 Jan 04 2 2

18 Jan 04 3 1

5 Feb 04 4 1

8 Feb 04 2 2

10 Feb 04 2 3

Prescription

PatientID Surname Forenames

1 Jones Alison

2 Smith Bob

3 Hope John

4 Johnson Sally

Patient

DrugID Drug Name

1 Co-codamol

2 Tegretol

3 Sorbitol

Drug

Mark Dixon 20

People Database (with Hobbies)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

HobbyID Description PersonID

1 Archery 1

2 Herpetology 1

3 Music 1

4 Football 2

5 Rugby 2

6 Hitting people with swords 1

Hobby

Person

Mark Dixon 21

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

Mark Dixon 22

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

Mark Dixon 23

Question: Which relationship type?

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

HobbyID Description PersonID

1 Archery 1

2 Herpetology 1

3 Music 1

4 Football 2

5 Rugby 2

6 Hitting people with swords 1

Hobby

Person

Person

Hobby

Mark Dixon 24

SQL: Joining tables

SELECT * FROM Person, Hobby;

Two tables

Cartesian set(all recordcombinations):

ID Surname Forenames Phone email HobbyID Description PersonID1 Dixon Mark 01752 232556 mark.dixon@plymouth.ac.uk 7 Archery 12 Smith John 01752 111111 john.smith@john.smith.ac.uk 7 Archery 13 Jones Sally 01752 888888 sally.jones@sally.jones.com 7 Archery 14 Bloggs Fred 01752 123123 fred.bloggs@aaaaaa.com 7 Archery 15 Anderson Genny 01752 987987 genny@bbbb.cccc.com 7 Archery 11 Dixon Mark 01752 232556 mark.dixon@plymouth.ac.uk 8 Herpetology 12 Smith John 01752 111111 john.smith@john.smith.ac.uk 8 Herpetology 13 Jones Sally 01752 888888 sally.jones@sally.jones.com 8 Herpetology 14 Bloggs Fred 01752 123123 fred.bloggs@aaaaaa.com 8 Herpetology 15 Anderson Genny 01752 987987 genny@bbbb.cccc.com 8 Herpetology 11 Dixon Mark 01752 232556 mark.dixon@plymouth.ac.uk 9 Music 12 Smith John 01752 111111 john.smith@john.smith.ac.uk 9 Music 13 Jones Sally 01752 888888 sally.jones@sally.jones.com 9 Music 14 Bloggs Fred 01752 123123 fred.bloggs@aaaaaa.com 9 Music 15 Anderson Genny 01752 987987 genny@bbbb.cccc.com 9 Music 11 Dixon Mark 01752 232556 mark.dixon@plymouth.ac.uk 10 Football 22 Smith John 01752 111111 john.smith@john.smith.ac.uk 10 Football 23 Jones Sally 01752 888888 sally.jones@sally.jones.com 10 Football 24 Bloggs Fred 01752 123123 fred.bloggs@aaaaaa.com 10 Football 25 Anderson Genny 01752 987987 genny@bbbb.cccc.com 10 Football 21 Dixon Mark 01752 232556 mark.dixon@plymouth.ac.uk 11 Rugby 22 Smith John 01752 111111 john.smith@john.smith.ac.uk 11 Rugby 23 Jones Sally 01752 888888 sally.jones@sally.jones.com 11 Rugby 24 Bloggs Fred 01752 123123 fred.bloggs@aaaaaa.com 11 Rugby 25 Anderson Genny 01752 987987 genny@bbbb.cccc.com 11 Rugby 21 Dixon Mark 01752 232556 mark.dixon@plymouth.ac.uk 12 Hitting people with swords 12 Smith John 01752 111111 john.smith@john.smith.ac.uk 12 Hitting people with swords 13 Jones Sally 01752 888888 sally.jones@sally.jones.com 12 Hitting people with swords 14 Bloggs Fred 01752 123123 fred.bloggs@aaaaaa.com 12 Hitting people with swords 15 Anderson Genny 01752 987987 genny@bbbb.cccc.com 12 Hitting people with swords 1

Mark Dixon 25

SQL: Joining tables

SELECT *FROM Person, HobbyWHERE Person.ID = Hobby.PersonID;

Two tables

Matching recordsID Surname Forenames Phone email HobbyID Description PersonID

1 Dixon Mark 01752 232556 mark.dixon@plymouth.ac.uk 1 Archery 1

1 Dixon Mark 01752 232556 mark.dixon@plymouth.ac.uk 2 Herpetology 1

1 Dixon Mark 01752 232556 mark.dixon@plymouth.ac.uk 3 Music 1

1 Dixon Mark 01752 232556 mark.dixon@plymouth.ac.uk 6 Hitting people with swords 1

2 Smith John 01752 111111 john.smith@john.smith.ac.uk 4 Football 2

2 Smith John 01752 111111 john.smith@john.smith.ac.uk 5 Rugby 2

Mark Dixon 26

SQL: Joining tables

ID Surname

1 Dixon

1 Dixon

1 Dixon

1 Dixon

2 Smith

2 Smith

SELECT ID, SurnameFROM Person, HobbyWHERE Person.ID = Hobby.PersonID;

Mark Dixon 27

Question: SQL Joining Tables• Write an SQL query to join the following:

Track Title Artist ID

Paranoid 1

Falling in Love 2

Pink 2

Love in an Elevator 2

Smooth Criminal 3

Meaning of Life 4

The Game 4

Voices 4

Down with the Sickness 4

Track Artist

ID Artist Name Country

1 Black Sabbath UK

2 Aerosmith US

3 Alien Ant Farm US

4 Disturbed US

Mark Dixon 28

MS Access: SQL Query• Open database• Create new query• Select SQL item from drop-down list

SQL View DataSheet View

Mark Dixon 29

SQL: More• Loads more:

– 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

Mark Dixon 30

Tutorial Exercise: Countries• LEARNING OBJECTIVE:

avoid duplicate data by splitting a tablecreate a web-page to temporarily re-join and display the data

• Task 1: Create the Countries database (from the lecture) with the Country table.

• Task 2: Split the Country table to remove duplicate data.identify the duplicate dataput it in a separate tablecreate a primary key for the new tablecreate a foreign key in the old table that links to the new table

• Task 3: Create a web page to display a list of Countries with associated data (Population, Land Mass, and Continent).

• Task 4: Add a text box and button to your page. When the user clicks the button the page should display only countries that match the continent entered in the text box (e.g. if the user types in Africa and clicks the button, only the African countries should be displayed).

Mark Dixon 31

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.

Mark Dixon 32

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.

top related