Category: Structures and links (database)

Higher – Structures and links database (Revision)

A database is a collection of data stored in a structured, organised manner.

Flat file databases

A flat file database has all data is contained in one file (a table or entity).

Data is organised into fields (columns or attributes) and records (rows or occurrences).

Field Types

Basic field types include: text, numeric, date, time.

Other field types include:

  • Object (a container) – can contain graphic, audio, video, etc.
  • Link – contains a hyperlink.
  • Calculation – contains a formula to calculate its contents from other data in the record. Automatic calculation of data reduces the chance of human error.
    • HDB1
  • Boolean – stores Yes/No or True/False.
  • Summary – calculates data based on data from multiple records in database. For example, totals up the contents of a particular field across all records.

Relational Databases

Relational databases have two or more files linked together. Using linked tables reduces unnecessary duplication of data, therefore reducing the opportunity for error.

Good database design avoids data duplication by using linked tables where appropriate and reduces errors in data entry by using suitable validation techniques.

Relationships

There are 3 types of relationships between tables in a relational database.

One-to-one (1:1)

  • One record in the first table can only link to exactly one record in the second table.
  • One-to-one relationships are rarely used, except as a means of splitting up a large, unwieldy table or treating part of a table differently (e.g. keeping some fields secure).
  • Example
    • In a database with a Customer Details table linked to an Account Details table – one customer can only have one account, and vice versa.
    • However, the Customer Details table can be made available to all staff whilst the Account Details table could only be available to senior staff.

One-to-many (1:M)

  • One record in the first table can link to two or more records in the second table.
  • Each record in the second table can only be linked to exactly one record in the first table.
  • Example
    • In a database with a Customers table linked to a Tickets table – one customer can have many tickets, but each ticket may only have one customer.

Many-to-many (M:M)

  • One record in the first table can link to two or more records in the second table.
  • One record in the second table can also link to two or more records in the first table.
  • Many-to-many relationships are usually undesirable in relational database design.
  • Example
    • In a database with a Pupils table linked to a Teachers table – one pupil can have many teachers, and vice versa.

Entity Relationship Diagrams

Relationships can be represented graphically using an Entity-Relationship (ER) diagram.

There are many different notations for ER diagrams, we will use crow’s foot notation.
• The names of entities are written in boxes joined by straight lines.
• At the “many” end the line forks.
• At the “one” end the line it judt joins the box.

ERD

Keys

  • Primary Key
    • A primary key is a field which is a unique identifier for each record.
    • A file may have only one primary key.
    • At the design stage, primary keys are usually identified by underlining.
  • Foreign Key
    • A foreign key is the primary key copied from another file, used to link two files.
    • In order to create a link, the primary key from the “one” entity gets copied into the “many” entity as a foreign key.
    • At the design stage, foreign keys are usually identified using an asterisk.
  • Compound Key
    • A compound key is a key which consists of two or more fields in order to create a unique identifier. This is required when no single field can be used to uniquely identify a record.

Example

Litters	(Litter ID
		Sire
		Dame
		Number in litter
		DOB)

Puppies	(Puppy ID
		Puppy name
		Sex
		Cost of puppy
Litter ID*)

Customers	(Customer name
		Address
Puppy ID*)

There are 3 tables in this database.

•	The Litters table has Litter ID as a primary key.
•	The Puppies table has Puppy ID as a primary key.
•	The Customers table uses Customer name and Address as a compound key.

•	Litter ID is a foreign key in the Puppies table.
This creates a one-to-many relationship between Litters and Puppies.

•	Puppy ID is a foreign key in the Customers table.
This creates a one-to-many relationship between Puppies and Customers.

Field Validation

Using appropriate validation reduces the chance of error when data is input.

Various validation techniques can be used to ensure data is appropriate:

 

  • Presence check – ensures field cannot be left empty, good validation for a primary key.
  • Restricted choice – the user is presented with a list of options to choose from using a drop-down menu, option buttons or similar. This is often used for the input of data for a foreign key, by automatically generating the list of options from the linked file.
  • Length check – ensures an appropriate number of characters is input, e.g. minimum of 8 characters, maximum of 20.
  • Range check – used on numeric fields to ensure number is within certain range e.g. between 0 and 100.

Queries

Queries allows the user to find information in a database.

Users may perform simple queries looking at the contents of one field or complex queries looking at the contents of many fields.

When answering exam questions always state what data is being searched for and which field it should be in.

Examples
To find all the male pupils who are over 12 SEARCH for “male” in the Gender field AND >12 in the Age field
To find all the people who live in Edinburgh or Glasgow SEARCH for “Edinburgh” in the Town field OR “Glasgow” in the Town field
To find all the people born in the 1990s SEARCH for >31/12/89 in the DOB field AND <1/1/00 in the DOB field

 

Sorting

Sorting puts database records in order based on the contents of particular fields.

  • Ascending order goes from A to Z, smallest number to largest number.
  • Descending order goes from Z to A, largest number to smallest number.
Examples
Put the customers in alphabetical order SORT on Surname field in Ascending order then the Forename field in Ascending order.
Find the tallest person in the database SORT on Height field in Descending order then look at the first record in the list.

Reports

Reports allow the presentation of selected data from the database.

The output can be customised in a variety of ways, including displaying only chosen fields and formatting the data in a particular way.

Here is a report which has been produced to show details of all employees who earn more than £30000.

HDB2

The above report has less fields than the full database and also includes a header and summary fields. Changes to the formatting of the Birth Date column and other column headings.

Forms

Database creators can prepare forms layouts specially designed for inputting data, to help improve usability.

A well laid-out form, using suitable techniques such as drop-down menus or checkboxes, reduces the typing required and makes data input easier and faster.

DB3

Data dictionaries

A data dictionary is a design notation used to show the fields required in each table of a relational database, including field types, validation required, etc.

dd

Thank you to C O’Toole & A Madill from Braidhurst High School for allowing me to edit and publish this here.

National 5 – Structures and links database (Revision)

Database structure:

  • field – the fields below; Exhibitor, Company Name, Area, Stand Number, Product Reference, Item Name, Price (£)
  • record – A single row in a table, there are 8 records below
  • file – the entire database

Flat file database

  • flat file all the data is stored in one table
  • DB1
  • Drawbacks Data duplication Data inconsistency or update/ deletion/insertion anomalies Data integrity errors (due to data inconsistency) Inconsistent search results in multi-value fields
  • Flat file databases can lead to errors as shown below

    DB4

    Is it a man John Silver or a woman Joan Silver for member 1034?

Linked tables

  • Table would be split into
    • EXHIBITOR(Exhibitor Code, Company Name, Area)
    • PRODUCT(Product Ref, Item name, Price (£), Exhibitor Code*)
  • primary keys – these are unique identifiers for each row in a table
  • foreign keys – a primary key from a different table

Database operations

  • Simple search – a search on ABC Music would return the following
    • DB2
  • Complex search – Searching on two (or more) fields at once for example Company name = FutureTech and Price > 1500 would return the following
    • DB2
  • Simple sort – Sorting a table by one field, class registers are sorted by surname
    • Allan, Tom
    • Bennet, Gordon
    • Clark, Petula
    • Donaldson, Luke
    • Elliot, Paula
  • Complex sort – same as above but if two people have the same second name they are sorted by first name as well
    • Allan, Tom
    • Bennet, Gordon
    • Clark, Petula
    • Donaldson, Luke
    • Elliot, Paula
    • Ferguson, Alex
    • Ferguson, Sarah
  • This is in an Ascending order

Field types

  • text – A Roberts, ML1 3XF
  • numbers – 124
  • date – 29 April 2012
  • time – 08:30
  • object – Picture, video or sound file.
  • calculated – pay * 20%
  • link – www.bbc.co.uk
  • Boolean – either yes or no

Validation

  • presence check – data must be entered before the user can continue, primary and foreign keys are normally required.
  • restricted choice – please see below, the user can only select one option.
    • DB3
  • Benefits
    • Reduces the chance of human error
    • Does not require the user to type a text response
    • Speeds up the ordering process as inputs are reduced to mouse clicks
    • Allows the use of a touchscreen
    • Good design to avoid data duplication and modification errors (insert, delete, update)

 

HTML – Lists

A HTML list is composed of two types of tag

List Type

This tag tells the browser what type of list we will be displaying. the two main types are

Unordered List <ul></ul>, this list displays bullet point. It is used when you do not want to give a ranking to the list items.

  • Item 1
  • Item 2
  • Item 3

Ordererd List<ol></ol>, this list is numbered. This is used when you want to give a ranking to the pages.

  1. Item 1
  2. Item 2
  3. Item 3

List Item

The list type must be placed around the List Item <li></li>, the browser will then display the items with the format specified from the list type.

Unordered List example

<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>

Ordered List example

<ol>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ol>

HTML – Hyperlinks

The H in HTML stands for Hyper,but what does that mean?

Hypertext is text which can link to other files, this is called a hyperlink. A hyperlink is composed of two parts, the anchor and the destination.

<a href=”http://www.google.com”>Google</a>

In the example above the anchor tag <a></a> is used to set an object (text or image) as an anchor, in this case Google. The href attribute is used to identify the destination of the link, in this case http://www.google.com. This is called an external hyperlink as the destination is on another server. If you were linking to a file on the same server then the internal hyperlink would be.

<a href=”page1.html”>Page 1</a>

The anchor tag can be placed around other HTML elements such as images.

<a href=”http://www.google.com><img src=”googleLogo.png”></a>

This would place a border around the image indicating that it is an anchor.

You can specify where to open the linked document using the target attribute.

Value Description
_blank Opens the linked document in a new window or tab
_self Opens the linked document in the same frame as it was clicked (this is default)
_parent Opens the linked document in the parent frame
_top Opens the linked document in the full body of the window

So if you want the Google link above to open in a new tab you would change it to.

<a href=”http://www.google.com” target=”_blank”>Google</a>