Professional Documents
Culture Documents
CBDB3203 Database Implementation PDF
CBDB3203 Database Implementation PDF
CBDB3203
Database Implementation
Rohaizak Omar
Open University Malaysia
Tutorial 135
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.
Study
Study Activities
Hours
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 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
special type of form which consists of several buttons that can be clicked to
execute a particular operation.
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.
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
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.
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.
Silberschatz, A., Korth, H., F & Sudarshan, S. (1997). Database system concept.
(3rd ed.). New Jersey: McGraw-Hill.
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.
This topic will explain what SQL is and the importance of learning the language
in building a current database application.
ACTIVITY 1.1
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.
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.
(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.
(iv) It is not dependent on capital and small alphabets but it must be typed as
per the content of the database; and
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
2. What are the two main components of SQL and state the function
of each component?
ACTIVITY 1.1
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
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.
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
(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.
(i) Upper case letters are used to represent reserved words and lower case
letters are used to represent users-defined words.
(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:
X operator Y
Operator Meaning
= equals to
< less than
> more than
<= less or equals to
>= more or equals to
<> not equal to
The following exercise will assist you in better understanding the topic you have
just learnt.
SELF-CHECK 1.2
Once you have learnt the basic SQL structure, let us rest our mind before
proceeding to the next section.
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.
You can also command a system to calculate arithmetic equations such as average
amount, plus, minus, multiply and divide.
ACTIVITY 1.3
ACTIVITY 1.4
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?
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.
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.
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?
Student
MatricNo StudentName Major
A1000 John Science Computer
A1001 Fatimah Multimedia
A1002 Ali Science Information
A1003 Muthu Networking
A1004 Ah Chong Multimedia
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
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.
ACTIVITY 2.2
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.
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.
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:
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.
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:
Look at the following example to assist you in understanding the above situation.
Example
DROP TABLE Student
Solution
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.
These activity will assist you in better comprehending the topics you have just
learned. This concludes Topic 2.
ACTIVITY 2.3
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.
x You have successfully completed this topic. The next topic will discuss on
data management.
LEARNING OUTCOMES
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.
The INSERT is used to add one row into an existing table through a format as
shown below:
where
(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.
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:
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.
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:
ACTIVITY 3.1
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.
Example 1:
Extend a salary increase of 5% to all workers.
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:
Because the Command WHERE is not stated, all lines in the COURSE table
will be deleted.
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.
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
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.
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:
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.
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:
Example:
List the matriculation number and student name from the Student list.
Solution:
SELECT MatricNo, StudentName
FROM Student
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
Example:
List all Students major.
Solution:
SELECT Major
FROM Student
The result:
Major
Science Computer
Multimedia
Science Information
Networking
Multimedia
DISTINCT is used in the SELECT statement if the output of SQL query contains
repeated and unwanted rows. DISTINCT will eliminate all repeated rows.
The result:
Major
Science Computer
Multimedia
Science Information
Networking
ACTIVITY 4.1
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
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
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:
The reserved word AS is a choice in one SQL statement. There are several reasons
why we name all tuples and relationships:
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:
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)
The rules with WHERE command becomes complex with the usage of
comparison tests. Brackets can be used to show the priority order.
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.
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.
SQL applies two special characters as corresponding design symbols which are
the percentage symbol (%) and character as (-). (Refer to Table 4.2).
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.
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.
ACTIVITY 4.3
1. Try to come up with a situation with the output and command LIKE '
A%', LIKE '%A%' and LIKE'.
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).
Solution:
SELECT TutorNo, TutorName, Post
FROM Tutor
WHERE TutorNo LIKE' S_ _ _ _ '
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.
ACTIVITY 4.4
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.
Example:
List the tutor salary in descending order
Solution:
SELECT TutorNo, TutorName, Post, Salary
FROM Tutor
ORDERBY DESC salary
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
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.
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.
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.
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
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
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
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.
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.
Example:
Count the number of students registered for each major.
Solution:
SELECT Major, COUNT (*) AS bilangan
FROM Student
GROUP BY Major
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
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
Result:
Major Sum_Student
Multimedia 2
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.
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
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
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.
Example:
List all students that have registered for the SK100 course
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
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.
Points to Note:
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.
Solution:
SELECT MatricNo, StudentName, Major
FROM Student
These statements are easy to apply by using the reserved word OR but it is
too long if there are too many majors chosen .
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:
Result:
MatricNo StudentName
A1004 Ah Chong
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.
ACTIVITY 4.8
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.
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:
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.
Example:
List tutors with a salary greater than each of the tutor with assist lecturer post.
Solution:
SELECT Tutor
FROM Tutor
Where Salary > ALL
(SELECT Salary
FROM Tutor
WHERE Post = 'Assist Lecturer')
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:
Result:
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
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'
Result:
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
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.
Example:
List all MM1100 course
Solution:
SELECT MatricNo
FROM EXISTS
WHERE (SELECT *
FROM Course K
WHERE D.CourseCode = K.CourseCode
AND K.CourseCode = 'MM100')
SELECT MatricNo
FROM RegisterD, CourseK
WHERE D.CourseCode = K.CourseCode
AND K.CourseCode = 'MM100'
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.
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
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
Example:
List students who have registered for the Java Course and Programming tool.
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
Example:
List students who have registered for the Java Course and but not for the
Authoring Course.
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.
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.
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.
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.
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.
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.
Let us focus on the creation of tables within the MentorMentoree database in the
following section.
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.
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.
Figure 5.5: The Field element for programme field of mentoree table in Design View
(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
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.
(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.
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.
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.
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
Note: For further explanation with example, student can use the help
function in Microsoft Access for each field elements that you have
leant.
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.
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.
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.
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
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.
The result of this query can be found in Figure 5.15 as shown below:
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.
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
Figure 5.19: The result of the query that counts the number of mentorees for each mentor
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
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:
ACTIVITY 5.3
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.
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.
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
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
Database
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 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.
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.
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.
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.
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.
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.
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.
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.
(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.
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.
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.
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
2. Create a report that lists NoID, Name, Address, City and Saving
for each customer.
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.
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.
The Dialogue Box of the Switchboard Manager displays a message that only
main switchboard exists on the database as shown in Figure 6.14.
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.
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.
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.
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.
Form
Report Design
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.
ACTIVITY 7.1
What can you comprehend from the word transaction? Is it the same
as transactions at the market or supermarket?
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.
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.
begin transaction
X:= X 50;
X:= Y + 50;
commit work;
end transaction
(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.
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.
(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.
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
ACTIVITY 7.2
Complete the following table.
Properties of Transaction Detail
1. Atomicity
2. Consistency
3. Isolation
4. Durability
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.
Once you have understood the concept of concurrency control, answer the
following question to ascertain your understanding.
ACTIVITY 7.3
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.
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.
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.
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:
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.
Table 7.5: The Usage of 2-Phase Locking to Solve the Lost Update Problem
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
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.
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.
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
ACTIVITY 7.6
SELF-CHECK 7.5
Concurrency control
Transaction
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.
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.
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
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.
Servers Client
Apache Netscape Navigator
Netscape Communication Server Microsoft Internet Explorer
Microsoft Information Server Opera
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
SELF-CHECK 8.2
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:
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.
<Protocol>://<hos>:[:<port>]pass_absolute [? Argument]
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.
(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
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.
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.
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.
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.
(a) Simplicity
Easy to apply HTML as a document formatting language for naive users as
well as application developers.
(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.
(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?
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.
(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.
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.
ACTIVITY 8.3
HTTP WEB-DBMS
URL
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
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.
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.
X TASK
List out the SQL statement that corresponds to these tasks:
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:
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.
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.
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.
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.
OR
Thank you.