Basic Relational Databases

  • 3,636 views
Uploaded on

 

More in: Education , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
3,636
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
5
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. DatabasesCreating Relational Databases
  • 2. School DatabaseStudent ID Student Student Form Sex Age Class Class Room Surname FirstName Name Teacher NumberS10204 HALL Taighan 10B F 13 Maths101 PRJ 651S10204 HALL Taighan 10B M 13 GEO101 RW 532S10204 HALL Taighan 10B F 13 SCI10 CW 161S10412 CRIST Graeme 10D M 14 GEO101 RW 532S10412 CRIST Graeme 10D M 14 HIS101 HJD 541S10101  CRIST Graeme 10D M 14 Maths101 PRJ 651S10414 HASLETT Oliver 10D M 14 Maths101 PRJ 651S10101 FUNG JUNE 10A F 15 Maths101 PRJ 651S10101 FUNG JUNE 10B F 15 GEO101 HJD 532S10101 FUNF  JUNE 10A F 15 HIS101 HJD 541This is part of a database showing all students andtheir classes in a school.
  • 3. Data integrity and Data RedundancyThese issues can have a significant impact onthe ability for a database to actually do thejob that it is intended to do.Data Integrity is the idea that informationalways keeps its meaning. This is particularlydifficult when databases get really big. If youimagine the database above includes everystudent and every class in the school it couldcontain over 10000 records.
  • 4. School DatabaseStudent ID Student Student Form Sex Age Class Class Room Surname FirstName Name Teacher NumberS10204 HALL Taighan 10B F 13 Maths101 PRJ 651S10204 HALL Taighan 10B M 13 GEO101 RW 532S10204 HALL Taighan 10B F 13 SCI10 CW 161S10412 CRIST Graeme 10D M 14 GEO101 RW 532S10412 CRIST Graeme 10D M 14 HIS101 HJD 541S10101  CRIST Graeme 10D M 14 Maths101 PRJ 651S10414 HASLETT Oliver 10D M 14 Maths101 PRJ 651S10101 FUNG JUNE 10A F 15 Maths101 PRJ 651S10101 FUNG JUNE 10B F 15 GEO101 HJD 532S10101 FUNF  JUNE 10A F 15 HIS101 HJD 541 This is part of a database showing all students and their classes in a school.
  • 5. Examples of Data integrityExamples of Data integrity  A simple typo means that it might meanthat information gets lost if we search for Junein the data. This problem is much more likelybecause of the redundancies issues we will lookat later.
  • 6. School DatabaseStudent ID Student Student Form Sex Age Class Class Room Surname FirstName Name Teacher NumberS10204 HALL Taighan 10B F 13 Maths101 PRJ 651S10204 HALL Taighan 10B M 13 GEO101 RW 532S10204 HALL Taighan 10B F 13 SCI10 CW 161S10412 CRIST Graeme 10D M 14 GEO101 RW 532S10412 CRIST Graeme 10D M 14 HIS101 HJD 541S10101  CRIST Graeme 10D M 14 Maths101 PRJ 651S10414 HASLETT Oliver 10D M 14 Maths101 PRJ 651S10101 FUNG JUNE 10A F 15 Maths101 PRJ 651S10101 FUNG JUNE 10B F 15 GEO101 HJD 532S10101 FUNF  JUNE 10A F 15 HIS101 HJD 541 This is part of a database showing all students and their classes in a school.
  • 7. Examples of Data integrityExamples of Data integrity This example can cause real problems itshows that the same information in thedatabase can mean two different things iss10101 Graeme or June.
  • 8. School DatabaseStudent ID Student Student Form Sex Age Class Class Room Surname FirstName Name Teacher NumberS10204 HALL Taighan 10B F 13 Maths101 PRJ 651S10204 HALL Taighan 10B M 13 GEO101 RW 532S10204 HALL Taighan 10B F 13 SCI10 CW 161S10412 CRIST Graeme 10D M 14 GEO101 RW 532S10412 CRIST Graeme 10D M 14 HIS101 HJD 541S10101  CRIST Graeme 10D M 14 Maths101 PRJ 651S10414 HASLETT Oliver 10D M 14 Maths101 PRJ 651S10101 FUNG JUNE 10A F 15 Maths101 PRJ 651S10101 FUNG JUNE 10B F 15 GEO101 HJD 532S10101 FUNF  JUNE 10A F 15 HIS101 HJD 541 This is part of a database showing all students and their classes in a school.
  • 9. Examples of Data integrityExamples of Data integrity Here we have June in two classes? Whichone is right? What if we need to find June inregistration urgently
  • 10. Hard to find?They can be really hard to find in this Databasethere are more integrity errors can you findthem?
  • 11. Student ID Student Student Form Sex Age Class Class Room Surname FirstName Name Teacher NumberS10204 HALL Taighan 10B F 13 Maths101 PRJ 651S10204 HALL Taighan 10B M 13 GEO101 RW 532S10204 HALL Taighan 10B F 13 SCI10 CW 161S10412 CRIST Graeme 10D M 14 GEO101 RW 532S10412 CRIST Graeme 10D M 14 HIS101 HJD 541S10101  CRIST Graeme 10D M 14 Maths101 PRJ 651S10414 HASLETT Oliver 10D M 14 Maths101 PRJ 651S10101 FUNG JUNE 10A F 15 Maths101 PRJ 651S10101 FUNG JUNE 10B F 15 GEO101 HJD 532S10101 FUNF  JUNE 10A F 15 HIS101 HJD 541
  • 12. Bad or unnecessary Fields NamesAge is an example of a bad field name for theobvious reason that it can change at anytime.It would be much better to use DOB as agecan always be calculated and ofcourse it does not change.Form may also be unnecessary in this case asthe reference number is CODED to give theform.Can you explain the code?
  • 13. Data RedundancyData redundancy is the where data needs to beentered and stored on more than one occasion.For example if you look at the table you can seethat we have entered Oliver Haslett’s first nameand surname every time we put him into aclass. This can cause problems such asincreasing the chance of someone entering hisname wrong. It also means more time isneeded to enter data and more space is neededwhen it is saved.
  • 14. School DatabaseStudent ID Student Student Form Sex Age Class Class Room Surname FirstName Name Teacher NumberS10204 HALL Taighan 10B F 13 Maths101 PRJ 651S10204 HALL Taighan 10B M 13 GEO101 RW 532S10204 HALL Taighan 10B F 13 SCI10 CW 161S10412 CRIST Graeme 10D M 14 GEO101 RW 532S10412 CRIST Graeme 10D M 14 HIS101 HJD 541S10101 CRIST Graeme 10D M 14 Maths101 PRJ 651S10414 HASLETT Oliver 10D M 14 Maths101 PRJ 651S10101 FUNG JUNE 10A F 15 Maths101 PRJ 651S10101 FUNG JUNE 10B F 15 GEO101 HJD 532S10101 FUNF JUNE 10A F 15 HIS101 HJD 541 Table showing data redundancy .
  • 15. Relational DatabasesThe simple way to think about this is thatwhen you create a table of data in a databaseyou should have fields which directly relate tothe key field of the table.Student Student Student First Form Sex Age Class Name Class Teacher RoomID Surname NumberS10204 HALL Taighan 10B F 13 Maths101 PRJ 651S10204 HALL Taighan 10B M 13 GEO101 RW 532S10204 HALL Taighan 10B F 13 SCI10 CW 161
  • 16. Relational DatabasesIn our table we can see that the Class teacher,Class Name and Room Number are not relatedto the Student. The Student ID is unique sothe first name, surname, form, Sex is allrelated to the student but the other fields donot.Student Student Student First Form Sex Age Class Name Class Teacher RoomID Surname NumberS10204 HALL Taighan 10B F 13 Maths101 PRJ 651S10204 HALL Taighan 10B M 13 GEO101 RW 532S10204 HALL Taighan 10B F 13 SCI10 CW 161
  • 17. Relational DatabasesWe can now look at how to remove redundancyissues a process called normalization.This can be very complex and we are only goingto look it is a simple way.Student Student Student First Form Sex Age Class Name Class Teacher RoomID Surname NumberS10204 HALL Taighan 10B F 13 Maths101 PRJ 651S10204 HALL Taighan 10B M 13 GEO101 RW 532S10204 HALL Taighan 10B F 13 SCI10 CW 161
  • 18. Removing Redundancy (“Normalization”)Instead of one single table we create 2 tables asshown below. CLASS STUDENT ClassID StudentID Class Name Student First Name Class Teacher Student Surname Room Number Student DOB Student Form
  • 19. Removing Redundancy (“Normalization”) CLASS STUDENT ClassID StudentID Class Name Student First Name Class Teacher Student Surname Room Number Student DOB Student FormUnfortunately these tables have what is called a many tomany relationship. That is there are many students ineach class and there are many classes for each student.This kind of relationship will not work for what we need.
  • 20. Step 2To solve this problem we create an extra table which containsthe StudentID and ClassID for each student in each class.What this does is remove the many to much relationship andreplace it with a one to many or many to one relationship.That is one student can be in many classes and one class hasmany students.We can use the relationships to find the information we need ineach case making use of something called Queries that we willlook at later.
  • 21. How it looksSTUDENT CLASSStudentID ClassIDFirstName Class NameSurname Class TeacherDOB Room NumberForm STUDENTTAKES StudentID ClassID 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 ?
  • 22. STEP 3 (optional?) STUDENT CLASS StudentID ClassID FirstName STUDENTTAKES Class Name Surname StudentID Class Teacher DOB ClassID Room Number FormRoom number is not actually related to the class. What happens if aclass is in more than one room for example? If you were to do thenormalization properly you would need to remove Room Numberand create another table.
  • 23. Entity Relationship DiagramThis is called an Entity Relationship Diagram and they areused to show the data structure in a database. Student StudentTakes Class Room Class Room