You are on page 1of 10

Databases

In this chapter you will learn how to:


★ understand what a database and its elements
★ understand flat-file and relational database structures
★ understand primary and foreign key fields
★ understand the advantages of relational databases
★ understand data types and subtypes
★ create a flat-file database from an existing data file
★ create a relational database by creating relationships between tables
★ set appropriate data types

1
Databases
A database is an organised collection of data. A database program is software which stores and retrieves data in a structured
way. This includes the data that is stored and the links between the data items.
A field is a single item of data, such as a forename or date of birth. Each field has a field name that is used to identify it within
the database. Each field contains one type of data, for example numbers, text or a date.
A record is a collection of fieldsv where all the information corresponds to one object like a person or an item or a car. These
may contain different data types.
A file (in database terms) is an organised collection of records, usually where all the records are organised so that they can be
stored together. A file can have one or more tables within it.

Flat-file databases - A flat-file database stores its data in one table, which is organised by rows and columns. For
example, in the following database about teachers, each record (row) in the table contains data about one person.
Each column in the table contains a field, which has been given a field name, and each cell in that column
has the same, predefined data type. See below a part of the flat file:

2
Databases
Relational databases
A relational database stores data in more than one linked table, stored in a file. They are designed so that the same
data is not stored many times. The tables within a relational database are linked with relationships.
Key fields - Each table within a relational database will have a key field. The relationships linking the tables use these
key fields.
Primary and foreign key fields
Most tables will have a primary key field that holds unique data (no two records are the same in this field) and is the
field used to identify that record.
A foreign key field in one data table stores values from a primary key field in another table. They are only used to link
the two tables. From the previous table, we divided into two tables:

These two tables are linked with a ‘one-to-many’


relationship, because one teacher’s record is linked to many
students’ records. The primary key fields (which must
contain unique data) are the Student_ID and Teacher_ID.

3
Databases
Advantages using relational databases
• Internal memory and external storage space is saved by not storing data more than once.
• Records/data can be added, edited or deleted to individual tables.
• As data is not repeated in a relational database, each change to an item of data or to a record has to be made only
once.
• It is also much easier for users to produce reports from a relational database, where data is held in two or more
tables, than from two or more flat-file databases.

Datatypes and subtypes:


Alphanumeric/Text/Short Text 
• Data can store alpha characters (text) or numeric data (numbers) that will not be used for calculations.
• Telephone number is text as it can begin with a zero, it can have any special character/space within and they
are only for representation and not for calculations.
• All numbers which are representations (Credit card Number, Bank Account Number, Bar code numbers) are
all Text

A numeric data type which stores numeric values which are ued for calculations. This does not include numeric data
such as telephone numbers, which should be stored in an alphanumeric data type. These include
– integer which stores whole numbers. (less than 32000)
– long integer stores long integer values
– decimal/double which allows a large number having decimal places. This is one which is not a currency like
length, height, volume, area etc
– currency, which will allow currency formatting to be added to the display. This includes currency
symbols and regional symbols. The database does not store these symbols as this would use up valuable
storage space
– date and time, which stores a date and/or time as a number.

A Boolean (or logical) In Access data type stores data as 0/-1 but can display it as Yes/ No (or True/False, 0/1) but in
general it stores values which have only two possibilities.
4
Data Verification:
Databases
1) To check whether the data has been typed correctly.
2) What ever was supposed to be typed has been typed or not.
3) It is also to check whether data has been accurately copied from one medium to another but it does not
check that data is correct.
4) Verification can be used to help make sure that the data in your database contains as few mistakes as
possible.
5) It can be carried out by the user or even by the by the computer.

The two methods are


a) Visual Verification: Read through the data after it has been typed. This is done by the user. It is quick but not
that accurate as the user may overlook. (example typing an account number, email id, student information in
the database)
b) Double entry: Data has been typed twice and the software compares the two sets of data. (Example:
Password)

Data Validation:
 To check whether the data is sensible, reasonable or acceptable to the data type for a given field.
 If original data is incorrect it will still be incorrect after it has been copied accurately
 Validation is always carried out by the computer.
 Validation helps in keeping clean, correct and useful data for processing.
 It if the process of checking the data against the set of validation rules which you set up when developing your
new database

5
Data Validation:
Databases
1. Length Check is used to check whether the data is of exact number of characters / digits.
Example : AE021 , The centre number is only 5 characters. Not more not less.
2. Format /picture Check – is used to check whether the data is of the given format.
Example : AE021, The centre number is only 5 characters. Out of which first two letters for the country and the
next 3 digits for the centre number.
3. A range check - is used with data consists of numbers, currency or dates/times. To check whether the data is
within the range.
Example: day of the week is between 1 and 7 or also can be given as Day >=1 and <=7
4. Limit check : Only one boundary of range is known . Example: Cost of book is not less than 20 >=20
5. Type Check is used to ensure data is of the correct data type
Example : only numbers are allowed. Can not type 1oo instead of 100.\
6. Presence check to ensure that the field is not left blank. Example : password.
7. Boolean Logical data (invalid character) check : Only one of the two values are allowed. (yes/no),
(true/false) (On/Off) in Access. But in real life has only two values and you choose one value.
8. Lookup list: (choose one from more than two values )
- speeds up data entry because it is usually much faster to pick from a list than to type each individual entry
- improved accuracy because they reduce the risk of spelling mistakes
- limit the options to choose from by only presenting the required options
9. Check digit The computer will perform a complex calculation on all of the numbers and then
compare the answer to the check digit. If both match, it means the data was entered correctly.
Example : ISBN 1 84146 201 2

6
Databases
Q(1) A school library has a file for storing details of the books it has and a file for storing details of its borrowers. The
two files are linked using a common field.

(a) How many records are there in the book file?


(b) How many fields are there in the borrower file?
(c) What type of database do these two files form?
(d) Give two reasons why this type of database system is used rather than having two flat files.
(e) Which field is the primary key in the Book file?
(f) Which field is the foreign key?
(g) Which field has the data type currency?
(h) What data type would be most appropriate for the Due Back field?

7
Databases
Q(2)An airline keeps a database of all the flights it operates. A short extract is shown below.

(a)Explain fully, what is meant by the following validation checks using the extract above.
Range check:
Format check :
Length check :

(b)Using the extract above, give an example of one field which would contain:
Text data: Numeric data :

Q(3)The manager of a computer shop uses a database to store data about the computers he sells. The part of the
database is shown below.
(a) The records shown are to be sorted in ascending order of Makers_name then ascending order of Processor_type.
Give the Processor_type of the last record in the database after it has been sorted.
(b) Give the field that could be amended to contain Boolean data.
(c) Name and describe the most appropriate validation check other than a length check which would be carried out on
the Processor_type field.

8
Databases
Q(4)Many database software packages have a report generation feature. The following is part of a video hire shop
database showing films available in Blu-ray format.

(a) Each of the following fields contains data that is numeric. Give a more precise data type
for each of these fields.
Price ………………………………………………………………………… Length ……………………………………………………………………….
(b) Blu-ray discs have one of three region codes or are region-free. Describe how the field Regions could be set up to
reduce the possibility of inaccurate data entry.

Q(5)Just-discs Limited stocks many music CDs. Below is a small selection of CDs stored on their database. The data has
been sorted on two fields. Part of the database is shown below:

a) Write down the field which was used as the primary sort in the database and the order in which it was sorted.

a) Write down the field which was used as the secondary sort in the database and the order in which it was sorted

9
Databases
Q(6)Enrique has developed a new database system for a supermarket. Below is one record from the database. He
needs to test the validation checks that he has developed for the system.

a) A length check is set for the Barcode field. However, when he entered the code 5012472141308 by mistake, the
system still accepted it. Describe the check he could have used to prevent this error.

a) The Reorder_level for each item is never more than 100. He developed a validation check to make sure this is the
case. However, when he entered the number 100 it was rejected. Name and describe the validation check he
should use and explain why the number could have been rejected.

a) He also designed a length check for the Supplier_code field. However, when he entered EZQ23 by mistake, the
system still accepted it. Describe how he could have prevented this error.

d)For each field write down the most appropriate data type. For any numeric field specify the type of number.
Barcode ……………………………………………………….. Price ………………………………………………………..
Supplier_code ……………………………………………. Reorder_level ……………………………………..
Contents ……………………………………………………. Weight(kg) ……………………………………………..

10

You might also like