Sie sind auf Seite 1von 157

Faculty of Science and Technology

CBDB3203
Database Implementation

Copyright Open University Malaysia (OUM)


CBDB3203
DATABASE
IMPLEMENTATION
Dr Shahrul Azman Mohd Noah
Zaihosnita Mohd Hood
Mariayati Mohd Yusof
Kamsuriah Ahmad
Suhaila Zainuddin
Rohaizak Omar

Copyright Open University Malaysia (OUM)


Project Directors: Prof Dato Dr Mansor Fadzil
Assoc Prof Dr Norlia T. Goolamally
Open University Malaysia

Module Writers: Dr Shahrul Azman Mohd Noah (Leader)


Zaihosnita Mohd. Hood
Mariayati Mohd Yusof
Kamsuriah Ahmad
Suhaila Zainuddin
Universiti Kebangsaan Malaysia

Rohaizak Omar
Open University Malaysia

Editors: Nor Aisyah Fadil


Majumin Hanum Abdul Samad
Open University Malaysia

Translator: Maszalida Hamzah

Reviewed: Ahmad Zambri Shahuddin

Developed by: Centre for Instructional Design and Technology


Open University Malaysia

First Edition, August 2008


Copyright Open University Malaysia (OUM), March 2011, CBDB3203
All rights reserved. No part of this work may be reproduced in any form or by any means
without the written permission of the President, Open University Malaysia (OUM).

Copyright Open University Malaysia (OUM)


Table of Contents
Course Guide ix - xiv

Topic 1 Introduction to SQL 1


1.1 Background of SQL 2
1.2 History of SQL 3
1.3 Basic Structure of SQL Statements 4
1.4 Four Queries for SQL Development 7
1.4.1 What is the Required Output? 7
1.4.2 What Have You Learnt? 8
1.4.3 What are the Tables Involved? 8
1.4.4 How are Tables Linked? 8
Summary 9
Key Terms 10

Topic 2 Data Definition Language 11


2.1 Data Relationship Structure 12
2.2 Data Definition in SQL 14
2.2.1 SQL Data Types and Identifiers 14
2.3 Definition a Database Schema 16
2.4 Creating a Tables 16
2.5 Removing a Tables 17
Summary 18
Key Terms 19

Topic 3 Data Management 20


3.1 Insert Command 21
3.2 Update Command 22
3.3 Delete Command 23
Summary 25
Key Terms 25

Topic 4 Query Data 26


4.1 Solution Strategy 27
4.2 Simple Query 27
4.2.1 To Retrieve all Rows and Tuples 28
4.2.2 To Retrieve Specific Rows and Tuples 28

Copyright Open University Malaysia (OUM)


iv X TABLE OF CONTENTS

4.2.3 DISTINCT Application 29


4.3 To Select Rows 31
4.3.1 To Retrieve Tuples and Select Rows 31
4.3.2 Rename Tuples 32
4.3.3 Comparison between Ranges 33
4.3.4 Rules with Multiple Comparisons 33
4.3.5 Comparing Sequence 35
4.3.6 Design Correspondence 36
4.3.7 NULL Search (IS NULL/IS NOT NULL) 37
4.4 Sorting the Result 38
4.4.1 Sorting One Attribute 38
4.4.2 Sorting More than One Attribute 39
4.5 Aggregate Function 40
4.5.1 COUNT (*) Application 41
4.5.2 COUNT and DISTINCT Application 41
4.5.3 COUNT and SUM Application 42
4.5.4 MIN, MAX and AVG Application 42
4.6 Gathering Results 43
4.6.1 GROUP BY Clause 43
4.6.2 HAVING Clause 45
4.7 Sub-Queries 46
4.7.1 Using a Sub-query with Equality 46
4.7.2 Using a sub-query with an Aggregate Function 47
4.7.3 Nested Sub-queries (Use of IN) 48
4.7.4 NOT IN Clause 50
4.8 Set Comparison Using ANY, SOME and ALL 51
4.8.1 ANY and SOME Clause 51
4.8.2 ALL Clause 52
4.9 Multiple Table Statement 54
4.9.1 Simple Union 54
4.9.2 Structuring Union 55
4.10 EXIST and NON EXIST Clause 56
4.10.1 EXIST Clause 56
4.11 Combining Table 57
4.11.1 UNION Clause 58
4.11.2 INTERSECT Clause 58
4.11.3 EXCEPT Clause 59
Summary 61
Key Terms 61

Copyright Open University Malaysia (OUM)


TABLE OF CONTENTS W v

Topic 5 Database Development I 62


5.1 Introduction to Access 62
5.2 Creating an Access Database 64
5.3 Creating Tables 67
5.3.1 Field 67
5.3.2. Primary Key 68
5.3.3 Field Element 68
5.4 Relationship 71
5.4.1 Define Relations and Referential Integrity 71
5.5 Creating Queries 74
5.5.1 Query by Example 74
5.5.2 Developing Query by Example 74
5.5.2.1 Create a Query Field 80
5.5.3 SQL Query 83
5.5.4 SQL Query Creation 83
5.5.5 The Usage at Multiple Criteria 85
Summary 88
Key Terms 88

Topic 6 Database Development II 89


6.1 Form 89
6.1.1 Creating a Form 90
6.1.2 Creating a Sub-form 92
6.1.3 The Guideline of Form Design 96
6.2 Producing a Report 96
6.2.1 The Guideline of Report Design 99
6.3 Switchboard 100
6.3.1 Creating a Switchboard 100
Summary 103
Key Terms 103

Topic 7 Transaction Management 104


7.1 Transaction Support 105
7.1.1 Properties of Transaction 106
7.2 Concurrency Control 109
7.2.1 The Requirement 110
7.2.2 Concurrency Control Techniques 113
7.2.3 Deadlock 117
7.2.4 Time Stamping 117
7.2.5 Optimistic Technique 118
Summary 119
Key Terms 119

Copyright Open University Malaysia (OUM)


vi X TABLE OF CONTENTS

Topic 8 Databases And Web Technology 120


8.1 An Introduction to the Internet and Web 121
8.1.1 The Web 122
8.1.2 HTTP 123
8.1.3 HTML 123
8.1.4 URL 124
8.1.5 Static and Dynamic Web Pages 124
8.2 The Web as a Database Application Platform 126
8.2.1 Requirements for Web-DBMS Integration 126
8.2.2 Web-DBMS Architecture 127
8.2.3 Advantages and Disadvantages of the
Web-DBMS Approach 129
8.2.4 Approaches to Integrating the Web and DBMS 132
Summary 134
Key Terms 134

Tutorial 135

Copyright Open University Malaysia (OUM)


COURSE GUIDE

Copyright Open University Malaysia (OUM)


Copyright Open University Malaysia (OUM)
COURSE GUIDE ix

COURSE GUIDE DESCRIPTION


You must read this Course Guide carefully from the beginning to the end. It tells
you briefly what the course is about and how you can work your way through
the course material. It also suggests the amount of time you are likely to spend in
order to complete the course successfully. Please keep on referring to the Course
Guide as you go through the course material as it will help you to clarify
important study components or points that you might miss or overlook.

INTRODUCTION
CBDB 3203 Database Implementation is one of the courses offered by the Faculty
of Science and Technology at Open University Malaysia. This course is worth 3
credit hours and should be covered over 8 to 15 weeks.

COURSE AUDIENCE
This course is targeted to all the IT students especially those specialising in
Computer System or Information System who needs to understand how database
is developed using entity relationship approach. Students enrolled in other IT-
related specialisation also will find this course useful as this course will answer
many of their questions regarding database system development.

As an open and distance learner, you should be able to learn independently and
optimise the learning modes and environment available to you. Before you begin
this course, please confirm the course material, the course requirements and how
the course is conducted.

STUDY SCHEDULE
It is a standard OUM practice that learners accumulate 40 study hours for every
credit hour. As such, for a three-credit hour course, you are expected to spend 120
study hours. Table 1 gives an estimation of how the 120 study hours could be
accumulated.

Copyright Open University Malaysia (OUM)


x COURSE GUIDE

Table 1: Estimation of Time Accumulation of Study Hours

Study
Study Activities
Hours

Briefly go through the course content and participate in initial discussions 3

Study the module 60


Attend 5 tutorial sessions 10
Online participation 12
Revision 15
Assignment(s), Test(s) and Examination(s) 20

TOTAL STUDY HOURS 120

LEARNING OUTCOMES
By the end of this course, you should be able to:
1." Describe the meaning and application of SQL and its importance;
2." Apply the Microsoft Access effectively to develop a complete database;
3." Define the meaning of transaction and its benefits; and
4." Explain the advantages and disadvantages of web as database platform.

COURSE SYNOPSIS
This course is divided into 8 topics. The synopsis for each topic is presented
below:

Topic 1 introduces to the background of SQL concept such as the meaning and
application of SQL and its importance. Basically, there are two components
involved in the SQL language which are Data Definition Language and Data
Manipulation Language. These languages are important in enabling us to identify
and update a database. The most important thing in this topic is the
understanding of the basic structure of SQL statement. By understanding this
concept you will know how to develop the SQL query.

Topic 2 defines what is meant by Data Definition Language, a component of the


SQL Language, and how this language is used to define or create a database. As

Copyright Open University Malaysia (OUM)


COURSE GUIDE xi

we have already known, the main step involved in developing a database


application is to create a database schema by defining the data used in a database.
You will also learn how to delete and create tables.

Topic 3 discusses Data Management Language. The Data Management Language


is part of the Data Manipulation Language used to update data within the
database. Among the commands found in Data Management are INSERT,
DELETE and UPDATE. Each of these commands has its own function which will
be explained later. In this topic you will also learn how the commands can be
applied and examples of its usage. The implementation of these commands is
important as they form the basic commands for data management. Without these
basic commands the data stored may not be significant and may not be organised
or managed properly.

Topic 4 describes the SQL instructions, beginning from simple queries that
involve the table, and proceed to complex queries used for data query. For this
purpose, your will be given an example of database schema as the case study.
You will be guided on how to retrieve all data from rows and tuples by using the
simple query and also using comparison method to retrieve more specific data.
This topic also teaches you how to sort the data result according to value or
particular attributes. Other functions such as aggregate function, function for
gathering result, combination table that involve several clauses will also be
discussed through this topic.

Topic 5 explains the basic development of a simple database. This topic will begin
with the introduction of the concept of database such as DBMS, data field
elements and the basic concept of relationship. 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, it will focus only
on how to create the table and query by using QBE and SQL. 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.

Topic 6 discusses about Microsoft Access. You will learn how to create form and
sub-form to provide users with an interface for data in database application. This
is followed by producing of report to represent the content of the data in a simple
form. Some guideline for form and report design will also be discussed. At the
end of this topic, you will be introduced to switchboard element which is the

Copyright Open University Malaysia (OUM)


xii COURSE GUIDE

special type of form which consists of several buttons that can be clicked to
execute a particular operation.

Topic 7 introduces to transaction and its importance in the implementation of a


database. You will also learn the four main aspects to be fulfilled by a particular
transaction which is atomicity, consistency, isolation and durability, also known
as ACID. With the corresponding example, this topic explains transactions and
their characteristics. You will also learn several problems relating to transaction
management. Once these problems have been addressed, you will learn how to
implement concurrency control in order to solve the problem that arises.

Topic 8 describes how the web is used as a platform to enable users to


communicate in web database. In addition, several architectures to obtain this
synchronisation will be discussed. Lastly, the strengths and weaknesses of the
process will be explained.

TEXT ARRANGEMENT GUIDE


Before you go through this module, it is important that you note the text
arrangement. Understanding the text arrangement should help you to organise
your study of this course to be more objective and more effective. Generally, the
text arrangement for each topic is as follows:

Learning Outcomes: This section refers to what you should achieve after you
have completely gone through a topic. As you go through each topic, you should
frequently refer to these learning outcomes. By doing this, you can continuously
gauge your progress of digesting the topic.

Self-Check: This component of the module is inserted at strategic locations


throughout the module. It is inserted after you have gone through one sub-
section or sometimes a few sub-sections. It usually comes in the form of a
question that may require you to stop your reading and start thinking. When you
come across this component, try to reflect on what you have already gone
through. When you attempt to answer the question prompted, you should be
able to gauge whether you have understood what you have read (clearly, vaguely
or worse you might find out that you had not comprehended or retained the sub-
section(s) that you had just gone through). Most of the time, the answers to the
questions can be found directly from the module itself.

Activity: Like Self-Check, activities are also placed at various locations or junctures
throughout the module. Compared to Self-Check, Activity can appear in various
forms such as questions, short case studies or it may even ask you to conduct an
observation or research. Activity may also ask your opinion and evaluation on a

Copyright Open University Malaysia (OUM)


COURSE GUIDE xiii

given scenario. When you come across an Activity, you should try to widen what
you have gathered from the module and introduce it to real situations. You should
engage yourself in higher order thinking where you might be required to analyse,
synthesise and evaluate instead of just having to recall and define.

Summary: You can find this component at the end of each topic. This component
helps you to recap the whole topic. By going through the summary, you should
be able to gauge your knowledge retention level. Should you find points inside
the summary that you do not fully understand, it would be a good idea for you to
revisit the details from the module.

Key Terms: This component can be found at the end of each topic. You should go
through this component to remind yourself of important terms or jargons used
throughout the module. Should you find terms here that you are not able to
explain, you should look for the terms from the module.

References: References is where a list of relevant and useful textbooks, journals,


articles, electronic contents or sources can be found. This list can appear in a few
locations such as in the Course Guide (at References section), at the end of every
topic or at the back of the module. You are encouraged to read and refer to the
suggested sources to elicit the additional information needed as well as to
enhance your overall understanding of the course.

PRIOR KNOWLEDGE
Students should have taken a Data Analysis and Modeling course and are
expected to have a basic knowledge of the windows operation system and
Microsoft Access application.

ASSESSMENT METHOD
Refer to myINSPIRE.

REFERENCES
Abdullah Embong. (2000). Sistem pangkalan data. Kuala Lumpur: Tradisi Ilmu
Sdn. Bhd.

Atzeni et.al, (2000). Database systems: Concepts, languages and architectures.


New Jersey: McGraw-Hill.

Copyright Open University Malaysia (OUM)


xiv COURSE GUIDE

Connolly & Begg. (2001). Database systems: A Practical approach to design,


implementation, and management. (2nd ed.). Addison-Wesley.
Salha Abdullah & Nazlia Omar. (2000). Sistem pangkalan data. Kuala Lumpur:
Sprint Print, Prentice Hall.

Silberschatz, A., Korth, H., F & Sudarshan, S. (1997). Database system concept.
(3rd ed.). New Jersey: McGraw-Hill.

Copyright Open University Malaysia (OUM)


Topic X Introduction
to SQL
1
LEARNING OUTCOMES
By the end of this topic, you should be able to:
1. Explain the meaning and application of SQL and its importance.
2. Present the basic structure of SQL and transform-oriented language;
and
3. List and answer from queries before building an SQL.

X INTRODUCTION
In the previous course, you learnt that data model relations form the basis of
database technology. Data technology comprises a strong basic structure based on
the relation theory in mathematics. Currently, a simple and user-friendly query
language in commercial applications is high in demand.

Structured Query Language or SQL is a language derived from the


development of data model relations. It has become the standard language for
database relationship. This standard was defined by the American National
Standards Institute (ANSI) in 1986 and certified by the International Standards
Organisation (ISO) in 1987. Currently, there are more than 100 database
management systems supporting the SQL and it is applicable in various computer
environments, from personal computers to mainframes.

This topic will explain what SQL is and the importance of learning the language
in building a current database application.

ACTIVITY 1.1

Copyright Open University Malaysia (OUM)


2 X TOPIC 1 INTRODUCTION TO SQL

1.1 BACKGROUND OF SQL

SQL is a query language for database. You have learnt that there are many
software applied in a database such as Access, Oracle and others. Is SQL suitable
for all types of database software?

Why do you need a query language? In reality, a structured and systematic query
system is required to minimise errors in interpreting the required space. We also
require a standard system which is applicable to various peripherals and database
systems from different suppliers. This enables users and developers to learn
techniques conforming to most database systems.

Ideally, a database language enables users to:


(i) Create the database and relation structure;
(ii) Perform insertion, modification and deletion of data from the relationship;
and
(iii) Perform both simple and complex queries.

SQL was introduced to fulfill the above requirements and it is the most widely
used database language.

In addition, a database language must perform these tasks with minimal user
effort. Its command structure and syntax must be relatively easy to learn and most
importantly the language must be portable, that is, it must conform to some
recognised standard so that the user can use the same command structure and
syntax when moving from one DBMS to another. SQL is intended to satisfy these
requirements.

As a language, SQL has two major components:

(i) A Data Definition Language (DDL) for defining the database structure and
controlling access to data; and
(ii) A Data Manipulation Language (DML) for retrieving and updating data.

SQL is easy to learn because:

(i) It is a non-procedural language; users only specify what information is


required and not the access methods to data;

(ii) It is essentially a free-format, meaning the format is easy to understand;

Copyright Open University Malaysia (OUM)


TOPIC 1 INTRODUCTION TO SQL W 3

(iii) Its application is in the standard English language;

(iv) It is not dependent on capital and small alphabets but it must be typed as
per the content of the database; and

(v) It can be used by a range of users such as database administrators (DBA),


programmers and end users.

Information on SQL may be obtained from many books. Among those that can
enrich your knowledge is Database System; Principles, Design and
Implementation written by Catherine Ricardo, published by New York McMillan,
1990.

Before you proceed, attempt the following questions based on the basic concepts
you have just learnt.

SELF-CHECK 1.1

1. What is SQL and state its importance?

2. What are the two main components of SQL and state the function
of each component?

1.2 HISTORY OF SQL

ACTIVITY 1.1

SQL is a structured query language used to develop a database and to


retrieve important data in a database. How has SQL developed since its
inception?

The history of the relational model started with the publication of the seminal
paper by E.F. Codd in 1970 while he was with IBM. At that point in time,
"Structured English Query Language (SEQUEL) was introduced as an upgraded

Copyright Open University Malaysia (OUM)


4 X TOPIC 1 INTRODUCTION TO SQL

version of SEQUEL. A revised version, SEQUEL/2, was defined in 1976, but the
name was subsequently changed to SQL for legal reasons as SEQUEL was used

by other parties. SQL was then introduced in a DBMS command by the company
ORACLE.

Each implementation of SQL by particular companies is known as a dialect.


Currently, there are several SQL dialect languages in the market, each with
additional or variant versions of the standard required. In 1986, a standard
version of SQL was used by ANSI. The SQL version, according to this standard,
was known as SQL-86. An additional feature was introduced in the 1989
standards. The most current or updated version of DBMS supports SQL-89. A
revision on SQL standards was built in 1992 and it resulted in the birth of SQL-92
or SQL2. Recent development has shown that SQL 3 is underway.

SQL is the first standard database language to gain wide acceptance. It is an


important language in most commercial DBMS in various companies and
organisations as a standard global reference. The SQL ISO standard does not use
the formal terms of relations, attributes and tuples; instead it uses the terms table,
column and rows. It should also be noted that SQL does not adhere strictly to the
definition of the relational model and furthermore, SQL allows for repeated rows.
SQL permits users to order the rows of a table. It also allows the null value to state
non or unknown values.

Visit http://www.sqltraining.org for further information on SQL.

1.3 BASIC STRUCTURE OF SQL STATEMENTS

ACTIVITY 1.2

You know that SQL is a structured language; can you think of why it
is referred to as structured?

Each query in the SQL language consists of three basic clauses in the following
format

Copyright Open University Malaysia (OUM)


TOPIC 1 INTRODUCTION TO SQL W 5

SELECT (DISTINCT | ALL) column_name


FROM table name
WHERE condition

We can make several observations as follows:

(a) SQL statements are words defined by SQL and words defined by users.
Words defined by SQL are known as reserved words with unchangeable
meaning. Words defined by users are normally for tables, column, views
and situations. All these must adhere to the fixed syntax.

(b) Although SQL statements are case insensitive, to simplify your


understanding, we shall adhere to the following indentation and lineation.

(i) Upper case letters are used to represent reserved words and lower case
letters are used to represent users-defined words.

(ii) A vertical bar ( | ) indicates a choice, for example a choice between x, y


and z is written as X | y | and z.

(iii) Square brackets [ ] indicate optional elements; for example [ X ].

(iv) Curly braces { } indicate a required element, for example { X } means X


is required.

(v) Literals must be typed as stores in the database. Literals are constants
in SQL statements. All literals must be enclosed in single quotes, for
example "Database", unless it is numeric.

Although SQL is a free format for standardising and easy reading we shall follow
the following formats:

(a) Each clause in a statement should begin with a new line.


(b) The beginning of each clause should be lined up with the beginning of other
clauses.

Let us look at the syntax and meaning of each SQL clause.

(a) SELECT column_name


The SELECT clause is followed by one or more column names required in
an output. Column name refers to tables defined in FROM clause. The
comma (,) is located between column names if more than one column is
listed.

Copyright Open University Malaysia (OUM)


6 X TOPIC 1 INTRODUCTION TO SQL

(b) FROM table name


The FROM clause is followed by one or more table name. This table will be
scanned by the system. The comma (,) is located between table names if
more than one table is listed.

(c) WHERE condition


The WHERE commands is followed by situation K, which is a BOOLEAN
phrase. K is a combination of sequences connected by AND, OR and NOT.
The sequence is as follows:

X operator Y

where X and Y represent the column name, constants or arithmetic statement. An


operator is a comparison element in SQL statement (see Table 1.1):

Table 1.1: Operators and Their Meaning.

Operator Meaning
= equals to
< less than
> more than
<= less or equals to
>= more or equals to
<> not equal to

In short, K states the condition of how a column name is in SELECT command.


The command WHERE is a choice and if it is not stated, K will always be true.

(d) DISTINCT I ALL


Distinct or ALL are words defined by SQL according to the SELECT clause.
The purpose of DISTINCT is to delete repeatable rows from the table
produced. Instead, ALL defines all repeated rows and is maintained. ALL is
a default value, DISTINCT is a choice and if it is left, SQL will allow
repeatable rows in tabling output.

The following exercise will assist you in better understanding the topic you have
just learnt.

Copyright Open University Malaysia (OUM)


TOPIC 1 INTRODUCTION TO SQL W 7

SELF-CHECK 1.2

1. What is meant by these statements?


(a) SQL language dialect; and
(b) SQL reserved language.

2. Write an example of an SQL command using three main clauses.

Once you have learnt the basic SQL structure, let us rest our mind before
proceeding to the next section.

1.4 FOUR QUERIES FOR SQL DEVELOPMENT


Before we try accessing a relations database, ask yourself four more questions:

1. What is the output required?


2. What have you known (what are the restrictions involved)?
3. What are the tables involved?
4. How are tables linked?

You must remember these four questions without thinking of the SQL structure.
In creating an SQL, develop a habit of answering questions. You may be able to
answer simple questions automatically but for complex questions, you may need
to answer the question part by part. Proceed to the other questions until you are
able to comprehend each question as a whole.

1.4.1 What is the Required Output?


Normally, this question is answered by selecting columns from several tables
stored in the database. You need to identify the column names to answer this
question. The most difficult part is to see the entire table list and identify the
required column. It will be even more difficult if the database has hundreds of
tables and thousands of columns to choose from.

Copyright Open University Malaysia (OUM)


8 X TOPIC 1 INTRODUCTION TO SQL

You can also command a system to calculate arithmetic equations such as average
amount, plus, minus, multiply and divide.

1.4.2 What Have You Learnt?


In many instances, you are required to limit your search based on a particular
character. For example, you may be interested in sales from several departments
or sales derived from one department only. A search must be changed to a
standard BOOLEAN notation (AND or OR statement). The most important aspect
in this move is to rewrite all situations/occurrences that can assist you in
understanding the purposes of statement required.

1.4.3 What are the Tables Involved?


With minimal number of tables, quarters are easy to handle but with hundreds of
tables, you may require more time in determining which table is required. With
the help of a data dictionary, you should be able to create queries. It is advised
that tables be given names that correspond to the content and purpose of the
development.

1.4.4 How are Tables Linked?


This question is related to the issue of modeling data that is most important in
relations database. Each table is linked by the data in the same column. For
example, the table STUDENT is linked to the column MatricNum. The data in the
table COURSE is also linked to the column name MatricNum. In many cases, a
column linked to the table has the same name (for example, MatricNum).
However, in others situation, not all columns have same names. For example, the
table GRADE may have the column name StudentNum in order to refer to the
column MatricNum in the table STUDENT.

Copyright Open University Malaysia (OUM)


TOPIC 1 INTRODUCTION TO SQL W 9

ACTIVITY 1.3

The following queries are related to a university environment. Most of


the queries are columns as they involve only one table. These are
common queries by an administrator or dean of a particular faculty. Give
your opinion on four of the queries you below.

 List all A grade students in the TM30202 course.


 State the percentage of students according to race in each academic
year.
 State the percentage of male students compared to the female
students in one academic year.
 List all students with CGPA 2.0 and below.
 List all students graduated with first class honours.

ACTIVITY 1.4

If you are a manager of a computer peripheral sales company, what


are the questions that may occur to ensure the management of a
company is safeguarded? State three queries.

x You have learnt what an SQL statement is and the importance of learning this
language. Two components involved in the SQL language are the Data
Definition Language and Data Manipulation Language. These languages are
important in enabling us to identify and update a database.

x Before any queries in the SQL language are developed, we have to ask four (4)
important questions:
x What is the output required?
x What have you learnt? What are the restrictions?
x What are the tables involved?
x How are the tables linked?

Copyright Open University Malaysia (OUM)


10 X TOPIC 1 INTRODUCTION TO SQL

x By answering all the questions involved, you will be able to construct the SQL
query language. It is hoped that you will be able to comprehend a higher level
of SQL language in the next topic.

Structured Query Language

Copyright Open University Malaysia (OUM)


Topic X Data
Definition
2 Language

LEARNING OUTCOMES
By the end of this topic, you should be able to:
1. Create a database scheme;
2. Use the DROP TABLE command to eliminate tables produced by
a database; and
3. Use the CREATE TABLE command to produce tables that will be
used in a database.

X INTRODUCTION
In the previous topic, you learnt what the SQL language is and the components
involved in developing this language. In this topic, you will learn what is meant
by Data Definition Language, a component of the SQL Language,, and how this
language is used to define or create a database.

As we have already known, the main step involved in developing a database


application is to create a database schema by defining the data used in a
database. You will also learn how to delete and create tables.

Copyright Open University Malaysia (OUM)


12 X TOPIC 2 DATA DEFINITION LANGUAGE

2.1 DATA RELATIONSHIP STRUCTURE


A university environment is used as a case study to enable the learning of
commands in a language. Figure 2.1 is an entity of STUDENTS, COURSE,
REGISTRATION and TUTOR.

ACTIVITY 2.1
In the previous topic, you learnt about the structure of SQL statements.
Now you will learn the structure of data relations. Do you know what is
meant by data relations?

Figure 2.1: Entity relations diagram

Based on the ER Model, the STUDENT, COURSE, REGISTRATION and TUTOR


tables are generated. The tables are presented as follows:

Student
MatricNo StudentName Major
A1000 John Science Computer
A1001 Fatimah Multimedia
A1002 Ali Science Information
A1003 Muthu Networking
A1004 Ah Chong Multimedia

Copyright Open University Malaysia (OUM)


TOPIC 2 DATA DEFINITION LANGUAGE W 13

Course
CourseNo CourseName Tutor No
SK100 Java P1000
MM100 Graphic P1000
SM100 Search Engine P3000
RK200 Database P3500
MM200 Authoring Tool P3000

Registration
MatricNo CourseNo Grade Value
A1000 RK200 B 3.00
A1000 SK100 C+ 2.33
A1001 MM200 B- 2.67
A1001 SK100 A 4.00
A1002 SM100 A- 3.67
A1004 MM200 B 3.00
A1004 RK200 C 2.00

Tutor
TutorNo TutorName Salary (RM) Position

P1000 Kamal 3,000 Lecturer

S1500 Sanusi 1,000 Tutor

S2000 Chong 2,700 Assist Lecturer

P2500 Hasnah 3,000 Lecturer

P3000 Salleh 3,500 Lecturer

P3500 Zainab 3,300 Lecturer

S3000 Mary 2,900 Assist Lecturer

* Salary is presented monthly.

Data Definition Language (DDL) is explained in the book Database Systems: Concept,
Languages and Architecture written by Atzen et al, published by McGraw-Hillin year
2000.

Copyright Open University Malaysia (OUM)


14 X TOPIC 2 DATA DEFINITION LANGUAGE

2.2 DATA DEFINITION IN SQL


You have heard of topics on DDL in the previous topics. It explains the definition
of set relations and information in each relation. The information consists of:
(i) Relations schema
(ii) Table structure
(iii) Data type and domain for each attribute or column.

ACTIVITY 2.2

In your opinion, why is it important to define set relations and the


information in each relation?

2.2.1 SQL Data Types and Identifiers


Types of data supported by SQL are listed below.

Data String Type


String is divided into chart string and bit string.

Chart String may be declared through two forms:


CHAR (n)  String character consisting of n characters.
VARCHAR (n) - Where the number of string characters is changed from
one to n characters.

For example, if you declare a string as CHAR (10), but if it uses only seven
characters, then the three other spaces are filled with characters on the right and
your storage space in the memory is 10 characters. But if you declare a string as
VARCHAR (10) and only seven characters are used, the storage space in the
memory is seven characters only and as such, storage space can be saved.

Bit String is similar to character string. It can be declared as BIT (n) if the length is
fixed, or BIT VARYING (n) if the length varies. BIT consists of bit 0 or 1. Boolean
value T is represented by bit 1 and Boolean value F is represented by bit 0.

Numeric Data Type


Numeric data type consists of integer, decimal, float and real numbers. This is
explained in Table 2.1.

Copyright Open University Malaysia (OUM)


TOPIC 2 DATA DEFINITION LANGUAGE W 15

Table 2.1: Numeric Data Type

Numeric Data Details


Integer or INT for integer numbers
SMALLINT for small integers
DECIMAL (m,n) for decimal number with m digit amount and n decimal
points. Simplification of DECIMAL is DEC
NUMERIC (m,n) similar with DEC (m,n)
FLOAT for decimal point number
REAL for exact number
DOUBLE for exact number with required
PRECISION high precision

Date and Time Domain


Date types of domain are declared as DATE and true type of domain as
TIME. This is depicted in Table 2.2.

Table 2.2: Date and Time Domain

Data Type Details


DATE Consists of year, month and day
TIME Consists of hours and minutes

The types of data above are used by SQL to declare the attribute and column data
type. Column name is an identifier in SQL. SQL uses identifiers to identify or
name objects in a database such as a table and column name.

Identifiers can be defined as users or systems. Identifiers consist of set characters


allowed, which are letters a - z, capital lettesr A - Z, digits 0 - 9 and symbols " -".
An identifier can be no longer than 128 characters and must not contain a space.
Example of an identifier and declaration of data type is as follows.

Name CHAR (30)


Address VARCHAR
Working Hours TIME

Copyright Open University Malaysia (OUM)


16 X TOPIC 2 DATA DEFINITION LANGUAGE

2.3 DEFINITION OF A DATABASE SCHEMA


A database schema is a group of objects linked to each other. The objects in a
database schema may consist of tables, domains and a set of characters. The
creation and definition of database schema is implemented by the DBA or other
authorised personnel. The process of creating a database schema depends on the
implementation of a particular DBMS. Certain DBMS defines schema creation as
part of the DBMS process.

2.4 .4CREATING TABLES


The CREATE TABLE definition in the SQL command is used to create a database
schema. This statement enables a basic table and structure details to be
developed. The format is:

CREATE TABLE table_name


(column_name, data_type (NULL, NOT NULL)

The reserved words NULL and NOT NULL are a choice; a default value (values
keyed in automatically when a new application is built) is NULL. We will have to
determine the key column maintained as NOT NULL.

For example, if we are asked to build and define a table structure in a database
for a university environment, the format is:

CREATE TABLE Student (


MetricNo CHAR (5) NOT NULL,
StudentName VARCHAR (20)
Major VARCHAR (15));
CREATE TABLE Tutor (
TutorNo CHAR(5) NOT NULL
TutorName VARCHAR (20)
Position VARCHAR (15)
Salary DECIMAL (7,2));

In this example, only the definition of STUDENT and TUTOR is shown, the
COURSE and REGISTRATION table is left as an exercise for you. As you can see,
the column of salary is defined as DECIMAL (7,2) consisting of seven digits with
two decimal points.

Copyright Open University Malaysia (OUM)


TOPIC 2 DATA DEFINITION LANGUAGE W 17

2.5 REMOVING TABLES


Normally if you delete any files on your computer, it is sent to the Recycle Bin. In the
case of table deletion, will it appear in the Recycle Bin?

Generally, tables are created only once with certain values known as record.
The record can be added through INSERT INTO or deleted by the DELETE
FROM command. Over time, tables will not be required due to changes in
organisational structure. SQL allows a table to be dropped in the statement
below:

DROP TABLE table_name [ RESTRICT | CASCADE ]

Look at the following example to assist you in understanding the above situation.

Example
DROP TABLE Student

Solution

Table 2.3: Command to Remove Table

Command Details
DROP Removes tables and structure within it
Safeguards tables from being removed if the attribute is a
RESTRICT
foreign key
CASCADE Allows all attributes dependents on the table to be removed.

The command DROP TABLE is different from DELETE FROM because DELETE
FROM only drops rows but DROP TABLE removes both the table and its
structure. When a table is dropped, you are unable to add a value to it.

For further explanation and tutorial, view http://www.sqlcourse.com/ intro.html

Copyright Open University Malaysia (OUM)


18 X TOPIC 2 DATA DEFINITION LANGUAGE

These activity will assist you in better comprehending the topics you have just
learned. This concludes Topic 2.

ACTIVITY 2.3

Define the data type used for the following statements.

1. STATUS attribute represents value 1 if the status is active and 0 if


the status is non-active.

2. CGPA attribute consisting of number 0.00 - 4.00.

3. Date of Birth Attribute.

4. Remove STAFF table but the system must check if the table
consists of a foreign key and as such, removing is not allowed.

x You have learnt to create and define the database schema. In addition, SQL
enables you to remove the defined tables due to changes in the database
where tables are no longer needed. The following table shows types of data
used to define the database schema.

Table 2.4: Types of Data

Data Type Declaration


Character Char
Bit BIT BIT VARYING
Integer INTEGER INT SMALL INT
Point DECIMAL DEC NUMERIC
DOUBLE
Real Number FLOAT REAL
PRECISION
Date / time DATE TIME

x You have successfully completed this topic. The next topic will discuss on
data management.

Copyright Open University Malaysia (OUM)


TOPIC 2 DATA DEFINITION LANGUAGE W 19

Database Relationship Structure SQL Data Types


Database Scheme

Copyright Open University Malaysia (OUM)


T op i c X Data
3 Management

LEARNING OUTCOMES

By the end of this topic you should be able to:


1. Apply the updating process through the INSERT, UPDATE and
DELETE commands

X INTRODUCTION
Having learnt how to define data in a database, we will now discuss Data
Management Language. The Data Management Language is part of the Data
Manipulation Language used to update data within the database.

Among the commands found in Data Management are INSERT, DELETE and
UPDATE. Each of these commands has its own function which will be explained
later. In this topic you will learn how the commands can be applied and
examples of its usage. The implementation of these commands is important as
they form the basic commands for data management. Without these basic
commands the data stored may not be significant and may not be organised or
managed properly.

Copyright Open University Malaysia (OUM)


TOPIC 3 DATA MANAGEMENT W 21

3.1 INSERT COMMAND

The INSERT is used to add one row into an existing table through a format as
shown below:

INSERT INTO table_name [ column_list ]


VALUES (value_data_list)

where

(a) column_list is a list of column names to be added. You may choose to


ignore this list. You may also choose to list only a part of the column name
and this means that the column not stated takes up a null value.

(b) value_data_list is a list of value data to be added. This list must be similar
to the column_list stated or to the column name inside a table if the
column_list is not stated.

This example gives a clearer insight of the topic discussed.

Example:
To add new student into STUDENTS table.

Solution:
INSERT INTO Student
VALUE ( A1005, Faridah, Science Information)

In the example above, you have added new values into all columns. In this case,
list of column names is not stated. You also may insert a value into selected
columns, but the names of columns need to be stated as shown below:

INSERT INTO Registration ( MatricNo, CourseCode)


VALUE ( A100, RK200)

Take note:

(a) The structure of the column name is not required to correspond to the
structure of the column inside the table, but it is better if you follow these
structures.
(b) The structure of the data value must correspond to the position and type of
data with the stipulated column name.
(c) The value for non-listed column is null.

Copyright Open University Malaysia (OUM)


22 X TOPIC 3 DATA MANAGEMENT

If you choose not to list the tuple names, the null value must be stated clearly.
The command for the example above is as follows:

INSERT INTO Registration


VALUES (A1000, RK200, NULL, NULL)

ACTIVITY 3.1

Suggest how to link the two commands of INSERT stated in the


previous explanation (student table and register table) as only one
INSERT command

3.2 UPDATE COMMAND


Generally, the UPDATE command changes the values of specified columns in one or
more rows in a table or view. In the case of a database, can the UPDATE command be
applied for tables without record?

SQL allow users to update data within one line of the table content. UPDATE
only changes the content of the table and not the table structure. The format is as
follows.

UPDATE table name


SET column_name 1 = value_data1 [column _name 2 =
value_data2]
[WHERE condition]
where

(a) table_name is name of the table to be updated.


(b) column_name is the name of the column for the updated table.
(c) value data is the new data.
(d) The reserved name SET lists one or more columns with the updated value.

Let us have a look at several examples depicting the topic discussed.

Example 1:
Extend a salary increase of 5% to all workers.

Copyright Open University Malaysia (OUM)


TOPIC 3 DATA MANAGEMENT W 23

Solution:
UPDATE Tutor
SET salary = salary * 1.03

All salary values in the TUTOR table will be updated with the 3% upgrade from
the existing salary.

The statement WHERE is a choice; if it is left, all columns listed will be updated.
It is stated as only a row that complies with the rules that will be updated.

Example 2:
Give a 3% salary increase to all tutors holding the post of Lecturer.

Solution:
UPDATE Tutor
SET Salary = Salary * 1.03
WHERE post = Lecturer

In the example above, tutors with the post Lecturer will receive a 3% salary
increase. The result of the command is as follows:

Table 3.1: Result of UPDATE Command

TutorNo TutorName Post Salary (RM)


P1000 Kamal Lecturer 3,090
S1500 Sanusi Clerk 1,000
S2000 Chong Administrator 2,700
P2500 Hasnah Lecturer 3,090
P3000 Salleh Lecturer 3,605
P3500 Zainab Lecturer 3,399
S3000 Mary Administrator 2,900

3.2 DELETE COMMAND


You may delete one or more lines from a table using the reserve word DELETE.
The format is as follows.

Copyright Open University Malaysia (OUM)


24 X TOPIC 3 DATA MANAGEMENT

DELETE FROM table_name


[WHERE condition]

The table_name is the table to be deleted. The command WHERE is a choice. If


stated, it must comply with the ruling statement to search for deleted lines. The
only lines that comply with the table will be deleted. A missing WHERE clause
specifies that all lines in the table are to be deleted. If one line is deleted, all
column values will be deleted. The following is a command to delete all lines and
to delete a particular line only.

(a) Deleting All Lines


Delete all records from the COURSE table

DELETE FROM Course

Because the Command WHERE is not stated, all lines in the COURSE table
will be deleted.

(b) Deleting Particular Line


Delete statements with MatricNo A 1004

DELETE FROM Student


WHERE MatricNo = 'A 1004'

In this example only the student Ah Chong is deleted from the STUDENT table;
this is required as Ah Chong may have retracted from his studies and his data
must be taken out of the database.

You can see further examples of the INSERT, UPDATE and DELETE commands
in different situations in the book Sistem Pangkalan Data written by Salha
Abdullah and Nazir Omar, published by Prentice Hall 2000.

We have come to the conclusion of topic 3. The summary that follows willl
enable us to revise what has been learnt in this topic and comprehend the
contents.

For further examples on the INSERT, UPDATE and DELETE commands, visit the
following website: http://www.sqlcourse.com/intro.html.

Copyright Open University Malaysia (OUM)


TOPIC 3 DATA MANAGEMENT W 25

ACTIVITY 3.2
Write the SQL language for each of the following:
1. Add one new worker named Aziz with worker number S18000.
2. Change all courses taught, from Kamal (PI000) to Hasnah (P2500).
3. Delete all students with grade E.

x You have learnt how to update data within a database through the INSERT,
UPDATE and DELETE commands that involve only one table. You have also
learnt the format used to apply this command. In short, this command can be
used to update all rows or a particular row through the command WHERE
and this statement is required to include particular rules.

Delete command
Update command

Copyright Open University Malaysia (OUM)


T op i c X Query Data
4
LEARNING OUTCOMES
By the end of this topic, you should be able to:
1. Provide examples of data query in one table and in multiple tables;
2. Apply the access ability of data processed and data from tables;
3 Use the aggregate function of SUM, AVG, MIN, MAX and COUNT;
and
4. Manipulate the result of tables using UNION, INTERSECT and
EXCEPT.

X INTRODUCTION
As explained earlier, two of the most important parts of SQL are DDL and DML.
This topic is an extension of Data Manipulation Language B for data
management and language needed for query data.

You will learn the SQL instructions, beginning from simple queries that involve
the table, and proceed to complex queries used for data query. For this purpose,
we shall refer to the university database schema as the case study. This has been
explained in detail in topic 2 section 2.1.

Copyright Open University Malaysia (OUM)


TOPIC 4 QUERY DATA W 27

4.1 SOLUTION STRATEGY


The SELECT command is the main and basic command of all SQL queries. The
command SELECT is used to access and present data from one or more tables. It
uses the Backus Naur Form (BNF). You will learn in greater detail how to build
the SQL queries in this section.

In topic 1, you were taught the basic structure of a SQL statement. Before we
build the SQL statement language, let us have a look at the solution strategy to
one SQL query in general. For example, a query is given as follows:

SELECT DISTINCT A1, A2


FROM T1, T2
WHERE S

Database management system solves these questions by:


(a) Obtaining the product of T1 and T2 crossed multiplication.
(b) Deleting rows that do not comply with the S ruling.
(c) Deleting tuples except A1 and A2.
(d) Deleting repeated rows if required.

4.2 SIMPLE QUERY


A simple query refers to the SQL query. What is the basic syntax normally used for this
simple query?

Most of the SQL commands are based on algebraic relations. As such, all
algebraic relation statements can be restated by using SQL. The following three
subtopics will introduce you to this process.

Copyright Open University Malaysia (OUM)


28 X TOPIC 4 QUERY DATA

4.2.1 To Retrieve All Rows and Tuples


You may be able to produce all information from a particular table as shown in
the following example.

Example:
List all information on students

Solution:
SELECT MatricNo, StudentName, Major
FROM Student

Observe that all tuple names are presented in the SELECT statement. As there are
rules to be complied to, the statement WHERE need not be used. SQL gives an
option of placing the (*) symbol for all tuples. The above statement can be
presented as follows:

SELECT *
FROM STUDENT

The result:

MatricNo StudentName Major


A1000 John Science Computer
A1001 Fatimah Multimedia
A1002 Ali Science Information
A1003 Muthu Networking
A1004 Ah Chong Multimedia

4.2.2 To Retrieve Specific Rows and Tuples


In addition, you may retrieve only a part of the information required by a
particular tuple. Look at the next example.

Example:
List the matriculation number and student name from the Student list.

Solution:
SELECT MatricNo, StudentName
FROM Student

Copyright Open University Malaysia (OUM)


TOPIC 4 QUERY DATA W 29

The SELECT Statement will retrieve the columns of studentName and Matric no
from the student table. The result is shown on the table below:

The result:
MatricNo StudentName
A1000 John
A1001 Fatimah
A1002 Ali
A1003 Muthu
A1004 Ah Chong

4.2.3 DISTINCT Application


Before we attempt the DISTINCT application, let us have a look at the SQL
statements to distinguish hits usage. Again, look at the example below to help us
understand better.

Example:
List all Students major.

Solution:
SELECT Major
FROM Student

The result:
Major
Science Computer
Multimedia
Science Information
Networking
Multimedia

Copyright Open University Malaysia (OUM)


30 X TOPIC 4 QUERY DATA

DISTINCT is used in the SELECT statement if the output of SQL query contains
repeated and unwanted rows. DISTINCT will eliminate all repeated rows.

SELECT DISTINCT Major


FROM Student

The result:
Major
Science Computer
Multimedia
Science Information
Networking

ACTIVITY 4.1

Fill in the blanks for the following questions.

1. Provide a solution to the commands below in order to


produce LoanNo and loan_date from the loan table

SELECT _______________ , _______________


FROM _______________

2. Complete the commands below to present worker_name,


worker_no and female workers from the worker table.

_______________ worker_name, worker_no, salary


FROM worker
_______________ _______________ = 'female'

Copyright Open University Malaysia (OUM)


TOPIC 4 QUERY DATA W 31

4.3 TO SELECT ROWS


SQL allows us to produce specific rows and tuples. To choose rows, the rule is
applied after the command WHERE. Among the types of tests you can apply
when choosing the rules of rows are shown in Table 4.1.

Table 4.1: Type of Tests

Rule Details
Comparison of one value statement after another value
Comparison
statement
To test if any value statement falls within a particular
Range test
range value
To test whether one design is suitable with another
Design matching
design
To test ifr one value statement is identical to one set
Set Number
element
Null Set Number to test whether a tuple consists of the null
value

4.3.1 To Retrieve Tuples and Select Rows


You can select a particular line for viewing. As such, the command WHERE is
required to choose the particular lines. Let us have a look at the following
example to better understand the topic discussed.

Example:
List tutors holding the Lecturer post

Solution:
SELECT TutorNo, TutorName
FROM Tutor
WHERE Post = 'Lecturer'

The result:
TutorNo TutorName
P1000 Kamal
P2500 Hasnah
P3000 Salleh
P3500 Zainab

Copyright Open University Malaysia (OUM)


32 X TOPIC 4 QUERY DATA

4.3.2 Rename Tuple


SQL allows you to renew the tuple or change the title of a particular tuple for a
specific purpose. The method of renewing or changing is as follows:

Example:
Present the Lecturers number and name under the heading LecturerNo and
LecturerName.

Solution:
SELECT TutorNo AS LecturerNo, TutorName AS Lecturer Name
FROM Tutor
WHERE Post = 'Lecturer'

The result:
LecturerNo LecturerName
P1000 Kamal
P2500 Hasnah
P3000 Salleh
P3500 Zainab

You may present one tuple under a different name using the reserve word AS.
You may also use the reserve word AS to name all relationships. It can be used in
the SELECT or FROM commands in the following form:

Previous name AS new name

The reserved word AS is a choice in one SQL statement. There are several reasons
why we name all tuples and relationships:

(a) When output produces two similar tuples;


(b) When output produces tuples without a name, which is where we use the
arithmetic statement in SELECT command;
(c) When we require other names in the output presentation.

Copyright Open University Malaysia (OUM)


TOPIC 4 QUERY DATA W 33

4.3.3 Comparison between Ranges


At times we need to present names according to a particular tuple. This SQL
command can be produced using the reserved name AND as shown in the
following example.

Example:
List tutors with salaries between RM2,500 and RM3,000.

Solution:
SELECT TutorNo, TutorName
FROM Tutor
WHERE Salary >= 2500 and Salary <=3000

Based on the SQL query above, the table can be presented as:

TutorNo TutorName Salary (RM)


P1000 Kamal 3,000
S2000 Chong 2,700
P2500 Hasnah 3,000
S3000 Mary 2,900

4.3.4 Rules with Multiple Comparisons


Multiple comparison means that you can use more than two comparison rules
through the reserve word AND or OR.

An example of the application can be seen below:

Example:
List workers with the Tutor or Assist Lecturer post who obtain less than RM2,500
or more than RM2,900 salaries.

Solution:
SELECT TutorNo, TutorName, Salary
FROM Tutor
WHERE (Salary <= 2500 or Salary >= 2900)
AND (Post= Tutor or Post = Assist Lecturer)

Copyright Open University Malaysia (OUM)


34 X TOPIC 4 QUERY DATA

The rules with WHERE command becomes complex with the usage of
comparison tests. Brackets can be used to show the priority order.

The statement rule is valued in the following order of importance:

(a) From left to right;


(b) Statement in brackets is valued first; and
(c) The reserved word NOT is more primary than AND and AND is more
primary than OR.

The statement lists tutors with the post of a tutor or an Assist Lecturer with a
salary of either RM2,500 and below, or more than RM2,900. The table is shown
below.

TutorNo TutorName Salary (RM)


S1500 Sanusi 1,000
S3000 Mary 2,900
S2000 Chong 2,700

Before proceeding further, try completing the exercise which tests your
understanding of this new topic.

ACTIVITY 4.2
In the example you have just seen, the bracket symbol is used to show
the statement structure according to priority. Try presenting what the
product is and an example of the particular statement if the bracket is
not placed in the WHERE command.

Copyright Open University Malaysia (OUM)


TOPIC 4 QUERY DATA W 35

4.3.5 Comparing Sequence


SQL allows a comparison to be applied over one sequence in the WHERE
command. To execute the corresponding design in sequence, SQL allows the use
of reserved word LIKE in the command WHERE as per the following format:

WHERE r' LIKE 'c'

Where r is the sequence and c is the design to find.

SQL applies two special characters as corresponding design symbols which are
the percentage symbol (%) and character as (-). (Refer to Table 4.2).

Table 4.2: Symbols in SQL

Symbol Detail
Has a corresponding value with any sub-string consisting of
%
zero or more characters
_ Consists of corresponding value with any single character

Design is sensitive to lower and uppercase letters, meaning that a lower case
letter character does not correspond to an uppercase character. All characters in a
particular design, apart from % and , represent their own self except the
opening sequential quotation (' ') as illustrated in Table 4.3.

Table 4.3: Example of Commands using % and _.

Command Details
LIKE '%' any string ending with A
LIKE 'A%' first letter begins with A followed by any character
LIKE '%A%' any string that contains A
NOT LIKE '%A% any string not consisting of A
LIKE '_ _ any string with two characters
LIKE' A%' any string where the second character is A

It is hoped that you are able to follow the topic you have just learnt. The
following exercise is based on this topic.

Copyright Open University Malaysia (OUM)


36 X TOPIC 4 QUERY DATA

ACTIVITY 4.3

1. Try to come up with a situation with the output and command LIKE '
A%', LIKE '%A%' and LIKE'.

2. Explain the meaning of each corresponding design in the following


SQL statement and show examples that explain the differences
between the symbols.
(a) LIKE '%xy'
(b) LIKE 'xy%'
(c) LIKE '%xy%'

4.3.6 Design Correspondence


You may choose tuples by comparing the sequence (string). The application is as
follows:

Example 1:
List all majors beginning with science

Solution:
SELECT Major
FROM Student
WHERE Major LIKE 'Science%'

Like 'Science%' finds a sequence that begins with the word Science. Output from
the SQL statement above is shown below:

Major
Computer Science
Information Science

Example 2:
List tutors with support post (assume the tutor number begins with S followed
by four characters).

Copyright Open University Malaysia (OUM)


TOPIC 4 QUERY DATA W 37

Solution:
SELECT TutorNo, TutorName, Post
FROM Tutor
WHERE TutorNo LIKE' S_ _ _ _ '

Output from the SQL statement above is as follows

TutorNo TutorName Position


S1500 Sanusi Assist Lecturer
S2000 Chong Tutor
S3000 Mary Assist Lecturer

4.3.7 NULL Search (IS NULL / IS NOT NULL)


At times, we need to search for tuples without value or with the value NULL.
This can be done by referring to the reserved word IS NULL / IS NOT NULL.
The usage is as shown in the following example:

Example:
List the course without teaching workforce.

Solution:
SELECT CourseCode, CourseName
FROM Course
WHERE TutorNo IS NULL

Output from the SQL statement above is shown in the following table:

CourseCode CourseName
SM100 Search Engine

IS NOT NULL is used to test the value without NULL. The technique of usage is
similar to IS NULL but the output result takes into consideration the TutorNo
and presents one more tuple with TutorNo heading.

Copyright Open University Malaysia (OUM)


38 X TOPIC 4 QUERY DATA

4.4 SORTING THE RESULT

ACTIVITY 4.4

Why do we need to insert results obtained? Present a situation that


requires this command.

In general, the output for a particular result is not isolated by SQL. To sort
according to value of a particular attribute, we may use the ORDER BY
statement. The ORDER BY statement is normally represented by the last line in a
particular SQL statement. You may attempt to sort by using the reserved word
(ascending or descending order). If ASC or DESC is not applied, SQL will sort the
output in ascending order.

4.4.1 Sorting One Attribute


You may sort the query result through one attribute. Please refer to the following
example:

Example:
List the tutor salary in descending order

Solution:
SELECT TutorNo, TutorName, Post, Salary
FROM Tutor
ORDERBY DESC salary

Output for SQL statement is shown in the following table:


TutorNo TutorName Position Salary (RM)
P3000 Salleh Lecturer 3,500
P3500 Zainab Lecturer 3,300
P1000 Kamal Lecturer 3,000
P2500 Hasnah Lecturer 3,000
S3000 Mary Assist Lecturer 2,900
S2000 Chong Assist Lecturer 2,700
S1500 Sanusi Tutor 1,000

Copyright Open University Malaysia (OUM)


TOPIC 4 QUERY DATA W 39

4.4.2 Sorting More than One Attribute


If there is more than one given attribute to be sorted, the first attribute is sorted
before the second attribute. This can be seen clearly by looking at the following
example.

Example:
List tutors according to post in alphabetical order and salary in descending order.

Solution:
SELECT TutorNo, TutorName, Post, Salary
FROM Tutor
ORDER BY Post, DESC Salary

The output for the SQL statement above is as follows:

TutorNo TutorName Position Salary (RM)


S1500 Sanusi Tutor 1,000
S2000 Chong Assist Lecturer 2,700
S3000 Mary Assist Lecturer 2,900
P1000 Kamal Lecturer 3,000
P2500 Hasnah Lecturer 3,000
P3500 Zainab Lecturer 3,300
P3000 Salleh Lecturer 3,500

ACTIVITY 4.5
The ORDER BY statement is used to isolate more than one attribute but
do you know how to isolate attributes? Is the numbering for attribute
infinite? Give out your opinion.

4.5 AGGREGATE FUNCTION


SQL adopts five aggregate functions according to ISO standard which can be
implemented as one tuple and table to produce one value for a particular tuple.
The functions are shown in Table 4.4.

Copyright Open University Malaysia (OUM)


40 X TOPIC 4 QUERY DATA

Table 4.4: Aggregate Functions

Aggregate Functions
returns the numeric value in a tuple (according to repeated
COUNT
value)
SUM returns the value amount in a tuple
AVG returns the average value in a tuple
MIN return sthe smallest value in a tuple
MAX returns the biggest value in a tuple

Each operation in a tuple determines one product value based on the particular
operations. COUNT, MIN and MAX can be used in the numeric and non-
numerical domains. All functions do not take into account the blank space or
null, and operate on fields with values except COUNT (*). COUNT (*) is a special
function that counts all rows in a figure including null values or similar values at
different rows.

To ascertain a repeatable value in one tuple is not counted, you must use the
reserved word DISTINCT before naming the tuple. DISTINCT does not give any
difference when it is used with MIN or MAX but it can change the value with the
SUM or AVG statement.

The aggregate function can be used in SELECT and HAVING statements. If the
SELECT application contains an aggregate function and devoid of the GROUP
BY command, SELECT is unable to recognise rows. For example, the SQL
statement below is not allowed, as the CourseCode in SELECT statement is not
the purpose of usage for one aggregate function.

SELECT CourseCode, AVG (Grade)


FROM Registration

Instead, the correct statement is:

SELECT CourseCode, AVG (Grade)


FROM Registration
GROUP BY CourseCode

Copyright Open University Malaysia (OUM)


TOPIC 4 QUERY DATA W 41

4.5.1 COUNT (*) Application


Count (*) is used to count all lines that comply with the ruling stated in the
WHERE command. Please refer to the following example.

Example:
Count the number of students registered under the Computer Science Major.

Solution:
SELECT Count (*) AS counting
FROM Student
WHERE Major = 'Computer Science'

The result:
Number
1

Here, COUNT is used to count all lines, and DISTINCT deletes all repeated lines.
Let us look at the next example to assist our understanding.

4.5.2 COUNT and DISTINCT Application


Here, COUNT is used to calculate all lines and DISTINCT to erase all repeated
lines. Let us look at the example below.

Example:
Count the number of different majors registered by students.

Solution:
SELECT COUNT (DISTINCT Major) AS CountMajor
FROM Student

As we are not interested in calculating the repeated majors, you can use the
reserved name DISTINCT. The majors registered by student are from STUDENT
table. The output for this query is shown in the table below.

Result:
CountMajor
4

Copyright Open University Malaysia (OUM)


42 X TOPIC 4 QUERY DATA

4.5.3 COUNT and SUM application


COUNT is used to return the value of one tuple but SUM returns the value
amount of one tuple. The application is shown in the following example:

Example:
Produce the number of tutors and total up their salaries.

Solution:
SELECT COUNT (TutorNo) AS Tutor_Amount,
SUM (Salary) AS Salary_Amount
FROM Tutor

The result:
Number of Tutors Salary_Amount
7 19,400

4.5.4 MIN, MAX and AVG Application


In SQL, you can find the smallest, biggest and average amount of a particular
tuple with the use of MIN, MAX and AVG. Let us look at the following example.

Example:
Find the minimum and maximum amount and the average tutor salary.

Solution:
SELECT Min (salary) as MINIMUM,
MAX (salary) AS MAXIMUM,
AVG (salary) AS Average
FROM Tutor

Result:
Minimum Maximum Average
1,000.00 3,500.00 2,771.43

Copyright Open University Malaysia (OUM)


TOPIC 4 QUERY DATA W 43

Keep in mind the new reserved words you have just learnt as these words are
widely used in this study. Before you proceed with your reading, answer the
question below to ensure that you understand what you have learnt so far.

ACTIVITY 4.6

What is the restriction used in the aggregate function of the null value
that influences the aggregate function?

You have learnt an important part of an SQL statement. It is advised that you rest
your mind for a while before proceeding to the next topic.

4.6 GATHERING RESULTS


In a certain situation you may need to collect tuple values for groups within the
same characteristic. For this purpose, SQL applies the GROUP BY statement
followed by group attribute within as part of the gathering characteristic. The
GROUP BY statement is written after the WHERE statement. GROUP BY collects
data according to the product of one set value from the union of two groups.

The SELECT and GROUP BY statements must be synchronised,, that is, all tuple
names are presented in the SELECT statement except for GROUP BY statement
which is normally built upon the group attribute and aggregate function.

GROUP BY is used after the statement WHERE. This means, grouping is only
applied upon lines that comply with rulings presented in WHERE.

4.6.1 GROUP BY Clause


We can use the GROUP BY Clause to group rows according to the group stated.
The application is shown in the following example:

Example:
Count the number of students registered for each major.

Solution:
SELECT Major, COUNT (*) AS bilangan
FROM Student
GROUP BY Major

Copyright Open University Malaysia (OUM)


44 X TOPIC 4 QUERY DATA

What transpires in SQL is that it groups rows according to group majors and
then counts the number of rows for each major.

Result:
Major Count Number
Computer Science 1
Multimedia 2
Information Science 1
Networking 1

4.6.2 HAVING Clause


At times you may want to choose a group of information that fulfils a particular
standard. The WHERE clause places a standard for particular rows. To place the
standard in a particular group, SQL provides HAVING clause that must be used
after the GROUP BY clause. The ISO standard requires that tuple names in the
HAVING clause must also appear as a grouping attribute in the GROUP BY
clause or exist as a dispute to a union operation. The condition in HAVING
clause must remain a single value for each group. This also applies to each
attribute in the SELECT clause when the GROUP BY clause is used.

(i) HAVING and ORDER BY Clause


The ORDER BY clause must be used after HAVING clause. The product
obtained may be isolated by the ORDER BY clause. This is depicted in the
following example.

Example:
For each major group with more than one student, find out the number of
students for each major group and list the output according to numeric
major names.

Solution:
SELECT Major, COUNT (MatricNo) AS Sum_Student
FROM Student
GROUP BY Major
HAVING COUNT COUNT (MatricNo) > 1
ORDER BY Major

Copyright Open University Malaysia (OUM)


TOPIC 4 QUERY DATA W 45

Result:
Major Sum_Student
Multimedia 2

The implementation process performed by SQL is:


(a) Combines rows according to group major;
(b) Counts number of rows for each group major;
(c) Eliminates group majors with less than two number of rows
(d) Isolation according to alphabetical order of major name
(e) Displays the output on tables

If all six statements exist within the SQL statement, they must be written in
this chronology: SELECT, FROM, WHERE, GROUP BY, HAVING and
ORDER BY.

(ii) HAVING and GROUP BY Clause


At times, you may need to group values in a particular tuple according to
groups of the same characteristic. For this purpose, SQL provides the
GROUP BY statement to group the respective tuples. It must also contain a
specific characteristic signified by the HAVING clause. This can be viewed
in the following example.

Example:
List the students matriculation number unit average value grade of more
than 2.9.

Solution:
SELECT MatricNo, AVG (value) as Average
FROM Student
GROUP BY MatricNo
HAVING Average > 2.9

Copyright Open University Malaysia (OUM)


46 X TOPIC 4 QUERY DATA

Result:
MatricNo Average
A1000 3.00
A1001 4.00
A1002 3.67
A1004 3.00

Attempt the following exercise to sharpen your understanding on the topic you
have just learnt.

ACTIVITY 4.7

Explain how the GROUP BY clause functions and what is the


difference between the WHERE clause and the HAVING clause.

4.7 SUB-QUERIES
Up to now you have learnt how to create a query from only one table. In this
section you will learn that several SQL commands may contain the SELECT
query in other SELECT queries. To obtain information from one or more tables,
you may combine two or more tables to apply the sub-query. The combination of
tables in sub-query is simple. Just state the tables to be combined through the
command FROM. Sub-query may be applied in WHERE and HAVING clauses
that exist in the SELECT clause. In addition, sub-queries can be used in INSERT,
UPDATE, and DELETE commands.

4.7.1 Using a Sub-query with Equality


To use the sub-query with outer equality, the symbol "=" can be used to return
the value of inner sub-query. Look at the following example for a clearer picture.

Example:
List all students that have registered for the SK100 course

Copyright Open University Malaysia (OUM)


TOPIC 4 QUERY DATA W 47

Solution:
SELECT MatricNo, StudentName
FROM Student
WHERE MatricNo IN
( SELECT MatricNo
FROM Registration
Where CourseCode= 'SK100')

The inner SELECT statement will choose students with the SK100 course
registration, whereas the outer SELECT statement produces information on
SK100 students.

Result:
MatricNo StudentName
A1000 John
A1001 Fatimah

Try this question. Can a sub-query or SELECT statement combine different


tables? Discuss in class.

4.7.2 Using a Sub-query with an Aggregate Function


The outer statement can be combined with the outer sub-query by using the
aggregate function. An example of its usage is as follows:

Example:
List all tutors with greater than the average salary.

Solution:
SELECT TutorNo, TutorName, Salary
FROM Tutor
Where Salary >
(SELECT AVG (Salary)
FROM Tutor )

The command WHERE Salary > AVG (salary) cannot be used. The sub-query
must be used to search for the average salary (RM2,771.43) and then the tutors
salary depicting greater than the average salary presented.

The outer statement can be simplified as:

Copyright Open University Malaysia (OUM)


48 X TOPIC 4 QUERY DATA

SELECT TutorNo, TutorName, Salary


FROM Tutor
WHERE Salary > 2771.43

Based on the SQL statement, the table below is generated

TutorNo TutorName Salary (RM)


P1000 Kamal 3,000.00
P2500 Hasnah 3,000.00
P3000 Salleh 3,500.00
P3500 Zainab 3,300.00
S3000 Mary 2,900.00

Points to Note:

1. The ORDER BY command cannot be used in sub-query (it can be used


in outer SELECT).

2. The SELECT sub-query must contain only one tuple or command


except the sub-query using the EXIST clause.

3. The tuple name refers to tables in the FROM command in the sub-
query. It can also refer to the tables in the FROM command by using the
ALIAS clause.

4.7.3 Nested Sub-queries (Use of IN)


The reserve word IN and NOT IN is used to test set members. A set may refer to
a conclusive-given value or produced by a query. We will see how set members
are tested and the usage of IN in sub-query through the example below.

(i) Testing Set Numbers


Example:
List students with the same major in either Networking, Computer Science
or Multimedia.

Solution:
SELECT MatricNo, StudentName, Major
FROM Student

Copyright Open University Malaysia (OUM)


TOPIC 4 QUERY DATA W 49

WHERE Major = ( 'Networking' OR


Major = 'Computer Science' OR
Major = 'Multimedia)

These statements are easy to apply by using the reserved word OR but it is
too long if there are too many majors chosen .

The specific method in using IN is as follows:

SELECT MatricNo, StudentName, Major


FROM Student
WHERE Major IN
('Networking', 'Computer Science', 'Multimedia')

Output for the above statement is:

MatricNo StudentName Major


A1000 John Computer Science
A1001 Fatimah Multimedia
A1003 Muthu Networking
A1004 Ah Chong Multimedia

(ii) Usage of IN in Sub-query


Example:
List students majoring in Multimedia with a grade A in MM200 course.

Solution:
SELECT MatricNo, StudentName
FROM Student
WHERE Major = 'Multimedia' AND
MatricNo IN
(SELECT MetricNo
FROM Registration
WHERE Grade = 'A AND
CourseCode= 'MM200' )

In this example, the sub-query returns one student taking the MM200
course per number with grade value 4.00 for the particular course. The first
SELECT (also known as outer SELECT) chooses Students majoring in
Multimedia from this set. The output for the above example is as shown:

Copyright Open University Malaysia (OUM)


50 X TOPIC 4 QUERY DATA

Result:
MatricNo StudentName
A1004 Ah Chong

4.7.4 NOT IN Clause


The NOT IN clause is used to test set members obtained in the internal sub-
query. The example of its usage is as follows

Example:
List the tutors who are not teaching any courses.

Solution:
SELECT TutorNo,
FROM Tutor
WHERE TutorNo NOT IN
( SELECT TutorNo
FROM Course

The sub-query in the example above lists down information on all tutors.
However, the external SELECT statement lists out all courses taught by
Lecturers. The difference between both statements derives the required elements
in the internal statement which does not exist in the external statement.

The output for the above statement is shown in the table below:

Result:
TutorNo TutorName
S1500 Sanusi
S2000 Chong
P2500 Hasnah

Test your understanding on the new topics that you have just learnt and answer
the following question.

Copyright Open University Malaysia (OUM)


TOPIC 4 QUERY DATA W 51

ACTIVITY 4.8

When is a sub-query used and in what circumstances is it used?

Rest your mind before continuing reading.

4.8 7 SET COMPARISON USING ANY, SOME


AND ALL
SQL allows the comparison of sets through the reserved words ANY and ALL.
ANY and ALL can be used in sub-query to produce one tuple number. The
reserve, word ANY is similar with SOME clause in ISO standard.

If one sub-query begins with the reserved word ANY or SOME, the rules of
comparison returns the TRUE value if one of the elements produced by the sub-
query fulfils the required rule. If the reserve word ALL is used, the comparison
rule returns the TRUE statement only if all elements in the sub-query produced
fulfil the particular comparison.

4.8.1 ANY and SOME Clause


The ANY or SOME clause can be used to search for the smallest value within the
internal sub-query. The example is as follows.

Example:
Find a tutor with a salary that is equal to or more than the Assist Lecturers
salary.

Solution:
SELECT TutorNo, TutorName, Post, Salary
FROM Tutor
WHERE Salary > SOME
(SELECT Salary
FROM Tutor
WHERE Post =' Assist Lecturer')

The internal query produces sets 2700 and 2900 while the external query presents
the TUTOR information where their salary is more than any of the values in this,
set (greater than the minimum value 2700).
Copyright Open University Malaysia (OUM)
52 X TOPIC 4 QUERY DATA

Result:

WorkerNo WorkerName Post Salary


P1000 Kamal Lecturer 3,000.00
P2500 Hasnah Lecturer 3,000.00
P3000 Salleh Lecturer 3,500.00
P3500 Zainab Lecturer 3,300.00
S3000 Mary Assist Lecturer 2,900.00

The above query can be stated by using the sub-query search for the minimum
salary of administrative tutor. The example below depicts this usage with the
SOME clause used widely.

SELECT TutorNo, Post, Salary


FROM Tutor
WHERE Salary >
(SELECT MIN (salary)
From Tutor
Where Post = 'Assist Lecturer')

4.8.2 ALL Clause


Compared to the SOME clause, the ALL clause is used to find the biggest value
from the set produced in the internal sub-query. This can be seen below:

Example:
List tutors with a salary greater than each of the tutor with assist lecturer post.

Copyright Open University Malaysia (OUM)


TOPIC 4 QUERY DATA W 53

Solution:
SELECT Tutor
FROM Tutor
Where Salary > ALL
(SELECT Salary
FROM Tutor
WHERE Post = 'Assist Lecturer')

This query is similar to the previous example.

The internal query produces sets of 2700 and 2900 while the external query lists
information on TUTOR where their salary is greater than all values within this
set (greater than the maximum value of 2900). This is similar to the SQL
statement below:

SELECT Tutor No, Tutor Name, Post, Salary


FROM Tutor
WHERE Salary >
(SELECT MAX (Salary)
WHERE Post = 'Assist Lecturer')

Result:

Tutor No Tutor Name Post Salary


P1000 Kamal Lecturer 3,090
P2500 Hasnah Lecturer 3,090
P3000 Salleh Lecturer 3,605
P3500 Zainab Lecturer 3,399

For further information, view the website


http://www.sqlcourse2.com/boolean.html

Copyright Open University Malaysia (OUM)


54 X TOPIC 4 QUERY DATA

4.9 MULTIPLE TABLE STATEMENT

ACTIVITY 4.9
You have learnt what a table is and how it converts statement into a
table form. However, are the statements different from the multiple
tables?

A sub-query can be used with rules and tuples produced from the same table. If
the result of the tuple is from a different table, combinations will have to be
implemented. To combine, tables will have to be presented with FROM clause
symbolised as ' , '.

Usually, the command WHERE is used to combine tables by stating tuples with
the same value within each table. SQL allows the use of alias, another name to
represent table_name. This is defined in the command FROM by inserting alias
after the table and the reserved word AS (a choice that can be neglected). For
example

FROM Student AS P, Registers AS D


or
FROM Student P, Registers D

P is a comparison operator represented by students, G is a variable representing


Student and D is the comparison operator representing Registration.

4.9.1 Simple Union


You may combine more than one table in a query. The example is as follows.

Example:
List lecturers teaching the Database Course.

Solution:
SELECT P. Tutor No, P. Tutor Name, K.CourseName
FROM Course K, Tutor P
WHERE K. Tutor No = P. Tutor No
AND P.Post = 'Lecturer'
AND K.CourseName = 'Database'

Copyright Open University Malaysia (OUM)


TOPIC 4 QUERY DATA W 55

Result:

Tutor No Tutor Name CourseName Post


P2500 Hasnah Database Lecturer

4.9.2 Structuring Union


If the there is more than one table used in a query, you are allowed to isolate the
product obtained. The example of its usage is as follows:

Example:
List students for each course.

Solution:
SELECT D.MatricNo, CourseCode, K.CourseName
FROM Register D, Course K
WHERE D.CourseCode = K.CourseCode
ORDER BY D.MatricNo, P.CourseCode

Result:
MatricNo CourseCode CourseName
A1000 RK200 Database
A1000 SK100 Java
A1001 MM200 Graphic
A1001 SK100 Java
A1002 SM100 Search Engine
A1004 MM200 Authoring tools
A1004 RK200 Database

Copyright Open University Malaysia (OUM)


56 X TOPIC 4 QUERY DATA

4.10 EXIST AND NON EXIST CLAUSE


The set comparison can be produced through the reserved words EXIST and
NOT EXIST. They are specifically used with the sub-query to produce a true or
false statement. EXIST returns the TRUE value if the sub-query produces at least
one statement, and returns the FALSE value if the sub-query produces blank
tables.

NOT EXIST is the opposite command of EXIST. As EXIST and NOT EXIST only
test if the rows exist or not in the table, the sub-query can contain many tuple
counts, normally through the SELECT statement.

4.10.1 EXIST Clause


The EXIST clause is used to test the existence of a statement for a query. If the
statement exists than the product will be presented, else the table produced will
remain blank. This is shown in the following example.

Example:
List all MM1100 course

Solution:
SELECT MatricNo
FROM EXISTS
WHERE (SELECT *
FROM Course K
WHERE D.CourseCode = K.CourseCode
AND K.CourseCode = 'MM100')

The above statement is an example of a multiple table where the sub-query is


valued by each statement in the internal query. The MatricNo in the
REGISTRATON table will be listed if EXIST returns the TRUE value for the
particular couplets. If the value is returned then the blank table will be returned
as well. The above query can be coupled without using the following sub-query.

SELECT MatricNo
FROM RegisterD, CourseK
WHERE D.CourseCode = K.CourseCode
AND K.CourseCode = 'MM100'

Copyright Open University Malaysia (OUM)


TOPIC 4 QUERY DATA W 57

The output for these statements is blank.

MatricNo
NULL

Try reading the book Distribution Pagination Data: Konsep Asas, Rekabentuk
dan Perlaksanaan. Check the conclusion for sections 8.4.2 to 8.4.4. It will refresh
your understanding of operations involving IN, ANY/SOME, ALL, EXIST,
UNIQUE.

4.11 COMBINING TABLE


Set commands in algebraic relations allow us to group relations produced by two
or more queries within one relation. This is done through union ( ), intersect
( ) or difference ( - ).

SQL also allows similar operations set through INTERSECT (with intersection)
and EXCEPT (similar with differentiation).

The rule for the command is both Tables A and B to be grouped must be from the
same tuple count from equable tuples, and contain the tuple of data and the same
domain.

Command Detail
A UNION B A will return all rows in tables A or B or both
A INTERSECT B Returns all rows in A that exist in B
A EXCEPT B Returns all rows in A which do not exist in B

Copyright Open University Malaysia (OUM)


58 X TOPIC 4 QUERY DATA

4.11.1 UNION Clause


UNION can be used to combine results from two or more tables. An example of
its usage is as follows.

Example:
List students majoring in Multimedia or students registered with the RK200
course.

Solution:
(SELECT MatricNo
FROM Student
WHERE Major = 'Multimedia')
UNION
(SELECT MatricNo
FROM Registration
WHERE CourseCode = 'RK200')

Although STUDENT and REGISTRATION are not the same set but the tables
produced by the first sub-query consist of one tuple (MatricNo) are the same
with the tables produced by the second sub-query with also one tuple (MatricNo)

Both these tables are combined into one table as shown below:

MatricNo
A1001
A1004
A1004
A1000

4.11.2 INTERSECT Clause


The INTERSECT clause can be used to derive the product in one table and also in
another table. The example is as follows:

Example:
List students who have registered for the Java Course and Programming tool.

Copyright Open University Malaysia (OUM)


TOPIC 4 QUERY DATA W 59

Solution:
(SELECT D.MatricNo
FROM Course.K, RegisterD
WHERE K. CourseCode = D.CourseCode
AND K.CourseName= 'Java')
INTERSECT
(SELECT D.MatricNo
FROM Course K1, Registration D1
WHERE K1.CourseCode = D1.CourseCode
WHERE K1.CourseName = 'Authoring Tool'

What transpires in this query is the process of obtaining the same rows in
between tables produced by the first sub-query and the second sub-query. Both
tables consist of the studentno tuple.

MatricNo
A10011

Result:
The above statement can be explained through the example below or with the
AND clause. However, the AND clause is not a natural compound for the use of
INTERSECT.

SELECT MatricNo
FROM Registration D, Registration D1, Course K, Course K1.
AND K.CourseName = 'Java'
AND K1.CourseCode= D1.CourseCode
AND K1.CourseName= ' Holding Tool'
AND D.MatricNo = D1.MatricNo

4.11.3 EXCEPT Clause


EXCEPT can be used to return the value of a table but not values in other tables.
An example of the usage is as follows:

Example:
List students who have registered for the Java Course and but not for the
Authoring Course.

Copyright Open University Malaysia (OUM)


60 X TOPIC 4 QUERY DATA

Solution:
( SELECT D.MatricNo
FROM Course K, Registration D
WHERE K.CourseCode = D.CourseCode
AND K.CourseName = Java )
EXCEPT
( SELECT D1.MatricNo
FROM Course K1, Registration D1
WHERE K1.CourseCode = D1=CourseCode
AND K1.CourseName = Authoring Tool)

What transpires in this statement is the choice of rows from the table produced
by the second sub-query.

Result:
MatricNo
A1000

You have completed this topic and as such you have successfully completed this
unit. Take a rest before moving on to the summary.

Before you rest, browse http://www.schools.com/sql/default.asp. In this


website you will be able to revise the topics discussed. You can also attempt
several tutorial questions at http://sqlzoo.net to strengthen your competency in
building an SQL statement.

Copyright Open University Malaysia (OUM)


TOPIC 4 QUERY DATA W 61

x In this topic, you have learnt various commands, characteristics and


strengths of SQL based on the basic SQL example and advanced stage. The
SQL can be categorised as the most commercialized of query languages and
the widest used language in database applications today. However, this topic
does not aim to serve as a manual for a particular SQL implementation. It
also exposes you to the general and overall approach of SQL.

Null Search Tuples


Query

Copyright Open University Malaysia (OUM)


Topic X Database
DevelopmentI
5
LEARNING OUTCOMES
By 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:

Copyright Open University Malaysia (OUM)


TOPIC 5 DATABASE DEVELOPMENT I W 63

(a) Inserting and Updating a Data


x Access provides a simple mechanism for adding, updating and deleting
data, including the ability to make a large change with only one
operation.
(b) Query
x 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.
(c) Form
x Users can create an interesting and useful form to display and update
the data.
(d) Report
x Access provides a feature that allows users to produce a sophisticated
report to represent data in a clear and concise way.
(e) Web Supporting
x 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:
x Creating databases;
x Adding, changing, and deleting data in databases;
x Sorting data in databases;
x Retrieving data from databases; and
x 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.

Copyright Open University Malaysia (OUM)


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.

Copyright Open University Malaysia (OUM)


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.

Copyright Open University Malaysia (OUM)


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.

(a) Open key - To open the existing element.


(b) Design key - To open design Windows from elements.
(c) New key - To create a new element.

Let us focus on the creation of tables within the MentorMentoree database in the
following section.

Copyright Open University Malaysia (OUM)


TOPIC 5 DATABASE DEVELOPMENT I W 67

5.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
example shown, most fields are presented as Text including Name, programme,
address and Mentor_ID. Matric_No is stated as Number.

Copyright Open University Malaysia (OUM)


68 X TOPIC 5 DATABASE DEVELOPMENT I

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.

Copyright Open University Malaysia (OUM)


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.
Copyright Open University Malaysia (OUM)
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.

Copyright Open University Malaysia (OUM)


TOPIC 5 DATABASE DEVELOPMENT I W 71

ACTIVITY 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.

Copyright Open University Malaysia (OUM)


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

Copyright Open University Malaysia (OUM)


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.

ACTIVITY 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.

Copyright Open University Malaysia (OUM)


74 X TOPIC 5 DATABASE DEVELOPMENT I

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:
(a) Query using QBE ( Query By Example)
(b) 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.

Copyright Open University Malaysia (OUM)


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

Copyright Open University Malaysia (OUM)


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

Copyright Open University Malaysia (OUM)


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:

Copyright Open University Malaysia (OUM)


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.

Copyright Open University Malaysia (OUM)


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

Copyright Open University Malaysia (OUM)


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.3 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

Copyright Open University Malaysia (OUM)


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

Copyright Open University Malaysia (OUM)


82 X TOPIC 5 DATABASE DEVELOPMENT I

ACTIVITY 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.

Copyright Open University Malaysia (OUM)


TOPIC 5 DATABASE DEVELOPMENT I W 83

5.5.4 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.5 To Create a SQL Query


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

(a) SELECT <field1, field2, , fieldn>


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

(b)  FROM <table>


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

(c)  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.

(d) ; <semi colon>


All query of SQL must end by semi colon. If you are forgotten to include it,
Access will put it automatically.

Copyright Open University Malaysia (OUM)


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.

Copyright Open University Malaysia (OUM)


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.6 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.

Copyright Open University Malaysia (OUM)


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

Copyright Open University Malaysia (OUM)


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

ACTIVITY 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

Copyright Open University Malaysia (OUM)


88 X TOPIC 5 DATABASE DEVELOPMENT I

x 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.

Database
Microsoft Access

Copyright Open University Malaysia (OUM)


Topic X Database
DevelopmentII
6
LEARNING OUTCOMES
By the end of this topic you should be able to:
1. Create a form using Microsoft Access;
2. Create, combine and link a sub-form with the main form;
3. Produce a report; and Produce a switchboard

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 Microsoft Access functions are
the storage, isolation and retrieval of data for various applications. This software
is equipped with tools to create the database, tables, forms, reports and queries.
Microsoft Access is based on stand-alone or multiple user networks. For this
chapter, each step involved in operating Microsoft Access will be explained. As
such, it would be more helpful if you could read this module in front of a
personal computer with the Microsoft Access programme running.

6.1 FORM
Form provides users with an interface for data in database application. The form
also allows database developers to add details to the presentation of data on
computer screen. In other words, the form is used to simplify the process of
inserting data into tables that have been created.

As a query, the form does not contain any data. It only provides a window where
tables and queries can be displayed. Look at Figure 6.1 to understand the relation
between tables, queries and forms.

Copyright Open University Malaysia (OUM)


90 X TOPIC 6 DATABASE DEVELOPMENT II

Figure 6.1: Relation between tables, queries and forms

6.1.1 Creating a Form


Figure 6.2 shows the New Form dialogue box. You can use Form Wizard to
create a form. You also can select any table to become the main form by using
tables or queries. After the table has been selected, all fields in the table will
appear on Available Fields. From this, you can select any field to be inserted into
the Selected Fields.

Figure 6.2: New Form dialogue box

Copyright Open University Malaysia (OUM)


TOPIC 6 DATABASE DEVELOPMENT II W 91

The basic elements on design screen are shown in Figure 6.3. If the toolbox
window and field list does not appear, use the View menu to activate them.
Figure 6.3 also shows how the field is inserted into the provided form. You need
to drag the field from the field list to the form. Other than the field, you also can
put others element into the form such as labels, text boxes, buttons, pictures and
others. To view the form, select View-form menu.

Figure 6.3: Basic elements to design a form

Besides creating the form manually, you also can modify the forms design to
present your data by using Form Wizard.

ACTIVITY 6.1
Complete the form as shown in Figure 6.3 by keying-in all the fields
into the form. Use your creativity to arrange the fields by using
Microsoft Access.

Copyright Open University Malaysia (OUM)


92 X TOPIC 6 DATABASE DEVELOPMENT II

6.1.2 Creating a Sub-Form


In many cases, for a large database we need to create several forms and these
forms need to be linked with each other. The technique used for this purpose is
the sub-form, which is used to create a form inside another form. This sub-form
will be inserted into the main form.

The procedure used to create the sub-form within the main form is:
(a) Create and save each form separately;
(b) Drag sub-form into main form;
(c) Ensure both sub-form and main form are linked to each other.

The process to create a main-form and sub-form is same, as discussed in Topic


6.1.1. However, in this section, you will create a form using Form Wizard. By
using the mentor table, the form created will be as shown in Figure 6.4. The main
form for the mentor table will be produced as shown in Figure 6.5.

Figure 6.4: Using wizard to create a form

Copyright Open University Malaysia (OUM)


TOPIC 6 DATABASE DEVELOPMENT II W 93

Figure 6.5: Main form for the mentor

To create a sub-form, we can use the Form Wizard method. Now, you can create
a sub-form using the mentoree table as shown in Figure 6.6.

Figure 6.6 (i): Use of Form wizard to create mentoree sub-form

Copyright Open University Malaysia (OUM)


94 X TOPIC 6 DATABASE DEVELOPMENT II

Figure 6.6 (ii):Use of Form wizard to create mentoree sub-form

To link the main form with the sub-form, you need to drag the sub-form into
main form as shown in Figure 6.7.

Figure 6.7: Drag mentoree sub-form into mentor main-form

Copyright Open University Malaysia (OUM)


TOPIC 6 DATABASE DEVELOPMENT II W 95

The advantages of using the drag and drop technique is that the size of the sub-
form controller will automatically be set according to the sub-form size. To
ensure the main form is linked with sub-form, you can check the link on the sub-
form property as shown in Figure 6.8.

Figure 6.8: To verify link between main form and sub-form

The result of the linkage between the main form and sub-form is shown in Figure
6.9. For each form, there is a navigation button at the bottom part.

Figure 6.9: The linkage between main form and sub-form

Copyright Open University Malaysia (OUM)


96 X TOPIC 6 DATABASE DEVELOPMENT II

6.1.3 The Guidelines for Form Design


In designing and creating a form, the following points need to be taken into
consideration:

(a) Form is commonly used for a long term purpose. A form that is not
arranged properly and consists of a lot of visual effects (colour, font,
graphic and others) will disrupt the users concentration.

(b) Place and arrange all fields into a group of the same logic. Fields that
related to each other need to put neatly on the form created.

(c) If data input is acquired from paper-based form, make a form design in
Access similar to the paper-based form.

6.2 PRODUCING A REPORT

ACTIVITY 6.2
You surely know the function of reports during meeting. How about the
function of reports in a database?

Reports, like forms, are produced by using the same concept and tool. The
difference is that the purpose of the report is to represent the content of the
database in simple form. As such, a report consists of calculation of control which
calculate the aggregate function. Forms also can be printed but are not used
interactively.

Using Report Wizard, a report can be created through Access. The steps to
produce a report that contains a list of mentorees for each mentor are shown in
Figure 6.10.

Copyright Open University Malaysia (OUM)


TOPIC 6 DATABASE DEVELOPMENT II W 97

Figure 6.10 (i): Using a wizard to produce a report

Figure 6.10 (ii): Using a wizard to produce a report

Copyright Open University Malaysia (OUM)


98 X TOPIC 6 DATABASE DEVELOPMENT II

Figure 6.10 (iii): Using a wizard to produce a report

To view a created report, you need go to File-Print Preview menu. The result of
the created report is shown in Figure 6.11.

Figure 6.11: Result of the report

Copyright Open University Malaysia (OUM)


TOPIC 6 DATABASE DEVELOPMENT II W 99

6.2.1 The Guidelines for Report Design


In designing and creating forms, the following points need to be taken into
consideration:

(a) The purpose of producing a report is to give specific information. In


designing a report, ask yourself whether the report is able to produce the
information that is needed? Is the meaning of each row and column inside
the report clear? Is the name of each column easily interpreted?
(b) Create an empty space within the group. If the space is not enough, you
need to enlarge the Group Footer.
(c) The use of different types of fonts with various sizes is encouraged, as long
as it is suitable. However, using many font styles will make the report
design look confusing.
(d) The design of all reports needs to be consistent.

ACTIVITY 6.3
Shown below is a database with two tables.

Customer Table
IDNo Name Gender Age Address City
111 Ali M 25 Jln. Sijangkang Penang
222 Niza F 30 2 Desa Pandan Kajang
333 Zafrul M 28 3 Seri Telawi Seremban
444 Vince M 28 4 Jalan Murni Kulai
555 Rosma F 32 5 Taman Daya Johor Bahru

Customer_Balance Table
IDNo Savings (RM) Current (RM)
111 20,000.00 5,000.00
222 18,000.00 8,000.00
333 23,000.00 3,000.00
444 12,000.00 2,000.00
555 35,000.00 5,000.00

1. Create a form for the Customer table.

2. Create a report that lists NoID, Name, Address, City and Saving
for each customer.

Copyright Open University Malaysia (OUM)


100 X TOPIC 6 DATABASE DEVELOPMENT II

6.3 SWITCHBOARD
Switchboard is a special type of form which consists of several buttons that can
be clicked to execute a particular operation. The button on the main switchboard
prompts the users to other switchboards. That button can be used to open a table
or form, to display a report on display mode or to print the report.

6.3.1 Creating a Switchboard


To create the switchboard, you need to use the Database Utilities instruction on
Tools menu, and select Switchboard Manager. The Switchboard Manager is a
tool used to create, modify and delete the switchboard forms in any application.
The steps to create a switchboard are explained from Figure 6.12 to Figure 6.16
below:

Figure 6.12: The Tools menu to create a switchboard

The dialogue box shown in Figure 6.13 displays a message that tells us if there
are no existing switchboard for the mentor-mentoree database. Click the Yes
button to create a new switchboard.

Figure 6.13: Dialogue Box for Switchboard Manager

Copyright Open University Malaysia (OUM)


TOPIC 6 DATABASE DEVELOPMENT II W 101

The Dialogue Box of the Switchboard Manager displays a message that only
main switchboard exists on the database as shown in Figure 6.14.

Figure 6.14: Display of a page inside switchboard

When the Create New dialogue box is displayed, insert the name of the
switchboard page that will be created, as shown in Figure 6.15. Repeat this step
several times to create a switchboard page that displays the form and table.

Figure 6.15: Producing a new switchboard page named Display a Report

When the switchboard page has been created, you can use the switchboard by
right-clicking on the switchboard object and selecting Open as shown in Figure
6.16.

Copyright Open University Malaysia (OUM)


102 X TOPIC 6 DATABASE DEVELOPMENT II

Figure 6.16: Menu to display switchboard

The switchboard page displayed is as shown in Figure 6.17.

Figure 6.17: Display of switchboard main page

When the button of Display a Form is clicked, the switchboard page for Display
All Forms will be displayed as shown in Figure 6.18.

Copyright Open University Malaysia (OUM)


TOPIC 6 DATABASE DEVELOPMENT II W 103

Figure 6.18: The Display Form page when its button has been clicked

To close the switchboard and database, click on the Exit button on the main
switchboard page.

ACTIVITY 6.4
Using any information from an organisation as your example, create
a database by using Microsoft Access for the organisation. Produce
suitable tables and links between each table and the created.

x Microsoft Access can be used to develop a complete database including a


table, query, form and report. In table development, there are several points
that need to be known when designing a table, form and report in order to
ensure the database fulfills the users requirements. As such, you are required
to practise various exercises involving database development to enhance
your skills.

Form
Report Design

Copyright Open University Malaysia (OUM)


Topic X Transaction
Management
7
LEARNING OUTCOMES
By the end of this topic, you should be able to:
1. Define the meaning of transaction and its importance;
2. Discuss the importance of concurrency control; and
3. Describe the concurrency control techniques.

X INTRODUCTION
In this topic you will be introduced to transaction and its importance in the
Implementation of a database. You will also learn the four main aspects to be
fulfilled by a particular transaction which is atomicity, consistency, isolation and
durability, also known as ACID. With the corresponding example, this topic
explains transactions and their characteristics. You will also learn several
problems relating to transaction management.

Once these problems have been addressed, you will learn on concurrency control
implemented to solve the problem that arises. In this topic, transaction and
concurrency control will be discussed in the context of a DBMS.

Copyright Open University Malaysia (OUM)


TOPIC 7 TRANSACTION MANAGEMENT W 105

7.1 TRANSACTION SUPPORT

ACTIVITY 7.1

What can you comprehend from the word transaction? Is it the same
as transactions at the market or supermarket?

From the point of view of a database user, a group of operations on a DBMS is


seen as a single unit. For example, a consumer sees his cash transfer from a
savings to current account as a single operation. However, from a database
perspective, this process involves several operations. It is required that each
operation be implemented perfectly as an error could affect the whole operations.
The consumer in the example above would surely dread errors in his cash
transaction if they were to occur.

A group of operations that shapes one logical work unit is called a transaction
(Silberschatz et. al 1997). Connoly et. al (1999) defines transaction as an act or
series of actions implemented by one user or readable programs or updates on
the content of a database. Transaction is produced from the implementation of
user programmes through a higher language to manipulate data such as SQL,
COBOL and others.

Example of transaction is a programme, a row or part of a row of commands


such as INSERT or UPDATE. Each transaction contains two statements, which
are INSERT and UPDATE. Each transaction falls within two statements i.e. begin
transaction and end transaction. In the transaction code two of the working
commands will appear - commit and rollback/abort. Each terminology shows a
particular action taken relating to the transaction.

Each transaction is ended when one of the two commands above is executed. It is
ended by the command commit. Each transaction must change the database
from one consistent state to another consistent state. If the rollback command is
used, no changes will be executed upon the database as all operations will be
automatically aborted. In such an instance the database must be returned to its
consistent state before the transaction is implemented.

Any transaction following the commit command cannot be aborted. If the


transaction is an error, such as depositing to the wrong account, we need to
implement other rollback transaction error. This is known as compensating.

Copyright Open University Malaysia (OUM)


106 X TOPIC 7 TRANSACTION MANAGEMENT

Try to understand the following transaction:

begin transaction
X:= X  50;
X:= Y + 50;
commit work;
end transaction

This transaction can be translated as an operation at the bank to transfer RM50


from account X to account Y. This is a transaction representative. In the actual
situation, the codes used are more complex and are written by programming
languages mentioned earlier.

7.1.1 Properties of Transaction


There are properties all transactions should possess. The four properties or ACID
are:

(a) Atomicity
The 'all' or 'nothing' property. A transaction is performed entirely or not
performed at all.

(b) Consistency
A transaction must transform the database from one consistent state to
another consistent state.

(c) Isolation
Transactions are executed independently of one another. The partial effects
of incomplete transaction should not be visible to other transactions.

(d) Durability
The effects of a successfully completed transaction are permanently
recorded in the database and must not be lost because of a subsequent
failure.

Reading of databases is based on the following two operations:

(a) read (x)


This operation transfers data X for the database to stack, owned by
transactions implementing the read operations.

Copyright Open University Malaysia (OUM)


TOPIC 7 TRANSACTION MANAGEMENT W 107

Figure 7.1: Read (X)

(b) write (X)


This operation transfers the X data from the stack of transaction that
executes the write operations over to the database.

Figure 7.2: Write (X)

To comprehend the characteristics above, let us have a look at one example.


Assume that T is a transfer transaction that transfers RM500 from Anna's
account to Dahlias account. The transaction can be stated as:

begin transaction
read (A);
A:= A  500;
write (B);
D:= D + 500;
write (B);
end transaction

Now let us see how the requirements for ACID are maintained in the
implementation of the above transaction.

(a) Consistency
The consistency to be fulfilled is the account total of Anna's and Dahlias
accounts which has not changed with implementation of transaction.

Copyright Open University Malaysia (OUM)


108 X TOPIC 7 TRANSACTION MANAGEMENT

Without this consistency requirement, cash transaction can be created out


of nothing or deleted through transaction. In the development of
application, application programmes through an encoded key is
responsible for ensuring individual transactions are consistently updated.

(b) Atomicity
Assume that Anna's account has RM1000.000 and Dahlia's account,
RM2000.00. Both accounts total RM3000.00. Assuming that failure occurs
when a transaction is performed, T is disabled from being executed. Types
of failure include power failure, peripherals and programmes.

These failures may occur between write (A) and read (B) as shown in Table
7.1.

Table 7.1: Failure between Write (A) and Read (B)

Balance A Balance D Amount


Begin transaction 1,000 2,000 3,000
read (A) 1,000 2,000 3,000
A:=A- 50 950 2,000 2,950
write (A) 950 2,000 2,950
FAILURE 950 2,000 2,950

In this case, Anna's and Dahlia's account balances are presented in the
database as RM950 and RM2,000. The value RM50 has been deleted due to
this failure. In Figure 7.1, Anna's and Dahlia's accounts show a balance of
RM2,950 and not RM3,000. This failure has failed to show the actual
occurrence in a database.

The idea behind atomicity is that a DBMS requires old values to be stored in
a CD-ROM for any data undergoing write operations in any transaction. If
transaction failure occurs in the process, the old values can be returned and
the product shows as if no transaction has occurred.

(a) Durability
When the application of transaction management has been completed and a
user informed of the transaction that took place has begun to go through,
data on monetary transfer will not be lost even if system failure occurs.

(b) Isolation
Although transaction can determine atomicity and consistency, the operation
may clash once it has begun to transact in a concurrent and complex
environment. One way of handling this setback is to transact one by one. The

Copyright Open University Malaysia (OUM)


TOPIC 7 TRANSACTION MANAGEMENT W 109

isolation factor ensures that a concurrent transaction management equals the


situation where the transaction is implemented one by one.

ACTIVITY 7.2
Complete the following table.
Properties of Transaction Detail
1. Atomicity
2. Consistency
3. Isolation
4. Durability

7.2 CONCURRENCY CONTROL


You have just learnt about isolation. Here, concurrency control is mentioned. Do
you understand the meaning of concurrency control?

A DBMS frequently faces with many applications and requires the response of
many users concurrently (Atzeni et. al 2000). The burden of application being
carried by the DBMS is normally in the number of transactions per second (tps).
The system applied in banks or in the financial information system must respond
to this burden in numbers of decimal points of up to hundreds of tps. Flight
booking companies, for instance, transact thousands of tps.

As such, transactions for these companies must be performed concurrently


and not one by one.

With the implementation of concurrency control, the number of transactions to


be processed can be maximised and responses towards transactions can be
minimised. Concurrency control is required to manage a transaction that occurs
concurrently.

Connolly et. al (2001) defines concurrency control as the process of managing


several concurrent operations upon the DBMS and thus, ensuring that operations
do not affect each other.

Once you have understood the concept of concurrency control, answer the
following question to ascertain your understanding.

Copyright Open University Malaysia (OUM)


110 X TOPIC 7 TRANSACTION MANAGEMENT

ACTIVITY 7.3

What is meant by concurrency control?

7.2.1 The Requirement


Why is concurrency control required in a DBMS? The major objective of
developing a database is to enable many users to access shared data
concurrently. Concurrent access is relatively easy if all users are only reading
data and they do not interfere with one another. However, when two or more
users are accessing the database simultaneously and at least one is updating data,
there may be interference that can result in inconsistencies.

This objective is similar to the objective of multi-user computer systems, which


allow two or more programmes (or transactions) to be executed at the same time.
For example, two transactions have been reading the same data in the database.
Both transactions are correct but during operations, it is interleaved. This may
result in errors occurring which will affect consistency and integrity. We will see
further examples on concurrency process.

There are three problems related to concurrency:


x The lost update problem.
x The uncommitted dependency.
x The inconsistent analysis problem.

(a) Lost Update Problem


An example of the lost update problem is shown in Table 7.2.

Copyright Open University Malaysia (OUM)


TOPIC 7 TRANSACTION MANAGEMENT W 111

Table 7.2: Lost Update Problem

Time Transaction 1 Transaction 2 X Balance


t1 Begin_transaction 100
t2 Begin _transaction Read (x balance) 100
t3 Read x balance x balance= x balance + 100 100
t4 x balance= x balance-10 Write (x balance) 200
t5 Write x balance Commit 90
t6 Commit 90

In the example above, Transaction 1 is being executed simultaneously with


Transaction 2. Transaction 2 is adding RM100 in x account whereas
Transaction I is misusing RM10 from the same account. If both transactions
are conducted one by one, no problems may occur as the final balance in x
is RM190 regardless which transaction occurs first. Both transactions occur
at the same time and both read the x balance as RM100. Transaction 2 adds
RM100 to x balance (100 + 100 = 200) and stores this update in the database.
Simultaneously, Transaction I stores this balance in the database. This task
eliminates the previous update and will erase the RM100 stored which was
previously added as balance.

The best method to ensure transaction updates are preserved in Transaction


2 is by commanding Transaction I to stop reading x balance until updates
on Transaction 2 is completed.

(b) The Uncommitted Dependency


An example of uncommitted dependency is shown in Table 7.3.

Table 7.3: The Uncommitted Dependency

Time Transaction 3 Transaction 4 Balance X


Begin_transaction 100
Read (balance X) 100
t1 balance= balance X + 100 100
r2 Begin _transaction Write (balance X) 200
r3 Read balance X 200
r4 Balance = balance X -10 Rollback 100
r5 Write balance X 190
R6 Commit 190

Copyright Open University Malaysia (OUM)


112 X TOPIC 7 TRANSACTION MANAGEMENT

This problem may occur if a particular transaction (Transaction X) is


allowed to view the latest product of the other transaction (Transaction Y)
before Transaction Y commits. In this example, Transaction 3 and
Transaction 4 are reading the x balance. Transaction 4 has updated the x
balance as RM200 but Transaction 4 is held up by the command abort. The
x balance should have been returned to its actual amount which is RM100.

But at this point in time Transaction 3 has already read the new x balance
which is RM200 and deducted RM10 from the existing balance (200  10 =
190). This produces an incorrect balance which is RM190. The correct
balance is RM90.

The rollback that occurs may have been due to several factors such as an
error in transaction of account credit. Transaction 3 assumes that
Transaction 4 has successfully been completed although rollback occurs.
This problem can be solved by withholding Transaction 3 from reading x
balance until Transaction 4 makes the decision to commit or abort.

(c) The Inconsistent Analysis Problem


We have examined both problems above as a concurrency control. Both
problems are related to updates in database and possible interference in the
database. However, transactions reading the database may potentially
commit errors if the allowed transaction which has not been calculated is
being updated in the database. This is known as the inconsistent analysis
problem.

This problem occurs when a transaction reads several values in the


database and there exist other transactions which are updating the values in
the database while the first transaction is still being performed. For
instance, one transaction currently calculating the balance from the
database may obtain a different balance if other transactions are updating
its balance.

This problem is shown in Table 7.4. Transaction 6 is concurrently


performed with Transaction 5. Transaction 6 is calculating the balance for
account x (RM100), y account (RM50) and z account (RM25).
Simultaneously, Transaction 5 has transferred RM10 from x balance to z
balance. An error has occurred in the current transaction (more than RMI0).
The correct amount should be RM170.

This problem can be solved by withholding Transaction 6 from reading x


and z balance until Transaction 5 has completed all updates.

Copyright Open University Malaysia (OUM)


TOPIC 7 TRANSACTION MANAGEMENT W 113

Table 7.4: The Inconsistent Analysis Problem

Balance Balance
Time Transaction 5 Transaction 6 BalanceY Total
X Z
T1 Begin 100 50 25
_transaction
T2 Begin Sum = 0 100 50 25 0
_transaction
T3 Read balance X Read balance 100 50 25 0
X
T4 Balance X = Sum = sum + 100 50 25 100
balance X - 10 balance X
T5 Write balance X Read balance 90 0 25 100
Y
T6 Read balance Z Sum = sum + 90 50 25 150
balance Y
T7 Balance Z = 90 50 35 150
balance Z + 10
T8 Write balance Z 90 50 35 150
T9 Commit Read balance 90 50 35 150
Z
T10 Sum = sum + 90 50 5 185
balance Z
T11 Commit 90 50 35 185

You may obtain further information on these problems by reading the book
Database System: A practical Approach to Design, Implementation and
management by Connoly & Begg, year 2001 published by Addison Wesley.

7.2.2 Concurrency Control Techniques


There are two main concurrency control techniques:
(a) Pessimistic approach (conservative)
(b) Optimistic approach

The pessimistic approach causes transactions to be delayed in case they conflict


with other transactions in the future.

Copyright Open University Malaysia (OUM)


114 X TOPIC 7 TRANSACTION MANAGEMENT

The optimistic approach is based on the premise that conflict is rare so it allows
transactions to proceed unsynchronised and only check for conflicts at the end.

Among the techniques of the pessimistic approach are:


(a) Locking
(b) Time stamping

According to Connoly et. al (1997), locking is a procedure to read concurrency


control on data. When a transaction is reading data, a lock may deny access to
other transactions to prevent incorrect results. Locking methods are the most
widely used approach in concurrency control implementation. There are several
types of reading locks that can be shared, such as a write lock which is exclusive
on one data item before writing or reading the database.

Methods of using the read and write lock.

(a) Read Lock


If transaction T locks data x with the read lock, T can read x but is not able
to update (write) x. As such several transactions may apply the read lock on
the same data item in an exclusive property (not shared).

(b) Write Lock


If transaction T applies the write key on data item x, T can read and write
on data item x. This is why the write lock is exclusive (not shared) as data
updates can be performed. As such, the write key disables other
transactions from making adjustments or reading the data item.

Locks are conducted through the following steps:


(a) Any transaction that needs to access a data item must first lock the item,
requesting a shared lock for read only access or an exclusive lock for both
read and write access.
(b) If the item is not already locked by another transaction, the lock will be
granted.
(c) If the item is currently locked, the DBMS determines whether the request is
compatible with the existing lock. If a shared lock is requested on an item
that already has a shared lock on it, the request will be granted; otherwise,
the transaction must wait until the existing lock is released.
(d) A transaction continues to hold a lock until it explicitly releases it either
during execution or when it terminates (aborts or commits). It is only when

Copyright Open University Malaysia (OUM)


TOPIC 7 TRANSACTION MANAGEMENT W 115

the exclusive lock has been released that the effects of the write operation
will be made visible to other transactions.

In addition to these rules, several systems allow transactions to place the read
key on data items and then upgrade the lock status from a read lock to a write
lock. This allows transactions to check data and then reach a conclusion on
whether to update it or not. Several systems enable transactions to obtain the
write lock and then downgrade it to a read lock.

Now we shall examine the protocol lock most widely used which is 2-Phase
Locking (2PL). According to 2PL, each transaction may be divided into two
phases:

(a) Growing Phase


In this phase, transactions obtain all locks required but cannot release any
locks.

(b) Shrinking Phase


In this phase, transactions can release all locks but transactions cannot be
carried out simultaneously.

The rules are:


(a) A transaction must acquire a lock on an item before operating on the item.
The lock may be read or write, depending on the type of access needed.
(b) Once the transaction releases a lock, it can never acquire any new locks.

Let us see how the 2-Phase Locking is applied to solve the problems that have
been discussed earlier.

Table 7.5 presents how the lost update problem is solved using the 2-Phase
Locking.

Copyright Open University Malaysia (OUM)


116 X TOPIC 7 TRANSACTION MANAGEMENT

Table 7.5: The Usage of 2-Phase Locking to Solve the Lost Update Problem

Time Transaction 5 Transaction 6 Balance X


T1 Begin _transaction 100
T2 Begin _transaction Read_key (X balance) 100
T3 Read_key (X balance) Read balance X 100
T4 WAIT Balance = balance X + 10 100
T5 WAIT Write balance X 200
T6 WAIT Commit/balance X open key 200
T7 Read balance X 200
T8 Balance = balance X -10 200
T9 Write balance X 190
T10 Commit/balance X open key 190

To prevent the lost update problem from occurring, Transaction 2 will request
the x balance from the write lock. Then it will read the x balance value from the
database, add it with RM100 and write the new balance in the database. When
Transaction 1 begins, it will also request the read key on x balance. Because x
balance is locked with the write key by Transaction 2, this request is denied and
Transaction 1 must wait until Transaction 2 releases the lock. Transaction 2 will
release lock through the command commit.

ACTIVITY 7.4

1. Give an example of:


(i) Read Lock
(ii) Write lock

2. Fill in the blanks


(i) ................... occurs when two or more transactions wait upon
the key held by other transactions.
(ii) ................... ................... is applied to arrange transaction
implementation.

Copyright Open University Malaysia (OUM)


TOPIC 7 TRANSACTION MANAGEMENT W 117

7.2.3 Deadlock
In several implementations of concurrency control, deadlock may result when
two or more transactions are each waiting for locks to be released that are held
by the other (Connolly et. al 2001). Deadlock is depicted in Figure 7.3.

Figure 7.3: Deadlock occurs when T and T are each waiting for the other to release its lock

Figure 7.3 above presents two transaction Ta and Tb that stay during deadlock
because each transaction waiting for the other to releases their key.

When deadlock occurs, the application involved will not be able to solve the
problem. As such the DBMS must identify the existence of deadlock and break
the deadlock in some way. The best way to break a deadlock is by breaking one
or more transactions involved with deadlock. This means, the impact of all
operations performed must be cancelled.

7.2.4 Time Stamping

Apart from use of locks, time stamping is also applicable for transaction
implementation.

Time stamping differs from the lock method. No locks are involved and therefore
there can never be a deadlock. The locking methods generally prevent conflicts
by making transactions wait. With the time stamp method, there is no waiting.
Transactions involved in conflict are simply rolled back and started. Connolly et.
al (2001) defines time stamping as a unique introduction created by DBMS to
indicate that a transaction has begun. Time stamping is generated by using the
timing system at the point where transaction has been performed or by using the
counter when each transaction begins.

Copyright Open University Malaysia (OUM)


118 X TOPIC 7 TRANSACTION MANAGEMENT

Time stamping is a concurrency control protocol with the main aim of organising
transactions for older transactions (transactions with smaller timestamp) to
obtain the priority when conflict occurs.

ACTIVITY 7.5

In your opinion, which method is better, lock or time stamping?


Share your opinion with classmates.

7.2.5 Optimistic Technique


To date, we have discussed the pessimistic technique. Let us now discuss about
the optimistic technique. Optimistic techniques are based on the assumption that
conflict is rare and that it is more efficient to allow transactions to proceed
without imposing delays to ensure a series of continuity. When a transaction
commits, a test is conducted to see whether conflict exists. If conflict has already
occurred, the transaction is required to rollback and restart.

According to the pessimistic approach, transaction is delayed to prevent conflict


in future transactions. The optimistic approach, on the other hand, assumes
conflict as rarely occurring. As such this approach allows transactions to proceed
without synchronisation and conflict tests are carried out at the end of the
transaction (commit transaction).

ACTIVITY 7.6

1. What is the difference between the optimistic technique and


pessimistic technique?

2. Explain the four properties that must be fulfilled by a particular


transaction.

3. Show the application of 2-Phase Locking problem and


inconsistency analysis problem.

SELF-CHECK 7.5

Copyright Open University Malaysia (OUM)


TOPIC 7 TRANSACTION MANAGEMENT W 119

x Transaction is a logical work unit in a database. Each transaction must fulfill


the ACID properties. Concurrency control is a process of administering
concurrent operations without interfering with one another. There are two
main concurrency controls which are the pessimistic technique (lock and
timestamp) and the optimistic technique. Deadlock is an occurrence where
several transactions await other transactions to release the lock. One of the
ways to break a deadlock is by aborting one or more of the transactions
involved in a deadlock situation.

Concurrency control
Transaction

Copyright Open University Malaysia (OUM)


T op i c X Databaseand
Web
8 Technology
LEARNING OUTCOMES
By the end of this topic, you should be able to:
1. Explain the fundamentals of the Internet, Web HTTP, HTML and
URL;
2. Identify the strengths and weaknesses of the database platform; and
3. Discuss the approaches applied in the synchronisation process from a
database to a Web environment.

X INTRODUCTION
One of the most important phenomenon in information technology is the internet
evolution. This is proven by the increase in internet usage in the corporate world
and among personal users. The technology closely related to the internet is the
World Wide Web (www) or Web.

Silberschatz et al. (1997) defines the World Wide Web as a powerful networked
information system based on the hypertext. Most of the existing websites are
based on files where each Web document is stored in different files. For a smaller
Web this may invite administrative difficulty.

In their initial stage, websites were static. The have now evolved into dynamic
data such as information on stock amount and pricing. It is a great challenge to
organiae such information. As such, the method that allows data to be read
directly from the Web is much more friendly and dynamic in content. Web
information stored in a database is able to support file application or
automatically eliminate the application of files.

Copyright Open University Malaysia (OUM)


TOPIC 8 DATABASE AND WEB TECHNOLOGY W 121

This topic aims to examine this latest technology involved in synchronising the
Web and databases. This topic also aims to expose students to the latest
developments in this technology.

8.1 AN INTRODUCTION TO THE INTERNET


AND WEB
There are many definitions relating to the internet. Among the definitions is that
the internet is a computer network which is inter-connected around the world
(Connolly et al 2001). The internet consists of different interrelated networks.
Among the owners of these networks are education institutions, research,
governments, commercial businesses and Internet Information Providers. The
common features applied by users are electronic mail, transfer and sending of
files, chat room and information searches through the search engine.

Apart from the internet, there is also the term intranet, a website or group of sites
owned by particular organisations which is only readable by that particular
organisation only. Internet standards for exchanging email and publishing Web
pages are becoming increasingly popular for business use within closed
networks called intranets. Intranet is connected to the internet through the
firewall. There are several restrictions involved in the input and output of
information from the intranet. For example, workers in an organisation can send
an electronic mail and access many websites in the internet but people outside
the organisation can only send electronic mail to the organisation and they do not
have access to the website content on the intranet.

In addition, there exists another term called extranet, which is an intranet with
contents accessible by particular outsiders. If the intranet is located behind the
firewall and is only accessible by members of the organisation, extranet provides
multiple accesses with valid user names and passwords. The user name and
password determines the sections allowed to be accessed. Extranets are becoming
a very popular method for business partners to exchange information.

The following simple exercise is designed for you to assess your own
understanding on the topic you have just learnt. Answer the question before
proceeding with your studies.

SELF-CHECK 8.1

What is meant by internet? Explain

Copyright Open University Malaysia (OUM)


122 X TOPIC 8 DATABASE AND WEB TECHNOLOGY

8.1.1 The Web


The World Wide Web (Connolly et at. 2001) provides a simple 'point and click'
means of exploring the immense volume of information residing on the internet.
Information on the Web is presented on Web pages which appear as a collection
of text, graphics, pictures, sound and video. In addition, a Web page can contain
hyperlinks to other Web pages, which allow users to navigate in a non-sequential
way through information.

The Web is a simple tool for users to provide, use and refer to information
distributed around the world. Furthermore, it provides users with the ability to
browse multimedia documents independently of the computer hardware being
used. It is also compatible with other existing communication protocol such as
FTP, NNTP and Telnet.

Generally, the Web consists of a network of computers that can act in two roles:
as servers, providing information; and as clients, requesting information. Table
8.1 illustrates the most widely used types of servers and clients.

Table 8.1: Types of Servers and Clients

Servers Client
Apache Netscape Navigator
Netscape Communication Server Microsoft Internet Explorer
Microsoft Information Server Opera

All information on the Web is stored in documents through the usage of


Hypertext Markup language (HTML). To display the documents, servers are
required to understand and translate HTML.

The protocol that organises the exchange of information between servers and
clients is known as Hypertext Transfer Protocol (HTTP). Documents and
locations are identified from the address defined as Uniform Resource Locators
(URL).

ACTIVITY 8.1

Give an example of a URL. Find the HTML file of the URL.

Copyright Open University Malaysia (OUM)


TOPIC 8 DATABASE AND WEB TECHNOLOGY W 123

The following Figure 8.1 shows the components in an Internet environment.

Figure 8.1: The internet components

SELF-CHECK 8.2

List the internet components and explain each of the components.

8.1.2 HTTP
Hypertext Transfer Protocol (HTTP) is a protocol used to transfer Web
documents in the internet. A protocol defines how servers and clients
communicate. HTTP is an object oriented and a generic protocol. The basis of
HTTP is paradigm and response. The HTTP transaction consists of the following
stages:

Connection - The client establishes a connection with the Web server.


Request - The client sends a request message to the Web server.
Response - The Web server sends a response (for example, an HTML
document) to the client.
Close - The connection is closed by the Web server.

8.1.3 HTML
HTML is a formatting language used to design the website. It marks up, or tags a
document so that it can be published on the Web. HTML is also known as a
Standardized Generalized Markup Language (SGML).
Copyright Open University Malaysia (OUM)
124 X TOPIC 8 DATABASE AND WEB TECHNOLOGY

8.1.4 URL
Uniform Resource Locators (URLs) define uniquely where documents (resources)
can be found on the Internet. Connolly defines URL as a string of alpha numeric
characters that represents a location or address of a particular source in the
internet and how the source can be obtained. The URL syntax is easy to
understand and consists of three main divisions:
(a) The protocol used to connect.
(b) Host name.
(c) Document password on host.

Apart from this, URL can place the port where connection to host is positioned
(default 80 for HTTP) and string of queries being the main method of passing
data to clients and servers.

The URL syntax is recorded as:

<Protocol>://<hos>:[:<port>]pass_absolute [? Argument]

A protocol defines the mechanism used by surfers to communicate with the


source. The common access methods are HTTP, S-HTTP (secure HTTP), file
(download from CD-ROM), FTP, mailto (send electronic mail to a particular mail
address), Gopher and Telnet. An example of URL is as follows:

http://www.oum.edu.my

The URL is to identify the website for Open University Malaysia in WWW. The
protocol is HTTP, host is www.oum.edu.my.

8.1.5 Static and Dynamic Web Pages


An HTML document stored in a file is an example of a static Web page: the
content of the document does not change unless the file itself is changed. The
static web page features the information required. On the other hand, the
dynamic web page is generated each time it is accessed. It produces output based
on the input given. The dynamic web page consists of various characteristics that
do not consist on static web pages. These characteristics are:

Copyright Open University Malaysia (OUM)


TOPIC 8 DATABASE AND WEB TECHNOLOGY W 125

(i) Response to user input from the browser. For example, returning data
requested by the completion of a form or the results of a database query.

(ii) Be customised by and for each user. For example, once a user has specified
some preferences when accessing a particular site or page (area of interest
or web page), this information can be retained and information returned
appropriate to these preferences.

Servers need to generate hypertext to obtain the dynamic web site, for example
returning output of query on database. As such, we need to write scripts that
perform conversions on different data formats to HTML. This script must be able
to read queries from clients through HTML form and the output obtained by
applications with DBMS data.

A database is dynamic, changing as users create, insert, update and delete data.
Hence generating dynamic Web pages is a more appropriate approach than
creating static ones for the purpose of data manipulation. Several methods will
be discussed in the next topics.

SELF-CHECK 8.3

Answer the following questions:

1. What is the protocol used to simplify web documents in the


internet?
2. What is the formatting document language used to design a
website?
3. What are the functions of URL (Uniform Resource Language)?
4. Explain the difference between a static web page and a
dynamic web page.

8.2 THE WEB AS A DATABASE APPLICATION


PLATFORM
You already know that the web consists of many functions in providing current
information. Try to think how a web can become a platform for a database.

Copyright Open University Malaysia (OUM)


126 X TOPIC 8 DATABASE AND WEB TECHNOLOGY

In this section, we shall examine how the Web is used as a platform to enable
users to communicate in Web database. In addition, several architectures to
obtain this synchronisation will be discussed. Lastly, the strengths and
weaknesses of the process will also be explained.

8.2.1 Requirements for Web-DBMS Integration


Several requirements that must be fulfilled in order to synchronise a Web
database are listed below. Not all of the requirements can be achieved but it is
hoped that they can guide the process.

(i) The ability to access valuable corporate data in a secure manner.


(ii) Data and vendor independent connectivity to allow freedom of choice in
the selection of the DBMS now and in the future.
(iii) The ability to interface to the database independent of any proprietary Web
bowser or Web server.
(iv) A connectivity solution that takes advantage of all features of an
organisation's DBMS.
(v) An open-architecture approach to allow interoperability with a variety of
systems and technologies.
(vi) A cost-effective solution that allows for scalability, growth and changes in
strategic directions and helps reduce the costs of developing and
maintaining applications.
(vii) Support for session- and application-based authentication.
(viii) Acceptable performance.
(ix) Minimal administration head.
(x) A set of high-level productivity tools to allow applications to be developed,
maintained and deployed with relative easy speed.

8.2.2 Web-DBMS Architecture


In this section, we will examine two types of DBMS which are the two-tier client-
server architecture and three-tier. Figure 8.2 illustrates the two-tier architecture.

Copyright Open University Malaysia (OUM)


TOPIC 8 DATABASE AND WEB TECHNOLOGY W 127

Figure 8.2: Two-tier architecture

Various business applications that organise data consist of four main


components:
(i) Database
(ii) Logic transaction
(iii) Logic Application
(iv) User interface

In the main mainframe environment, all components were in one place, as one
would have expected, in a highly centralised business environment. To
accommodate an increasingly centralised business environment, the client-server
system was developed to support this scenario. Figure 8.2 shows the output of
two-tier architecture which provides separation of task. A client (tier 1) is
primarily responsible for the presentation of data to the user. The server (tier 2) is
primarily responsible for supplying data services to the client.

Presentation service provides limited business application logic, typically


validation that the client is unable to carry out due to lack of information. The
data can come from relational DBMSs, object-relational DBMSs, object-oriented
DBMSs, legacy DBMSs and others. The client would run on end-user desktops
and interact with a centralised database server over a network.

Copyright Open University Malaysia (OUM)


128 X TOPIC 8 DATABASE AND WEB TECHNOLOGY

Three-tier Architecture
In the mid 1990s, applications became more complex with thousands of users.
Subsequently, client server application presented two problems:
(a) A 'fat client', requiring considerable resources on the client's computer to
run effectively. This includes disk space, RAM and CPU power.
(b) A significant client-side administration overhead.

In 1996 a variation to the client-server two-tier model was introduced to solve the
scalability problem. The architecture consists of three tiers. Each tier would run
on a different platform:
(a) The user interface layer, which runs on the end-user's computer.
(b) The business logic and data processing layer. This middle tier runs on a
server and is often called the application server.
(c) A DBMS, which stores the data required by the middle tier. This tier may
run on a separate server called the database server.

Figure 8.3 illustrates the three-tier architecture.

Figure 8.3: Three-tier architecture

Copyright Open University Malaysia (OUM)


TOPIC 8 DATABASE AND WEB TECHNOLOGY W 129

The client is now responsible for the application's user interface and perhaps
performing some simple logic processing such as input validation, thereby
providing a thin layer physically connected to the client and database server over
a local area network (LAN) or wide area network (WAN). One application server
is designed to serve multiple clients.

The three tier design has many advantages over the traditional two-tier which
include:
(a) The need for less expensive hardware because the client is 'thin'.
(b) Application maintenance is centralised with the transfer of the business
logic for many end users into a single application server.
(c) The added modularity makes it easier to modify or replace one tier without
affecting the other tiers.
(d) Load balancing is easier with the separation of the core business logic from
the database functions.

In addition, the three-tier architecture maps naturally in the Web environment.


Servers act as clients and clients as Web servers as application servers. The three-
tier architecture can be extended to n-tiers to provide more flexibility and
scalability.

8.2.3 Advantages and Disadvantages of the Web-


DBMS Approach
The World Wide Web can be seen as the biggest database in the world. However,
WWW was built independently, without the concept of database in it. In the
previous topics, you were presented with the advantages of a DBMS approach
compared to the Filing system. In reality, there are more advantages of a DBMS
in the application of a Web-DBMS. The DBMS simplifies the Web-DBMS
administrative functions and HTML content can be safeguarded with DBMS'
features such as security and integrity.

There are several advantages and disadvantages relating to Web-DBMS which


will be discussed in this topic. These are:

(a) Simplicity
Easy to apply HTML as a document formatting language for naive users as
well as application developers.

Copyright Open University Malaysia (OUM)


130 X TOPIC 8 DATABASE AND WEB TECHNOLOGY

(b) Platform Independence


Independent surfers (Web client), free from any platform. This remains the
pillar of traditional database application in its expansion. Most traditional
database users had to undergo various changes before performing at
different platforms.

(c) Graphical User interface


Most surfers provide a general and easy graphical user interface. General
interface lessens the training cost for end-users.

(d) Standardisation
All Web surfers adhere to HTML standards. This enables access over
HTML documents from one computer to other users in any location in the
world that is plugged into the internet and Web browser.

(e) Cross Platform Support


Web surfers may be connected from any computer platform. Support in
between platforms enables users to apply any type of computer connected
to a database from any location. With such advantage, information can be
dispersed with minimal effort and time.

(f) Transparent Network Access


Web users can access transparent network. This support enables easy
database access and lessens the need for expensive network software
application. It also lessens the problems that may occur in synchronising
different platforms.

(g) Scalable Deployment


The two-tier architecture produces a fat client that requires the processor
of interface users and logical application. Web based solution produces a
three-tier architecture that enables scalability. By storing the application on
a separate server, the Web lessens the time and costs associated with user
application.

(h) Innovation
Web users open up new services and new clients that can be accessed
globally.

ACTIVITY 8.2

Can you list other advantages apart from the ones listed?

Copyright Open University Malaysia (OUM)


TOPIC 8 DATABASE AND WEB TECHNOLOGY W 131

Disadvantages:

(a) Reliability
The internet is currently an unreliable and slow communication medium.
Whenever a request is put in, there is no guarantee that it will reach the
receivers as the term server down may occur. This can be experienced
when users try to obtain information from servers at peak hours. As such
the servers become slower in speed. The issue of reliability is closely linked
to security. As such most organisations prefer to apply their own intranets
rather than the internet especially when critical application is required.

(b) Security
Security is of great concern for an organisation that makes its database
accessible on the Web. User authentication and secure data transmissions
are critical because of the large number of potentially anonymous users.

(c) Cost
The cost of maintaining a website is particularly high especially with the
rising demands from workers. Forrester Research indicated the cost of
maintaining a website falls between $300 thousand to $3.4 million. This cost
also depends on the mission of the organisation pertaining to their
respective websites.

(d) Scalability
Web applications can face unpredictable and enormous peak loads. As such
they require high performance server architecture.

(e) Limited Functionality of HTML


HTML provides easy-to-use interface. However, this property limits the
same user-friendliness as it difficult for it to be interactive with
applications.

(f) Bandwidth
This is one of the sources of internet that restricts the ability of a DBMS.

(g) Performance
Many parts of web database client are required to interpret languages,
making them slower in processing than the traditional data processing. This
problem is inherent in time critical applications.

Copyright Open University Malaysia (OUM)


132 X TOPIC 8 DATABASE AND WEB TECHNOLOGY

8.2.4 Approaches to Integrating the Web and DBMS


There are many approaches to integrating databases into the web environment
such as:
x Common Gateway Interface (CGI)
x Extension to web servers
x Scripting languages

(i) Common Gateway Interface (CGI)


CGI is a specification to transfer information between Web servers and CGI
program. Usually, Web surfers do not require a comprehensive command
of language on the documents requested. Servers may be able to retrieve
the documents sent to them and send them to clients. Servers can also
activate other programmes. When it is certain that the URL points to a
programme (script), servers will implement scripts and the script will be
output to browser.

CGI defines how communication scripts communicate with web servers.


The CGI script is an input and output data script that adheres to CGI
specification. Figure 8.4 illustrates the CGI environment. CGI is a
standardisation for the Web user interface and outer applications. It is also
the most common method to connect Web application with data source.

CGI has several advantages such as being user friendly, independent of any
languages, independent of any web servers and is widely accepted by web
applications developers. However, there are several disadvantages to CGI
such as developing a new process each time CGI is performed. This may
affect users during peak hours. Figure 8.4 illustrates the CGI components.

Copyright Open University Malaysia (OUM)


TOPIC 8 DATABASE AND WEB TECHNOLOGY W 133

Figure 8.4: CGI components

(ii) Extension to Web Server


Many servers provide Application Programming Interface (API). API is
used to extend server function or to chance server properties. This gives the
method to create interface between server and back end application. The
efficiency of API application is depending to the technique of
programming. Two main API are Netscape API (NSAPI) and Microsoft
Internet Information Server API (ISAPI).

(iii) Scripting Language


Scripting language is used to extend server and browser with database
function. Scripting language creates the function that being inserted into
HTML. Among of the most popular script language examples are Javascript
and VBscript.

ACTIVITY 8.3

1. Using an appropriate example, discuss the security problem that


occurs in web environment. What are the modes to avoid those
problems from happen?

2. Compare the advantages and disadvantages CGI approach and


Extended Web Server as approaches to integrating database into
the web environment.

Copyright Open University Malaysia (OUM)


134 X TOPIC 8 DATABASE AND WEB TECHNOLOGY

x The Internet is a computer network which is inter-connected around the


world. World Wide Web is the system that provide user to find information
on the Internet. All information on the web is stored in Hypertext Markup
Language (HTML). Web browser exchanges the information with web server
using the Hypertext Transfer Protocol (HTTP).

x The two-tier architecture or client server is used by various business


organizations to organize their business. Nowadays, the two-tier architecture
had being replaced with three-tier architecture to solve the scalability
problem. Among the advantages of Web-DBMS are simplicity, platform
independence, and many others. There are three approaches to integrating
Web-DBMS such us Common Gateway Interface (CGI), Extension to Web
Server and the usage of Scripting Languages.

HTTP WEB-DBMS
URL

Copyright Open University Malaysia (OUM)


TUTORIAL QUESTIONS W 135

TUTORIAL QUESTIONS

TUTORIAL QUESTION 1
X INTRODUCTION
The main purpose of this tutorial is to enable you to implement the knowledge
you have gathered in this course. You need to comprehend all commands in the
SQL query language to assist you in attempting the following tutorial.

X PROBLEM
Kita Clinic has a database system capable of storing all related information on
daily business transactions. The following is part of the database schema data of
Kita Clinic that shows relations among doctors, patients and treatment. Assume
that a patient does not wish to meet the same doctor more than once a day. The
data is written as day, month and year with monthly salary.

DOCTOR
StaffNo StaffName Expertise Salary
D100 Nora Gynaecologist 10,000.00
D200 Raju Otorhinolaryngologist 12,000.00
D300 Ai Ling Gynaecologist 9,000.00
D400 Ridzuan Ali Sergeon 8,000.00

PATIENT
PatientNo PatientName Town Age
P100 Ismail Kajang 35
P200 Mee Lee Bangi 40
P300 Nora Serdang 29
P400 Kumari Bangi 25
P500 David Semenyih 50

Copyright Open University Malaysia (OUM)


136 X TUTORIAL QUESTIONS

TREATMENT
StaffNo PatientNo Date Treatment Type Payment
D100 P200 151299 Tumor 5,000.00
D300 P400 020100 Give birth 2,000.00
D400 P200 010101 O&G Surgery 12,000.00
D200 P100 311299 Cornea 4,000.00
D100 P300 311299 Give birth 2,000.00
D400 P500 010200 Nose Surgery 10,000.00

X TASK
Give an equal SQL statement for the following queries:

1. List the names and expertise of all the doctors at Kita Clinic.

2. List patient names and the doctors who treated patients that visited Kita
Clinic in the month of November 2001.

3. Based on question (2) list the names of patients that requested for treatment
in last November (list of patients from the month end to the start of the
month) and those who received treatment on the same date, listed in
alphabetical order.

4. For each expertise specification with more than two doctors, list the number
and names of doctors in it.

5. List the doctors names and payments received based on the treatment
given. All the doctors names with the most payments are to be placed at
the top of the list.

6. With the SQL statement below, state the output product based on values in
the table.

SELECT StafNo, Name


FROM Doctor D
WHERE EXISTS
(SELECT *
FROM Treatment T
WHERE D.StafNo = T.StafNo
AND Payment >=10,000)

Copyright Open University Malaysia (OUM)


TUTORIAL QUESTIONS W 137

7. SELECT PatientNo, Name


FROM Patient P
WHERE (SELECT COUNT (*)
FROM Treatment T
WHERE T. PatientNo = P.PatientNo) >=2;

TUTORIAL QUESTION 2
X INTRODUCTION
The main purpose of this tutorial is to enable you to implement the knowledge
you have gathered in this course. You must comprehend all commands in the
SQL query language to assist you in attempting the following tutorial.

X PROBLEM STATEMENT
Sri Angsana Hotel has subsidiaries within the city. This hotel contains all
information related to daily business transactions. The following is part of the
database schema for the hotel such as HOTEL, ROOM, RESERVATIONS and
CUSTOMER relations.

HOTEL (Hotel_No, Name, Address)


Room (Room_No, Hotel_No, Type, Price)
Reservations (Hotel_No, Customer_No, Input_Date, Output_date, Room_No
Customer (Customer_No, Name, Address)

X TASK
List out the SQL statement that corresponds to these tasks:

(a) List all information on the Hotel table


(b) List the complete information of all hotels in Kuala Lumpur.
(c) List the names and addresses of all customers in Kuala Lumpur
accordingly.
(d) List all reservations where the output_date is stated.
(e) Count the number of hotels.
(f) Count the average price and types of rooms in Bintang Hotel.
(g) List all customers currently staying in Bintang Hotel.

Copyright Open University Malaysia (OUM)


138 X TUTORIAL QUESTIONS

(h) List all rooms in each hotel in Johor Bahru.


(i) One new hotel, Bangi Indah has opened in Bandar Baru Bangi. The hotel
number is H234.

TUTORIAL QUESTION 3
X INTRODUCTION
This tutorial will focus on Database Development. You will use knowledge that
you have leant and also from others Microsoft Accesss materials either reference
book or reference Web.

X PROBLEM
Puan Saadah doing a business that selling snack likes kerepek and traditional
cake. Among type of kerepek sold are pisang, pisang manis, ubi pedas, ubi
masin, keledek and sukun. While tradisional cake such us tempeyek, jejari ikan
bilis and maruku. Each type of kerepek and cake sold with difference price and
minimum quantity is 0.5 kg. Selling price listed has been increased 40 percent
from original capital. The list of the price as below:

Items Selling Price RM / 1 kilo


Kerepek Pisang 7.00
Kerepek Pisang Manis 7.00
Kerepek Ubi Pedas 14.00
Kerepek Ubi Masin 7.00
Kerepek Keledek 14.00
Kerepek Sukun 21.00
Tempeyek 21.00
Jejari Ikan Bilis 14.00
Maruku 7.00

Copyright Open University Malaysia (OUM)


TUTORIAL QUESTIONS W 139

Usually, individual customers will buy direct from her house, but there are
regular customers would book early, especially for customers whose request to
buy in bulk quantity (five kilo and above for each type of kerepek and cake).

All regular customer should register and pay deposit as many as RM50. Deposit
will be returned after the customer unsubscribe with Puan Saadah.

Puan Saadah get raw material like ubi kayu, pisang tanduk, keledek and sukun
from supplier. While materials as flour, salt, sugar, dry chilli, cooking oil,
vicissitudes of life, anchovies and packer plastic from others supplier. The prices
of raw material depend to current price.

Puan Saadah employs three workers which one of them for packaging task and
others two workers are for preparation of kerepek and cake.

Working hours for all workers start from 8oclock morning until 5 afternoon. Rest
hour is from 12oclock mid afternoon until 2 afternoon. Salary is calculated
according to time with rate RM 5.00 per-hour.

Currently, Puan Saadah handles the processing of reservation and sale as


manually. To support the demanding of selling and reservation that is increasing,
especially during festive season, she requests you to develop a database system
that can used to replace manually work.

X TASK
Based on knowledge which you has been study from this module, develop the
reservation and sale system to Puan Saadah. Among reports will be generated by
the system are:

1. New bookings which have been received from regular customer daily.
2. Total profits for each type of kerepek and cake daily.
3. Total of salary that should be pay monthly.
4. Amount paid of raw material to supplier.

Copyright Open University Malaysia (OUM)


140 X TUTORIAL QUESTIONS

TUTORIAL QUESTION 4
X INTRODUCTION
You are required to use knowledge you have gathered in this course and others
information related to database and web. You also could gain latest information
from computer or internet magazine.

X PROBLEM
You are System Analyst in property company, Teratak Indah Sdn Bhd with
headquarters in Bandar Baru Bangi, Selangor. Teratak Indah managing sales of
property main towns like Kuala Lumpur and Johor Bahru and also have several
branches within Peninsular Malaysia. Among property project has being
operated by the company are Bukit Putra and Seri Mahkota at Nilai, Negeri
Sembilan.

Managing Director of Teratak Indah, Encik Imran intends to extend company


branch to Sabah and a Sarawak. Since established 15 years ago, Teratak Indah
had started with the file-based system and currently have used database since
several years ago. The Database system software had being used before is
Microsoft Access and now company had emigrated to DB2 from IBM to meet
need improvement of requirement.

X TASK
Lately, Encik Imran has read many newspaper report of e-commerce activity and
he thinks that to be success in future, Teratak Indahs company must deep
participate into e-commerce area. Among the aspects he wants to implement is
the ability to access companys database via internet. According to his opinion,
this would facilitate the acquisition and updating data by each employee within
Malaysia. Furthermore, it also is the way to improve value of services for existing
customer and attract new customer.

He asked you to study and preparing a report of feasibility to enable accessing


data of Teratak Indah from the Internet. You are required to study of technical
issue, technical solution, advantage and disadvantages of this proposal. Apart
from that, you should also think about the problems that may occur. The report
need to be ended with that conclusion of overall proposal to Teratak Indah.

Copyright Open University Malaysia (OUM)


MODULE FEEDBACK
MAKLUM BALAS MODUL

If you have any comment or feedback, you are welcome to:

1. E-mail your comment or feedback to modulefeedback@oum.edu.my

OR

2. Fill in the Print Module online evaluation form available on myINSPIRE.

Thank you.

Centre for Instructional Design and Technology


(Pusat Reka Bentuk Pengajaran dan Teknologi )
Tel No.: 03-27732578
Fax No.: 03-26978702

Copyright Open University Malaysia (OUM)


Copyright Open University Malaysia (OUM)

Das könnte Ihnen auch gefallen