Flat File Vs. Relational Databases

advertisement
Database Unit Workbook
Table of Contents
For the Exam: ...................................................................................................................................... 1
Introduction to databases: ............................................................................................................... 2
Flat File Vs. Relational Databases .................................................................................................... 2
The Database Management System............................................................................................... 3
Database Design Tools ...................................................................................................................... 3
Entity Relationship Diagrams ..................................................................................................................................................................... 4
The worked example: ............................................................................................................................................................................... 5
Homework: More Examples .................................................................................................................................................................. 6
Example 1: Unispares Orders ................................................................................................................................................................... 6
Example 2: Unispares Invoices ............................................................................................................................................................... 7
The Data Dictionary ........................................................................................................................... 8
Normalisation ...................................................................................................................................... 8
Understanding Normal Forms............................................................................................................................................................... 8
The normalization example:.................................................................................................................................................................. 9
Un-Normalised Form ....................................................................................................................................................................................... 9
First Normal Form ............................................................................................................................................................................................... 9
Second normal Form:.................................................................................................................................................................................... 9
Third Normal Form .......................................................................................................................................................................................... 10
Exercises: ......................................................................................................................................................................................................... 10
Unispares Orders ............................................................................................................................................................................................. 10
Unispares Invoices .......................................................................................................................................................................................... 10
Final Thoughts on Normalisation...................................................................................................................................................... 10
Building the Database ..................................................................................................................... 11
Examples ......................................................................................................................................................................................................... 11
Database Architectures .................................................................................................................. 12
Additional Notes .................................................................................................................................. 13
For the Exam:





describe flat files and relational databases, explaining the differences between
them;
design a simple relational database to the third normal form (3NF), using entityrelationship (E-R) diagrams and decomposition;
define, and explain the purpose of primary, secondary and foreign keys;
describe the structure of a DBMS including the function and purpose of the data
dictionary, data description language (DDL) and data manipulation language
(DML);
use SQL to define tables and views, insert, select and delete data and to produce
reports
Mr B Gristwood, 2012
1
Database Unit Workbook
Introduction to databases:
Databases are probably the most fundamental thing in computers, they influence every
aspect of our everyday lives and there are many jobs that focus on the maintenance and
use of the database, they even come with their own programming language!
Define a database
Sate some places where you would find a database:
Flat File Vs. Relational Databases
There are two main types of database, flat file and relational, they both have their own
distinct advantages and disadvantages and when you do some research it will become
clear which one is the most important and used most often in computers
Define: What is a flat file database
Define: what is a relational Database
In the space below, note down the differences between the two.
Flat File Databases
Relational Databases
Can you think of an example of when you would use each?
Which do you think is the most important and why?
Mr B Gristwood, 2012
2
Database Unit Workbook
The Database Management System
What is the purpose of a DBMS:
What features does a DBMS offer:
Database Design Tools
There are a number of database design tools that we can use in order to make sure that
we do it correctly.
Just like programming, using one of the tools will provide you with a better solution than if
you just hacked it, but using a combination of all the tools will give you an efficient
solution that someone will pay loads of money for!
When designing databases, like all products, we have to design it first. Datbases come
with a number of tools that can be used in order to make sure that the databases works
as efficiently as possible.
In the space below, note down some of the problems that could happen if a database
was not designed before being produced
What issues are there with poorly designed databases?
Mr B Gristwood, 2012
3
Database Unit Workbook
Entity Relationship Diagrams
The entity Relationship Diagram is a useful tool for making databases
What does an ERD do?
There are a number of key definitions when learning about Entity Relationship Diagrams
Define: Entity
Define: Attribute
What is the difference between a Primary and Foreign Key?
What are the 3 different relationships in an entity relationship diagram? Draw
them
Relationship 1:
Diagram:
Relationship 2:
Diagram:
Relationship 3:
Diagram:
Mr B Gristwood, 2012
4
Database Unit Workbook
What is the overall aim when designing an entity relationship diagram? WHY?
The worked example:
You are charge of setting up a new database for the school system. You know that there
are 5 major Entities in the model. These are:
•Classes
•Teachers
•Students
•Grades
•Rooms
IN the space below Draw the ERD for this model, Detailing the Primary and foreign keys for
each table
Diagram
Justify how this diagram works (sometimes it makes it easier to write out the relationships
as well as drawing the diagram)
Mr B Gristwood, 2012
5
Database Unit Workbook
Homework: More Examples
Below is a number of extra examples, your homework is to finish the extra examples and
bring the completed diagrams to next week’s lesson.
Example 1: Unispares Orders
You have been provided with a sample purchase order form for Unispares. In the space
below, detail the Entities and the primary key for each of the fields.
Unispares Order Form
Unispares Computer Ltd
99 turton brow, Bolton, BL2 3BP
Tel: 01642 123456 Fax: 01642 654321
Unispares Purchase order No: 002594
Supplier Name:
Selwood Consultancy LTD
Address:
Kingsdale House
Marintet Road
Thornaby
Cleveland
TS17 0BB
Order Date: 15.12.12
Order Details:
Part#
CPU012
PR16214
MON023
CON061
CPU015
KEY031
CPU072
Part Description
Pentium 100 16/1000
HP Deskjet 660C
Phillips 17” Colour Monitor
HP Deskjet 660C Cartridge
Pentium 166 32/2000 CD
Extended Keyboard
Pentium 120 16/1000
QTY
2
3
1
4
1
10
2
Payment Terms: COD/7Days/30Days/45 Days
In case of queries, please contact: FRED BLOGGS
Ext:321
Describe the entities, primary and secondary keys
Draw the Entity Relationship Diagram
Mr B Gristwood, 2012
6
Database Unit Workbook
Example 2: Unispares Invoices
Unispares Invoice
Invoice No: 002594
Invoice Date: 15.12.12
Customer Name: Canon Slade School
Cust. Address:
Bradshaw Brow
Bolton
BL2 3BP
Cust Contact: Ben
Delivery Date: 16.12.12
Cust PO:
09123
Delivery Area: 4
Staff#: 12
Staff Name: Fred Bloggs
Invoice Details:
Part#
CPU012
PR16214
MON023
50.00
CON061
CPU015
KEY031
100.00
CPU072
Part Description
Pentium 100 16/1000
HP Deskjet 660C
Phillips 17” Colour Monitor
QTY
2
3
HP Deskjet 660C Cartridge
Pentium 166 32/2000 CD
Extended Keyboard
4
1
Pentium 120 16/1000
2
Unit£
200.00
150.00
1
Total
400.00
450.00
50.00
25.00
800.00
10
100.00
800.00
10.00
500.00
Nett £
VAT £
Gross £
1000.00
2900.00
450.00
3350.00
Terms of Business: COD/7Days/30 Days/45 Days
Mr B Gristwood, 2012
7
Database Unit Workbook
The Data Dictionary
In programming we have already covered the purpose of the data dictionary and what it
can be used for. In databases, due to the word “data” in databases, Data dictionaries
are indispensable.
What is the purpose of the data dictionary?
What kind of information do you find in a data dictionary?
Look for examples of data dictionaries in your programming code work.
Normalisation
Normalisation is the compliment to the ERD. Using one of these design tools will work, but
in order to make sure your database is correct.
Making mistakes in the design stage of any project will cost you time, with databases
even missing off one field from a table will cost you 3 hours later on, and if you get to
implementation and miss it off, that could cost you thousands of pounds(take it from a
man who has learned the hard way!)
Define the process of normalization
When normalizing work, it is important to work on levels. By breaking down data one step
at a time, it makes it less likely that you will make a mistake.
Understanding Normal Forms
First Normal Form:
Second Normal Form:
Third Normal Form:
Mr B Gristwood, 2012
8
Database Unit Workbook
The normalization example:
Suppose we have a manual file of customer orders, like this:
Customer Order
Customer No: 12345
Name: Gristwood
Address: Canon Slade
Date: 01/01/2012
Slalesperson No. 01
Salesperson Name: Fred Bloggs
Item No
Description
QTY
Price
0001
16mb SIMM
10
50.00
0004
32mb DIMM
2
100.00
Order Value 700.00
Un-Normalised Form
Un-Normalised form is a single talbe of data holding a list of all the fields in the order sheet
Write out all the names of the fields with any of the repeating information in brackets:
First Normal Form
To make improvbements in our table we must remove any repeating groups. This is
referred to as First Normal form.
First Normal Form:
Second normal Form:
To make improvements in our table we must remove any partial key dependency(this is
where it gets complicated) Go through each of the fields and ask: “do you have to use
the primary key to access this information?”
“the key, the whole key and nothing but the key”
Second Normal Form
Mr B Gristwood, 2012
9
Database Unit Workbook
Third Normal Form
This is about removing any Non-key dependancies. Identify fields that are related to other
fields, but not on the key. Move these to a new table.
Third Normal Form
Exercises:
Look at the entity relationship examples for Unispares and make a normalization example
for them. Show the Un-Normalised, first, second and third normal forms.
Unispares Orders
Un-Normalised Form
First Normal Form
Second Normal Form
Third Normal Form
Unispares Invoices
Un-Normalised Form
First Normal Form
Second Normal Form
Third Normal Form
Final Thoughts on Normalisation
Normalisation is probably one of the hardest concepts to teach, like most things, there is
no right or wrong answer, it is how you justify the answer you have been given
Mr B Gristwood, 2012
10
Database Unit Workbook
Building the Database
Once we have designed a database, we need to build it. We are used to building a
database using graphical interfaces(such as Access). But as you know, all computer
systems have to be programmed. To do this we use a language called SQL. Every
database is based on SQL, they just have a prettier user interface on top of them.
What does SQL stand for and describe how we use it
SQL has 2 different parts to the language. The Data Description Language and the Data
Manipulation Language
What is the purpose of the Data Description Language (………)
What is the purpose of the Data Manipulation Language( …….)
Examples
Using your knowledge of SQL complete the examples below, detailing what each of the
commands do(or writing your own) and describing wether this is part of the Database
Description Language or the Database Manipulation Language
Below are a number of SQL queries, what I want you to do is to write in the box on the left
what each of the queries does.
SQL Query
SELECT population FROM bbc
WHERE name = 'France'
What it Does
Selects the population field from the bbc
database where the name field is equal to
France.
SELECT name, population/area FROM bbc
WHERE area > 5000000
SELECT name , region
FROM bbc
WHERE population < 2000000
AND gdp > 5000000000
INSERT INTO t_peep VALUES (2, 'gordon');
UPDATE t_peep
SET name = 'andy', id=39
WHERE id=1;
Mr B Gristwood, 2012
11
Database Unit Workbook
SELECT * FROM t_peep
DELETE FROM t_peep
WHERE id=2;
SELECT * FROM nobel
WHERE yr = 1970
AND subject IN ('Cookery', 'Chemistry',
'Literature')
Join – links 2 tables together
SELECT who, country.name
FROM ttms JOIN country
ON (ttms.country=country.id)
WHERE games = 2000
INSERT INTO t_peep (id, name) VALUES (99,
‘bob’);
Select someone’s firstname and surname
from the table patients where their
patientid is 1336 and their postcode is bl2
3bp
Insert a product into the products table. The
fields are Id, name, description, price. The
product is up to you.
Database Architectures
Databases are like onions, they have layers (amazing how many times that works!) Just
like a network operating system they have different levels of access and these different
levels provide a different options, these are called “Schema’s”
In the space below detail the 3 different types of schema in order of access:
1)
Description
2)
Description
3)
Description
Mr B Gristwood, 2012
12
Database Unit Workbook
Additional Notes
Mr B Gristwood, 2012
13
Download