Mark Dixon Page 1 03 – Database Design: Multiple Tables
Mark Dixon 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
Mark Dixon Page 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 Page 4 Problem: Data Duplication takes up lots of space can become inconsistent (misspellings) difficult to search (misspellings) difficult to change (need to change each instance)
Mark Dixon Page 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 Page 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 Page 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 Page 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 Page 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 Page 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 Page 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 Page 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 Page 13 Example: Barbican Usage Add Use table for use codes:
Mark Dixon Page 14 Queries: Multiple Tables Add multiple tables:
Mark Dixon Page 15 Tutorial Exercise: Music Task 1: Create the Music database (from the lecture) with the Track and Artist tables. Task 2: Create a query to list all track titles by artists from US
Mark Dixon Page 16 Tutorial Exercise: Prescriptions Task 1: Create the Prescription database (from the lecture) with the Prescription, Patient, and Drug tables. Task 2: Create query to list all patients who have been prescribed Tegretol
Mark Dixon Page 17 Tutorial Exercises: Barbican Task 1: Download the Barbican database. Task 2: Add a new table for Use codes. Task 3: Create a query to display 1895 data with Description of use (not code). Task 4: Create a query to list all houses used for industry in 1955.