chapter 56 relational database design compiled by eddie moorcroft

Download Chapter 56 Relational Database Design Compiled by Eddie Moorcroft

Post on 29-Jan-2016

221 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • Chapter 56Relational Database DesignCompiled by Eddie Moorcroft

  • The BasicsA relational database is a widely-used type of DBMS (Database Management System)Data is held in tablesThe relationships are in fact links between common fields in different tables. One row of a table holds one record.Each column in a table holds one field or attribute.

  • Typical tableIMPORTANT Standard NotationBOOK(AccessionNo, DeweyCode, Title, Author,DatePublished) Entity name in uppercaseKey field (unique identifier) is underlinedAll attributes are shown in brackets, separated by commasN.BA PRIMARY key uniquely references each recordSIMPLE key is based on one attribute (field)COMPOSITE key is based on two or more attributes (fields)

    Accession NumberDeweyCodeTitleAuthorDatePublished88121.9Lets CookChan, C1992123345.440ElectricityGlendenning, V1995300345.440RidersCooper, J1995657200.00Greek in 3 weeksStavros, G1990etc

  • Linking database tablesTables are linked through the use of a common field.This field must be a key field in another table.It is known as a foreign key.Consider this library database relationship:One possible model in standard database notationBORROWER(BorrowerID, Name, Address)BOOK(AccessionNo, DeweyCode, Title, Author, DatePublished, BorrowerID, DateDue)

    Consider possible flaws in this model and suggest a better one.

  • NormalisationNormalisation is a process used to come up with the best possible design for a relational database. Tables should be organised in such a way that:No data is unnecessarily duplicatedData is consistent throughout the databaseThe structure of each table is flexible enough to allow you to enter as many or as few items as you want toThe structure should enable a user to make all kinds of complex queries relating data from different tables

  • First normal formDefinition:A table is in first normal form if it contains no repeating attributes or groups of attributes.i.e. if data is repeated, then it should be held in another table, with a relationship see example following.

  • First normal formConsider the following un-normalised data.STUDENT (Admission number, Surname, Forename, Title, Address, Telephone no., Gender, Date of birth, Course code, Course Title, Level of course, Date joined, Date left, Lecturer number, Lecturer name, Room number )We see that information about each course is repeated for each student.So use the model of two entities STUDENT and COURSE.STUDENT (Admission number, Surname, Forename, Title, Address, Telephone no., Gender, Date of birth, Course code )COURSE (Course Code, Course Title, Level of course, Date joined, Date left, Lecturer number, Lecturer name, Room number )A student can take several courses, and each course has several students attending. The relationship can be represented by the entity-relationship diagram:

  • Sample data to be held in a databaseIt is always useful to view a sample of the un-normalised data, as above. Often, it is worthwhile to put this data in an Excel spreadsheet, and reorder it by different fields, to see if data is repeating.

    Student numberStudent nameDate of birthSexCourse numberCourse nameLecturer numberLecturer name12345Jones, P20/05/1984MITA201A Level ICTT345267Moorcroft, E22433Phillips, K16/02/1983FITA201A Level ICTT345267Moorcroft, E22433Phillips, K16/02/1983FMUA201A Level MusicT773351Parry, D22433Phillips, K16/02/1983FSPA201A Level SpanishT876541n/a66688Smith, J25/12/1984MMDA201A Level MathsT666758Newton, I66688Smith, J25/12/1984MMUA201A Level MusicT773351Parry, Detc

  • In the previous example, the data should be split into two tables, STUDENT and COURSE, in standard database notation:STUDENT(StudentNumber, StudentName, DateOfBirth,Sex)COURSE(CourseNumber,CourseName,LecturerNumber, LecturerName) Consider the problems of creating a relationship between these two tables. Remember a link has to made between common fields

    Course numberCourse nameLecturer numberLecturer nameITA201A Level ICTT345267Moorcroft, EMDA201A Level MathsT666758Newton, IMUA201A Level MusicT773351Parry, DSPA201A Level SpanishT876541n/aetc

    Student numberStudent nameDate of birthSex12345Jones, P20/05/1984M22433Phillips, K16/02/1983F66688Smith, J25/12/1984Metc

  • Creating the relationshipWe need a common field, but the problem is that because this is a many-to-many relationship, whichever table we put the link field into, there needs to be more than one field.e.g. STUDENT (student number, student name, date of birth, sex, course number) is no good because the student is doing several courses, so which one would be mentioned?Similarly, COURSE (course number, course name, lecturer number, lecturer name, student number) is no good either because each course has a number of students taking it.How about allowing space for 3 courses on each student record? STUDENT (student number, student name, date of birth, sex, course1, course2, course3)Why is this not a good idea?

  • Creating the relationship What we have engineered is a repeating attribute unacceptable in 1st normal form.In other words, the field course number is repeated 3 timesThe table is therefore NOT in first normal form. It would be represented in standard notation with a line over the repeating attribute: STUDENT (student number, student name, date of birth, sex, course number)To put the data into first normal form, the repeating attribute must be removed.

  • Creating the relationshipIn its place, the field course number becomes part of the primary key in the student table. The tables are now as follows:STUDENT (student number, student name, date of birth, sex, course number)COURSE (course number, course name, lecturer number, lecturer name) Discussion: What is a primary key? Why does course number have to be part of the primary key?

  • The situation so far:STUDENTCOURSE

    Student numberStudent nameDate of birthSexCourse number12345Jones, P20/05/1984MITA20122433Phillips, K16/02/1983FITA20122433Phillips, K16/02/1983FMUA20122433Phillips, K16/02/1983FSPA20166688Smith, J25/12/1984MMDA20166688Smith, J25/12/1984MMUA201etc

    Course numberCourse nameLecturer numberLecturer nameITA201A Level ICTT345267Moorcroft, EMDA201A Level MathsT666758Newton, IMUA201A Level MusicT773351Parry, DSPA201A Level SpanishT876541n/aetc

  • Second Normal FormDefinitionOnly applicable if table has a compound keyA table is second normal form (2NF) if it is in first normal form (1NF) and no column that is not part of the primary key is dependent on a portion of the primary key:This may be said as: A table in 2NF contains no partial dependencies

  • The STUDENT table the earlier slide (see above) is not in 2NF because, for example, student name is dependent only on student number and not on course numberTo put the tables in 2NF we need to introduce a third table to link the two entities

    Student numberStudent nameDate of birthSexCourse number12345Jones, P20/05/1984MITA20122433Phillips, K16/02/1983FITA20122433Phillips, K16/02/1983FMUA20122433Phillips, K16/02/1983FSPA20166688Smith, J25/12/1984MMDA20166688Smith, J25/12/1984MMUA201etc

  • The tables in 2nd Normal Form (2NF)STUDENT (student number, student name, date of birth, sex)

    STUDENT_ COURSE(student number, course number)

    COURSE (course number, course name, lecturer number, lecturer name)

    What we had with the two entities was a many-to-many relationshipThis situation will always need a link table to create two one-to-many relationships

  • STUDENTCOURSESTUDENT_COURSE

    Student numberStudent nameDate of birthSex12345Jones, P20/05/1984M22433Phillips, K16/02/1983F66688Smith, J25/12/1984Metc

    Course numberCourse nameLecturer numberLecturer nameITA201A Level ICTT345267Moorcroft, EMDA201A Level MathsT666758Newton, IMUA201A Level MusicT773351Parry, DSPA201A Level SpanishT876541n/aetc

    Student numberCourse number12345ITA20122433ITA20122433MUA20122433SPA20166688MDA20166688MUA201etc

  • Third Normal FormDefinitionA table in third normal form (3NF) contains no non-key dependencies In other words, the data in each entity (table) is checked to see if it relates entirely to the primary key, and not to any other attribute (i.e. field)

  • The tables in 3rd Normal Form (3NF)The COURSES table Contains an attribute for lecturer number and also one for lecturer name.Lecturer name is dependent on lecturer number (not on course number)Therefore a new table should be created for LECTURER

  • Normalised FilesSTUDENT (student number, student name, date of birth, sex)STUDENT_COURSE (student number, course number)COURSE (course number, course name, lecturer number)LECTURER (lecturer number, lecturer name)

    This is the optimum way of holding this data, with no duplication.The tables in Relational Databases should be in Third Normal Form

  • Comparing flat-files with RDBA relational database is able to create links between tables representing different entities such as STUDENT and COURSE, through the use of foreign keys. A flat file system is not able to link tables It is only useful for very simple databases which contain information about just one entity. It is impossible to normalise a database in a flat file system, since this involves correctly establishing links between tablesFlat-file systems do not have any of the sophisticated features of a full DBMS such as the ability to set individual user-access rights, or allow several people to access the database at the same time