DCBA 1303-Introduction To Database Management Systems
DCBA 1303-Introduction To Database Management Systems
Contents
Module 1- Database Design .................................................................................................................... 2
1. What is a database? ......................................................................................................................... 2
2. Database Management Systems (DMBS) ....................................................................................... 3
3. Database Organizations .................................................................................................................. 3
3.1 3.1 The Relational Database ................................................................................................... 3
4. Database Design.............................................................................................................................. 5
4.1 Conceptual Database Design .................................................................................................. 5
4.2 Logical Database Design ...................................................................................................... 10
Module 2- Working with Microsoft Access ......................................................................................... 13
1. Getting Started .............................................................................................................................. 13
1.1.1 Creating a Blank Database ............................................................................................ 13
1.1.2 Access Objects .............................................................................................................. 14
1.1.3 Change Views ............................................................................................................... 14
2. Creating Microsoft Access Tables ................................................................................................ 15
2.1 Create a table using Design View ......................................................................................... 15
2.2 Working with Relationship ................................................................................................... 18
2.3 Querying the Database .......................................................................................................... 20
2.3.1 Creating a Query ........................................................................................................... 22
2.3.2 Querying based on Criteria ........................................................................................... 27
2.3.3 Calculation Query ......................................................................................................... 31
2.3.4 Creating a Parameter Query .......................................................................................... 33
2.4 Forms .................................................................................................................................... 35
2.4.1 Creating Forms .............................................................................................................. 35
2.4.2 Modifying a Form ......................................................................................................... 39
2.4.3 Creating a command button in Forms ........................................................................... 40
2.4.4 Using controls in a form................................................................................................ 43
2.5 Reports .................................................................................................................................. 43
2.5.1 Use the Report Button ................................................................................................... 43
2.5.2 Create a report by using the Wizard.............................................................................. 44
Appendix (A) – Structured Query Language ........................................................................................ 46
1
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
1. What is a database?
We define a database as an organized collection of logically related data. A database may be of any
size and complexity.
For example, a salesperson may maintain a small database of customer contacts on her laptop
computer that consists of a few megabytes of data. A large corporation may build a very large
database consisting of several terabytes of data on a large mainframe computer that is used for
decision support applications. Very large data warehouses contain more than a peta byte of data (a
petabyte is a quadrillion bytes).
By the term organized we mean that the data are structured so as to be easily stored, manipulated, and
retrieved by users. By the term related, we mean that the data describe a domain of interest to a group
of users and that the users can use the data to answer questions concerning that domain. For example,
a database for an automobile repair shop contains data identifying customers (customer's name,
address, work phone number, home phone number, and preferred credit card number), automobiles
belonging to those customers (make, model, and year), and repair histories for each of those
automobiles (date of service, name of person who worked on vehicle, type of repair performed, and
rupee amount of work performed).
For both individuals and organizations, there are many advantages of having databases:
• Sharing: In organizations, information from one department can be readily shared with
others.
• Security: Users are given passwords or access only to the kind of information they need to
know. Thus, the payroll department may have access to employees' pay rates, but other
departments would not.
• Fewer files: With several departments having access to one file, there are fewer files. Excess
storage, or what is called "data redundancy," is reduced.
• Data integrity: Older filing systems many times did not have "integrity." That is, a change
made in the file in one department might not be made in the file in another department. As
you might expect, this can cause serious problems and conflicts when data is used for
important decisions affecting both departments.
(O’leary, T.J. & O’leary, L.I., 1997)
2
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
A DBMS program helps organizations use their integrated collections of data records and files known
as databases. It allows different user application programs to easily access the same database. For
example, a DBMS makes it easy for an employee database to be accessed by payroll, employee
benefits, and other human resource programs. A DBMS also simplifies the process of retrieving
information from databases in the form of displays and reports. Instead of having to write computer
programs to extract information, end users can ask simple questions in a query language such as
Structured Query Language (SQL). Thus, many DBMS packages provide fourth-generation languages
(4GLs) and other application development features. Examples of popular mainframe and midrange
DBMS packages are DB2 by IBM and Oracle 9i by Oracle Corporation.
3. Database Organizations
The purpose of a database is to integrate individual items of data-that is, to transform isolated facts
into useful information. We saw that files can be organized in various ways (sequentially, for
example) to best suit their use. Similarly, databases can also be organized in different ways to best fit
their use. Although other arrangements have been tried, the most common formats are hierarchical,
network, relational, and object-oriented. Since the relational database is the one that firmly established
in the database application world, and there are many commercial relational DBMS packages, we will
discuss the relational databases in detail in the following section.
The most flexible type of organization is the relational database. In this structure, there are no access
paths down a hierarchy to an item of data. Rather, the data elements are stored in different tables, each
of which consists of rows and columns. A table is called a relation.
An example of a relational database is shown in our illustration (See Figure 1). The book table
contains the reference number, title, author number, and price for all books in a particular library.
Within the table, a row resembles a record for example, information about one book. A column entry
resembles a field. The title of a book is one field; the author number is another field. All tables must
have a field to identify each record in the table uniquely (primary key field). So, the reference number
is a key field of the book table. Another might be the member number of the member table.
Information stored on one table can be linked with information stored on another by using the primary
key of a one table as a field in another table (Foreign key). Therefore a librarian who sees an overdue
book (in the circulation table) can recall it using the borrower information (from the member table)
and the book information (from the book table). He can locate this information from these two tables
easily using the reference number and the borrower number of the corresponding record of the
circulation table.
The most valuable feature of relational databases is their simplicity. Entries can be easily added,
deleted, and modified. The hierarchy and network databases are more rigid. The relational
organization is common for microcomputer DBMSs, such as Access, Paradox, dBASE, and R: Base.
3
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Relational databases are also becoming very popular for mainframe- and minicomputer based
systems.
Circulation Table
.. . . . .
. . . . .
4
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
4. Database Design
Database designing is the process of designing the database. It has three main phases
The more carefully you design, the better the physical database meets users' needs. In the process of
designing a complete system, you must consider user needs from a variety of viewpoints.
Case Study
Suppose you have to design a database for 1st year students of Management Faculty of the
University. The faculty consists of several departments that offer courses for registered
students. One department can offer more than one course. Faculty also records details
about lecturers who belong to departments. A lecturer is assigned to one or more courses.
A Student can follow one or many Courses. The grades obtained by each student for each
course should also be recorded in the database.
5
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Make a list of all possible entities. An entity can be physical objects such as Students, lecturers,
lecture halls in a university system or conceptual objects such as degree programs, courses.
An entity is a thing that holds information important for the database. Entities can be physical or
conceptual. Entities may be add or remove in the next steps to maintain a good design.
Department Student
Course Lecturer
Step 2: List the attributes about the entities
There is a great deal of information associated with every entity. In this step, you should list the
attributes about an entity and then eliminate the attributes that are not important to the solution of the
problem.
The Student entity, for example, can have many attributes associated with it: Registration number,
First name, Last name, Date of birth etc. In this case, it is not important to keep information about the
height, weight or blood group of the student. The Faculty needs only the information it will use now
and possibly in the future.
Other than above attributes, they need to store data about student grades, lecture times and venue.
To determine the relationship among the entities, take each entity and look at how that entity may be
related to another. Keep in mind that not every relationship existing between entities is important. The
relationships that are important are those that allow you to model the database in the real-world
situation that the database represents.
A simple approach to do this is using a relationship matrix. This is a table that has rows and columns
for each of the identified entities. With four entities there are four rows and four columns. Each cell is
used to indicate whether or not that combination of entities has an association.
6
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Your completed relationship matrix now contains a list of all the entities and all the relationships
between those entities. This is enough information to create a rough ERD.
Types of Relationships
One-to-one relationships. For any given row in Table A, there is only one row in Table B. For any
given row in Table B, there is only one row in Table A. There are no one-to-one relationships in the
Faculty database. An example of a one-to-one relationship is that of employee data and private
employee data. General information, such as employee name, address, and start date, is kept in one
table, and to ensure privacy, personal information, such as salary, is kept in another table.
One-to-many relationships. For any given row in Table A, there are many rows in Table B. For any
given row in Table B, there is only one row in Table A. The relationship between Department and
Course is one-to-many, because one department may offer more than one course, but a Course is
related to only one Department.
Many-to-many relationships. For any given row in Table A, there are many rows in Table B. For
any given row in Table B, there are many rows in Table A. There is a many-to-many relationship
between the Lecturer table and the Course table. A Lecturer can be assigned to different Courses and
a Course can be conducted by many different Lectures.
7
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
In order to identify the relationships between entities, first identify the number of occurrences of one
entity for a single occurrence of the related entity.
Department
Each lecturer belongs to one department
Each course offer by one department
Courses
Each Department offer one or more courses
Each student takes one or more courses
Each lecturer conduct one or more courses
Lecturer
Each department has one or more lecturers
Each course teach by one or more lecturers
Student
Each course has one or more students
8
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Primary Key column is a column that uniquely identifies each row in a table. A primary key can be an
account number, social security number, part number, license number, or any other numeric value or
combination of characters that are unique. A composite key is one that is derived from more than one
column.
No other row in the table can have the value of the primary key column(s). If a company name is
universally unique, it is used as a unique row identifier. However, if there is any possibility another
company could have the same name, then it is not unique and must not be employed as a primary key
column. Do not use any column as a primary key where the possibility exists for a duplicate. A
primary key column cannot contain null values.
Department Student
Primary Key is DeptID Primary Key is RegNo
FIRSTNAME may not be
unique
Course Lecturer
Primary Key is Primary Key is LecID
CourseID
9
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Department
PK- DeptID
Offers
Course Student
Has Takes
PK- CourseCode PK- RegNo
Teaches
Lecturer
Pk- LecID
1. Remove features not compatible with the relational model (optional step)
2. Determine the Links
3. Final Logical design.
Through this section we are going to learn only remove many-to-many binary relationship types.
10
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
The many-to-many relationship causes problems when attempting to retrieve data and when relating a
value in one table to its corresponding value in the other table. It is important to understand this
relationship to be able to recognize and control this situation when it arises.
According to above case study a many-to-many relationship exists between Lecturer and Course. A
Lecturer can conduct many courses and a course can be conducted by many lecturers.
But which key will we use for a link? If LecID is placed in the Course table, then all of the Course
data would have to be repeated for each Lecturer that contains that Course. If CourseID is placed in
the Lecturer table, then the Lecturer information has to be repeated for each Course contained in the
Lecturer. This leads to redundant data, and the potential for invalid data is increased. Performance
may suffer.
The solution to many-to-many relationships is to create an associate table. This table should contain
the key columns from both tables. This is illustrated in the following diagram.
If you have been careful about designating primary key columns, you also have determined
the linking columns. Links provide a way to tie information (rows) in one table to another
table. Tables are linked together through their primary key columns. When a Primary key of
one entity is inserted as a column in the other related entity that column is referred to as
Foreign key.
To determine the placement of the links, you must first know the type of relationship among
the entities or tables. Once you know the type of relationship among tables, it is much easier
to determine where to place the linking column to tie two tables together.
11
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
12
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Modulee 2- Work
king with Microsoft
M A
Access
1. Geetting Starrted
When youu start Accesss, the Gettingg Started with Microsoft Offfice Access screen
s appearrs. You can use
this screenn to do follow
wing things.
• C
Create a blank database
• C
Create a databaase using a teemplate
• O
Open an existing database
1.1.1 C
Creating a Bla
ank Databasse
A blank database
d is a database withh nothing in it. You can create a dataabase by building your ow
wn
tables, forrms, reports, and
a other dataabase objects. To create a blank databasse;
Clickk Create.
3. Beginn typing to add data, to the default table (Tablee 1). If you do not wannt to enter
inform
mation in Tab
ble1 at this tim
me, click Close.
1
13
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
When you create a database, Access offers you Tables, Queries, Forms, Reports, Macros, and
Modules.
Objects
Tables In Access, data is stored in tables. A table is a set of columns and rows, with
each column referred to as a field. Each value in a field represents a single
type of data. Each row of a table is referred to as a record.
Queries You use queries to retrieve specific data from your database and to answer
questions about your data. For example, you can use a query to find the
names of the employees in your database who live in a particular state.
Forms Forms give you the ability to choose the format and arrangement of fields.
You can use a form to enter, edit, and display data.
Reports Reports organize or summarize your data so you can print it or view it
onscreen. You often use reports when you want to analyze your data or
present your data to others.
Macros Macros give you the ability to automate tasks. You can use a macro to add
functionality to a form, report, or control.
Modules Like macros, modules give you the ability to automate tasks and add
functionality to a form, report, or control. Macros are created by choosing
from a list of macro actions, whereas modules are written in Visual Basic for
Applications.
A view is a way of looking at an object. For example, in Access, data is stored in tables. Two of the
possible ways you can view a table are Datasheet view and Design view. You can see the data
14
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
contained in a table in Datasheet view. You can see the design of a table in Design view. You can use
the View button on the Home tab to change views, or you can click the proper button in the lower-
right corner of the window.
Tables are the foundation of an Access database. Access stores data in tables.
You can add new tables to an existing database by using the options in the Tables group on the
Create tab.
In Design view, you first create the new table's structure. You then switch to Datasheet view to enter
data.
You can use the Data Type property to specify the type of data stored in a table field. Each field can
store data consisting of only a single data type.
15
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Field Properties
In addition to selecting a data type, you can set the following options for each filed in Design view.
Item Description
Field Size Allow you to set the size of the field (i.e. limit text entries to 10
characters).
Format Format in which to display the values for the field to the users. (i.e. show a
postal code with the space in the middle).
16
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Decimal Set the number of decimal places to show for a numeric field.
Places
Input Mask Restrict the data that the user can enter. Used to ensure correct data entry.
(i.e. for a postal code, you want to make sure that the first character entered
is a letter and not a number.
Caption If you want a different name to appear for the column header in the
datasheet view or the label in form view. (i.e. originally call field
"EmpNo" but you want the labels to say "Employee Number"
Default A value to be entered in to the table if the user does not enter one.
Value
Validation Can be used to validate the information the user enters. (i.e. make sure the
Rule value is always less than 100)
Validation Allow you to set the message that appears if the validation rule fails. If
Text nothing is entered for this value, Access will display a default message.
Required If turned on, the user must enter something in this field in order to save the
record.
In a relational database, tables are connected to each another through unique field. Unique field is a
field with values that occur only once in a table. For an example, National Identity card number
uniquely identifies a person. A field or a combination of fields that uniquely identify each record in a
table is called a Primary Key. In some table it takes more than one field to create a primary key and
that is called as Composite Key.
1. In the table design grid, select the field or fields that you want to use as the primary key.
2. On the Design tab, in the Tools group, click Primary Key.
A key indicator appears to the left of the field or fields that you specify as the primary key.
17
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Primary Keey
Create Relationships
R s
Steps:
1. On th
he Database Tools
T tab, in the Show/Hid
de group, clicck Relationsh
hips.
2. Add tables
t or quarrries from thee Show Tablee dialog box.
3. Drag a field (typiccally the prim
mary key) from
m one table too the commonn field (the fo
oreign key)
in thee other table.. To drag mu
ultiple fields, press the CT
TRL key, clicck each fieldd, and then
drag them.
t
The Edit
E Relation
nships dialog box appears.
1
18
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
4. Verify that the field names shown are the common fields for the relationship. If a field name
is incorrect, click the field name and select a new field from the list.
To enforce referential integrity for this relationship, select the Enforce Referential Integrity
check box.
Click Create.
Referential Integrity ensures that records in related tables have related values in the primary table.
Referential integrity prevents users from accidentally deleting or changing records in a primary table
when records in a related table depend on them, making sure that there are no orphaned records in the
related tables, such as employees without departments.
To change present Referential Integrity in a relationship open the relationship window and right click
the join line and click edit relationship. In the Edit Relationship dialog box provide options to change
present Referential Integrity.
19
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Cascade Update
When cascade update related fields enabled, changing the value of the primary key in the primary
table automatically updates that value in the foreign key of the related table’s matching record.
Cascade Delete
When cascade delete related records is enabled, deleting a record in the primary table delete nay
related records in the elated tables. Cascade update and delete make your database more efficient by
ensuring that related records always match their primary records.
Queries are the primary mechanism for retrieving information from a database and consist of
questions presented to the database in a predefined format. Queries are often called by the user
through a form. They can be used to search for and grab data from one or more of the tables, perform
certain actions on the database and even carryout a variety of calculations depending on the needs.
Microsoft Access provides the user with several types of queries. They are explained below.
Select Query
The select query is the simplest type of query and because of that, it is also the most
commonly used one in Microsoft Access databases. It can be used to select and display data
from either one table or a series of them depending on what is needed.
Action Query
When the action query is called, the database undergoes a specific action depending on what
was specified in the query itself. This can include such things as creating new tables, deleting
rows from existing ones and updating records or creating entirely new ones.
Action queries are very popular in data management because they allow for many records to
be changed at one time instead of only single records like in a select query.
20
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
1. Append Query – takes the set results of a query and "appends" (or adds) them to an existing
table.
2. Delete Query – deletes all records in an underlying table from the set results of a query.
3. Make Table Query – as the name suggests, it creates a table based on the set results of a
query.
4. Update Query – allows for one or more field in your table to be updated.
Parameter Query
When you want a query to ask for input every time that you run it, you can create a parameter
query. In Microsoft Access, a parameter query works with other types of queries to get
whatever results you are looking for.
It is often chosen because it allows for a dialog box where the end user can enter whatever
parameter value they wish each time the query is run. The parameter query is just a modified
select query.
Aggregate Query
A special type of query is known as an aggregate query. It can work on other queries (such as
selection, action or parameter) just like the parameter query does, but instead of passing a
parameter to another query it totals up the items by selected groups.
It essentially creates a summation of any selected attribute in the table. This can be further
generated into statistical amounts such as averages and standard deviation, just to name a
couple.
• Sum • Count
• Avg • StDev
• Min • Var
• Max • Expression
MS Access provides a Graphical User Interface to create queries. This avoids the
writing of lengthy codes using Structured Query Language to create queries. Access
21
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
builds the SQL in the background, sends it to the database and returns the result in a
datasheet format.
To create a query, go to the ‘other’ group of the create ribbon. You can use the query wizard
or the query design options to create a query.
Step 1: Select Simple Query option from the dialog box that appears when you click on the
Query wizard option. Click OK to Continue.
22
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Step 2: Select the relevant table or the query from the Tables/Queries combo box. After that,
add the relevant fields from the available fields listbox to the selected fields listbox. You can
add fields from multiple related tables for a single query.
Step 3: Now you almost completed your query design. Type a meaningful title to identify the
query if you do not like the title given by default. Then click finish to end the wizard. The
query will be opened either in design view or in the datasheet view depending on the options
you set.
23
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Step 1: Select the relevant table(s) from the show table dialog box that appears once you click
on the ‘Design Query’ option
Step 2: Double click the fields one by one from the field list to add them to the query. You
can see the fields are entered to the “Field” section of the bottom pane of the query window.
24
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
If you like, instead of double clicking, you can add fields by selecting them from the ‘field’
section of each column of the bottom section of the query window.
If you want to include all the fields of a table, you can choose “*” symbol from the field list.
(Or else, you can add all the fields one by one).
25
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
The query will show the results in the datasheet view as follows.
Press save button if you want to save the query. Saved queries are shown in the queries
category of the navigation pane.
SQL View
You can see the SQL statement executed behind this query by selecting the SQL view from
the results group of the design ribbon.
26
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
When you want to limit the results of a query based on the values in a field, you should use
query criteria. The criteria should be specified in the criteria row of the relevant field of the
query.
Following picture shows the query designed to obtain the details of the novels from the Books
table.
Notes:
• If the data type of the field is text, the criteria that you typed for that field will be
automatically included with in double quotes.
• Do not use quote (‘’ or “”) when specifying criteria for fields based on number,
currency or Date/Time data types.
• =,>,<, <=, >=,< >, between, not between keywords can be used when mentioning the
criteria.
• A query can be created by specifying more than one criterion for the fields it
contains. For example, a query can be prepared to get the details of the all Novels
(Category Field) written by Martin Wickramasinghe.(AuthorName Field)
27
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Example:
Display Title of the book, Author Name and Number of copies of the books of whom the
number of copies exceeds 5.
Results:
Wildcards
Example:
Display the titles and the names of the authors of the books written by authors who has the
name “Alexander” in somewhere in his full name.
28
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
In the phrase “*Alexander*”, the * searches for zero or more characters before the word
Alexander and Zero or more characters after the word Alexander.
Result:
When specifying multiple conditions / criteria for a one attribute, conditional operators such
as AND, OR can be used. You also can specify different criteria for different attributes in a
single query.
OR Conditional Query
The OR condition allows us to select different sets of criteria for the same field.
Example: Display the titles of the books that starts with “G” or ends with “a”.
In the criteria cell of the title column, just type G* or *a. The keyword “Like” and the
quotation marks will be automatically inserted.
29
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Result:
The AND condition allows us to set the criteria for the query based on multiple fields.
Result:
30
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Note:
Example:
Display the book titles which start with letter “G” and end with letter “a”.
Result:
A calculation query allows us to perform arithmetic and string calculations on the field in the
query.
Example:
Following picture shows the attributes/ fields of a table called “Exam Marks”.
31
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Calculate the total marks obtained by each student by using a query. Display Student Exam
Number and the total marks only.
Steps:
1 Create a query using the design view and add the ExamNo as a field of the query.
2 Right click on the field cell of the adjacent column and select build.
3 In the expression builder, select the “Exam Marks” table from the tables. Now
you can see the fields of the table. Double click to select the field and use “+”
operator add fields together.
4 Type a suitable text (“Total Marks” in this example) just in front of first expression to
identify the calculated result. Use “:” to separate the text and the expression.
32
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Result:
Parameter query allows the user to specify input every time he runs it.
Example 1:
Display the title and the author of the books published by a publisher that the user
selects.
Steps
1. Create a select query, and then open the query in Design view.
2. In the Criteria row of a field for which you want a parameter applied, type the
text that you want the parameter dialog box to display, enclosed in square
brackets, for example:
When you run the parameter query, the prompt appears in a dialog box
without the square brackets.
33
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Example : Visidunu
Result:
Example 2:
Create a query to help user to retrieve the books written by an author. User only knows a part
of the author name.
For this purpose, the “like” statement and “*” wildcard should be used with the parameter.
Step 2: Type Like "*" & [Author Name] & "*" in the criteria cell of the AuthorName
column
Step 3: Run the query and type a part of the author name in the parameter prompt.
34
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Result:
Notes:
You can also use an expression with your parameter prompts, for example:
2.4 Forms
A form is a database object that you can use to enter, edit, or display data from a table
or a query. You can use forms to control access to specific rows or fields of data.
Controls can be added to a form to automate frequently performed actions.
You can use them to enter, edit, or display data. They are based on tables. When using
a form, you can choose the format, the arrangement, and which fields you want to
display. This lesson teaches you how to create forms.
Access can automatically create several types of forms. For example, when you click
the Form button on the Create tab, Access places all fields in the selected table on a
35
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
form. If the table has a one-to-many relationship with one other table or query, Access
creates a stacked form (the records are displayed in a column) for the primary table
and a datasheet for the related table. If there are several tables with a one-to-many
relationship, Access does not create the datasheet.
2. Click the table or query on which you want to base your form.
3
4
You can use the Navigation bars to move through the records on a form.
36
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
1 2 3 4 5 6 7
1. Go to First Record
2. Go to Previous Record
3. The Current Record
4. Total Number of records
5. Go to Next Record
6. Go to Last Record
7. Create a New (Blank) Record
Tip: After you create a form, you can save it. You can open a saved form at any
time.
A split form is a form in which the same data is displayed in two views
simultaneously. One part of the form displays in Form view (stacked fields), while the
other part displays in Datasheet view.
2. Click the table or query on which you want to base your form.
4. Click Split Form in the Forms group. Access creates a split form.
37
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
3
4
You can use the Multiple Items button on the Forms tab to create a form that displays
multiple records, one record per row.
2. Click the table or query on which you want to base your form.
4. Click Multiple Items in the Forms group. Access creates a multiple items form.
38
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
3
4
After you create a form, it opens in Layout view, where you can modify it.
1. Click a side of the field and drag to change the width of the field.
2. Click the top or bottom of a field and drag to change the height of a field.
To move a datasheet:
2. Click and drag the four-sided arrow in the upper-right corner to move the datasheet.
To resize a datasheet:
39
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
3. Click the top or bottom of the datasheet and drag to change the height.
To apply an AutoFormat:
The AutoFormat option on the Format tab enables you to apply formats quickly, such
as background colors, field colors, field label colors, and fonts.
When you create a form, by default, Access uses the form name as the title.
Steps:
A toolbox appears.
40
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
2. Click the Control Wizards button in the toolbox. In the top right hand corner of the toolbox,
this button resembles a wand and three stars.
4. Move to the form and click in the spot where you want the button to go.
5. Follow the directions in the wizard to choose the command associated with the button.
Click Finish.
Insert a command button and then it will display the following dialog box,
Select Form Operations from the Categories and select Close Form from Actions category.
41
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Then Select the Text or Picture to insert a text or a picture for the button face.
Finally give a name for the button and then save the form.
Open the form again in form view to operate the button. (See the following result)
42
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
2.5 Reports
Reports organize and summarize data for viewing online or for printing. You can
include summary data such as totals, counts, and percentages in a detail report. A
summary report does not list the selected records but instead summarizes the data and
presents totals, counts, percentages, or other summary data only. Access has several
report generation options that you can use to create reports.
The Report button creates a simple report that lists the records in the selected table or
query in a columnar format.
2. Click the table or query on which you want to base your report.
4. Click the Report button in the Reports group. Access creates your report and displays
your report in Layout view. Also you can modify the report later.
3
4
43
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
1. Click the table or quarry on which you want to base the report
5. Then select the required fields for the report and Select Next
44
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
6. If you need you can add grouping levels for the report (This is optional)
7. Then select Next and configure other necessary options that are shown in the forthcoming
windows, and click finish.
You can see the following Report under the Categories, such as Car, Van and Land
45
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
What is SQL?
Although SQL is an ANSI (American National Standards Institute) standard, there are many
different versions of the SQL language.
However, to be compliant with the ANSI standard, they all support at least the major
commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.
Note: Most of the SQL database programs also have their own proprietary extensions in
addition to the SQL standard!
SQL Statements
Most of the actions you need to perform on a database are done with SQL statements.
The following SQL statement will select all the records in the "Persons" table:
SELECT * FROM Persons
In this tutorial we will teach you all about the different SQL statements.
46
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Semicolon is the standard way to separate each SQL statement in database systems that allow
more than one SQL statement to be executed in the same call to the server.
We are using MS Access and SQL Server 2000 and we do not have to put a semicolon after
each SQL statement, but some database programs force you to use it.
SQL DML and DDL
SQL can be divided into two parts: The Data Manipulation Language (DML) and the Data
Definition Language (DDL).
The query and update commands form the DML part of SQL:
The DDL part of SQL permits database tables to be created or deleted. It also define indexes
(keys), specify links between tables, and impose constraints between tables. The most
important DDL statements in SQL are:
SELECT column_name(s)
FROM table_name
and
SELECT * FROM table_name
47
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Now we want to select the content of the columns named "LastName" and "FirstName"
from the table above.
LastName FirstName
Hansen Ruwan
Samaraweera Ravi
Perera Gamini
SELECT * Example
Now we want to select all the columns from the "Persons" table.
We use the following SELECT statement:
SELECT * FROM Persons
In a table, some of the columns may contain duplicate values. This is not a problem,
however, sometimes you will want to list only the different (distinct) values in a table.
The DISTINCT keyword can be used to return only distinct (different) values.
48
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Now we want to select only the distinct values from the column named "City" from the table
above.
We use the following SELECT statement:
City
Panadura
Kadawatha
The WHERE clause is used to extract only those records that fulfill a specified criterion.
Now we want to select only the persons living in the city "Panadura" from the table above.
49
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
This is wrong:
SELECT * FROM Persons WHERE FirstName=Ravi
This is correct:
SELECT * FROM Persons WHERE Year=1965
This is wrong:
SELECT * FROM Persons WHERE Year='1965'
Operators Allowed in the WHERE Clause
The AND operator displays a record if both the first condition and the second condition is
true.
50
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
The OR operator displays a record if either the first condition or the second condition is true.
Now we want to select only the persons with the first name equal to "Ravi" AND the last
name equal to "Samaraweera":
OR Operator Example
Now we want to select only the persons with the first name equal to "Ravi" OR the first name
equal to "Ruwan":
51
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
You can also combine AND and OR (use parenthesis to form complex expressions).
Now we want to select only the persons with the last name equal to "Samaraweera" AND the
first name equal to "Ravi" OR to "Ruwan":
ORDER BY Example
The "Persons" table:
Now we want to select all the persons from the table above, however, we want to sort the
persons by their last name.
52
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Now we want to select all the persons from the table above, however, we want to sort the
persons descending by their last name.
The first form doesn't specify the column names where the data will be inserted, only their
values:
The second form specifies both the column names and the values to be inserted:
53
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
The following SQL statement will add a new row, but only add data in the "P_Id",
"LastName" and the "FirstName" columns:
54
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Note: Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies which
record or records that should be updated. If you omit the WHERE clause, all records will be
updated!
Now we want to update the person "Thilakawardana, Janitha" in the "Persons" table.
UPDATE Persons
SET Address=’56, Maligawaththa', City='Panadura'
WHERE LastName='Thilakawardana' AND FirstName='Janitha'
55
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Be careful when updating records. If we had omitted the WHERE clause in the example
above, like this:
UPDATE Persons
SET Address='56, Maligawaththa', City='Panadura'
Note: Notice the WHERE clause in the DELETE syntax. The WHERE clause specifies which
record or records that should be deleted. If you omit the WHERE clause, all records will be
deleted!
Now we want to delete the person "Thilakawardana, Janitha" in the "Persons" table.
56
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
It is possible to delete all rows in a table without deleting the table. This means that the
table structure, attributes, and indexes will be intact:
Note: Be very careful when deleting records. You cannot undo this statement!
SQL aggregate functions return a single value, calculated from values in a column.
SQL scalar functions return a single value, based on the input value.
57
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Tip: The aggregate functions and the scalar functions will be explained in details in the next
chapters.
AVG() Function
OrderAverage
950
Now we want to find the customers that have an OrderPrice value higher than the average
OrderPrice value.
58
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Customer
Pathirana
Nawarathna
Jayaweera
Count()Function
The COUNT() function returns the number of rows that matches a specified criteria.
The COUNT(column_name) function returns the number of values (NULL values will not be
counted) of the specified column:
The COUNT(DISTINCT column_name) function returns the number of distinct values of the
specified column:
SELECT COUNT(DISTINCT column_name) FROM table_name
Note: COUNT(DISTINCT) works with ORACLE and Microsoft SQL Server, but not with
Microsoft Access.
59
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
The result of the SQL statement above will be 2, because the customer Nawarathna has
made 2 orders in total:
CustomerNawarathna
NumberOfOrders
6
Now we want to count the number of unique customers in the "Orders" table.
NumberOfCustomers
60
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
Notes:
61
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP
DBMS HNDA II ATI-Sammanthurai
62
MIM Riyath
MSc-Management (R-SJP), BBA-Finance, ACMA, HANDA, CAB, AMGARP