0% found this document useful (0 votes)
8 views7 pages

MSACCESS2

Uploaded by

wong009hg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

MSACCESS2

Uploaded by

wong009hg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Tutorial 7

Microsoft Access 2016 Lab Two


Learning Outcomes
• Changing and Customizing Tables
• Simple Query

Relationships between Tables


In database, it is unusual to store everything in just one table. The information is
normally stored in different tables with each carrying one theme only to facilitate data
search and data management. The tables within a database may relate to each other,
thus we have “relationship” between tables. The following diagram showed a classic
relationship between two tables, Employee and Unit. Unit Code is the common field
found in both tables. It forms the relationship between them.

Employee Unit
Table Table

Access allows you to define relationships between tables. Once a relationship is


defined however, if you delete a record of a table related to another one, problem
arises. For instance, in the diagram above, if you delete the Ka Wah row in Employee,
you need to delete the Eng row in Unit as well, because Employee and Unit are now
related or linked. In so doing however, the Charles row in Employee has to be
deleted as well, because it is also linked to the Eng row in Unit. This is where the
problem arises, because only Ka Wah is needed to delete, but not Charles in the first
place.

Access prevents these problems by enforcing referential integrity.

In database, referential integrity means that DBMS will not let you
delete/update a record from one table that has related records in
another table, resulting in orphan/incomplete records.

Taking the above example as an illustration, when Employee and Unit established a
relationship, referential integrity is enforced between these two tables. If you now
wish to refer to EmployeeID 1, it now includes Ka Wah, Eng from Employees and Eng,
Engineering Unit from Unit. Therefore, deleting Ka Wah needs deleting the Eng row
in Unit as well, because (1, Ka Wah, Eng) and (Eng, Engineering Unit) now form a
complete record under the influence of reference integrity. The same situation
happens in other rows as well. This then explains why if we delete the Eng row in
Unit, Charles has to be taken out as well, otherwise only half of the Charles record is
gone, but the other half remains. Charles become incomplete/orphan. In view of

1
that, DBMS simply does not allow us to do so, until we disable the referential integrity
enforcement intentionally.

Access protects against orphan records from happening by giving you two options
upon your choosing to Enforce Referential Integrity.

Option 1: It modifies related records when you change the value of


a primary key.
Cascade Update
Related Fields For example, if you change a Unit Code from Eng to Egr
in Unit table, all records in Employee table associated
with that unit will have their unit codes changed from Eng
to Egr.
Option 2: It deletes related records in other tables when you
remove a record from the first table.
Cascade Delete
Related Records For example, if you delete a unit code record in Unit
table, all records in Employee table associated with that
unit code are also removed.

A connection or link between tables that have related data is known as a join. They
reflect the business intelligence in an organization, and therefore are liable to change
from time to time, allowing the organization to better adapt to its business
environment.

Creating a Relationship between Two Tables


• In the database window, select Database Tools tab, Relationships… from the
Menu bar of Access.

• The Show Table dialog box is displayed.


• Select the table which you want to define relationship, e.g. Position
• Click Add
• Select on another table you want to define relationship with e.g. Employee
• Click Add
• Click Close button

A Relationship window is displayed with the two tables shown on it.


• Click on the field of the first table which you want to link with the second table, e.g.
PositionCode
• Drag the field onto the same field of the second table
• The Edit Relationships window is displayed
• Select option “Enforce Referential Integrity”, the One-to-Many option is chosen
for you
• Select options “Cascade Update Related Fields” and “Cascade Deleted
Related Records”

2
• Click the OK button
• Click the Create button
• Select Design, Close. Access then asks “Do you want to save changes to the
layout of ‘Relationships’?”. Answer Yes if you want to save the assigned
relationship.

Adding Validation Rules to Tables

Validation rules put restrictions on data entry. One of the common validation rules is
to prevent a null value in a field, i.e. the field must have a value in it.

• Open the table in Table Design mode.


• Move to the field where you want to add rules.
• In the Field Properties box, go to the Validation Rule field and type Is Not Null.
• You can type in the explanation text in the Validation Text field, e.g. “Fill in Data”.
This text will be shown if the rule is violated during data entry.
• Close the table. Access then asks “Do you want to save changes to the design
of table tablename?”. Answer Yes if you want to save the changes you have
made.
• Then Access will prompt “Data integrity rules have been changed; existing data
may not be valid for the new rules. This process may take a long time. Do you
want the existing data to be tested with the new rules?” Answer Yes if you want
to check the existing data.

Creating Indexes

An index is a database structure offered by Access that acts like the index of a book -
it tells Access where to look for a piece of information. You may want to index the
fields that you use most often, e.g., LastName.

• Open the table in Table Design mode.


• Select Design tab, Indexes from the Menu bar.

• The Index definition window will appear.


• Type the index name (usually same as the field name to be indexed) in the Index
Name box.
• Select the field name in the Field Name box.
• Select the Ascending or Descending in the sort order box.
• Close the Index definition window.
• Close the table. Access then asks “Do you want to save changes to the design
of table tablename?”. Answer Yes if you want to save the changes you have
made to indexes.

3
Access Exercise 3

1. Open the database file you created in the Access Exercise 2.

2. Open the table Employee, change Position to PositionCode. Then save the
change and close the table.

3. Create a new table called Position.

4. Add the following fields and values into Position.

Fields of Position
Field name Field type Field size
PositionCode Short Text 2
PositionDescription Short Text 35

Data records in Position table


PositionCode PositionDescription
LA Librarian
AL Assistant Librarian
PS Personal Secretary
EO Executive Officer
CO Clerical Officer

5. Set the field PositionCode as the primary key. Save and then close Position.

6. Define relationship for the Position and Employee table, link them by the field
PositionCode. Select “Enforce Referential Integrity” and then Cascade Update
Related Fields and Cascade Deleted Related Records.

7. Add index to LastName.

8. Make LastName and PositionCode mandatory in data entry. Enter meaningful


Validation Text (hint: Is Not Null) for these two fields by means of validation rule.

9. Test the validation rule: Open the Datasheet View of the Employee Table. Enter
a record without any LastName / PositionCode. Note the display of validation
text. Finally, undo the data entry.

10. Save the database as yourname_StudentID.accdb. For example, if your name is


Chan Tai Man, then save the file as ChanTaiMan_00123321a.accdb.

4
Setting Up Queries
A query can be set up to look for the data stored in a database, e.g., to return the
records for salary over 30000. You may do this in Excel by sorting on the salary
column, but if you need to return information from multiple tables, you need the
database querying function involving multiple tables. In Access, you have to specify
the conditions and restrictions imposed on data to limit the information presented.
These are called criteria.

• In the database window, choose the Create tab, Query Design.

• You will see the Show Table window. You can add the appropriate tables you
want as you have done in the Relationships window. The links between the
tables are shown automatically as well (information when you define the
relationships).
• You will see the Query Design window at the bottom and you can add the fields
and table names in the window, or else you can drag the fields from the tables
directly to the cells in the bottom window.
• Specify any criteria and sorting order you want (more to follow).
• When you are done, select File tab, Save to save the query under a certain name.
• To return the result of the query, double-click the query, or in Design View click
File tab, Run.

Specifying Criteria

Sort Criteria
You can display the result in unsorted order, ascending order or descending order of
the values of any field you want.
• Choose the Query, Home tab and click the Design View.
• Click on the Sort entry of the field you want to sort, select Ascending or
Descending.
• Click elsewhere to finish.
• Click the Run button to return the new result.

5
Selection Criteria
You may want to find out those whose salary is over 30000.
• Choose the Query, Home tab and click the Design View.
• Click on Criteria entry of the intended field.
• Enter your criteria, e.g., > 30000.
• Click elsewhere to finish.
• Click the Run button to return the new result.
• The following gives some additional examples of the use of other operators for
setting criteria.

Operators Example Meaning


= =“Wong” Find record(s) with value equal to Wong
> >23 Find record(s) with value greater than 23
< <40 Find record(s) with value fewer than 40
>= >=50 Find record(s) with value greater than or equal to 50
<= <=49 Find record(s) with value fewer than or equal to 49
<> <>“Wong” Find record(s) with value not equal to Wong
Like Like “W*” Find record(s) with value beginning with W
Between … Between “A”
Find record(s) with value within the A – M range
And … And “M”
In In(“CO”,
Find record(s) with value equal to CO, EO or LA
”EO”, “LA”)

Show Field
You may want to inhibit the display of the value of a field. For example, you want to
know those whose salary is over 30000, but you do not want to show the exact value
of the salary. You can then choose not to show the field Salary.

• Choose the Query, Home tab and click the Design View.
• Click on Show entry of the field you want to display or not to display in a query.
• Click elsewhere to finish.
• Click the Run button to return the new result.

6
Access Exercise 4
Create the following queries 1 to 4. The results returned by them include EmployeeID,
LastName, FirstName, Salary, and PositionCode from Employee.

1. A simple query sort by LastName in ascending order; save it as Employee Last


Name list.

2. A new query with the following criteria; save it as Executive Officer.


PositionCode = “EO”

3. A new query with the following criteria; save it as Salary 20000 up.
Salary >= 20000

4. A new query with the following criteria; save it as Employees’ Last Name starting
with L.
LastName Like “L*”

5. Create a new Query with fields LastName, FirstName, Salary for all Male
employees. Save it as Male staff.

6. Create a new Query with fields LastName, FirstName, Salary for all employees
with salary range from 10000 to 15000. Save it as Salary from 10000 to 15000.

7. Create a new Query with fields LastName, FirstName, Salary for all employees
living in Mong Kok, Yau Ma Tei, Shatin or Sheung Wan. Save it as Kowloon and
NT Staff.

You might also like