0% found this document useful (0 votes)
220 views42 pages

SG10 ICT Chapter9

The document discusses databases, including defining a database, advantages of databases, features of databases including minimal data redundancy and data consistency, and differences between manual and electronic databases. It also introduces relational databases as a collection of related tables.

Uploaded by

Rislan Mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
220 views42 pages

SG10 ICT Chapter9

The document discusses databases, including defining a database, advantages of databases, features of databases including minimal data redundancy and data consistency, and differences between manual and electronic databases. It also introduces relational databases as a collection of related tables.

Uploaded by

Rislan Mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

9

Database

In this chapter you will learn;

² definition of database
² advantages of using a database
² special characteristics of a database
² using fields to create tables
² identifying key fields
² relational databases
² relationships between tables
² using queries to get data
² using data forms
² using reports to furnish information

9.1 Database Concept

Chapter one included a description on what data and information are. This
chapter covers saving data in an organized manner.
Higher the amount of data it becomes difficult to keep in our memory. On
such occasions writing these data is one method. That is called manual database.
The other method is to maintain the data on an electronic medium. This is called an
electronic database. Data can be retrieved efficiently when needed if it is saved on a
database in a systematic way. Hence, the database is needed to save a large amount
of data and to retrieve them very efficiently.

251
For free distribution 251
251
Examples
It would be easy for you to remember names of your friends, but it would be difficult
to retain all their phone numbers in your memory. You can get these details easily
when needed if you write names and phone numbers in a book (Figure 9.1) or save
them in a mobile phone (Figure 9.2). Thus, the phone book can be considered a
simple database.

Figure 9.1 – telephone numbers (manual) Figure 9.2 – telephone numbers (electronic)

Defining a Database - Collection of organized data can be defined as a database

9.1.1 Advantages of Databases


² More efficient in retriving information.
Required information can be found very quickly as data is properly
organized in tables in a database.
²² Easy to obtain copies.
Electronic databases can be copied very easily.
²² Smaller physical space to store data.
Though more physical space is needed to maintain a manual
database, a small space is sufficient to save data on an electronic medium.
²² Data analysis can be carried out efficiently.
Only the necessary data can be selected and processed.
²² Data can be shared.
The same database can be used in different application software.
²² Independence of data.
Though database and software are connected, database
tables can be updated without changes to the application software.
Therefore, data and software are independent.

252
252 For free distribution
9.1.2 Features of a Database
• Minimal data redundancy

If the same data is stored in several tables, this is called data redundancy.

For instance, the name of a student may be recorded separately in the
admission card and the library membership card. When a name is changed, that
name should be edited separately in all documents. Given below is an example
where the name ‘Firosa Lai’ is changed as ‘Firosa Rafeeq’.

Table 9.1 - Admission table


Student Name Date of Birth Date of Name of the
Number Admission Guardian
100 Manoj Dayarathne 2$9$2008 1$1$2014 G. Dayarathne
101 Firosa Lai Rafeeq 4$2$2008 1$1$2014 K. Fais

Table 9.2 - Library table
Student Name Name of the Book Borrowed Date Return Date
Firosa Lai Rafeeqa Folk Songs 2$9$2014 16$9$2014
Manoj Dayarathne Drama 3$9$2014 17$9$2014
Firosa Lai Rafeeq Basic Maths Concept 3$9$2014 17$9$2014
Manoj Dayarathne Drama 4$9$2014 18$9$2014

However, by storing related database in tables, data redundancy can be


minimized. In the following table, a student name is recorded only in one table.

253
For free distribution 253
253
Table 9.3 - Relational Database
Student Name Date of Date of Name of the
Number Birth Admission Guardian
100 Manoj Dayarathne 2$9$2008 1$1$2014 G. Dayarathne
101 Firosa Lai Rafeeq 4$2$2008 1$1$2014 K. Fais
Student Book Borrowed Return Date
Number Number Date
101 2222 2/9/2014 16/9/2014
100 3333 3/9/2014 17/9/2014
101 1111 3/9/2014 17/9/2014
100 3333 4/9/2014 18/9/2014
Book Title Author’s Name
Number
1111 Basic Maths Sivathasan
Concept Ramanathana
2222 Folk Songs Jayantha Bandara
Drama Padmamali Menike
3333
² Data consistency : By minimizing data redundancy, the same
data being saved in various tables can be reduced or prevented. This enables
to maintenance of data consistency.
If you consider the example above, student data, data about books and
usage of books are saved separately in different tables. As such data such as
name of a student or a book should be recorded only once. Thus, data consistency
is maintained.
² Increases efficiency : Since database tables are properly organized, saving
and reading data can be carried out efficiently. Hence, efficiency is higher.
As per example above, let us consider the change of name from Firosa
Lai to Firosa Rafeeq. If the same data (Firosa Lai) are recorded in different
tables, more time is spent to find all these places. This leads to inefficiency.
However, in related databases data consistency increases speed. Hence,
efficiency is increased.
² Increases accuracy : Since data redundancy can be minimized in a database, data
consistency is maintained. This increases accuracy.
Let us consider the example above where Firosa Lai is changed to Firosa
Rafeeq. If several places are corrected, one mistake would lead to inaccurate
information.

254
254 For free distribution
However, in the above database only one table is need to be updated to
change Firosa Lai to Firosa Rafeeq. This ensures data consistency. Therefore,
the accuracy of data is very high.

² Increases validity : Properties of fields can be assigned when a database


is planned. Validity can be checked at the data entry stage. This increases the
validity of the database.
For instance, field attributes can be imposed as fees to be indicated in
two decimals with minimum fees being 1000 which is mandatory.

Field name Data Type

Fees

Field
properties

Figure 9.3 - Properties of data type

² Security : Unauthorized access can be controlled by using a password


and encypting the database. This increases security.
Data encrypting is translating data in such a way that unauthorized personnel
cannot read it. In the following figure 9.4, data available in Employee table are
encripted so as it cannot be understood by an unauthorized person.

255
For free distribution 255
255
Figure 9.4 - Data Encription

9.1.3 Electronic and Manual Databases


In an electronic database, data can be saved in a very small space which
consists of an audio or video medium. Given below is a comparison of electronic
and manual databases. (Table 9.4)

Table 9.4 – comparison of manual and electronic database


Manual Database Electronic Database
Less efficient More efficient
Less accurate More accurate
Less credible More credible
Difficult to analyse data Easy to analyse data
Should present in the order of input Can change the data input order
Difficult to delete unnecessary data Easy to delete unnecessary data
Difficult to update data Easy to update data
More space is needed to save data A very small space is needed to save data
More man power is needed Less man power is needed

256
256 For free distribution
9.1.4 Introduction to Relational Database
A collection of fields related to one person or object is called a record.
A collection of records is called a data table.
A collection of related tables is called a relational database.

Relational Database

Field
Student Name Date of Date of
Number Birth Admission
100 Manoj Dayarathne 2/9/2008 G. Dayarathne
101 Firosa Lai Rafeeqa 4/2/2008 K. Fais

Student Book Borrowed Return


Number Number Date Date
Tables
101 2222 2/9/2014 16/9/2014
100 3333 3/9/2014 17/9/2014
101 1111 3/9/2014 17/9/2014 Records
100 3333 4/9/2014 18/9/2014

Let us learn how a related database is built step by step. A collection of


fields make a record, a collection of records make a table and a collection of tables
make a relational database.
Relational database

Table 1 + Table 2 + Table 3 + Table 4 …….. (Tables)

Table

Record 1 + Record 2 + Record 3 + Record 4 ………. (Records)

Record

Field 1 + Field 2 + Field 3 + Field 4 + Field 5 ……… (Fields)

Figure 9.5

257
For free distribution 257
257
Example
Student record book is a very important document for every student. Biodata of a
student and the marks obtained for each subject with the rank in all three terms in
all grades are included.
Table 9.5 – Biodata form

Admission No : 1426 Admission No : 1427 Admission No : 1428


Name : Kavindu Name : Meenadevi Name : Mohommad
Prabashwara Ramanathan Malikkar
Date of Birth : 2005.05.23 Date of Birth : 2005.08.12 Date of Birth : 2005.02.07
Gender : Male Gender : Female Gender : Male
Tel No : 0352287571 Tel No : 035 22 35696 Tel No : 0352815402

Five details related to each student are given above. Thus, data such as
Kavindu Prabashwara, Meenadevi Ramanathan, Mohommad Malikkar represent
one field. To name such a field, a ‘field name’ is used.
Let us create a table recording details under each field name. (Table 9.6)
Table 9.6 - Student Records

Admission Name Date of Gender Student Phone


No Birth Numbers
1426 Kavindu 2005.05.23 male 0352287571
Prabashwara
1427 Meenadevi 2005.08.12 female 0352235696
Ramanathan
1428 Mohommad 2005.02.07 male 0352815402
Malikkar

A column of the table represents one field. Thus, the above table consists of
five fields. They are admission number, Name, Data or Birth, Gender and Telephone
number.
A collection of fields makes a record. Hence, the above table has three records.

258
258 For free distribution
Record Column
Field Names
Table 9.7 - Componants of Data Table
Admission Name Date of Gender Telephone No
No Birth
1426 Kavindu Prabashwara 2005.05.23 male 0352287571
1427 Meenadevi 2005.08.12 female 0352235696
Ramanathan
1428 Mohommad Malikkar 2005.02.07 male 0352815402

Row
There is the possibility of entering the same data again in the columns
such as Name, Date of Birth, Gender and Tel No. However, a datum entered in the
column of Admission No will not be repeated. Hence, the field Admission No is
more suitable to uniquely identify a record.
What are the books borrowed by Firosa Rafeeq? Let us learn how this is
found in relevent tables.
Table 9.6
Student Name Date of Date of Name of the
Number Birth Admission Guardian
100 Manoj Dayarathne 2$9$2008 1$1$2014 G. Dayarathne
101 Firosa Rafeeq 4$2$2008 1$1$2014 K. Fais

Student Book Date Date


Number Number Borrowed Returned
101 2222 2/9/2014 16/9/2014
100 3333 3/9/2014 17/9/2014
101 1111 3/9/2014 17/9/2014
100 3333 4/9/2014 18/9/2014

Book Title Author’s Name


Number
1111 Basic Maths Sivathasan
Concept Ramanathan
2222 Folk Songs Jayantha Bandara
3333 Drama Padmamali Menike

259
For free distribution 259
259
9.1.5 Primary Key

A column (or a combination of columns) which enables to identify a record


uniquely is called primary key.

Example - National Identity Card Number


School Admission Number

Figure 9.6
observation
Special attributes of a primary key
• Should not be empty (data being there is mandatory)
• No duplicate values (same values do not exist)

For instance, study the table given below which consists of data on a few books.
Table 9.7 - Book Table
Accession Title Author’s NamePrice of Number
No Book of Books
2131 Let’s Access Internet Mahesh Gankanda Rs.275.00 10
2132 Computer Studies Suranimala Rs.300.00 5
Basnayaka
2133 Essay Writing Nimali Bogoda Rs. 225.00 12
2134 Education Psychology Dayanath Rs.400.00 6
Jayasekara
2135 Tamil Language Learning Raj Sudakaran Rs.225.00 8
2136 Computer Studies Mohommad Niyas Rs.150.00 4
2137 Sinhala Tamil Dictionary Raj Sudakaran Rs.325.00 6

In this table,
• Accession numbers do not have duplicates (unique)
• Both authors Suranimala Basnayaka and Mohommad Niyas have authored
Computer Studies.
• Raj Sudakaran has authored both Tamil Language Learning and Sinhala Tamil
Dictionary.

260
260 For free distribution
• The price of both books Essay Writing and Tamil Language Learning is the
same.
• Both books Education Psychology and Sinhala Tamil Dictionary have six
copies.
According to these details, the Accession Number is the most suitable fields
for the primary key.

Note: The Primary key of a table is underlined to identify as unique. (table 9.8)

Table 9.8
Accession Title Author’s Price of the Number of
Number Name Book Copies


9.1.6 Composite Primary Key

The primary key which consists of two or more columns of a table uniquely
identifies a record is called Composite Primary Key.

Example
Look at the table given below about the captain and vice captain of a school.
Table 9.9 – Sports Table
Year Sport Captain Vice Captain
2013 Cricket Rashmi Mubharak Husein
Senarathne
2013 Football Jaliya Saranga Vinothen Raj
2013 Volleyball Ahammed Nawas Sudesh Tharanga
2014 Cricket Mubharak Husein Bhanu Dayarathne
2014 Football Jaliya Saranga Ahammed Nawas
2014 Volleyball Bhathiya Gamlath Sudesh Tharanga
Both the year and sport are needed to uniquely identify
Composite Key
a record.

261
For free distribution 261
261
Since the column data of this table containes duplicate. There is no single
column to uniquely identify records. There should be a definite captain and a vice
captain for a sport in a year. Hence, as far as the combination of two columns;
year and sport is considered, identity of records exists. Hence, combination of both
fields ‘year’ and ‘sport’ can be considered as the primary key. The composite key
made of the combination of these two columns is suitable for the primary key in this
table. When indicating the composite primary key, fields are separately underlined.
(Refer Table 9.10)
Table 9.10
Year Sport Captain Vice Captain

The composite primary key of this table = Year + Sport

9.1.7 Foreign Key

If a field (or a combination of fields) of one table can be used to uniquely


identify rows of another table, this/these field/s is/are called the foreign key.This
key builds connection of two tables.
Table 9.11 – Student Table
Registration Name Date of Date of Name of the
Number Birth Admission Guardian

100 Manoj 2/9/2014 1/1/2014 G. Dayarathne


Dayarathne
101 Firosa Rafeeq 4/2/2014 1/1/2014 K. Fais
When considering the tables 9.11 and 9.12, Registration
Number is the primary key of 9.11 - Student Table where
Primary Key as Registration Number is the foreign key of 9.12 - Subject
Table. Further, a separate primary key is available for subject
table and that is candidate number.
Foreign Key
9.12 - Subject Table
Candidate Maths Science Sinhala English Registration
No No
449683 B A C C 100
449697 A B B A 101

262
262 For free distribution
observation
Special features of a foreign key
• Builds a connection between two tables.
• The related data of the primary key field exist in the other
table.
• Foreign key of one table is the primary key of the other table.
Example
Given below in Table 9.13 are ICT subject marks of three students scored in the first
term.
Table 9.13 – First term marks
Admission No Name Date of Marks Term Year
Birth
1426 Kavindu Prabashwara 2005'05'23 69 1 2014
1427 Meenadevi 2005'08'12 82 1 2014
Ramanathan
1428 Mohommad Malikkar 2005'02'07 47 1 2014

When entering marks of these students scored in the second term, the fields
such as admission number, name and date of birth should be entered again. (Refer
Table 9.14)
Table 9.14 – Second term marks
Admission Name Date of Marks Term Year
No Birth
1426 Kavindu Prabashwara 2005'05'23 69 1 2014
1427 Meenadevi Ramanathan 2005'08'12 82 1 2014
1428 Mohommad Malikkar 2005'02'07 47 1 2014
1426 Kavindu Prabashwara 2005'05'23 79 2 2014
1427 Meenadevi Ramanathan 2005'08'12 68 2 2014
1428 Mohommad Malikkar 2005'02'07 66 2 2014
Recording column data repeatedly is called data duplication.
Disadvantages of data duplication
(i) Inability to select a primary key field to maintain identity of records.
(ii) Inability to analyse data properly.
Though the number of students who have scored above 60 marks is 5 as per
the above table, the actual number of students in the class is 3. Hence, a
proper data analysis cannot be done.
263
For free distribution 263
263
(iii) Reduced efficiency
When entering marks of several years in the above table, more time is spent
to enter the admission number, name and date of birth of the same student.
This reduces efficiency.
(iv) Possibility to get inaccurate data is high.
There can be errors in entering the same data repeatedly. Mohommad
Malikkar can be recorded as Mohommad Malakkar in another place. These
mismatches can lead to inaccurate data. Hence, accuracy becomes low.
(v) Difficult to input data.
To enter the marks of a student, his/her name and date of birth should be
entered repeatedly.
(vi) Possibility to make errors in deleting data.
As there are several records related to a student, there can be errors in
deleting data.
(vii) Difficult to update data.
As there are several records related to one student, attention should be paid to
each record in updating it.

These difficulties are created as all data are stored in one table. Hence, to
minimize such difficulties, a table can be divided into several tables.
Let us create two tables: Student Table and Marks Table, by removing the
fields which cause data duplication.
Let us create Student Table (Table 9.15) which consists of Admission Number,
Name and Date of Birth.
In the Student Table, there is the possibility of data in Name and Date of
Birth fields being duplicated. However, data does not duplicate under Admission
Number. Hence, Admission Number is more suitable to identify records uniquely.
Therefore, the primary key of the Student Table is the Admission Number.
Table 9.15 – Student Table
Admission No Name Date of Birth
1426 Kavindu Prabashwara 2005'05'23
1427 Meenadevi Ramanathan 2005'08'12
1428 Mohommad Malikkar 2005'02'07

264
264 For free distribution
Let us create a Marks Table (Table 9.16) which consists of the Admission
Number, Marks, Term and Year.
Table 9.16 – Marks Table
Admission No Marks Term Year
1426 69 1 2014
1427 82 1 2014
1428 47 1 2014
1426 79 2 2014
1427 68 2 2014
1428 66 2 2014

There is only one datum in the Student Table related to Admission Number
of Marks Table.
Hence, the field of the Admission Number is the primary key of the Student
Table and foreign key of the Marks Table.

9.2 Creating a Simple Database Manually


Data type : a type of data related to one field is called a data type. Let us learn about
some commonly used data type.

²² Numeric: used for calculations. Exists in several forms.


∗ Integer: plus or minus whole numeric figures.
E.g.: 12, -23
∗ Real: plus or minus numbers with decimal places.
E.g.: 8.125, -2.64, 4.00
²² text: composed of letters, numbers or special characters (Eg- %, *, -). Though
numbers are used, it is not used for calculations.
E.g.: National identity card number –889534731V
Telephone number –0112785123, 0112 - 985123
²² Currency: used to indicate monetary values.
E.g.: $12.45, Rs.35.00
²² Date / Time: indicates dates and time
E.g.: 12/23/2013, 7.35 a.m
²² Boolean: there are only two Boolean (logical) values; True and False.

The size of a field can be changed according to the data type and number of
data input to that field.

265
For free distribution 265
265
E.g.: Field Name (Column Name) Number of fields (Column Width)
Subject Name 15
Subject Code 5

Data type related to the field should be identified when the table is created.

Field Name Data Type
Accession number of the book Text
Title Text
Author’s Name Text
Price of Book Currency
Number of Books Number

Activity

(1) Tabulate details of 10 books in your library under the fields mentioned
above.
(2) Create a table which is suitable to enter marks scored by the students
in your class. Include Admission Number, Name and the Marks for
subjects you study in this table.

Mention the most suitable data type for the fields in Table 9.17 given below.

Table 9.17
Field Data Type
Admission Number
Date of Admission
Subject Code
Doctor’s Fee
Department Name
Number of Passengers
Is Registered?

(3) Explain with examples the importance of selecting the most suitable data type
for fields.

266
266 For free distribution
9.3 Converting a Manual Database to an Electronic
Medium

Let us learn electronic databases with the purpose of converting a manual
database to an electronic medium.

Definition
A collection of objects which facilitates planning and maintenance of the
database to the user is called a Database Management System (DBMS). This
consists of several objects like tables, queries, forms and reports. (Refer Figure 9.7)

Tables

Reports (DBMS) Queries

Forms

Figure 9.7 – Objects of a Database Management system

9.3.1 Database Management System Software



Given below in Table 9.18 are some DBMS software manufactured by
various companies.
Table 9.18 – Various database management systems and manufacturing companies
Software Manufacturing Company
Access Microsoft Company
Base The Document Foundation
Oracle Oracle Cooperation

267
For free distribution 267
267
9.3.2 Using of Database Management System Software
In this unit, only Microsoft Office Access and LibreOffice Base software
are discussed.
1' Starting DBMS software
Please note that the commands used to run software can be different
according to the operating system.
For Microsoft OfficeAccess''' For LibreOffice Base'''

Start→ Programs → Microsoft Office Start → Programs → LibreOffice →


→ Microsoft Office Access → Blank LibreOfficeBase → Create a new
Database → Select Folder and Type database → Next → Finish → Select
Folder and Type Name → Save
Name → Create

2' Planning a Data Table


Open database software and plan a table with the fields and data types as
given in 9.8. Name the table as ‘Library’.
For Microsoft OfficeAccess'''
For LibreOffice Base'''
Create → Table Design → Give the field Select 'Table' in Design View → Give
name and data type → Design properies the field name and data type → Design
→ Save the table Properties → Save the table.

Figure 9.8 – Fields and data types of ‘Library’ table.

3' Creating the Primary Key for a Data Table


Data should not be duplicated to uniquely identify rows of a table. Identity
of records can be maintained by using a column or a combination of columns of a
table as a primary key field. Convert the field of ‘Accession Number’ in the above
figure 9.8 to the primary key field.

268
268 For free distribution
For LibreOffice Base''''
For Microsoft OfficeAccess'''
Give field name and data type adjust
Give field name and data type → Select the
row/ rows that should be made as Primary properties → Right click on the line
key → Click on Primary key → save the which contains the field need making
table. primary key → Select Primary key →
save the table.

Figure 9.9 - Library Table with the Primary Key

4' Entering Data to the Table


The table view should be changed to input data to a prepared table.

For Microsoft Office Access'''


For LibreOffice Base'''
Select the table from 'All Access
Objects' window → Select' Data Sheet Double click the table under Tables or
View' → Enter data → Close the table. select Open by clicking the right button
→ Enter data → Close the table.

Enter data into the Library Table as shown in Figure 9.10

Figure 9.10 - Records of Library Table

269
For free distribution 269
269
9.4 Creating a Relational Database
You have studied the Primary Key and Foreign Key of a table in unit 9.1.
Rather than keeping all the data in a single table, it is easier, more efficient and
productive to input, update and delete data if those are recorded separately in
different tables. Hence, let us study this section well to break a data table into
several simple tables and to build relationships between these.
When creating a relational databases, several type of relations can exist
among tables. They are known as one-to-one, one-to-many and many-to-many.
9.4.2 One-to-one Relationship
If only a row of a table is related to a row of another table, it is called a
one-to-one relationship.
Two tables exist as A and B. A row in table A is related to one row in Table
B and a row in Table B is related to one row in Table A.
Example
Given below is Table 9.19 which includes the marks students scored of the Grade
5 scholarship exam.
Table 9.19– Scholarship Marks
Admission No Name Date of Index No Marks
Birth
1426 Kavindu Prabashwara 2005'05'23 23234 151
1427 Meenadevi Ramanathan 2005'08'12 23876 186
1428 Mohommad Malikkar 2005'02'07 23758 172
1429 Rashmi Janodara 2005'06'16 23765 193

Let us tabulate data in Table 9.19 into two tables: Table 9.20 – Student Table and
Table 9.21 – Marks Table. Table 9.21 – Marks Table
Table 9.20 – Student Table Admission Name Date of
Index No Marks Admission No Birth
No 1426 Kavindu 2005.05.23
23234 151 1426 Prabashwara
23876 186 1427 1427 Meenadevi 2005.08.12
Ramanathan
23758 172 1428
1428 Mohommad 2005.02.07
23765 193 1429 Malikkar

1429 Rashmi 2005.06.16
Janodara

270
270 For free distribution
A record of the Marks Table is related to only one record of the Student
Table. A record of the Student Table is related to only one record of the Marks Table
as well.
The primary key of the Student Table is the Admission Number and it is the
foreign key of the Marks Table. The Primary key of the Marks Table is the Index
Number. The relationship built between these two tables through the foreign key is
one-to-one. (Figure 9.11)


Student Table
Admission No Name Date of Birth

Marks Table
Index No Marks Admission No

An arrow is drawn from the foreign key to the primary key to indicate the
relationship.

Figure 9.11 – Indicating one-to-one relationship

9.4.2 One-to-many Relationship



If each record of a table is related to several records of the other table, this
is called a one-to-many relationship.
If the tables are A and B, and if B is the related table, the following Table
9.22 shows one-to-many relationship.

271
For free distribution 271
271
Table 9.22 – Features of a one-to-many relationship
Table A Table B (Related table)
A record in Table A can be related to A record in Table B can be related to
several records in Table B. only one record in Table A.
There can be records which are not There cannot be records which are not
related to records of Table B. related to records of Table A.
Data in the column which includes the Data in the column which includes the
primary key cannot duplicate. foreign key can duplicate.
Having a record in Table B related to a It is mandatory to have at least one row
record in Table A is not mandatory. in Table A which is related to Table B.

Example
Refer Table 9.23 given below which includes personal data of students and payments
of service and facility fees.

Table 9.23 - service and facility fees


Admission Name Date of Receipt Payment Fees
No Birth Number Date
1426 Kavindu 2005'05'23 1000 2014'01'04 40'00
Prabashwara
1427 Meenadevi 2005'08'12 R
Ramanathan
1428 Mohommad 2005'02'07 1001 2014'01'06 120'00
Malikkar
1426 Kavindu 2005'05'23 1002 2014'01'12 80'00
Prabashwara

In this table, the fields 'Payment Date' and 'Fees' depend on the Field Receipt
Number. Further, data is unique in the Receipt Number Field. The reason for this is
that receipt numbers will not be same. Anyway, there can be incomplete records in
the table as receipts should be issued in the order of payment. (Indicated as R in the
table) Further, data duplication can also be seen here.
Hence, let us break this table into two tables: Table 9.24 –Student Table and
9.25 – Fees Table.
The Receipt Number is the primary key of the Fees Table. Admission
Number is the primary key of the Student Table and the Foreign Key of the Fees
Table.

272
272 For free distribution
Table 9.24 – Student Table
Admission No Name Date of Birth
1426 Kavindu Prabashwara 2005'05'23
1427 Meenadevi Ramanathan 2005'08'12
1428 Mohommad Malikkar 2005'02'07

Table 9.25 - Facility Fees Table

Receipt Payment Fees Admission


Number Date No
1000 2014'01'04 40'00 1426
1001 2014'01'06 120'00 1428
1002 2014'01'12 80'00 1426

If a student can pay fees a maximum of three times, there can be 0, 1, 2 or 3


rows in Facility Fees Table related to a record of the Student Table
• If number of records is 0, this means fee is not paid.
• If number of records is 1, this means fee is paid once only.
• If number of records is 2, this means fee is paid twice.
• If number of records is 3, this means fee is paid thrice.
Thus, there can be no or several rows in the Fees Table related to a
row of the Student Table. Hence, the relationship between these two tables is
one-to-many
Student Table
Admission No Name Date of Birth

Facility Fee Table


Facility Receipt Payment Date Fees Admission No
No


Figure 9.12 - One-to-Many Relationship

273
For free distribution 273
273
9.4.3 Many-to-many Relationship
If a record of a table is related to several records of another table and if a
record of that related table is related to several records of the other table, there is a
many-to-many relationship between those two tables.
Example
Students take part in sports (team) as an extra curricular activity. There are sports
such as cricket, volleyball and football. A student can do at least one of these sports.
Further, there should be a team for each sport.
Refer Table 9.26 given below which includes these data. According to this table,
Figure 9.26 – Table of students who do sports
Admission Name Date of Sport Sport Team Number of
No Birth No permanent
members
1426 Kavindu 2005'05'23 S001 Cricket A 11
Prabashwara
1426 Kavindu 2005'05'23 S002 Football A 16
Prabashwara
1428 Mohommad 2005'02'07 S001 Cricket B 11
Malikkar
1428 Mohommad 2005'02'07 S002 Football A 16
Malikkar
1428 Mohommad 2005'02'07 S003 Volleyball B 06
Malikkar
1429 Janith Asheka 2005'04'15 S001 Cricket A 11
1429 Janith Asheka 2005'04'15 S003 Volleyball A 06
There is data duplication in this table. To minimize data duplication, let us break
these data into separate tables. Include Sports data into Sports Table (Table 9.27)
and student data into Student Table (Table 9.28).
Table 9.27 - Student Table
Table 9.28 - Sports Table
Admission Facility fees Date of
Number of
No Table Birth Sport Sport
No. permanant
1426 Kavindu 2005'05'23 Members
Prabashwara S001 Cricket 11
1428 Mohommad 2005'02'07 S002 Football 16
Malikkar
S003 Volleyball 6
1429 Janith Asheka 2005'04'15

274
274 For free distribution
Many-to-many relationship between Student Table and Sports Table is converted
to one-to-many relationships in related databases. For this, another table is needed
apart from the two tables given above. (Let us name that table as Students Sports
Table – Table 9.30) Let us include Admission Number of Student Table to represent
students and Sport Number of Sports Table to represent sports.
Figure 9.29 – Student Table
Admission No Name Date of Birth
1426 Kavindu 2005'05'23
Prabashwara
1428 Mohommad 2005'02'07
Malikkar
1429 Janith Asheka 2005'04'15
Figure 9.30 - Students Sports Table
Admission Sport No Team
No
1426 S001 A
1426 S002 A
1428 S001 B
1428 S002 A
1428 S003 B
1429 001 A
1429 S003 A
Figure 9.31 – Sports Table
Sport No Sport Number of permanent
members
S001 Cricket 11
S002 Football 16
S003 Volleyball 6
The Admission Number is the Primary key of the Student Table and the
foreign key of Students Sports Table.
The Sport Number is the primary key of the Sports Table and the foreign
key of Students Sports Table.
The primary key of the Students Sports Table is Admission Number + Sport
Number.
Refer Figure 9.13 which gives relationship when two tables like the
Student Table and Sports Table with many-to-many relationships are converted to
one-to-many relationships.

275
For free distribution 275
275

Student Table
Admission No Name Date of Birth

Students Sports Table


Admission No Sport No Team

Sports Table
Sport No Sport Number of permanent members

Representing two tables with many-to-many relationships as two


one-to-many relationship

Figure 9.13 - Many-to-Many Relationship


Thus,
There is a one-to-many relationship between the Student Table and the
Students Sports Table.
There is a one-to-many relationship between Sports Table and Students
Sports Table.
Hence, by using an extra table, many-to-many relationships can be converted
to one-to-many relationships.

9.5 Creating a Relational Database Using Database


Management System Software

Let us learn how to build relationships between tables using a Database
Management System software. For this, create the tables given below: Figure
9.14 – Student Table (using field names and data types given) and Figure
9.15 – Marks Table.

276
276 For free distribution
Figure 9.14 – Field names and data types of Student
Table.

Figure 9.15 – Field names and data types of marks table.

9.5.1 Build up relationship between tables

For Microsoft Office Access... For LibreOffice'''

Select Database Tools → Select Relationship Select Tools Menu → Select Relationship
command → Select the table which should command → Select the table which should
be related → Click 'Add' command button. be related → Click Add command button.
(Indicate the number of tables required) → (Indicate the number of tables required)
Drag the Primary Key to the foreign key of → Drag the Primary Key to the foreign
the table which should be related (Figure 9.16) key of the table which should be related →
→ Select 'Enforce Referential Integrity' on Right click on Relationship and select Edit
'Edit Relationship' Dialogue Box (Refer 9.17) → Select 'Update Cascade' under 'Update
→ Select 'Cascade Update Related Field' and Option' on Relations Dialogue Box. Select
'Cascade Update Related Records' → Click 'Delete Cascade' under 'Delete Option'
'Create' command button. (Figure 9.18) → Click 'OK' command
button.

277
For free distribution 277
277

Figure 9.16 - One-to-many relationship between two tables.

Figure 9.17 – Editing Relationship Dialogue Box (Access)

Figure 9.18 – A Relations Dialogue Box (LibreOffice Base)

278
278 For free distribution
The Admission Number is the primary key of the Student Table and the
Foreign Key of the Marks Table.
observation
Special Features in Related Tables

As soon as the values of a primary key field of the Student Table are updated
using 'Referential Integrity,' the foreign key field values of the related table are
automatically changed.
Further, when a record of the Student Table is deleted, the related records of
the related table are automatically deleted.
First, data should be entered to the student Table. After that, data can be
entered to the Marks Table.
After entering the marks to the Student Table, data can be entered to the
Marks Table through it.
For this, click on the + mark which is in front of the Admission Number of
the Student Table. Then data can be entered to Marks Table easily. (Refer Figure
9.19)

Figure 9.19 – Entering data to Student Table and Marks Table.

279
For free distribution 279
279
9.6 Creating Forms to Entering and Displaying Data

Now you have knowledge about creating a table and entering data to it.
Forms are used to enter data to a table in Database Management Systems. Further,
forms are used to display entered data as well.
Here, a row of a table is displayed and entered as a record in a form. (Refer
Figure 9.18)
Data Table
Name Address Telephone No Email
Anwar Athurugiriya 0773684949 [email protected]
Nimal Galewela 07184632481 [email protected]
Sudakaran Kandy 0817587484 [email protected]
Saman Kegalle
Figure 9.18 – Table and Data Form

Name Nimal
Address Galewela
Name Saman
Address Telephone No 07184637481
Kegalle
Email [email protected]
Telephone No
Email
Display data on form
Data input through form

Each input record is entered to the last column of the table. Each record of
the table can be filled one by one through the form. For this, the Record Navigation
Bar of a form is used. (Refer Figure 9.20)
Existing
First Record Record
Previous Record
Total Records


New (empty)
Record

Next Record
Last Record
Figure 9.20 – Checking records from a data form.

280
280 For free distribution
1' Building a Data Form

For Microsoft Office Access '''

Create → Form Design → Add Existing Fields → Show tables → Select the required table
→ Drag table fields to the form (Refer Figure 9.21)

Figure 9.21 –Data form to enter data to the Student

2' Showing Table Records in Data Form


For Microsoft Office Access '''

Home Tab → View → Form View (Refer Figure 9.22)

Figure 9.22 – Showing records in the Student Table


281
For free distribution 281
281
3' Entering Control Wizards to a Form

For Microsoft Office Access '''

Design Tab → Select 'Control Wizards' (Refer Figure 9.23) → Select the button indicated as
XXXX → Establish the button in data form → Select Record Operation in Command Button
Wizard (Refer Figure 9.24) → Select the Action which should be entered (E.g.: Add New
Record) → Give a proper name to the button → Finish

Figure 9.23 – controlling Tools

Figure 9.24 – Command Button Wizard

282
282 For free distribution
9.7 Designing Query to Get Information

Let us use a query to obtain necessary information from the data of related
tables. Query represents data of selected fields (columns) in each table. (Refer
Figure 9.25)


Student No Name Email Subject Subject
A1 Pubudu [email protected] No
A2 Faris [email protected] B1 Maths
A3 Kamalan Kam_n@hotmal. B2 Science
com B3 English
A4 Thanuja [email protected]

Subject Student Marks


No
A1 B1 67
A2 B3 89
A1 B3 50
A3 B2 75

When Name, Subject and Marks are taken by a query from the three table above,

Name Subject Marks


Pubudu Maths 67
Faris English 89
Pubudu English 50
Thanuja Science 75


Figure 9.25 - Obtaining data to a query

283
For free distribution 283
283
For Microsoft OfficeAccess'''

Create Tab → Query Design → Select the necessary tables (Refer Figure 9.26) → Select the
necessary field from the tables → Run the query

Designing a Query

Figure 9.26 –Dialogue Box to select tables

Figure 9.27 – Window to design query

284
284 For free distribution
Refer figure 9.28 given below for the information received after running the query.

Figure 9.28 – information received after running the query

As shown in Figure 9.28, marks scored by students in 2013 and 2014 are
shown. You can obtain marks scored by students in 2014 only (information shown
in Figure 9.28) by naming year 2014 as Criteria. (Refer Figure 9.29)

Figure 9.29 – Giving Criteria for query

Figure 9.30 – Information received after running query with


Criteria
Various calculations can be made using fields through query. Let us learn
how total marks scored by students are calculated.
Add a new column to the query as Total and build an expression indicating
the calculation needed. To get total marks of subject it should be typed as:

285
For free distribution 285
285
Total:[Marks]æ[Maths]¬[Marks]æ[Science]¬[Marks]æ[English]. This can be
typed (Refer Figure 9.31.) or can be performed by building the expression, right
click the Mouse button on the column which you expect to get the total and select
Build command. Then use the Dialogue Box you get. (Refer Figure 9.32)

Figure 9.31 – Enlarge 'Total' column in the query

Figure 9.32 – Dialogue Box to build the expression

After running the query built in the way explained above, you get information
as given in Figure 9.33 which is given below.

286
286 For free distribution
Figure 9.33 – Marks and total of students scored in 2013

9.8 Designing Reports to Present Information


Reports are used to obtain printed copies of important information received
from the data of related tables. Both tables and queries can be used to design reports.
Let us learn how a report is designed using the query given above in Figure 9.33.
Report Wizard can be used to design reports.
For Microsoft Office Access...
Create Tab → Report Wizard → Select the necessary table or query (Refer Figure 9.34) →
Select the fields needed for the report from the table or query → Design the report as you need
clicking Next button → Finish. Then you will get a report as shown in Figure 9.35.

Figure 9.35 - Report Wizard used to design reports

287
For free distribution 287
287
Figure 9.35 – Report which displays the total marks of students in 2013

Let us learn how numerical values included in a report are calculated.

For this, let us find the total of admission fees paid by the students. Create a
table as explained above using Report Wizard including Admission Number, Name
and Admission Fees. Then change from Report View to Design View. Then you
will get a report layout as shown in Figure 9.36.

Figure 9.36 – Report plan

Select the tool given as ‘ab’ on the tool barand establish the text box under
Report Footer. (Refer Figure 9.37)

288
288 For free distribution
Figure 9.37 – Establishing the text box

The text box is indicated as unbound. The reason for this is there is no
field given to it. To get the total of admission fees, get the Expression Builder
Dialogue Box to build expressions by clicking the button with three dots in front of
the Control Source on Property Sheet. (Refer Figure 9.39)


Figure 9'38 - Property Sheet

Figure 9.30 – Expression Builder Dialogue Box

289
For free distribution 289
289
To get the total of admission fees, type the expression = Sum ([Student
Table]![Admission Fees]) and type OK. Then change the view from Design View to
Report View. After that, you will get the report which displays the total of Admission
Fees. (Refer Figure 9.40)

Figure 9.40 – Report which displays the total of Admission Fees

Printing the Report


Change the 'Report View' to 'Print Preview'. Click on the tool shown in
Figure 9.41. Print the number of copies you need.

Figure 9.41 – Printing tool

Activity
1. Explain the advantages of electronic databases.

2. Explain with examples the features that are available in electronic databases.

3. Compare electronic and manual database.

4. Explain what is meant by data encryption is.

5. Explain relational databases using fields, records and tables.

6. Define primary key and provide three examples.

7. Define composite key and provide three examples.

290
290 For free distribution
8. Define foreign key and with three examples.

9. Write five disadvantages of data duplication.

10. a) What is a data type? Provide examples for data types .


b) Explain the reason as to why 'integer' data type is not the best data type for
field that stores telephone numbers.

11. Explain the relationships; one-to-one, one-to-many and many-to-many with


two examples for each.

12. Explain what 'objects' are in Database Management Systems and explain
the uses of the objects.

Sammary

² Database is defined as the collection of organized data.

² In a table a column is known as a 'Field'

² A raw of a table is known as a 'Record'

² Advantages of electronic database


• Efficient in searching data.
• Facilities to get copies.
• Less storage capacity used for storing data.
• Efficient analysis of data.
• Sharing data.
• Data independence.
² Characteristic of a database.

• Avoid data redundancy.


• Consistency of data.
• Increased efficiency
• Increased accuracy
• Increased validity
• Increase the security of data.

² A column or collection of columns in a table when records can be identified


uniquely is called primary key.

291
For free distribution 291
291
² If a field (or a combination of fields) of one table can be used to uniquely
identify rows in another table, these fields are called the foreign keys.
² A relationship is built up between two tables by using foreign key.

² Data redundancy means the same data is stored in several database tables.

² The type of data that is relevant to a field is known as data type.

² In a relational database a relationships such as one-to-one, one-to-many and


many-to-many can be exist.

² To enter and display data, forms can be used.

² Query is used to obtain information.

² To present data, reports are used.

292
292 For free distribution

You might also like