Top Banner
Databases Creating Relational Databases
23
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Basic Relational Databases

Databases

Creating Relational Databases

Page 2: Basic Relational Databases

Student ID Student Surname

Student FirstName

Form Sex Age Class Name

Class Teacher

Room Number

S10204 HALL Taighan 10B F 13 Maths101 PRJ 651

S10204 HALL Taighan 10B M 13 GEO101 RW 532

S10204 HALL Taighan 10B F 13 SCI10 CW 161

S10412 CRIST Graeme 10D M 14 GEO101 RW 532

S10412 CRIST Graeme 10D M 14 HIS101 HJD 541

S10101 CRIST Graeme 10D M 14 Maths101 PRJ 651

S10414 HASLETT Oliver 10D M 14 Maths101 PRJ 651

S10101 FUNG JUNE 10A F 15 Maths101 PRJ 651

S10101 FUNG JUNE 10B F 15 GEO101 HJD 532

S10101 FUNF JUNE 10A F 15 HIS101 HJD 541

This is part of a database showing all students and their classes in a school.

School Database

Page 3: Basic Relational Databases

Data integrity and Data Redundancy

These issues can have a significant impact on the ability for a database to actually do the job that it is intended to do. Data Integrity is the idea that information always keeps its meaning. This is particularly difficult when databases get really big. If you imagine the database above includes every student and every class in the school it could contain over 10000 records.

Page 4: Basic Relational Databases

Student ID Student Surname

Student FirstName

Form Sex Age Class Name

Class Teacher

Room Number

S10204 HALL Taighan 10B F 13 Maths101 PRJ 651

S10204 HALL Taighan 10B M 13 GEO101 RW 532

S10204 HALL Taighan 10B F 13 SCI10 CW 161

S10412 CRIST Graeme 10D M 14 GEO101 RW 532

S10412 CRIST Graeme 10D M 14 HIS101 HJD 541

S10101 CRIST Graeme 10D M 14 Maths101 PRJ 651

S10414 HASLETT Oliver 10D M 14 Maths101 PRJ 651

S10101 FUNG JUNE 10A F 15 Maths101 PRJ 651

S10101 FUNG JUNE 10B F 15 GEO101 HJD 532

S10101 FUNF JUNE 10A F 15 HIS101 HJD 541

This is part of a database showing all students and their classes in a school.

School Database

Page 5: Basic Relational Databases

Examples of Data integrity

Examples of Data integrity

A simple typo means that it might mean that information gets lost if we search for June in the data. This problem is much more likely because of the redundancies issues we will look at later.

Page 6: Basic Relational Databases

Student ID Student Surname

Student FirstName

Form Sex Age Class Name

Class Teacher

Room Number

S10204 HALL Taighan 10B F 13 Maths101 PRJ 651

S10204 HALL Taighan 10B M 13 GEO101 RW 532

S10204 HALL Taighan 10B F 13 SCI10 CW 161

S10412 CRIST Graeme 10D M 14 GEO101 RW 532

S10412 CRIST Graeme 10D M 14 HIS101 HJD 541

S10101 CRIST Graeme 10D M 14 Maths101 PRJ 651

S10414 HASLETT Oliver 10D M 14 Maths101 PRJ 651

S10101 FUNG JUNE 10A F 15 Maths101 PRJ 651

S10101 FUNG JUNE 10B F 15 GEO101 HJD 532

S10101 FUNF JUNE 10A F 15 HIS101 HJD 541

This is part of a database showing all students and their classes in a school.

School Database

Page 7: Basic Relational Databases

Examples of Data integrity

Examples of Data integrity

This example can cause real problems it shows that the same information in the database can mean two different things is s10101 Graeme or June.

Page 8: Basic Relational Databases

Student ID Student Surname

Student FirstName

Form Sex Age Class Name

Class Teacher

Room Number

S10204 HALL Taighan 10B F 13 Maths101 PRJ 651

S10204 HALL Taighan 10B M 13 GEO101 RW 532

S10204 HALL Taighan 10B F 13 SCI10 CW 161

S10412 CRIST Graeme 10D M 14 GEO101 RW 532

S10412 CRIST Graeme 10D M 14 HIS101 HJD 541

S10101 CRIST Graeme 10D M 14 Maths101 PRJ 651

S10414 HASLETT Oliver 10D M 14 Maths101 PRJ 651

S10101 FUNG JUNE 10A F 15 Maths101 PRJ 651

S10101 FUNG JUNE 10B F 15 GEO101 HJD 532

S10101 FUNF JUNE 10A F 15 HIS101 HJD 541

This is part of a database showing all students and their classes in a school.

School Database

Page 9: Basic Relational Databases

Examples of Data integrity

Examples of Data integrity Here we have June in two classes? Which one is right? What if we need to find June in registration urgently

Page 10: Basic Relational Databases

Hard to find?

They can be really hard to find in this Database there are more integrity errors can you find them?

Page 11: Basic Relational Databases

Student ID Student Surname

Student FirstName

Form Sex Age Class Name

Class Teacher

Room Number

S10204 HALL Taighan 10B F 13 Maths101 PRJ 651

S10204 HALL Taighan 10B M 13 GEO101 RW 532

S10204 HALL Taighan 10B F 13 SCI10 CW 161

S10412 CRIST Graeme 10D M 14 GEO101 RW 532

S10412 CRIST Graeme 10D M 14 HIS101 HJD 541

S10101 CRIST Graeme 10D M 14 Maths101 PRJ 651

S10414 HASLETT Oliver 10D M 14 Maths101 PRJ 651

S10101 FUNG JUNE 10A F 15 Maths101 PRJ 651

S10101 FUNG JUNE 10B F 15 GEO101 HJD 532

S10101 FUNF JUNE 10A F 15 HIS101 HJD 541

Page 12: Basic Relational Databases

Bad or unnecessary Fields NamesAge is an example of a bad field name for the obvious reason that it can change at anytime. It would be much better to use DOB as age can always be calculated and ofcourse it does not change.

Form may also be unnecessary in this case as the reference number is CODED to give the form.

Can you explain the code?

Page 13: Basic Relational Databases

Data Redundancy

Data redundancy is the where data needs to be entered and stored on more than one occasion.

For example if you look at the table you can see that we have entered Oliver Haslett’s first name and surname every time we put him into a class. This can cause problems such as increasing the chance of someone entering his name wrong. It also means more time is needed to enter data and more space is needed when it is saved.

Page 14: Basic Relational Databases

Student ID Student Surname

Student FirstName

Form Sex Age Class Name

Class Teacher

Room Number

S10204 HALL Taighan 10B F 13 Maths101 PRJ 651

S10204 HALL Taighan 10B M 13 GEO101 RW 532

S10204 HALL Taighan 10B F 13 SCI10 CW 161

S10412 CRIST Graeme 10D M 14 GEO101 RW 532

S10412 CRIST Graeme 10D M 14 HIS101 HJD 541

S10101 CRIST Graeme 10D M 14 Maths101 PRJ 651

S10414 HASLETT Oliver 10D M 14 Maths101 PRJ 651

S10101 FUNG JUNE 10A F 15 Maths101 PRJ 651

S10101 FUNG JUNE 10B F 15 GEO101 HJD 532

S10101 FUNF JUNE 10A F 15 HIS101 HJD 541

Table showing data redundancy .

School Database

Page 15: Basic Relational Databases

Relational Databases

Student ID

Student Surname

Student First Form Sex Age Class Name Class Teacher Room Number

S10204 HALL Taighan 10B F 13 Maths101 PRJ 651

S10204 HALL Taighan 10B M 13 GEO101 RW 532

S10204 HALL Taighan 10B F 13 SCI10 CW 161

The simple way to think about this is that when you create a table of data in a database you should have fields which directly relate to the key field of the table.

Page 16: Basic Relational Databases

Relational Databases

Student ID

Student Surname

Student First Form Sex Age Class Name Class Teacher Room Number

S10204 HALL Taighan 10B F 13 Maths101 PRJ 651

S10204 HALL Taighan 10B M 13 GEO101 RW 532

S10204 HALL Taighan 10B F 13 SCI10 CW 161

In our table we can see that the Class teacher, Class Name and Room Number are not related to the Student. The Student ID is unique so the first name, surname, form, Sex is all related to the student but the other fields do not.

Page 17: Basic Relational Databases

Relational Databases

Student ID

Student Surname

Student First Form Sex Age Class Name Class Teacher Room Number

S10204 HALL Taighan 10B F 13 Maths101 PRJ 651

S10204 HALL Taighan 10B M 13 GEO101 RW 532

S10204 HALL Taighan 10B F 13 SCI10 CW 161

We can now look at how to remove redundancy issues a process called normalization. This can be very complex and we are only going to look it is a simple way.

Page 18: Basic Relational Databases

Removing Redundancy (“Normalization”)

STUDENTStudentID Student First NameStudent Surname Student DOBStudent Form

CLASSClassIDClass NameClass Teacher Room Number

Instead of one single table we create 2 tables as shown below.

Page 19: Basic Relational Databases

Removing Redundancy (“Normalization”)

STUDENTStudentID Student First NameStudent Surname Student DOBStudent Form

CLASSClassIDClass NameClass Teacher Room Number

Unfortunately these tables have what is called a many to many relationship. That is there are many students in each class and there are many classes for each student. This kind of relationship will not work for what we need.

Page 20: Basic Relational Databases

Step 2To solve this problem we create an extra table which contains the StudentID and ClassID for each student in each class.

What this does is remove the many to much relationship and replace it with a one to many or many to one relationship.

That is one student can be in many classes and one class has many students.

We can use the relationships to find the information we need in each case making use of something called Queries that we will look at later.

Page 21: Basic Relational Databases

How it looksSTUDENT StudentID FirstNameSurname DOBForm

CLASSClassIDClass NameClass TeacherRoom Number

STUDENTTAKESStudentIDClassID

This goes a long way to solving our problem but there remains a potential problem with the room number field which could give use problems in what we want to do with the data. Why ?

Page 22: Basic Relational Databases

STEP 3 (optional?)

Room number is not actually related to the class. What happens if a class is in more than one room for example? If you were to do the normalization properly you would need to remove Room Number and create another table.

STUDENT StudentID FirstNameSurname DOBForm

CLASSClassIDClass NameClass TeacherRoom Number

STUDENTTAKESStudentIDClassID

Page 23: Basic Relational Databases

Entity Relationship Diagram

This is called an Entity Relationship Diagram and they are used to show the data structure in a database.

Student StudentTakes Class

Room

Room Class