Mark Dixon, SoCCE SOFT 131Page 1 19 – Databases: Multiple Tables
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
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
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)
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
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
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
Mark Dixon, SoCCE SOFT 131Page 8 Question: Solution DatePatientIDDrugID 6 Jan Jan Jan Feb Feb Feb 0423 Prescription PatientIDSurnameForenames 1JonesAlison 2SmithBob 3HopeJohn 4JohnsonSally Patient DrugIDDrug Name 1Co-codamol 2Tegretol 3Sorbitol Drug
Mark Dixon, SoCCE SOFT 131Page 9 People Database (with Hobbies) IDSurnameForenamesPhone 1DixonMark SmithJohn JonesSally BloggsFred AndersonGenny01752 HobbyIDDescriptionPersonID 1Archery1 2Herpetology1 3Music1 4Football2 5Rugby2 6Hitting people with swords1 Hobby Person
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
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
Mark Dixon, SoCCE SOFT 131Page 12 Question: Which relationship type? IDSurnameForenamesPhone 1DixonMark SmithJohn JonesSally BloggsFred AndersonGenny01752 HobbyIDDescriptionPersonID 1Archery1 2Herpetology1 3Music1 4Football2 5Rugby2 6Hitting people with swords1 Hobby Person Hobby
Mark Dixon, SoCCE SOFT 131Page 13 SQL: Joining tables SELECT * FROM [Person], [Hobby]; Two tables Cartesian set (all record combinations):
Mark Dixon, SoCCE SOFT 131Page 14 SQL: Joining tables SELECT * FROM [Person], [Hobby] WHERE [Person].[ID] = [Hobby].[PersonID]; Two tables Matching records IDSurnameForenamesPhone HobbyIDDescriptionPersonID 1DixonMark DixonMark DixonMark DixonMark01752 people with swords1 2SmithJohn SmithJohn01752
Mark Dixon, SoCCE SOFT 131Page 15 SQL: Joining tables IDSurname 1Dixon Smith 2 SELECT [ID], [Surname] FROM [Person], [Hobby] WHERE [Person].[ID] = [Hobby].[PersonID];
Mark Dixon, SoCCE SOFT 131Page 16 SQL: DISTINCT records SELECT DISTINCT [ID], [Surname] FROM [Person], [Hobby] WHERE [Person].[ID] = [Hobby].[PersonID]; IDSurname 1Dixon 2Smith
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: –
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.
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.