Unit 4 Normalisation and Relational Database Management Systems

Unit 4 Normalisation and Relational Database Management Systems

Files, records and fields Information in computer-based filing systems is stored in data files. A file is a collection of related records. Related records means that each record in a file will contain the same sort of information as all the other records. A record must have at least one field. A field contains one individual item of data.

Computerised databases A database is a organised collection of related data. Use a database when you need to store lots of data Key Field In this table, each column is a different field Each row is a record Data is organised into FIELDS and RECORDS. The KEY FIELD contains an item of data that is UNIQUE to that record.

FLAT-FILE or RELATIONAL? A database is a structured collection of related data. A Flat-file database has all the data organised into ONE table. Many modern databases are described as being relational. A relational database stores data in tables that are linked together using common fields.

Duplication in Flatfile Members No Forename Surname Address 1 Address 2 Tel No Video No 122 Evans Daniel 7 High St Aberaeron 01545588999 343 759 Lees Anna 2 Bridge St Lampeter 01570422967 523 122 Evans Daniel 7 High St Aberaeron 01545588999 455 Title Certificate Category Crash 12 Emma War of the Worlds Cost per day Date loaned Length of loan Date due Total cost Comedy £ 3. 00 12/09/05 1 day 13/09/05 £ 3. 00 12 Drama £ 3. 00 12/09/05 1 day 13/09/05 £ 3. 00 PG Sci FI £ 3. 59 14/09/05 2 days 16/09/05 £ 3. 50

Primary Key Foreign Key Loans ID Primary Key Linked data tables in a relational database

Normalisation

First Normal Form (1 NF) A table is in first normal form if all the data values are atomic values In English, this means that there can only be one value per attribute – The following table records managers of shoe shops. One manager can be a manager of more than one shop This is not in 1 NF because each Manager Shop attribute does not contain a single Shaw Gloucester, value. For example, Shaw is Bristol manager of Gloucester and Bristol. Jones Trafalgar To be in 1 NF these values need to Smith Ashford, be separated Greg Canterbury Brighton, Hove

First Normal Form (Cont. ) Ø Manager Shop Shaw Gloucester, Bristol Jones Trafalgar Smith Ashford, Canterbury Greg Brighton, Hove This is in 1 NF because each cell has only 1 value This is not in 1 NF because the values are not atomic Manager Shop Shaw Gloucester Shaw Bristol Jones Trafalgar Smith Canterbury Smith Ashford Greg Brighton Greg Hove

First Normal Form - Difficulties Terminology differences: – Atomic – Indivisible – Scalar Attributes to be careful of include: – Address – e. g 24 The Grange, Oxford. OX 4 6 JP is not scalar – Telephone – e. g 01234 567890 is not scalar (code and number) – Atomic does not only refer to the data, but to the use of data. Just because you can split data does not means that you should. Telephone should only be split if you need to sort by code for example

Terminology: Primary Key A primary key is a unique value which allows each record to be identified Ø Customer. ID First. Name Last. Name 1 Brian Smith 2 Harry Adams 3 Joe Jones 4 Harry Smith First. Name or Last. Name cannot be primary keys as they contain duplicate and un-unique data. Customer. ID uniquely identifies a row and is therefore suitable

Sometimes there is no single field appropriate as a primary key. In these circumstances, it is possible to select two fields which, when taken together create a unique value: Order. No Item. No Employee. No Customer. No Item. Name Quantity 121 3 4 1024 Nut 4 121 4 4 1024 Bolt 3 122 8 9 176 Washer 6 123 3 6 154 Bolt 5 123 8 6 154 Washer 4 There are no unique fields, so the Primary Key is best suited by Order. No and Item. No taken together

Functional Dependence Functional Dependency – If you know the length, height and width of a room, you can calculate its volume: Volume = width x height x length – Volume is functionally dependent on the length, height and width – Consider the following database, which holds orders. Each order is entered by a specific employee (only one employee is allowed to enter an order)

Functional Dependency Order. No Item. No Employee. No Customer. ID Item. Name Quantity 121 3 4 1024 Nut 4 121 4 4 1024 Bolt 3 122 8 9 176 Washer 6 123 3 6 154 Bolt 5 123 8 6 154 Washer 4 Employee. No is functionally dependent on Order. No functionally determines Employee. No Order. No is the determinant The relationship is only one way

Functional Dependency – More examples – A table containing Pupil. Name and Pupil. ID Pupil. Name is functionally dependent on Pupil. ID Ø Pupil. ID Pupil. Name 34 Smith 65 Sams 87 Hodd 654 Smith If you know the Pupil. ID you can find any Pupil. Name, but if you only have the name, it is not always possible to find the Pupil. ID

Second Normal Form (2 NF) To be in 2 NF a table must: – Be in 1 NF (obviously) – Have all non key fields fully functionally dependant on the primary key In English: – A non key field is one that is not part of the primary key – It means that you need to use the primary key to determine the value of the other fields in the table If you can find the value of other fields without using the primary key, you should remove that field from the table and place it in a separate table

Second Normal Form Order. No Item. No Employee. No Customer. ID Item Name Quantity 121 3 4 1024 Bolt 4 121 4 4 1024 Washer 3 122 8 7 176 Nut 5 122 3 7 176 Bolt 4 This table is not in 2 NF – The primary key is Order. No and Item. No (combined) – The quantity is functionally dependant on the Primary Key The Item. Name is functionally dependant on the Item. No not the primary key The Customer. ID is functionally dependant on the Order. No, not the Primary Key The Employee. No is functionally dependant on the Order. No, not the primary key

Second Normal Form (Cont. ) Order. No* Item. No* Employee. No Customer. ID Item. Name Quantity 121 3 4 1024 Bolt 4 121 4 4 1024 Washer 3 122 8 7 176 Nut 5 122 3 7 176 Bolt 4 We need to remove Item. Name, Customer. ID and Employee. No from the table – this involves setting up new tables

Second Normal Form (Cont. ) Employee. No and Customer. No are functionally dependant on the Order. No, therefore they are suited for a new table: Ø Ø The Quantity is functionally dependant on both the Order. No and the Item. No, therefore they have a new table: We are left with the Item. Name. This is functionally dependent on the item number only, therefore a new table is required: Orders Order. No* Employee. No Customer. No Order. Spec Order. No* Item. No* Quantity Stock Item. No* Item. Name

Second Normal Form (Cont. ) Do not be afraid to create new tables as appropriate but make sure that you are not breaking tables down for the sake of doing so. It should be appropriate and have advantages Make sure that each table you create is in both 1 NF and 2 NF as appropriate Ensure the original table can be rebuilt from the data contained in the new table

Second Normal Form A slightly different approach: – Each relation should only contain information about a single entity. If it contains information about more than one entity, then the table needs to be broken down – For example: HOUSE(House. Name, Street, Town, City, Postcode, City. Population) – The City. Population is a separate entity to the house details, so it needs to be removed to a separate table – To create a separate table, remove the attribute and a copy of the attribute on which it is dependent. The copy becomes the link between the two tables HOUSE(House. Name, Street, Town, City, Postcode) CITY(City, City. Population)

Third Normal Form (3 NF) To be in 3 NF a table must: – Be in second (and therefore also first) normal form – Have all non key fields non transitively dependent on the primary key In English: – Fields which do not form part of the primary key must always be solely dependent on the primary key and not on anything else, such as another non key field – Aside: it is usually difficult to create a table which is not in 3 NF – usually you will jump straight from 1 NF to 3 NF!

Third Normal Form (Cont. ) In the following example, the company keeps data on its employees. Each employee is allocated a city where they work and each city is given a City. ID. Their salary is dependent on the type of job they do. Initially check the table below to ensure it is in 2 NF before continuing Employee. No Last. Name First. Name City. ID Type Salary 1 Taylor Sarah Canterbury CB Manager £ 22, 000 2 Jones Sam London LN Sales Person £ 15, 000 3 Smith Sally Birmingham BM Admin Assistant £ 13, 500

Third Normal Form (Cont. ) Employee. No Last. Name First. Name City. ID Type Salary 1 Taylor Sarah Canterbury CB Manager £ 22, 000 2 Jones Sam London LN Sales Person £ 15, 000 3 Smith Sally Birmingham BM Admin Assistant £ 13, 500 City. ID is dependent on City Salary is dependent on Type Therefore this table is not in 3 NF

Third Normal Form (Cont. ) Remove City from the table and create a new cities table Remove Salary from the table and create a job type table Staff Employee. ID* Last. Name First. Name City. ID Type. ID Cities Job. Types City. ID* Type. ID* City Salary
- Slides: 25