Databases
Databases
1. Database
1. Basic Concepts: Following are the terms that are used for a specific purpose in computer
terminology.
i. Data: It is already defined and described in chapter number 1 of this book
ii. Types of Data: Before we enter data into a computer system, we usually need to tell the
computer what type of data it is. This is because the computer stores data in digital form
(combinations of binary 0 and 1) and processes different types of data in different ways.
data types
Note: Remember that it is a broad and general classification of data type in computer.
Actually, it may differ in different database.
iii. Database: A database is a structured collection of data that allows people to extract
information in a way that meets their needs. The data can include text, numbers, pictures;
anything that can be stored in a computer.
A single-table database contains only one table.
Why are databases useful?
Databases prevent problems occurring because:
• if any changes or additions are made it only has to be done once – data is consistent
• the same data is used by everyone
• data is only stored once in relational databases which means no data duplication.
iv. Table: It is an organized collection of related data in which data is arranged in the form
of columns and rows. It usually consists of a number of separate parts (usually a
completed line) called records. A portion of employees’ details for a large company
given below is an example of a table.
v. Record: A record is a collection of related data items or fields e.g. complete details
about one employee. It is usually one complete line in a table.
vi. Field: A field is a part of a record used to store one particular type of data item. Each
field contains one data item. e.g. employee's name, gender, age, department.
vii. Data Item: A data item is a piece of data that is stored in a field e.g. 21, 43, Accounts,
Taylor B etc.
viii. Key Field (Primary Key): A field of a table that is used to identify a record uniquely. It
contains an item which is unique for every record. This field is called the primary key.
2. Structured Query Language (SQL): It is the standard query language for writing scripts to
obtain useful information from a database. We will be using SQL to obtain information from
single-table databases.
3. SQL scripts: An SQL script is a list of SQL commands that perform a given task, often
stored in a file so the script can be reused.
Only the SELECT and FROM commands are mandatory in an SQL script. All other
commands are optional.
A SELECT statement takes the form:
SELECT Field1, Field2, Field3, etc. – this specifies the individual fields
to be shown.
SELECT * – this specifies that all fields (columns) are to be shown.
SELECT SUM (Field) – this specifies the field (column) for the calculation. The
field should be integer or real.
A COUNT statement takes the form:
SELECT COUNT (Field) – this specifies the field (column) to count if the given
criterium is met.
Example: the following SQL command for the PATIENT single-table database would
provide a list of all Mr Smith’s patients showing the hospital number, first name and family
name for each of his patients.
SELECT HospitalNumber, FirstName, FamilyName
FROM PATIENT
WHERE Consultant = 'Mr Smith';
This SQL command sorts the records in alphabetical order of family name:
SELECT HospitalNumber, FirstName, FamilyName
FROM PATIENT
WHERE Consultant = 'Mr Smith'
ORDER BY FamilyName;
Exercise
1. List three necessary fields for a book record in a school library.
Field 1: _________________________________________________________
Field 2: _________________________________________________________
Field 3: _________________________________________________________
2. A database is used to store details of staff. The format of the first four fields is as
follows.
FIELD NAME DATA TYPE DESCRIPTION FIELD LENGTH
STNAME Text Staff Name 50 characters
STADDRESS Text Staff Address 80 characters
TELNO Text Telephone No. 20 characters
DOB Text Date of Birth 6 characters
a. Complete the boxes below for the next numeric field which could be in the staff
record.
FIELD NAME DATA TYPE DESCRIPTION FIELD LENGTH
Numeric
3. A database, SOFASELECT, was set up to show the prices of suites, sofas and chairs for
sale from an online furniture warehouse. Part of the database is shown below.
Reason: _________________________________________________________
b. Give the data type for the REGISTRATION NUMBER and FINAL COST fields.
REGISTRATION NUMBER: ___________________________________
5. The following shows part of a computer database of cars and their owners. The
database is already loaded into the computer.
Reg. No. Make Model Colour Reg Year Owner Address
AN12BC FORD FIESTA RED 1992 J. SMITH 3 BEAN RD
HI23WSA ROVER 2I4S BLUE 1990 R. PATFI 4 HILLSIDE
ACBIW OPEL MANTA BLAC K 1978 G. A. SAUL 5 HIGH ST
a. Write SQL statement to find and display only the details of the car whose registration
number is WAR127B.
_____________________________________________________________________
b. Write SQL statement to find & display only the owner and their addresses of all cars
registered after 1990.
_____________________________________________________________________
_____________________________________________________________________
c. Police believe that blue car whose registration number starts with DX has been used
in a crime. Write SQL statement to display only the details of the vehicles which
satisfy this description.
_____________________________________________________________________
_____________________________________________________________________
c. Write down the SQL query to display the details of all the male students in surname
order. Details of female students must not be displayed.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
c. Describe two validation checks that should be made on the dates as they are entered
into the file.
Check 1: ____________________________________________________________
____________________________________________________________
Check 2: ____________________________________________________________
____________________________________________________________
_____________________________________________________________________
e. A printed report of this search is needed. The report must only contain the last three
fields. Write the SQL statement to achieve this.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
2: __________________________________________________________________
3: __________________________________________________________________
9. A school office sells stationery items to pupils. A database is used to keep details of
the stationery stock. Table(INVENTORY) of the database is shown below.
CODE ITEM INSTOCK SOLD PRICE($)
C301 Floppy Disk 24 16 11.5
G101 Rubber 16 14 1
G102 Pen 18 22 2
G103 Pencil 5 225 1
M101 Protractor 8 12 1.5
M202 Compass 8 10 2
Field 2: _____________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
10. A mail order company selling computers keeps details of its stock in a database.
Table(STOCK) of the database is shown below.
COMPUTER PROCESSOR RAM HARD DISK CD PRICE($)
CA200 P200 16 2 GB 4 6405
CA201 P200 16 2.5 GB 0 3397
CA233 P233 32 3 GB 6 6589
CA266 P266 32 4 GB 8 10679
CA267 P266 32 4 GB 10 12895
CA166 P166 16 1.5 GB 0 3019
_____________________________________________________________________
c. Write down the SQL query to show order of the COMPUTERS after the PRICE($)
field sorted in ascending order.
_____________________________________________________________________
_____________________________________________________________________