Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Dixon, SoCCE SOFT 131Page 1 19 – Databases: Multiple Tables.

Similar presentations


Presentation on theme: "Mark Dixon, SoCCE SOFT 131Page 1 19 – Databases: Multiple Tables."— Presentation transcript:

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

2 Mark Dixon, SoCCE SOFT 131Page 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

3 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

4 Mark Dixon, SoCCE SOFT 131Page 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 Mark Dixon, SoCCE SOFT 131Page 5 Relations (tables) Track TitleArtist ID Paranoid1 Falling in Love2 Pink2 Love in an Elevator2 Smooth Criminal3 Meaning of Life4 The Game4 Voices4 Down with the Sickness4 Artist ID Artist NameCountry 1Black SabbathUK 2AerosmithUS 3Alien Ant FarmUS 4DisturbedUS Track Artist Primary Key Foreign Key

6 Mark Dixon, SoCCE SOFT 131Page 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 Mark Dixon, SoCCE SOFT 131Page 7 Question: Prescriptions Identify duplication and separate: DateSurnameForenamesDrug Name 6 Jan 04JonesAlisonCo-codamol 11 Jan 04SmithBobTegretol 18 Jan 04HopeJohnCo-codamol 5 Feb 04JohnsonSallyCo-codamol 8 Feb 04SmithBobTegretol 10 Feb 04SmithBobSorbitol Prescription

8 Mark Dixon, SoCCE SOFT 131Page 8 Question: Solution DatePatientIDDrugID 6 Jan 0411 11 Jan 0422 18 Jan 0431 5 Feb 0441 8 Feb 0422 10 Feb 0423 Prescription PatientIDSurnameForenames 1JonesAlison 2SmithBob 3HopeJohn 4JohnsonSally Patient DrugIDDrug Name 1Co-codamol 2Tegretol 3Sorbitol Drug

9 Mark Dixon, SoCCE SOFT 131Page 9 People Database (with Hobbies) IDSurnameForenamesPhoneemail 1DixonMark01752 232556mark.dixon@plymouth.ac.uk 2SmithJohn01752 111111john.smith@john.smith.ac.uk 3JonesSally01752 888888sally.jones@sally.jones.com 4BloggsFred01752 123123fred.bloggs@aaaaaa.com 5AndersonGenny01752 987987genny@bbbb.cccc.com HobbyIDDescriptionPersonID 1Archery1 2Herpetology1 3Music1 4Football2 5Rugby2 6Hitting people with swords1 Hobby Person

10 Mark Dixon, SoCCE SOFT 131Page 10 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) PersonHobby

11 Mark Dixon, SoCCE SOFT 131Page 11 Relationship Types One-to-one One-to-many Many-to-one Many-to-many –(can't be implemented in relational database) ABABABAB

12 Mark Dixon, SoCCE SOFT 131Page 12 Question: Which relationship type? IDSurnameForenamesPhoneemail 1DixonMark01752 232556mark.dixon@plymouth.ac.uk 2SmithJohn01752 111111john.smith@john.smith.ac.uk 3JonesSally01752 888888sally.jones@sally.jones.com 4BloggsFred01752 123123fred.bloggs@aaaaaa.com 5AndersonGenny01752 987987genny@bbbb.cccc.com HobbyIDDescriptionPersonID 1Archery1 2Herpetology1 3Music1 4Football2 5Rugby2 6Hitting people with swords1 Hobby Person Hobby

13 Mark Dixon, SoCCE SOFT 131Page 13 SQL: Joining tables SELECT * FROM [Person], [Hobby]; Two tables Cartesian set (all record combinations):

14 Mark Dixon, SoCCE SOFT 131Page 14 SQL: Joining tables SELECT * FROM [Person], [Hobby] WHERE [Person].[ID] = [Hobby].[PersonID]; Two tables Matching records IDSurnameForenamesPhoneemailHobbyIDDescriptionPersonID 1DixonMark01752 232556mark.dixon@plymouth.ac.uk1Archery1 1DixonMark01752 232556mark.dixon@plymouth.ac.uk2Herpetology1 1DixonMark01752 232556mark.dixon@plymouth.ac.uk3Music1 1DixonMark01752 232556mark.dixon@plymouth.ac.uk6Hitting people with swords1 2SmithJohn01752 111111john.smith@john.smith.ac.uk4Football2 2SmithJohn01752 111111john.smith@john.smith.ac.uk5Rugby2

15 Mark Dixon, SoCCE SOFT 131Page 15 SQL: Joining tables IDSurname 1Dixon 1 1 1 2Smith 2 SELECT [ID], [Surname] FROM [Person], [Hobby] WHERE [Person].[ID] = [Hobby].[PersonID];

16 Mark Dixon, SoCCE SOFT 131Page 16 SQL: DISTINCT records SELECT DISTINCT [ID], [Surname] FROM [Person], [Hobby] WHERE [Person].[ID] = [Hobby].[PersonID]; IDSurname 1Dixon 2Smith

17 Mark Dixon, SoCCE SOFT 131Page 17 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

18 Mark Dixon, SoCCE SOFT 131Page 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. Use query strings to pass the artist ID.

19 Mark Dixon, SoCCE SOFT 131Page 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 4: 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 "Mark Dixon, SoCCE SOFT 131Page 1 19 – Databases: Multiple Tables."

Similar presentations


Ads by Google