Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Design ERD and Normalisation

Similar presentations


Presentation on theme: "Database Design ERD and Normalisation"— Presentation transcript:

1 Database Design ERD and Normalisation
FdSc in ICT

2 Problems with a flat file design
The same ID number can appears in multiple rows. You cannot retrieve a unique record with the ID number Entities may not have an ID number. Details are repeated – DATA REDUNDANCY, wasting space. Data can be INCONSISTENT as it is stored more than once Multiple records have to be updated. This is an AMENDMENT ANOMALY Multiple records have to be deleted. This is a DELETION ANOMALY More than one record may need to be added. This is an ADDITION ANOMALY Data may not be ATOMIC.

3 Improving flat files Split into smaller tables with a defined relationship between them At the design stage we can use an Entity Relationship Diagram (E-R)

4 E-R diagram A dog can only have one owner One owner can have many dogs
The relationship is that: A dog can only have one owner One owner can have many dogs This is a “one to many” relationship OWNER DOG

5 Result of E-R diagram ID Initial Surname Title Address Postcode
Dog Name Gender DOB Breed 1 A Smith Mrs 4 High St, Hereford HR1 1ZX Ant M 21/08/02 Alsatian Dec Jordan F 08/08/04 Terrier Ms Rooney 23/10/05 Poodle 2 C Miles Mr 72 Castle Road, Ledbury HR7 AA Fang 14/03/07 Chihuahua ID Initial Surname Title Address Postcode 1 A Smith Mrs 4 High St, Hereford HR1 1ZX 2 C Miles Mr 72 Castle Road, Ledbury HR7 AA DogID Dog Name Gender DOB Breed ID 1 Ant M 21/08/02 Alsatian 2 Dec 3 Jordan F 08/08/04 Terrier 4 Rooney 23/10/05 Poodle 5 Fang 14/03/07 Chihuahua

6 Keys Primary Key Primary Key Foreign Key ID Initial Surname Title
Address Postcode 1 A Smith Mrs 4 High St, Hereford HR1 1ZX 2 C Miles Mr 72 Castle Road, Ledbury HR7 AA DogID Dog Name Gender DOB Breed ID 1 Ant M 21/08/02 Alsatian 2 Dec 3 Jordan F 08/08/04 Terrier 4 Rooney 23/10/05 Poodle 5 Fang 14/03/07 Chihuahua

7 Why is this better? Member’s details are only stored once
Each dog has a unique ID New owners only entered once Changes only made in one place Details need only be deleted from one record ID Initial Surname Title Address Postcode 1 A Smith Mrs 4 High St, Hereford HR1 1ZX 2 C Miles Mr 72 Castle Road, Ledbury HR7 AA DogID Dog Name Gender DOB Breed ID 1 Ant M 21/08/02 Alsatian 2 Dec 3 Jordan F 08/08/04 Terrier 4 Rooney 23/10/05 Poodle 5 Fang 14/03/07 Chihuahua

8 Further improvements OWNER DOG BREED
Now many dogs can belong to a breed A dog can only be of one breed

9 The tables now look like this
ID Initial Surname Title Address Postcode 1 A Smith Mrs 4 High St, Hereford HR1 1ZX 2 C Miles Mr 72 Castle Road, Ledbury HR7 AA DogID Dog Name Gender DOB BreedID ID 1 Ant M 21/08/02 2 Dec 3 Jordan F 08/08/04 4 Rooney 23/10/05 5 Fang 14/03/07 Breed ID Breed 1 Alsatian 2 Terrier 3 Poodle 4 Chihuahua

10 The benefits are Member’s details are only stored once
Each dog has a unique ID New owners only entered once Changes only made in one place Details need only be deleted from one record We only need to store the details of each breed once A new breed can be added without an owner

11 Many to many relationships
Many students can take many exams This will be a problem for the tables STUDENT EXAM ID Name Exam 1 Exam 2 Exam 3 Exam 4 Exam n TOS Tosker 4046 3545 6354 2875 9756

12 Many to many problems How many columns do we need for exams?
A record in an exam table won’t relate to one student What is the solution?

13 A link table STUDENT TAKES EXAM What can we add here? ExamID Exam 1
French 2 German 3 Italian StudentID Name 1 Carl 2 Adam 3 Jane What can we add here? StudentID ExamID 1 3 2

14 to identify unique records
A better solution STUDENT TAKES EXAM Primary Key to identify unique records StudentID Name 1 Carl 2 Adam 3 Jane TakesID StudentID ExamID 1 2 3 4 5 6 ExamID Exam 1 French 2 German 3 Italian

15 The benefits are We only store the student details once
We only store the exam details once Each primary key is unique We can recombine data from the three tables into one record

16 Exercise Draw an ERD for:
A company which employs sales staff who take orders from customers DVDs which contain films with scenes in which actors appear

17 Optionality The name given to a relationship which may or may not exist A company employs sales staff It may also use agency sales staff One company employs one or more sales staff One sales staff is employed by the company or no company COMPANY SALES STAFF COMPANY SALES STAFF

18 Attributes An attribute is an item whose properties are associated with an entity Car – colour, model, VIN Student – Name, address, Attributes can only have one value at a particular point in time An address may change, but for anybody retrieving the data at a specific time, they only get one value

19 Attributes Attributes added under the entity name COMPANY SALES STAFF
Address Tel no Fax no VAT no Name Address Salary Age

20 Exercise Add attributes to the ERD diagram you drew for the DVD.

21 Exercise Look again at the coal merchant’s requirements
Assume that the solution will involve a database Create an ERD diagram for the database Draw a diagram and describe each step you take Clearly identify how you resolve many to many relationships Include optionality and attributes Underline the primary keys

22 Normalisation We have just down the top down Entity Relationship diagram method We now need to try the bottoms up approach called normalisation

23 Draw up a typical paper record for the project you are converting to a database
In this example we are using records of potential students enquiring about courses Course Name AVCE in ICT Length 2 years Tutor Bob Higgie Location Main campus Location code 001 Level 3 Date Name Address DOB Action 10/10/04 W Rooney Manchester 5/08/87 Send details 19/10/04 A Wenger London 5/3/47 Tutor to call 20/10/04 A Ferguson 28/4/42

24 Draw up a typical paper record for the project you are converting to a database
In this example we are using records of potential students enquiring about courses Course Name AVCE in ICT Length 2 years Tutor Bob Higgie Location Main campus Location code 001 Level 3 Date Name Address DOB Action 10/10/04 W Rooney Manchester 5/08/87 Send details 19/10/04 A Wenger London 5/3/47 Tutor to call 20/10/04 A Ferguson 28/4/42 Repeating group

25 Draw up a landscape table with 5 columns
0NF 1NF 2NF 3NF Table names

26 List all the attributes from the paper record in the 0NF column
Table names

27 List all the attributes from the paper record in the 0NF column
Table names Course Name Length Tutor Location Location Code Level Date Name Address DOB Action

28 Course Name Length Tutor Location Location Code Level (Date Name
Identify the group or groups of attributes that are repeated on the paper record. Put a bracket round all of them 0NF 1NF 2NF 3NF Table names Course Name Length Tutor Location Location Code Level (Date Name Address DOB Action)

29 Add a primary key for the repeating group and for the group only entered once.
0NF 1NF 2NF 3NF Table names CourseID Course Name Length Tutor Location Location Code Level (EnquirerID Date Name Address DOB Action)

30 Copy the non-repeating group across into the 1NF column
Copy the non-repeating group across into the 1NF column. This group is already in 1NF as it doesn’t have any brackets around it. 0NF 1NF 2NF 3NF Table names CourseID Course Name Length Tutor Location Location Code Level (EnquirerID Date Name Address DOB Action)

31 Move the repeating group across into the 1NF column, making sure it is separated from the group already there. Add the key from the non-repeating group to it and remove the brackets 0NF 1NF 2NF 3NF Table names CourseID Course Name Length Tutor Location Location Code Level (EnquirerID Date Name Address DOB Action) EnquirerID Action

32 This is now in first normal form
There are no repeating groups The second group has a compound primary key (EnquirerID/CourseID) This corresponds to 2 tables All records can be recombined into one record by using the keys To put the database into 2NF we need to make sure it is in 1NF and all the non-key attributes depend entirely on the primary key

33 Move groups which only have a simple primary key into the 2NF column
Table names CourseID Course Name Length Tutor Location Location Code Level (EnquirerID Date Name Address DOB Action) EnquirerID Action

34 CourseID Course Name Length Tutor Location Location Code Level
Next we identify those attributes in the other group that do not depend on both keys. 0NF 1NF 2NF 3NF Table names CourseID Course Name Length Tutor Location Location Code Level (EnquirerID Date Name Address DOB Action) EnquirerID Action

35 Move the non-key attributes that depend on only one key across to the 2NF column. Then copy the attribute they do depend on into the group with them. 0NF 1NF 2NF 3NF Table names CourseID Course Name Length Tutor Location Location Code Level (EnquirerID Date Name Address DOB Action) EnquirerID Action

36 The remaining group (EnquirerID, CourseID, Date and Action are then moved into the 2NF column and given a unique ID. In our case we call it the EnquiryID. 0NF 1NF 2NF 3NF Table names CourseID Course Name Length Tutor Location Location Code Level (EnquirerID Date Name Address DOB Action) EnquirerID Action EnquiryID

37 Look at attributes that are not part of a key and work out if they depend on another attribute and not the primary key of the group they are in. 0NF 1NF 2NF 3NF Table names CourseID Course Name Length Tutor Location Location Code Level (EnquirerID Date Name Address DOB Action) EnquirerID Action EnquiryID

38 Move Location across into its own group and copy Location Code into it as a primary key. Then Location Code is also left as a foreign key in the CourseID group. 0NF 1NF 2NF 3NF Table names CourseID Course Name Length Tutor Location Location Code Level (EnquirerID Date Name Address DOB Action) EnquirerID Action EnquiryID

39 The final task is to give meaningful names to each group, which are now the tables to go into the database. 0NF 1NF 2NF 3NF Table names CourseID Course Name Length Tutor Location Location Code Level (EnquirerID Date Name Address DOB Action) EnquirerID Action EnquiryID Course Enquirer Enquiry

40 Notation Standard notation starts with the name of the table, followed the fieldnames separated by commas, the primary key field is underlined NAME (FIELD 1, FIELD 2, FIELD 3) Course (CourseID, Course Name, Length, Tutor, Location Code, Level Location (Location Code, Location) Enquirer (EnquirerID, Name, Address, DOB ) Enquiry (EnquiryID, CourseID, EnquirerID, Date, Action)

41 The End The database is now in third normal form
It is possible that the design arising from the two methods differs. It is then up to the database designer to decide which solution to implement.

42 Examples Normalise a bank account Customer ID 43543 Surname Williams
Postcode BJ42 8AD Landline Account Number Account Type Description Balance Gold Savings £4560 Platinum Current £826.42 Silver ISA £3000

43 Example with 2 repeating groups
Applicant ID 2098 Address Case officer ID Name Internal phone Surname Di Canio Landline 231 Ferguson 417 First Name Paolo Date of birth 9/7/1968 Employer Job Start date End date Final salary Swindon County Ground Manager 20/5/2011 18/02/2013 £250,000 Sunderland Stadium of Light 31/03/2013 22/12/2013 £750,000 Subject Level Grade Year English Functional Skills Pass 2011 Sports Coaching Merit 2009

44 Assignment Task 5 (20%): From the coal merchant’s brief:
Identify the attributes for a bottom up model. Normalise the database to third normal form.


Download ppt "Database Design ERD and Normalisation"

Similar presentations


Ads by Google