Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Mark Dixon 1 21 – Databases: Multiple Tables

2 Mark Dixon 2 Questions: Databases How many records are in the following table? How many fields does the following table have? 6 4

3 Mark Dixon 3 Questions: SQL Write an SQL statement to display the name and land mass of all countries in Africa. SELECT Name, Land Mass FROM Country WHERE Continent = 'Africa';

4 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

5 Mark Dixon 5 Data Duplication Look for repeating data: 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

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

7 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

8 Mark Dixon 8 Relations (tables) Track Title Paranoid Falling in LoveAerosmithUS PinkAerosmithUS Love in an ElevatorAerosmithUS Smooth CriminalAlien Ant FarmUS Meaning of LifeDisturbedUS The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track Artist NameCountry Black SabbathUK

9 Mark Dixon 9 Relations (tables) Track Title Paranoid Falling in LoveAerosmithUS PinkAerosmithUS Love in an ElevatorAerosmithUS Smooth CriminalAlien Ant FarmUS Meaning of LifeDisturbedUS The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track Artist ID 1 Artist Artist NameCountry Black SabbathUK IDArtist NameCountry 1Black SabbathUK

10 Mark Dixon 10 Relations (tables) Track Title Paranoid Falling in Love PinkAerosmithUS Love in an ElevatorAerosmithUS Smooth CriminalAlien Ant FarmUS Meaning of LifeDisturbedUS The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track Artist ID 1 Artist IDArtist NameCountry 1Black SabbathUK AerosmithUS IDArtist NameCountry 1Black SabbathUK 2AerosmithUS 2

11 Mark Dixon 11 Relations (tables) Track Title Paranoid Falling in Love Pink Love in an ElevatorAerosmithUS Smooth CriminalAlien Ant FarmUS Meaning of LifeDisturbedUS The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track Artist ID 1 2 2 Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS

12 Mark Dixon 12 Relations (tables) Track Title Paranoid Falling in Love Pink Love in an Elevator Smooth CriminalAlien Ant FarmUS Meaning of LifeDisturbedUS The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track Artist ID 1 2 2 2 Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS

13 Mark Dixon 13 Relations (tables) Track Title Paranoid Falling in Love Pink Love in an Elevator Smooth Criminal Meaning of LifeDisturbedUS The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track Artist ID 1 2 2 2 3 Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS 3Alien Ant FarmUS

14 Mark Dixon 14 Relations (tables) Track Title Paranoid Falling in Love Pink Love in an Elevator Smooth Criminal Meaning of Life The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track Artist ID 1 2 2 2 3 4 Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS 3Alien Ant FarmUS 4DisturbedUS

15 Mark Dixon 15 Relations (tables) Track Title Paranoid Falling in Love Pink Love in an Elevator Smooth Criminal Meaning of Life The Game VoicesDisturbedUS Down with the SicknessDisturbedUS Track Artist ID 1 2 2 2 3 4 4 Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS 3Alien Ant FarmUS 4DisturbedUS

16 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 SicknessDisturbedUS Track Artist ID 1 2 2 2 3 4 4 4 Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS 3Alien Ant FarmUS 4DisturbedUS

17 Mark Dixon 17 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 Track Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS 3Alien Ant FarmUS 4DisturbedUS Primary Key Foreign Key

18 Mark Dixon 18 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

19 Mark Dixon 19 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

20 Mark Dixon 20 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

21 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) PersonHobby

22 Mark Dixon 22 Relationship Types One-to-one One-to-many Many-to-one Many-to-many –(can't be implemented in relational database) ABABABAB

23 Mark Dixon 23 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

24 Mark Dixon 24 SQL: Joining tables SELECT * FROM Person, Hobby; Two tables Cartesian set (all record combinations):

25 Mark Dixon 25 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

26 Mark Dixon 26 SQL: Joining tables IDSurname 1Dixon 1 1 1 2Smith 2 SELECT ID, Surname FROM Person, Hobby WHERE Person.ID = Hobby.PersonID;

27 Mark Dixon 27 Question: SQL Joining Tables Write an SQL query to join the following: Track TitleArtist ID Paranoid1 Falling in Love2 Pink2 Love in an Elevator2 Smooth Criminal3 Meaning of Life4 The Game4 Voices4 Down with the Sickness4 Track Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS 3Alien Ant FarmUS 4DisturbedUS

28 Mark Dixon 28 MS Access: SQL Query Open database Create new query Select SQL item from drop-down list SQL View DataSheet View

29 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

30 Mark Dixon 30 Tutorial Exercise: Countries LEARNING OBJECTIVE: avoid duplicate data by splitting a table create 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 data put it in a separate table create a primary key for the new table create 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).

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

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


Download ppt "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."

Similar presentations


Ads by Google