28
BS1904 Week 8 1 Computer Applications for Business (8) Last week: (except for EM) » Zip file practical » Databases part 1 – Two ways to approach databases: From requirement to hold and access information To map the business’s info requirements on to a computer Microsoft Access Practical – single table with queries This week: Databases part 2 » The Relational Model » Practical: using Microsoft Access as a relational database » Completing Currency Converter (production spreadsheet) » Final Assignment workshop (due this Wednesday)

BS1904 Week 8 1 Computer Applications for Business (8) l Last week: (except for EM) »Zip file practical »Databases part 1 – Two ways to approach databases:

Embed Size (px)

Citation preview

BS1904 Week 8 1

Computer Applications for Business (8)

Last week: (except for EM)» Zip file practical» Databases part 1 – Two ways to approach databases:

– From requirement to hold and access information– To map the business’s info requirements on to a computer– Microsoft Access Practical – single table with queries

This week: Databases part 2» The Relational Model» Practical: using Microsoft Access as a relational database» Completing Currency Converter (production spreadsheet)» Final Assignment workshop (due this Wednesday)

BS1904 Week 8 2

Week 7 Garages Practical Used a single table

» One record per vehicle» Fields chosen to contain key characteristics of car» Field properties ensure they can hold the data

Built, then modified table contents and layout» Should have imported External Data from spreadsheet» Exercise 1(d) involved adding to the table design

How many created queries to select and sort contents?» One criterion is easy to define…» … unless you need to match part of a field» Multiple criteria focus the selection further

We can go on to produce Reports and Forms

BS1904 Week 8 3

Concluding Single Table Practical

Exercise 2 – Creating Queries» Best done with Query Wizard;

prompts you to choose source and which fields to include» Modify the design to apply selection criteria

Selecting – what you put on the Criteria line» One criterion is easy to define…» … unless you need a wildcard – 2(d) and 2(g)» Multiple criteria focus the selection further –

Exercise 3 on p.204– (a) selects when either criterion is satisfied on one field OR – (b) selects when criteria are satisfied in both fields

AND Exercise 4 – producing a calculated field

BS1904 Week 8 4

Advantages of Database Managers

Where you have only one table, it could be stored in a “flat file” using an application like Excel

However, performance can be a problem:» Every record scanned in every sort operation» Multiple scans for complex sorting such as Date-acquired

within Make within Garage » Gets very slow when database size grows» Updates can involve making second copy of entire

database – do you have room on the disk? Database managers (DBMS):

» build indexes rather than sorting the records themselves» save data when it changes, leave most of DB unchanged

BS1904 Week 8 5

PRODUCT CUSTOMERGrommets Bloggs LtdWickets P Vole InnZummats Aardvark CoZummats Bloggs Ltd

CUSTOMER ADDRESSAardvark Co Banbury OX18 2ACBloggs Ltd Fareham PO15 1JBP Vole Inn Heckmondwyke HX13 3PV

Limits of Flat file Databases With a single table, we may need to duplicate data:

Proper DBMS can relate different tables of data, e.g.» Have database of purchases by customer» Also have Customers on Address database» How do we see what’s been selling in PO postcode area?

PRODUCT CUSTOMER ADDRESSZummats Aardvark Co Banbury OX18 2ACGrommets Bloggs Ltd Fareham PO15 1JBZummats Bloggs Ltd Fareham PO15 1JBWickets P Vole Inn Heckmondwyke HX13 3PV

BS1904 Week 8 6

Relational Databases As with simple database, data elements held in tables Also known as “relations” because

» each data element relates the row (record, tuple) it’s in, » with the column (field, domain) that describes its category

Database usually contains several tables, for example» Purchase table saying what each customer has bought» Customer address table

Database Management System lets you manipulate tables» Selecting records» Using one table to look up items in another

We’ll do this in Knight’s Conference exercise (p.220 ff)

BS1904 Week 8 8

Relationships Between Tables

One to One

One to Many

Many to Many

Customers

customer ref

name

address

Credits

customer ref

credit rating

1 1

Customers

customer ref

name

address

Orders

customer ref

order ref

product ref

1 M

Productsproduct refProd-namePrice

Ordersorder refcustomer refproduct refquantity

M

M

BS1904 Week 8 9

Structured Query Language

Relational Database Management Systems (RDBMS) implement SQL (usually pronounced “sequel”)

Examples:» SELECT sales.PRODUCT sales.CUSTOMER

FROM <table & database name>WHERE sales.PRODUCT=‘Grommet’ORDER BY sales.CUSTOMER

» JOIN tables that share a column to produce a new one

PRODUCT CUSTOMER ADDRESSZummats Aardvark Co Banbury OX18 2ACGrommets Bloggs Ltd Fareham PO15 1JBZummats Bloggs Ltd Fareham PO15 1JBWickets P Vole Inn Heckmondwyke HX13 3PV

BS1904 Week 8 10

How Database Managers Help

When you JOIN two tables, there will be redundancy in the table so produced» However, if you’re only interested in SELECTed records,

the DBMS doesn’t actually need to create the full table» This saves space and processing time

Access goes one better, and saves you writing SQL» Can SELECT, ORDER and JOIN graphically,

and using wizards» There is a View SQL option when you need it –

provides an easy way to sort records exactly as you want

We’ll cover this next week

BS1904 Week 8 11

Joining and Selecting

When you join tables, for example with a Query, the result set contains the records that appear in BOTH tables (and only those records)

You can also Select – in Access it uses the “Criteria” line in Design View of the query

Thus the criterion PRODUCT=Zummats would exclude the lines shown in pink from the query results

PRODUCT CUSTOMER ADDRESSZummats Aardvark Co Banbury OX18 2ACGrommets Bloggs Ltd Fareham PO15 1JBZummats Bloggs Ltd Fareham PO15 1JBWickets P Vole Inn Heckmondwyke HX13 3PV

BS1904 Week 8 12

Design Considerations What level of granularity is needed for fields?

» Sometimes fairly obvious, e.g. splitting out Postcode» But how much do you need to chop up a name?

– Surname ; title; rest-of-nameBodger; Mr; Eric Owen MA JP

– Surname ; title; given-name1; given-name2; given-name3; rest-of-name; suffix-lettersClinton; ex-President; William; Jefferson; ; III; MA LlB

» Similarly with addresses» Problems arise if you get it wrong in either direction:

– database can be full of empty fields– or you can’t access the data by the component you want

How do you make primary keys unique?» Often leads to artifices like “Customer numbers”

BS1904 Week 8 13

More Design Considerations

What are the appropriate tables?» If two groups of data are in a 1:1 relationship, it doesn’t

much matter whether you put them in one or two tables» If there’s a one-to-many relationship, it’s clearly beneficial

to separate them, thus avoiding duplication» Where there’s many-to-many relationship, you need to

create a table that bridges between them

Products

product ref

prod-name

Price

Orders

order ref

customer ref

M

Order Item

item #

order ref

product ref

Quantity

11 M

BS1904 Week 8 14

Data Validation

We saw techniques for validating data in the practical» Can reject non-numeric characters in a numeric field» Use lists to help enter members of an enumerated set » Or set ranges for numerical values

Sometimes we need to validate consistency» If car registered in 1992, it’ll probably have J or K prefix

– This is where you think about programming a macro» Best to detect errors at data-entry stage if possible GIGO

Design must define any consistency rules» And mechanisms for enforcing them» Don’t forget overrides where appropriate (examples?)

BS1904 Week 8 15

Database Creation

Once the database design is complete,Need to prime the tables with initial data» May be able to import it from a previous system

(as you did with the Garages table)» Or use skilled people to type direct into tables» If not, build forms to accept data in human-centred way

Complex validation rules may require programming» Inspect multiple fields on form and check consistency» Access supports Macros to do that

Database may also include “generated” fields» We’ll see why later

BS1904 Week 8 16

Forms and Reports Data isn’t much use while it’s locked into a database

» We need to access selected elements to get value Usual approach is to:

» Select records according to specified criteria(or maybe just one criterion)

» Order them as specified» Lay them out to be helpful to user» Generate summaries where appropriate

Further Practical – please do in your own time» You should already have completed Exercises 1 to 4 in

Knight (1999) Chapter 8» Optional: Report design exercises (starting on p.209)» Form design (p.212); Validation rules (p.215)

BS1904 Week 8 17

Search Precision Let’s assume we’re building a simple phone book

» Query takes form of user typing in name» Database system returns all record containing the name

Potential Problems:» Case: If I type “smith” and database contains “Smith” how

do we guarantee a match?» Different spellings: Dupre, Du Pre, Dupré;

MacPherson, Macpherson, McPherson “CALLUP” system generated a hidden, standardized field

» Squeezed out blanks, hyphens, apostrophes etc» Converted Mc to mac» “Folded” all names to upper-case» Did same thing to the target the user typed in

BS1904 Week 8 18

Fuzzy Searching Some databases go further, and apply very strong

transformations, for example:» Remove vowels and repeated consonants» “Patterson” and “Peterson” both go to “PTRSN”» “Smith” “Smyth” and “Smithe” all go to “SMTH”» Can even classify consonants, e.g. S and Z become S

(so “Pieterzoon” also becomes “PTRSN”) Usually search for exact match first, then near match Design choice whether to keep transformed versions

» Keeping them uses disk space,but lets you sort the table for efficient searching

» Generating them on the fly is expensive in processingand doesn’t support efficient searching

BS1904 Week 8 19

Joining Tables To join tables together, they

must share a field » In this example, it is Module

Module is a unique key in the Courses table

Joining Courses and Students gives a table with record for each intersection

Module Title Location

BS1904 ComApp HJB21

BS2911 Network HJB102

BS3909 InfoSys FWB204

Student Name Address Module

0701234 Alice WD12 BS1904

0701357 Bill StJ123 BS1908

0702345 Charles WD19 BS1904

Student Name Address Module Location Title

0701234 Alice WD12 BS1904 HJB21 ComApp

0702345 Charles WD19 BS1904 HJB21 ComApp

Students

Courses

Joined on Module

BS1904 Week 8 20

Relational Database Practical

Knight’s example 7 from p.220 involves linked tables Problem: organizing seminars for conference delegates

» Delegates’ details are held in a table» Seminar details are held in another table» Each delegate can attend one seminar

(but we hope each seminar will attract many delegates!)

DelegatesDelegate-refNameCompanyAddress...Phone numberSeminar-ref

SeminarsSeminar-refNameFromToFee

1

M

Note ambiguity: “Name” can mean SeminarName or DelegateName – these would be better field names

BS1904 Week 8 21

Relational Database Practical (2) Knight’s specification brings a problem:

» How do I write to someone if the name is in one piece?» OK in the window – “Mr H Huntington, BSc”» But we can’t derive “Dear Mr Huntington” from that

So let’s chop the name into components» That’s what I did in the table you can import

DelegatesDelegate-refTitleInitialsSurnameCompanyAddress1...Phone #Seminar-ref

SeminarsSeminar-refSeminar-NameFromToFee

1

M

BS1904 Week 8 22

How to do it – Exercise 7 (a) Design the Seminars table according to instructions

» Be sure to specify Seminar ref as primary key» Enter the data as given

(b) Set up the Delegates table as specified (not quite)» First download spreadsheet and import it to Access table

as External data » Get Access to use delegate refs as a primary key» Fix up the format as specified

(c) Use the Relationships tool to link the tables using Seminar ref» Select “enforce referential integrity” (I’ll explain later)

(d) Finally, produce a query using fields from both tables

BS1904 Week 8 23

Access Summary

Access is a reasonable & inexpensive Relational DBMS» Defines fields in a fairly intuitive way» Lets you work graphically » Provides most relational features» Saves you writing SQL for yourself (but you can View it)

Key terms» Table – where the data is actually kept» Query – view of table(s) manipulated according to rules

you set; joins, selects, sorts Less good for shared databases Good design environment before move to Oracle or DB2

BS1904 Week 8 24

Summary

Data stored is used to model the business» Tables hold the main “players” –

suppliers, products, customers, employees» Applications then work on the appropriate data elements

Avoid duplication by using multiple tables» You’ll learn next year how to design suitable structures» At minimum, separate customer/supplier demographics

from details of their dealings with you Link tables using a data element common to both

» This needs to be unique in one of the tables..» ..where it acts as a key» This is the place to filter to exclude irrelevant material

BS1904 Week 8 25

Optional Database Exercise

These exercises are taken from:Knight, J (1999) Computing for Business

Learn about Forms by doing Exercises 6(a)-6(c) on pp.212-4, and 7(e) on p.222

And about Reports in Exercises on pp.209-211 and 222» Of these, only p.222 is included in the hand-out

BS1904 Week 8 26

Assignment Workshop

Key points: Work out the Business Case for yourself

» Get a cash-flow chart for ideal case and for contingency Then design your report

» Executive Summary– Make the executive want to “buy” your proposal– For example: “If you invest X it will pay for itself

in Y months, yielding an extra profit of Z over six years”– Very efficient way to get marks!

Especially if you back it up with a graph» Body of report

– Where you back up with facts and logic the assertions you made in the Executive Summary

BS1904 Week 8 27

Business Report Structure

Title Page Executive Summary Vital in this assignment Contents always a good idea Introduction Body of Report Conclusions & Recommendations Bibliography Give credit to your sources (Appendix) Not needed here

BS1904 Week 8 28

Marking Criteria

Exec. Summary 20%

Analysis 20%

Spreadsheet 20%

Structure & integration 20%and following the spec

Credibility of Report 20%including accuracy of expression

BS1904 Week 8 29

Currency Converter

Example of spreadsheet used “in Production”

Main learning outcomes: MAX function Use of IF function to cope with empty and zero cells Need to choose solutions – e.g. between MAX and IF VLOOKUP (there’s also an HLOOKUP) Protection of cells and sheets How to indicate protection visually