You are on page 1of 4

Chapter 2 The Relational Model 1: Introduction, QBE (Query-by-Example), and

Relational Algebra
Relational Databases a collection of tables
# why this type of database is not called a table database or something similar, if
a database is nothing more than a collection of tables? Formally, these tables are
called relations, and this is where this type of database gets its name.
How a relational database handle entities, attributes and relationships?
Entity- stored in its own table
Attributes the fields or columns in the table.
Relationships through common columns in two or more tables
# Each column in a table must have a unique name, and all entries in each column
should be consistent with this column name
A relation is a two-dimensional table in which:
1. The entries in the table are single-valued; that is, each location in the table
contains a single value.
2. Each column has a distinct name ( technically called the attribute name)
3. All values in a column are values of the same attribute ( that is, all entries
must match the column name)
4. The order of columns is immaterial
5. Each row is distinct
6. The order of rows is immaterial
Relational database a collection of relations
Unnormalized relation a structure that satisfies all the properties of a relation
except for the first item, that is, some of the entries contain repeating groups and,
thus, are not single-valued.
Note: Rows in a table (relation) are often called records or tuples. Columns in a table
(relation) are often called fields or attributes
The primary key of a table (relation) is the column or collection of columns that
uniquely identifies a given row in that table.
The primary key provides an important way of distinguishing one row in a table from
another. Primary keys are usually represented by underlining the column or
collection of columns that comprises the primary key for each table in the database.
Query-by-Example (QBE)
Query is simply a question represented in a way that the DBMS can recognize and
process
QBE an approach to writing queries that is very visual. With QBE, users ask their
questions by entering column names and other criteria using an on-screen grid, and
data appears on the screen in tabular form.

SIMPLE QUERY
In Access, you create queries using the Query window, which has two panes. The
upper portion of the window contains a field list for each table you want to query.
The lower pane contains the design grid, the area in which you specify the format
of your output, the fields to be included in the query results, a sort order for the
query results, and any criteria the records you are looking for must satisfy.
# Clicking the run button in the results group on the query tools design tab runs the
query and displays the query results
# To display all fields and all rows in the Order table, you could add each field to the
design grid. There is a shortcut, however, in access, you can add all fields from a
table to the design grid by double-clicking the asterisk in the tables field list.
SIMPLE CRITERIA
When the records that you want to display in a querys result must satisfy a
condition, you enter the condition in the appropriate column in the design grid,
Conditions that data must satisfy are also called criteria. (a single condition is called
criterion)
# To enter a criterion for a field, include the field in the design grid, and then enter
the criterion in the row labelled Criteria for that field.
When you enter a criterion for a text field, such as CustomerNum, Access
automatically adds double quotation marks around the value when you run the
query or move the insertion point to another box in the design grid. Typing the
quotation marks is optional.
#Why is the CustomerNum field a Text field? Doesnt it contain numbers?
Fields such as the CustomerNum field that contain numbers, but are not involved in
calculations, are usually assigned the Text data type.
If you want something other than an exact match, you must enter the appropriate
comparison operator, also called a relational operator. The comparison operators
are = (equal to), > (greater than), < (less than), >= (greater than or equal to), <=
(less than or equal to), and NOT (not equal to)
Note: It is common in QBE to omit the = symbol in equal to comparisons, although
you can use it every time.
COMPOUND CRITERIA
You can also combine criteria to create compound criteria, or compound conditions.
In many query languages, you create compound criteria by including the word AND
or OR between the separate criteria. In an AND criterion, both criteria must be true

for the compound criterion to be true. In an OR criterion, the overall criterion is true
if either of the individual criteria is true.
In QBE, to create an AND criterion, place the criteria for multiple fields on the same
criteria row in the design grid; to create n OR criterion, place the criteria for multiple
fields on different Criteria rows in the design grid.
To indicate the either of two conditions must be true to select record, place the first
criterion in the criteria row for the first column and place the second criterion in the
row labelled or
To place two criteria in the same field, separate the criteria with the AND operator to
create an AND condition
COMPUTED FIELDS
A computed field or calculated field is a field that is the result of a calculation using
one or more existing fields.
Available credit is computed by subtracting the balance from the credit limit.
Because there is no available credit field in the customer table, you must calculate
it from the existing Balance and CreditLimit fields. To include a computed field in
query, you enter a name for the computed field, followed by a colon, and then
followed by an expression in one of the columns in the Field row.
To calculate available credit, you can enter the expression
AvailableCredit:CreditLimit-Balance in the desried field row in the design grid. When
entering an expression in the design grid, the default column size prevents you from
being able to see the complete expression. AN alternative method is to right-click
the column in the field row to display the shortcut menu, and then click zoom to
open the Zoom dialog box.
Note:
When a field name contains spaces or SQL reserved words, you must enclose the
field name in square brackets ([]). For example, if the field name were Credit Limit
instead of CreditLimit, you would enter the expression as [Credit Limit]-Balance. You
can also enclose a field name that does not contain spaces in square brackets, but
you do not need to do so.
You are not restricted to subtraction in computations. You can also use addition(+),
multiplication (*), or division (/). You can include parentheses in your expressions to
indicate which computation Access should perform first.
FUNCTIONS
All products that support QBE, including access, support the following built-in
functions (called aggregate functions in Access): Count, Sum, Avg ( average), Max
(largest value), Min (smallest value), StDev (standard deviation), Var (variance),

First, and Last. To use any of these functions in a query, you include them in the
Total row for the desired column in the design grid. By default, the Total row does
not appear automatically in the design grid. To display it, you click the Totals button
in the Show/Hide group on the Query tools design tab
GROUPING
You can use functions in combination with grouping, where calculations affect
groups of records. For example, you might need to calculate the average balance
for all customers of each sales rep. Grouping simply means creating groups of
records that share some common characteristic.
SORTING
In most queries, the order in which records appear doesnt matter. In other queries,
however, the order in which records appear can be very important. You might want
to see customers listed alphabetically by customer name or listed by rep number.
Further, you might want to see customer records listed alphabetically by customer
name and grouped by sales rep number.
To list the records in query results in a particular way, you need to sort the records.
The field on which records are sorted is called the sort key; you can sort records
using more than one field when necessary. When you are sorting records by more
than one field (such as sorting by rep number and then by customer name(, the first
sort field (RepNum) is called the major sort key (also called the primary sort key)
and the second sort field ( customer name) is called the minor sort key ( also called
the secondary sort key)
SORTING ON MULTIPLE KEYS
You can specify more than one sort key in a query; in this case, the sort key on the
left in the design grid will be the major (primary) sort key and the sort key on the
right will be the minor (secondary) sort key