MSACCESS2
MSACCESS2
Employee Unit
Table Table
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.
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.
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.
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.
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.
3
Access Exercise 3
2. Open the table Employee, change Position to PositionCode. Then save the
change and close the table.
Fields of Position
Field name Field type Field size
PositionCode Short Text 2
PositionDescription Short Text 35
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.
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.
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.
• 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.
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.
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.