Download SUNY Morrisville-Norwich Campus

Document related concepts

Microsoft Jet Database Engine wikipedia , lookup

Clusterpoint wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Relational model wikipedia , lookup

Object-relational impedance mismatch wikipedia , lookup

Database model wikipedia , lookup

Transcript
SUNY Morrisville-Norwich
Campus-Week 8
CITA 130
Advanced Computer Applications II
Spring 2005
Prof. Tom Smith
Objectives

Questions from Last Week

Review of Basic Database Functionality
Microsoft Access 2003
Tutorial 1 – Introduction To
Microsoft Access 2003
Define some key Access
terminology
Field – A single characteristic or attribute
of a person, place, object, event, or idea.
 Record – A set of related field values.
 Table – A collection of records that identify
a category of data, such as Customers,
Orders, or Inventory.

Illustration of fields, records
and a table
Relational database and keys
A relational database is a collection of
tables that are related to one another
based on a common field.
 A field, or a collection of fields, is
designated as the primary key.



The primary key uniquely identifies a record in
the table.
When the primary key of one table is
represented in a second table to form a
relationship, it is called a foreign key.
Relating tables using a common field
Use the task pane to open a database
Open an Access database table

To open a table you must first open a
database:




In the database window, select Tables on the
objects bar
In the list of tables, select the table you want
to open and click on the Open button
When a table is open it is in Datasheet view,
which shows the table's data as a collection of
rows and columns
Each row in the Datasheet view represents a
record in the table
A table in datasheet view
Learn how Access saves a database


The Save button in Access differs from the Save
button in other Windows programs.
When you press the Save button in Access, you
are saving the design of the Access objects and
NOT the data itself.


Access saves data as it is entered
For this reason, the location at which you are
storing your database must always be accessible
while working with a particular database.

If the database is located on a diskette, the diskette
must be in the diskette drive at all times while working
with this particular database
What is an Access query?
If you want to see just a portion of the
data in a table you can create a query.
 A query is a question you ask about the
data stored in a database table.
 Access responds by displaying the data
according to your question.


For example, if you ask to see all the
customers from New York, the response would
be to display only the records whose state field
matches with NY
Open an existing query
and create new queries
You can open an existing query by clicking
Queries on the Objects bar and then
selecting the query you want to open.
 You can also create your own queries by
clicking New on the Database window.
 To create a new query, you can use the
Simple Query Wizard, which will bring you
through the selections you want for your
query.

Sample Query Datasheet view
Selecting fields to include in a query


You can create a query that will display only
selected fields from a table instead of displaying
all fields.
In the Simple Query Wizard dialog box, select
which fields you want included in the query.





Move all the fields into the Selected Fields box
Move the fields one at a time
Remove fields out of the Selected Fields box by pressing
one of the remove buttons
If you wanted to select all the fields except one,
you can move them all to the selected fields list
and then remove the one field you don't want.
Once you have made your selections, press Next
to move to the next dialog box in the Wizard.
The Simple Query Wizard
Create an Access form
A form allows you to view your data one
record at a time.
 Forms are useful for maintaining, viewing,
and printing records in a database.
 You can create your own form in the
Forms Design window or you can use the
Forms Wizard to create a form.
 The easiest way to create a form is to use
the AutoForm Wizard.

Use the AutoForm Wizard
The AutoForm Wizard uses a table (or
query) you select as the basis to create a
form that displays all the fields of the
table (or query).
 Once you have created your form, you can
view the records one record at a time.
 The form has a navigation bar just like the
navigation bar you have already used in
the Table or Query Datasheet view.

Access Form View
Create, preview, and navigate a
report




You can create a report, which is a printed
version of your data, that is formatted
according to your specifications.
The data in the report can consist of data from
a single table or multiple tables.
Access has a Reports Wizard that allows you to
easily create a report.
The report can be based on a table or it can be
based on a query.

If the report is based on a query, it will contain the same
fields that were selected for the query
The Report Preview window
Backup and restore a database
You may want to create a backup of your
data so that if you lose or damage your
database, you can recover from the
backup.
 You can use a backup tool, such as the
Microsoft backup tool, or some other
backup program.
 To restore the data from the backup, you
need to use the same backup tool
according to the instructions associated
with that tool.

Compacting a database
It is a good idea to periodically compact
and repair a database to recover wasted
space created by adding, deleting, and
modifying records.
 Access has a Compact and Repair feature:




Open a database
Click on Tools and then Options
On the Options menu, select Compact on
Close. Selecting this option will cause your
database to be compacted and repaired every
time you close it
Setting the Compact on Close
option
Compacting reduces
database storage size
Convert an existing database
to Access 2003 format


It is possible to convert a database created in
Access 2000 or 2002 to an Access 2003
database.
To convert an Access 2000 or 2002 database to
Access 2003 format:




Make certain the database to be converted is closed
Click Tools on the menu bar, then point to Database
Utilities
Point to Convert database and choose the format to
convert to
Enter the name of the database to convert, and a name
for the converted database to be stored under
Microsoft Office Access
2003
Tutorial 2 – Creating And
Maintaining A Database
Choose a primary key
Choose a field or fields to serve as the
primary key for the table.
 A primary key must uniquely identify each
record in the table.
 Primary keys can consist of more than one
field.


Primary keys with more than one field are
called composite keys
Include a common field
When one table needs to be related to
another table, you must include a
common field.
 The common field will be the primary key
in one table.
 The common field is referred to as a
foreign key in the related table.
 The foreign key in a table can then be
used as a primary key to access the
record in the related table.

Setting field properties



When assigning a name to any object in Access,
carefully select a name that will indicate what
data is stored there.
Field properties include their data type, field
sizes, and an optional description of the field.
When selecting a field size, make sure the size is
big enough to hold the largest piece of data that
will be stored there.


Do not make the field larger then necessary because this
will result in wasted disk space
Make sure the data type you select for a field is
appropriate for the kind of data to be stored in
that field.
Access field types, slide 1
Access field types, slide 2
Define fields, and specify
a table's primary key

When you create a table, you name the fields and
define the properties for the fields.



The table structure is set up and modified in Design
View
In Design View, you will define each field that will be
included in the table
When all the fields have been defined, you will
then specify which field(s) will be the primary
key.

The primary key is not mandatory, but it is a good idea
to assign one
The database Design View window
Choosing a primary key
Click a row selector for the field you want
to use as the primary key, and then click the
Primary Key button on the toolbar.
After clicking the Primary Key
button, a key symbol appears in the
row selector to indicate the key field.
Add records to a table
using Datasheet View





Once the table has been created, you can then add records
to the table using Datasheet View.
As you enter records, they will be placed in the order in
which you enter them. However, when you close the table
and open it again, the records will be ordered according to
the primary key order.
The navigation bar at the bottom of the Datasheet view will
indicate how many records are in the table and what the
current record is (i.e., the record on which your cursor
currently rests).
You can move through the fields in the table by pressing
the Tab key.
Once your cursor is moved off a record, that record is
automatically saved on your disk.
Datasheet View with 2 records
added
Modify the structure of a table by
deleting, moving, and adding fields




The structure of a table can be modified after it
has been created.
To delete a field, enter Design View, right-click on
the field, and then click Delete Rows on the drop
down menu.
To move a field, (you also do this in Design View)
click on the field you want to move, and while
holding your mouse button down, move the field
to the desired location.
To add a field in Design View, right-click the field
where you want to insert the new field and then
click Insert Rows on the dropdown menu.
Moving a field in Design View
Adding a new field in Design View
1. Right-click the row selector for the
field you want to insert the new field
in front of--StartDate in this figure.
2. Select Insert Rows from the
shortcut menu, and a new, blank
row will be inserted before the
row you selected, as shown below.
3. Enter the
information for
the new field.
Change field properties

You can make additional modifications to the
structure of a table by changing the properties of
the table's fields.




For example, if you have a field that represents a
currency value but you do not want to display dollar
signs, you could change the format to a standard format
In the Design View, you will find a list of all formats
available to you. You can make a selection from the list
of options
Each data type has a separate set of options available in
the Field Properties portion of the Table Design Window
Field properties are changed in the Design View
window.
Changing field properties
in Design View
Copy records from another
Access database


If the data you want in your database already
exists in another Access database, you can copy
the records from that database into your
database.
You can use cut and paste to do this:




Open the database that contains the records you want to
copy
Select the records you want to copy and then press the
Copy button. This places the records onto the Windows
clipboard
Open the database to which you want to copy the
records and open the desired table to receive the copied
records
Place your cursor on the next available row in the table
and press the Paste button
Import a table from
another Access database
You can also import an entire table,
including its structure, into another
database.
 To import a database table:





Click the File menu
Point to Get External Data, and then Import
Locate and select the database and the table
you want to import
This differs from the cut and paste method
because you actually import the entire
table, its structure, and its data.
The Import Objects dialog box
Copy and import considerations

If you choose to copy records from one
table to another, it is very important that
you first determine that the two tables
have the exact same data structure.


If the two data structures differ in any way, the
copy will cause an error
When you import, you are importing an
entire table, including its data and its
structure.

This is a good way to include a table in your
database that was previously designed in a
different database
Delete and change records

Once records have been added to a table, they
can be deleted or modified:


To change or modify a record:



In Datasheet view, click the row selector for the record(s) you
want to delete and then click the Delete Record button on the
toolbar
Place your cursor on the record and then on the field you want
to change
You can then edit the field's value as you wish by typing in
new data
You can switch from navigation mode to editing
mode depending on what you want to do at the
time.


When you are editing a record, you are in editing mode,
Otherwise you are in navigation mode
Editing mode is indicated by a pencil symbol in the left margin
of the datasheet view
An alternate method
for deleting a record
Microsoft Office Access
2003
Tutorial 3 – Querying a
Database
Learn how to use the Query
window in Design view





The Query window in Design view allows you to
specify the results you want for a query.
In the query Design view, you can specify which
fields you want to be included in your query
results.
You can also control what records are displayed
in the query by specifying select query criteria.
Each column in the design grid represents a field
that will be used in the query.
You can run the query at anytime to view the
results according to the current specifications.
Add fields to a query in Design view


The design grid is used to specify the fields and
records you want to see.
Add fields to the design grid by double-clicking
the field in the field list or by dragging the field to
the design grid.



Remember that the results of a query provide a
temporary view of the data
The contents displayed in a query are a result of the
fields you select and the criteria you specify in the query
design grid
When you use the query design window, you use
Query By Example (QBE). When you use QBE,
you give Access an example of the information
you wish to see when you run your query.
The Query Design view window
Create, run, and save queries

From the fields list, either drag or double-click
fields that you want included in the query.




A query result differs from a table's datasheet view in
that only selected fields are displayed
If you move a field to the design grid and then
you want to remove it, you can click on the field's
column and then press the delete key.
The results of the query will be displayed in order
by the primary key of the table unless you
specify another sort order.
You can save the query and give it a name by
pressing the save button on the toolbar.
Selecting, displaying and sorting
fields
A sample query datasheet
Update data using a query




You can use the query datasheet to update data
in a table.
It is important to realize that the query datasheet
is a temporary view of the data. However, when
you update data by means of the query
datasheet, the updates are placed directly into
the underlying table.
You can only update fields that are represented in
the query.
To observe the actual changes made to the table,
close the query and open the table in datasheet
view.
Access is a relational database




A database is almost always a collection of
tables. Access is a relational database
management system that allows you to form
relationships between the tables.
When you form a relationship between tables,
you are joining the tables.
Tables are joined on common fields between the
tables.
When tables are joined, you can view data from
both tables as if the tables were one combined
table.
A one-to-many relationship


Tables can be joined in three ways; one-to-one,
one-to-many, and many-to-many.
A one-to-many relationship exists when one table
has many records associated with a given value
but the related table has only one record for that
value.



The table with one record is called the primary table
The table with many records is called the related table
Queries can be defined to use the relationship to
extract data from both tables in a single query.
Using referential integrity




When dealing with related tables, you need to
decide if you want to enforce referential integrity.
Referential integrity allows you to maintain the
integrity between related tables.
The rules associated with referential integrity
specify that when you add a record to a related
table, there must be a matching record in the
primary table.
If you choose to enforce referential integrity, you
can insure that you will not have orphaned
records (records that have no matching record in
the primary table).
Use cascaded updates
and cascaded deletes




In addition to referential integrity, you can also
tell Access to implement cascaded updates and
cascaded deletes.
If you choose cascaded updates, making a
change in a field that is common to two related
tables will cause the update to be made in both
tables.
Cascade deletes is similar. If you delete a field
that is common to two tables, the deletion will
take place in both tables.
You should carefully consider whether you want
to implement these features, as they can have
dramatic effects on your data.
Selecting the tables for a relationship
Setting relationship options
The Relationships window
Sort data in a query




You can control the order of data displayed in a
query by altering the queries sort key.
Choose a field on which you want the data to be
sorted and then specify whether you want the
data sorted in ascending or descending order.
Specify multiple fields as the sort key if you want
to arrange data in sorted order within groups of
data.
Specify sort criteria for the query in the query
design grid.
Sort results by data type
Sort single or multiple fields
Filter data in a query




Sometimes you may want to isolate a certain
portion of data in your query results.
You can set a filter, or a set of restrictions, to
specify what records you want to view in the
results. Only those records that match the
criteria will display in the results datasheet.
There are two methods used to make your
selections: Filter By Selection or Filter By Form.
If you no longer want to view the data with the
filter applied you can remove the filter.
Using Filter By Selection
Specify an exact match
condition in a query




You can specify an exact match condition, which
allows you to specify that only records that match
your criteria will be displayed.
To specify an exact match, use the criteria row in
your query design grid.
When you run the query after specifying an exact
match, only those records that meet the criteria
will be displayed.
An exact match condition specifies that the value
in the specified field must match the condition
exactly.
Exact match operators and results
Use the And and Or logical
operators

When you need to use multiple conditions for a
query, you can use the logical operators to
combine conditions.




The And logical operator specifies that both conditions
must be met
The Or logical operator specifies that one or the other of
the conditions must be met
When you enter two conditions on the same row
of the query design grid, an And condition is
created.
If you enter two conditions that are on separate
rows, an Or condition is created.
Illustration of And and Or logic
Creating And condition
in the design grid
Use multiple undo and redo




In Access 2003, you can Undo and Redo multiple
actions at one time.
While working in design view for tables, queries,
forms, and reports, you can use the undo recent
actions button.
The Undo button on the design toolbar has an
associated list box. You can open the list box and
then select the action you want to undo.
If you undo an action and then change your
mind, you can also redo actions by the same
method, using the Redo button.
Using the Undo actions button
Perform calculations in a query using
calculated fields and expressions





Queries can be built to perform a calculation as
part of the query.
Expressions can be entered into the query design
grid.
Make certain that you are following the rules of
precedence.
To perform a calculation in a query, you must add
a calculated field to the query design.
You have three options for entering expressions:



Enter the expression directly into the field text box
Enter the expression in the Zoom box
Enter the expression in the Expression Builder
Create a form using the Form
Wizard




You can create a form from scratch or you can
create a form using the Form Wizard.
The Form Wizard will lead you through a series of
choices to help you develop the form based on a
table or a query.
You will need to select the type of layout you
want for your form, the style of the form, and a
name for the form.
After you have selected the table or query on
which the form will be based, you will select
which field(s) will be used on the form.
Open the Form Wizard dialog box
Use Form Wizard to
choose a form layout
Use Form Wizard to select
a style for the form
Completed Form
Change a form's AutoFormat
You may want to change the appearance
of a form after it has been created with
the Form Wizard.
 Clicking on the AutoFormat button and
then make your selection.
 Click the button beside each format to
view a sample of that particular format.
 Once you have decided on a format, click
OK.

A completed form created
with the Form Wizard
The AutoFormat dialog box
Find data using a form



You can navigate through all the records in a
form, but you may want to find a particular
record more quickly.
The Find command allows you to specify the
record you want to see and then will navigate
directly to that record.
To use the Find command:



Place your cursor in the form on the field for which you
want to search
Press the Find button and enter the value you are
looking for
The form will display the record(s) that match
your Find criteria.
Setting up a Find operation
Using wildcard characters in a Find
Maintain table data using a form
Not only can you view your data in a form,
you can also make modifications to the
data right in the form.
 Once you have navigated to the record
you want to change, you can make your
changes.
 When you move off the record, the
changes are made directly to the table.
 Access will allow you to add, modify, and
delete records in forms view.

A form in editing mode
Add and Delete records in Form
view

In addition to making changes to a record
in the form, you can also delete an entire
record.



The deletion is made immediately and you will
not be able to recover that record once it is
deleted
Always approach deletions of any kind with
caution
Records can be added using a form as
well. To add a record:


Click the New Record button on the form view
toolbar
A blank form will appear. Enter the new data
values for the new record
Create a form with a
main form and a subform
You can create a form with a subform on
two tables that have an established
relationship.
 When the relationship between the tables
is a one-to-many relationship, the main
form will consist of data from the primary
table and the subform will consist of data
from the related table.
 By selecting two related tables in the Form
Wizard, you can produce a form with a
subform.
 The form with subform is a great way to
display data for tables that have a one-tomany relationship.

Form Wizard Form/Subform
dialog box
Form and subform data
Notice in the following figure that the main
form contains information about the
employer whose ID is 10122.
 The data in the subform are positions that
this particular employer has available.
 Also notice that you have two sets of
navigation buttons. You can navigate the
data for either form.



The outer navigation buttons apply to the main
form
The inner navigation buttons apply to the
subform
An example of a Form with
subform
Create a report using
the Report Wizard





You can easily create a formatted printout of data
in table(s) in a database by using the Report
Wizard.
The Report Wizard will ask you a series of questions
to help you format the report.
Once the report has been created, either with the
Report Wizard or your own design, you can change
the design later.
You will find that the choices you make in the
Report Wizard are similar to the choices in the Form
Wizard.
Choices include grouping and sorting options, as
well as report layout options. You can preview the
report to view how it will look when printed.