Sunteți pe pagina 1din 27

T o p ic 5 X Database

DevelopmentI

LEARNING OUTCOMES
At the end of this topic, you should be able to:
1. Apply Microsoft Access effectively to produce a complete
database;
2. Develop a query using the Query By Example (QBE); and
3. Produce forms and reports through Microsoft Access.

X INTRODUCTION
In this topic, you will learn the basic development of a simple database. The
development tool used is Microsoft Access, one of the Database Management
Systems applied in personal computers. Among the available Microsoft Access
functions are storage, isolation and retrieval of data for various applications. This
software is equipped with tools to create databases, tables, forms, reports and
queries. Microsoft Access is based on stand-alone or multiple user networks. For
this topic, each step involved in the use of Microsoft Access will be explained. As
such, it will be more helpful if you could read this module in front of a personal
computer with the Microsoft Access programme running.

5.1 INTRODUCTION TO ACCESS

Microsoft Access is a useful software for database management system. It


functions in the Windows environment and allows users to create and process
data in the database. A few unique characteristic are:
TOPIC 5 DATABASE DEVELOPMENT I W 63

Inserting and Updating a Data


Access provides a simple mechanism for adding, updating and deleting
data, including the ability to make a large change with only one operation.

Query
With Access, it easy for users to submit a complex query regarding the data
inside the database. The result is generated in a relatively short time.

Form
Users can create an interesting and useful form to display and update the
data.

Report
Access provides a feature that allows users to produce a sophisticated
report to represent data in a clear and concise way.

Web Supporting
Access allows users to save objects (form, table and others) into HTML
format in order to view them using a browser. Users also can create a page
for achieving real-time data using the Internet.

Before you start to create a database using Microsoft Access, it would be better if
you could recall the concept of database which you briefly learnt the in Analysis
and Data Modeling course.

Database refers to a collection of files containing arranged data that can be


retrieved, displayed and used. Database management systems like Access allow
users to use computers for:

Creating databases;
Adding, changing, and deleting data in databases;
Sorting data in databases;
Retrieving data from databases; and
Creating forms and reports using data in the databases.

In Access, the database consists of a collection of tables. Figure 5.1 shows an


example of a database for mentor-mentoree system. The mentor-mentoree
system refers to a learning support system implemented in the University where
a group of mentorees (students) is assigned to a mentor (lecturer). Mentor guide
mentorees in adapting to campus life.
64 X TOPIC 5 DATABASE DEVELOPMENT I

Figure 5.1: Database for mentor-mentoree system

The database containing of two tables which are mentorees and mentor table.
The rows of table are named as record. Record is containing information about
people, product, event and others. Mentorees table for example containing all
records of each mentorees.

Meanwhile the columns of the table are named as fields. Each fields containing
the specific information of the record. In mentorees table, the field of
MatricNumber refers to the matric number of mentorees (student). In this case,
MatricNumber is the primary key which refers to the field that can differentiate
all record uniquely.

5.2 2 CREATING AN ACCESS DATABASE


A system begins when the Access icon is clicked. At the initial stage, the
programme will enquire if we need to create a new database or open up the
existing database. The list of databases applied will also be presented. Each
database is kept in a file with the extension .mdb. If you wish to create a new
database, choose the command New database or Open database from the File
menu. If you choose an existing database, the dialogue box presented by
Microsoft Access Startup will be shown as in Figure 5.2.
TOPIC 5 DATABASE DEVELOPMENT I W 65

Figure 5.2: Windows Startup and Microsoft Access dialog box

Based on this dialogue box, we are able to create or open the current database.
When a new database is created, we can choose to start with a black object and
begin from start or use the Access Wizard to build the database. Access Wizard is
the expert in a database system that poses certain question to users about the
objects required such as the database and it constructs the object based on the
answers replied.

We will begin with the creation of a blank database named as MentorMentoree


without using the Database Wizard. Click on File_New_Database and when the
query is posed, name the database as MentorMentoree. You should be able to
view the image shown in Figure 5.3 on your computer screen.
66 X TOPIC 5 DATABASE DEVELOPMENT I

Figure 5.3: Dialog box for MentorMentoree database

This database stores all the components of a database structure. The Windows
database is the centre for us to build and use any objects in the database. Objects
of database components are tables, queries, forms, reports, macros and modules.
When one component is chosen, all element names in that component are shown
in Windows. In Figure 5.2, components to create tables are presented. There are
three keys on the left side of the Windows.

1. Open key - To open the existing element.


2. Design key - To open design Windows from elements.
3. Next key - To create a new element.

Let us focus on the creation of tables within the MentorMentoree database in the
following section.
TOPIC 5 DATABASE DEVELOPMENT I W 67

5.3 3 CREATING TABLES


Microsoft Access provides several methods of creating blank tables as shown in
the dialogue box. We can create tables with the help of Table Wizard or directly
insert data into blank data sheet when it is stored. The types of data and format
for each field are also be determined. The data sheet displays data in rows and
tuples such as electronic spreadsheets.

In this section, we shall build mentoree table with the correct command through
Design View. The computer screen should appear as shown in Figure 5.4.

Figure 5.4: Design view for Mentoree table presenting the field property for Name

5.2.1 Field
First, we have to determine attributes for each table. In Access, the attribute is
called Field and domain is known as type. We need to fulfill the information
required in the Table Design View for each field in Mentoree table such as name,
type and description. Figure 5.4 shows the creation of the Mentoree table in
Design View. Microsoft Access supports various types of data to store various
types of information. For each field in the Mentoree table, we need to choose the
suitable type of data and ensure the storage mechanism is effective. In the
68 X TOPIC 5 DATABASE DEVELOPMENT I

example shown, most fields are presented as Text including Name, programme,
address and Mentor_ID. Matric_No is stated as Number.

5.2.2 Primary Key


The primary key for Mentoree table is the Matric_No field as shown in the key
symbol of the field. The primary key determines that each matric number is
unique for each mentoree record in mentoree table.

Let us proceed to create the mentoree table by inserting several restrictions in the
table.

In the next subtopic, you will be taught about the field element in Microsoft
Access. This will assist you in determining how data is stored, managed or
presented.

5.2.3 Field Element


In Microsoft Access, the Field properties section in the Table Design View is
used to increase restrictions on a particular table as shown in the previous figure.
Each field has a group of elements used to determine how data is stored,
managed or presented. We can control the number of characters inserted in the
field text by placing values in the Field size element. The type of data chosen for
each field determines the property of the files. The usage of field element will be
discussed in the next section.

(a) Field Size


For data type Text, Number and Autonumber (counter), we use the Field
size to ascertain the maximum size of the data stored and the types of field
applied. For example, the field size element for Programme (Text) is fixed at
five characters as shown in Figure 5.5.
TOPIC 5 DATABASE DEVELOPMENT I W 69

Figure 5.5: The Field element for programme field of mentoree table in Design View

(b) Field Format


Field format element is used to ascertain the number structures, date, time
and text presented and printed. Microsoft Access provides various formats
that present several types of data. For example, domains with data type
Date/Time can present data in various formats such as Short Date, Medium
Date and Long Date. The date 5th of September, 1974 can be presented as
5/09/74 (Short Date), 5-09-74 (Medium date) or 5 September 1974 (Long
Date).

(c) Decimal Places


Decimal Places element is used to define the number of decimal places used
during the number presentation. For example, Matric_No field for the table
mentoree has 0 decimal places. This feature exists only for the domain
Single and Double.

(d) Input Mask


The Input Mask is used to assist input of data by controlling the data
format when it is inserted into the table. Mask determines the types of
character inserted. By applying the input mask, data insertion is simplified
by inserting data. For example, the value to be inserted into the field of
Mentor_ID in the mentoree table has a specific format.

(e) Caption
Caption is a name given to an attribute when it surfaces in a report form. It
is used for providing explanations about the field name domain and serves
as valuable information to users through the captions on objects. For
example, if the 'Matric Number' is inserted in the Caption feature as
Field_No, the tuple named 'Matric_Number' will be presented in the table
within the Datasheet and not the 'Matric_No; field.
70 X TOPIC 5 DATABASE DEVELOPMENT I

(f) Default Value


This element is to define the default value for attributes. To expedite and
lessen errors in data entry, the determination of a default value enables you
to fix the input value automatically in the domain when a new record is
created.

(g) Validation Rule/Validation Text


This element explains the restrictions that must be fulfilled by an attribute.
It is used to ascertain requirements for data input in a domain.

When the data input does not fulfill the Validation Rule the Validation Text
feature, warning messages are presented to users. The Validation Rule
determines the allowed value range for data domain or numeric. The rule
lessens the possibility of errors occurring when data is inserted into the
table.

(h) Required
The required domain is a domain with values in each record. If the feature
is fixed at 'Yes', we would need to insert the value inside the domain and
value must not remain zero or null. For example, each student must have a
matric number. As such, the matric_no in the mentoree table must consist
of values without any null value. The primary key field is to identify each
record uniquely. As such, the field of the primary key must not contain the
value null. When tables are created, the default value for each field,
including the primary key field, is set to 'Yes' according to the attribute
required.

(i) Allow Zero Length


This element is used to determine if the blank character (" ") is a valid input
in the table field. This element is used by Microsoft Access to keep the zero
and not null. When the field is left blank, we need to set the Allow Zero
length element and required element to Yes. Allow Zero Length does not
depend on the required element. The required element determines whether
the null value is valid for the field. If Allow Zero length is determined as
'Yes", the blank character become a valid value for a field without
depending on the required element.

(j) Index
This element is used to determine the field of index. This index expedites
the queries upon the index field and speeds up sorting and grouping
operations.
TOPIC 5 DATABASE DEVELOPMENT I W 71

SELF-CHECK 5.1

Create one new table name Mentor that containing all fields as below:
1. Name
2. Worker_Num
3. Faculty
4. Phone

5.4 RELATIONSHIP
The major error that always occur in developing database application systems is
that the entities are not modelled into a suitable relationship. The best practice is
for all entities and relationships to be placed in one table.

The advantage of this approach is that proper planning at the early stage of
application development Is not required. Nevertheless, this method also has
disadvantages such as waste of storage, difficulty to change data, difficulty to
remove data and also difficulty to add data.

These problems can be avoided by using an application development method


that uses a normalisation table with accurate relationship definition.

5.4.1 Define Relations and Referential Integrity


In a normalised database, related data may be kept in several tables. As such,
DBMS must connect information in different tables. To connect different tables,
we need to create relations among tables. When these relations are connected, we
need to determine whether referential integrity is fixed or otherwise. Referential
integrity is the restriction existing between tables. It produces relations between
the parent table and child table through the existing domain for each table.

The process to produce relations with referential integrity is presented by mentor


table and mentoree table. The child table is the mentoree consisting of a field
called Worker_No as a foreign key. The Worker_No field exists in the Mentor
table as the primary key. The Worker_No field that exists in both tables enables
those tables to be linked with one another.

This relation is shown in Figure 5.6.


72 X TOPIC 5 DATABASE DEVELOPMENT I

Figure 5.6: Window relationship shown in the Mentor (parent)


and Mentoree (child) linked through the Worker_no field

When tables are created,they are not linked to other tables. To create this relation,
drag the field required to be linked from one table to another. In this example,
click the Worker_No field in the Mentor Table and drag it into Mentor_ID in the
Mentoree table.

The ratio between the Mentor and Mentoree table 1 to many (1:M) relations. In
other words, these relations show that one Mentor is responsible of several
Mentorees. Referential Integrity is regulated by using Enforce Referential
Integrity while the relation is linked as shown in Figure 5.7.

Figure 5.7: The Referential Integrity dialogue box


TOPIC 5 DATABASE DEVELOPMENT I W 73

If the referential integrity is fixed and the user does not follow the regulation,
Microsoft Access will present the message and changes will not be allowed.
Examples of Referential Integrity are Cascade Update Related Fields and
Cascade Delete Related Record as shown in Figure 5.7. When Cascade Update
Related Fields is chosen, any changes in the primary key value in the parent table
will automatically be updated with the same value in the related tables. When
the Cascade Delete Related Record is chosen, deleting a particular record in the
parent table will also delete the same record in the child table (if it exists).

If you are reading this module with the Microsoft Access application in front of
you, your computer screen should display the same image as shown in Figure
5.7. To ensure that you truly understand this topic, answer the following
questions.

SELF-CHECK 5.2

Briefly explain the characteristics of the domain below:


(a) Field Size
(b) Format
(c) Input mask
(d) Caption
(e) Required
(f) Allow zero Length
(g) Index

Note: For further explanation with example, student can use the help
function in Microsoft Access for each field elements that you have
leant.
74 X TOPIC 5 DATABASE DEVELOPMENT I

5.5 .5 CREATING QUERIES


Database Management Systems (DBMS) such as Access offer interesting features
including the ability to respond to all questions from users. The question sent to
Access is called a query. When we retrieve a query, we are actually instructing
Access to find the a solution to this question.

To create a query using Access, there are two methods that can be used:

1. Query using QBE ( Query By Example)


2. Query using SQL ( Structured Query Language)

The important thing about the query is that it does not contain any data. A query
only arranges the data in database without changes in the table.

5.5.1 Query by Example


You have learnt how to develop a database statement. Microsoft Access
simplifies the methods to develop SQL through Query by Example. Is this similar
to what you have learnt in Topic 4?

This section will highlight the Query-By-Example (QBE) application in Microsoft


Access. QBE is used to enquire about stored information in tables and assure the
required domains in your answer. The record can be chosen based on several
criteria, and calculations can be applied on information in the table. QBE can also
be used to insert, delete and update data.

5.5.2 Developing Query by Example


The Select Query is a general query form. Select retrieves data from one or more
tables and produces the result in a datasheet. The Select Query can be developed
by using Query Wizard or through the Design View. Now, we will learn the
techniques of Query development with Design View. From Object, click on
Queries and click Create Query in Design View. A window as shown in Figure
5.8 will be displayed.
TOPIC 5 DATABASE DEVELOPMENT I W 75

Figure 5.8: Windows for producing query through Query by Example

Through the Show Table window, you can choose the table to be used in queries.
For this example, choose the Mentoree Table and press the Add button. The
Mentoree table will appear. The Query window will be divided into two sections.
The upper section will be left blank initially and will be filled with information
on the schema table involved in query.

The lower section of the query consists of a table with field, table, show and
criteria rows. Assume that you want to present all data in Mentoree table; you
need to click on Field, click on the arrows pointing downwards and choose
which domain needs to be displayed. Then, click on the Show box as shown in
Figure 5.9. To execute the query, click on the exclamation sign in red on the
toolbar menu.

Figure 5.9: Field selected for view in query product


76 X TOPIC 5 DATABASE DEVELOPMENT I

The result of this query is the records of the mentoree table that will displayed, as
shown in Figure 5.10. If we execute the same query to the mentor table, the result
will be as shown in Figure 5.11.

Figure 5.10: Record of mentoree

Figure 5.11: Record of mentor

Based on the records from the mentoree table, we can fill up the schema as
shown in Figure 5.12 below, to search for the mentoree who registered for the BIT
or DIT programme. The result from this query can be found in Figure 5.13

Figure 5.12: Query Search for mentoree who registered for BIT or DIT programme
TOPIC 5 DATABASE DEVELOPMENT I W 77

Figure 5.13: The result of the query to find the mentoree who
registered for the BIT or DIT programme
Most of the created queries involve more than one table. Figure 5.14 shows a
schema to retrieve all mentorees with mentors who are lecturers in the faculty of
information technology. This is where the query would need the combination of
mentoree and mentor tables.

Figure 5.14: Query to retrieve all mentorees with mentors who


are lecturers in the faculty of information technology

The result of this query can be found in Figure 5.15 as shown below:
78 X TOPIC 5 DATABASE DEVELOPMENT I

Figure 5.15: The result of the query to retrieve all mentorees with
mentors who are lecturers in the faculty of information technology

Some queries, the order of the query result give some important rule. Therefore,
Access provides a function that allows the result of query being arranged
according to the query requirement.

To arrange the displayed record, you can use the sort function. The field which
the record can be sorted is known as sort key. To sort record in Access, need to
state the type of sort in sort field as shown in Figure 5.16.

Figure 5.16: The query for displays Name, Matric_Number, Program and address that
need to be sorted by using the field of Program as ascending

Figure 5.16 refers to a query for displaying a record of student that be sorted by
Program field. The result of the query is shown in figure 5.17.
TOPIC 5 DATABASE DEVELOPMENT I W 79

Figure 5.17: The result of query for sorting the fields of Program as ascending

QBE can also use operational aggregate such as Sum, Avg, Min, Max and Count.
To implement this we need to add rows for the Total field by selecting the View-
Totals.

The following schema shows the query on number of mentorees for each mentor.
The attribute Name has a Group value in Total rows. This shows that the
attribute Name is used to collect rows in the mentoree table. The total field has
the operational Count in each group (Figure 5.18). Meanwhile, Figure 5.19 shows
the result of the query in Figure 5.18.

Figure 5.18: Query to count the number of mentorees for each mentor
80 X TOPIC 5 DATABASE DEVELOPMENT I

Figure 5.19: The result of the query that counts the number of mentorees for each mentor

5.5.2.1 Create a Query Field


Query field is a virtual field in a query where the value of the virtual field is a
function for one or several fields in a table. To create this query field, we need to
create one field to the combine matric_no and name..

The syntax for this query field is:


<name of query field>:<definition>

The name of the query field should be different from any existing fields, while
the definition is an expression that can be evaluated by Access.
In the example given, the syntax for the query field is:
Mentoree: matric_no & name
TOPIC 5 DATABASE DEVELOPMENT I W 81

Figure 5.20: SQL query to create query field that will


display the metric number and name in one field

Figure 5.20 shows the process to create the query field. In this example,
expression involves the tow field which is field_no and name (from the mentoree
table). The operator symbol & is used to combine data from these two fields.
Similarly the symbol is used to create empty spaces between these two fields.
The result from this query is shown in Figure 5.21 below:

Figure 5.21: Result from SQL


query for query field
82 X TOPIC 5 DATABASE DEVELOPMENT I

SELF-CHECK 5.3

1. Create a stationery table with field and data below:

Item Price Quantity


A4 Paper 8.00 2
2B Pencil 2.00 5
Paper file 5.00 4
Clip 2.00 3

2. Produce a query as shown in the figure below:

You are required to create query filed named total to calculate as


below:
Total : [Price] * [Quantity]
Run this query to get the result.
TOPIC 5 DATABASE DEVELOPMENT I W 83

5.5.3 SQL Query


What is the difference between query that uses SQL and QBE? QBE and SQL are
known as industrial standard language that could produce information from a
database. The advantages of QBE are the graphical interface and easier to use for
user. Meanwhile, the advantage of SQL is the popular usage among the
developer of database management system.

5.5.4 To Create a SQL Query


As you have leant before, SQL is a query form that containing basic components
as follow:

1. SELECT <field1, field2, , fieldn>


This component is to identify all fields that will be manipulated.

2. FROM <table>
This component is to identify table (or tables) that involved in the query.

3. WHERE <condition1 AND/OR condition2, conditionN AND / OR


conditionN>
This component is to identify one or more condition that need to be fulfilled
by each record to be inserted in set of result for the query.

4. ; <semi colon>
All query of SQL must end by semi colon. If you are forgotten to include it,
Access will put it automatically.
84 X TOPIC 5 DATABASE DEVELOPMENT I

Figure 5.22: Query display in SQL mode

An example to shows the usage of the SQL as shown in figure 5.23. This figure
shows the SQL command that need to find the names of mentorees whose are
under supervision Mentor who have ID = P00001.

Figure 5.23: SQL command to find the names of mentorees whose are under supervision
Mentor with ID =P00001

The result of this query as shown in figure 5.24. The result displays the name list
of Mentorees whose are under supervision Mentor with ID =P00001.
TOPIC 5 DATABASE DEVELOPMENT I W 85

Figure 5.24: The result of SQL


command to find the names
of mentorees whose are
under supervision Mentor
with ID =P00001

5.5.5 The Usage of Multiple Criteria (Compound)


Normally, query requires user to find data that fulfill several criteria. The usage
of more than one criterion is called as multiple criteria (compound). There are
two multiple criteria which is AND and OR criteria.

For AND criteria, each criterion certainly fulfilled to enable multiple criteria
accurate. For example, AND criteria enable user to find name list of mentorees
whose are under supervision Mentor with ID =P00001 and the mentorees also
registered BITM program. The SQL command to fulfill those criteria is shown in
figure 5.25 below:

Figure 5.25: SQL command to retrieve the names of mentorees whose are under
supervision Mentor with ID =P00001 and registered BITM program

The result that acquired from SQL command that was given from figure 5.25 is
shown as figure 5.26.
86 X TOPIC 5 DATABASE DEVELOPMENT I

Figure 5.26: The result of SQL command to find the names of mentorees whose are under
supervision Mentor with ID =P00001 and registered BITM program

Difference with AND criteria, OR criteria requires only one criteria to fulfilled to
enable the result of the query are accurate. This example is to retrieve the name
of mentorees whose are registered in BITM program and under supervision
mentor which either ID P00001 or P00003. The SQL command is shown in Figure
5.27.

Figure 5.27: SQL command to retrieve the names of mentorees whose are registered in
BITM program and under supervision mentor which either ID P00001 or P00003
TOPIC 5 DATABASE DEVELOPMENT I W 87

The result of the query is the name list of mentorees that fulfilled one of those
criteria will be displayed as shown in Figure 5.28.

Figure 5.28: The result of SQL command to find the names of mentorees whose are
registered in BITM program and under supervision mentor which either ID P00001 or
P00003

SELF-CHECK 5.4

Bellow is the employee table for MajuJaya Company:

IDNo Name Position Gender Age Salary


100 Jenny Toh Technician F 22 1,000
200 Mohd Kamal Executive M 28 2,500
300 Zafrul Azlan System M 28 2,800
Analyst
400 Siti Fatimah Secretary F 25 2,000
500 Usha Manager F 35 4,000
600 Amirah Zulaikha System F 25 2,800
Analyst

Create a query for each criteria below:


(i) List all the employee with gender is Female.
(ii) List all employee with salary more than RM 2500
(iii) List name and age for employee with the age below than 30 years
old and salary more than RM 2500
88 X TOPIC 5 DATABASE DEVELOPMENT I

Knowledge on Microsoft Access usage can be used to develop a complete


database with tables, queries, forms and report. In the process of table
development, there are several things that need to be known in order to
determine the element of field. The usage of query by QBE or SQL can assist
developers to acquire information regarding a database.

Microsoft Access
Database

S-ar putea să vă placă și