Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 56 Relational Database Design Compiled by Eddie Moorcroft.

Similar presentations


Presentation on theme: "Chapter 56 Relational Database Design Compiled by Eddie Moorcroft."— Presentation transcript:

1 Chapter 56 Relational Database Design Compiled by Eddie Moorcroft

2 Chapter 56 - ICT5 The Basics A relational database is a widely-used type of DBMS (Database Management System) Data is held in tables The 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.

3 Chapter 56 - ICT5 Typical table IMPORTANT Standard Notation BOOK(AccessionNo, DeweyCode, Title, Author,DatePublished) Entity name in uppercase Key field (unique identifier) is underlined All attributes are shown in brackets, separated by commas N.B A PRIMARY key uniquely references each record SIMPLE key is based on one attribute (field) COMPOSITE key is based on two or more attributes (fields) Accession Number DeweyCodeTitleAuthorDate Published 88121.9Let’s CookChan, C1992 123345.440ElectricityGlendenning, V1995 300345.440RidersCooper, J1995 657200.00Greek in 3 weeksStavros, G1990 etc…

4 Chapter 56 - ICT5 Linking database tables Tables 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 notation BORROWER(BorrowerID, Name, Address) BOOK(AccessionNo, DeweyCode, Title, Author, DatePublished, BorrowerID, DateDue) Consider possible flaws in this model and suggest a better one. BORROWERBOOK borrows

5 Chapter 56 - ICT5 Normalisation Normalisation 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: 1. No data is unnecessarily duplicated 2. Data is consistent throughout the database 3. The structure of each table is flexible enough to allow you to enter as many or as few items as you want to 4. The structure should enable a user to make all kinds of complex queries relating data from different tables

6 Chapter 56 - ICT5 First normal form Definition: 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.

7 Chapter 56 - ICT5 Consider 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: First normal form STUDENTCOURSE attends

8 Chapter 56 - ICT5 Sample data to be held in a database It 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 number Student name Date of birth SexCourse number Course nameLecturer number Lecturer name 12345Jones, P20/05/1984MITA201A Level ICTT345267Moorcroft, E 22433Phillips, K16/02/1983FITA201A Level ICTT345267Moorcroft, E 22433Phillips, K16/02/1983FMUA201A Level MusicT773351Parry, D 22433Phillips, K16/02/1983FSPA201A Level SpanishT876541n/a 66688Smith, J25/12/1984MMDA201A Level MathsT666758Newton, I 66688Smith, J25/12/1984MMUA201A Level MusicT773351Parry, D etc

9 Chapter 56 - ICT5 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 number Course nameLecturer number Lecturer name ITA201A Level ICTT345267Moorcroft, E MDA201A Level MathsT666758Newton, I MUA201A Level MusicT773351Parry, D SPA201A Level Spanish T876541n/a etc Student number Student name Date of birthSex 12345Jones, P20/05/1984M 22433Phillips, K16/02/1983F 66688Smith, J25/12/1984M etc

10 Chapter 56 - ICT5 Creating the relationship We 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?

11 Chapter 56 - ICT5 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 times The 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.

12 Chapter 56 - ICT5 Creating the relationship In 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?

13 Chapter 56 - ICT5 The situation so far: Student numberStudent nameDate of birthSexCourse number 12345Jones, P20/05/1984MITA201 22433Phillips, K16/02/1983FITA201 22433Phillips, K16/02/1983FMUA201 22433Phillips, K16/02/1983FSPA201 66688Smith, J25/12/1984MMDA201 66688Smith, J25/12/1984MMUA201 etc Course numberCourse nameLecturer numberLecturer name ITA201A Level ICTT345267Moorcroft, E MDA201A Level MathsT666758Newton, I MUA201A Level MusicT773351Parry, D SPA201A Level SpanishT876541n/a etc STUDENT COURSE

14 Chapter 56 - ICT5 Second Normal Form Definition Only applicable if table has a compound key A 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

15 Chapter 56 - ICT5 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 number To put the tables in 2NF we need to introduce a third table to link the two entities Student numberStudent nameDate of birthSexCourse number 12345Jones, P20/05/1984MITA201 22433Phillips, K16/02/1983FITA201 22433Phillips, K16/02/1983FMUA201 22433Phillips, K16/02/1983FSPA201 66688Smith, J25/12/1984MMDA201 66688Smith, J25/12/1984MMUA201 etc

16 Chapter 56 - ICT5 The tables in 2 nd 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 relationship This situation will always need a link table to create two one-to-many relationships

17 Chapter 56 - ICT5 Student numberStudent nameDate of birthSex 12345Jones, P20/05/1984M 22433Phillips, K16/02/1983F 66688Smith, J25/12/1984M etc Course numberCourse nameLecturer numberLecturer name ITA201A Level ICTT345267Moorcroft, E MDA201A Level MathsT666758Newton, I MUA201A Level MusicT773351Parry, D SPA201A Level SpanishT876541n/a etc Student numberCourse number 12345ITA201 22433ITA201 22433MUA201 22433SPA201 66688MDA201 66688MUA201 etc STUDENT COURSE STUDENT_COURSE

18 Chapter 56 - ICT5 Third Normal Form Definition A 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)

19 Chapter 56 - ICT5 The tables in 3 rd 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

20 Chapter 56 - ICT5 Normalised Files STUDENT (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

21 Chapter 56 - ICT5 Comparing flat-files with RDB A 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 tables Flat-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


Download ppt "Chapter 56 Relational Database Design Compiled by Eddie Moorcroft."

Similar presentations


Ads by Google