0% found this document useful (0 votes)
8 views

Tables

Uploaded by

92 man fan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Tables

Uploaded by

92 man fan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Access Tutorial 2: Tables

2.1 Introduction: The importance 2.2 Learning objectives


of good table design  How do I enter and edit data in the datasheet
Tables are where data in a database is stored; con- view of a table?
sequently, tables form the core of any database  How do I create a new table?
application. In addition to basic data, Access permits
 How do I set the primary key for a table?
a large amount of domain knowledge (such as cap-
tions, default values, constraints, etc.) to be stored at  How do I specify field properties such as the
the table level. input mask and caption?
 Why won’t an autonumber field restart
Extra time spent thinking about table design counting at one?
can result in enormous time savings during
later stages of the project. Non-trivial changes  What are the different types of keys?
to tables and relationships become increas-
ingly difficult as the application grows in size 2.3 Tutorial exercises
and complexity. In this tutorial, you will learn to interact with existing
tables and design new tables.

© Michael Brydon ([email protected])


Last update: 25-Aug-1997 Home Previous 1 o f 18 Next
2. Tables Tutorial exercises

2.3.1 Datasheet basics such as lecturers, department heads, departmental


• If you have not already done so, open the secretaries, and so on.
univ0_vx.mdb database file from Tutorial 1. • Return to the database window and create a new
• Open the Departments table. The important table as shown in Figure 2.3.
elements of the datasheet view are shown in • In the table design window shown in Figure 2.4,
Figure 2.1. type in the following information:
• Use the field selectors to adjust the width of the Description
Field name Data type
DeptName field as shown in Figure 2.1. (optional)
• Add the Biology department (BIOL) to the table, EmployeeID Text use employee
as shown in Figure 2.2. S.I.N.
• Delete the “Basket Weaving” record by clicking FName Text First name
on its record selector and pressing the Delete
LName Text Last name
key.
Phone Text
2.3.2 Creating a new table Salary Currency
In this section you will create and save a very basic
skeleton for table called Employees. This table • Select File > Save from the main menu (or press
could be used to keep track of university employees Control-S) and save the table under the name
Employees.

Home Previous 2 o f 18 Next


2. Tables Tutorial exercises

FIGURE 2.1: The datasheet view of the Departments table.

The field names are shown in the “field


selectors” across the top of the columns.  the
Resize the DeptName column by clicking near
column border and dragging the border to
the right.
You can temporarily sort the records
in a particular order by right-clicking
any of the field selectors.

The records are shown as rows.

The black triangle indicates the


“current record”.

The asterisk (*) indicates a


The grey boxes are “record selectors”. place holder for a new record.

The “navigation buttons” at the bottom of the window


indicate the current record number and allow you to go
directly to the first, previous, next, last, or new record.

Home Previous 3 o f 18 Next


2. Tables Tutorial exercises

FIGURE 2.2: Adding and saving a record to the table.

 ofAddthea “new
new record by clicking in the DeptCode field
record” field (marked by the asterisk).
It is seldom necessary to
explicitly save new
records (or changes to
existing records) since
Access automatically
saves whenever you
move to another record,
close the table, quit
Access, etc.

 data,
To permanently save the change to the
click on the record selector (note the
icon changes from a pencil to a triangle).

Home Previous 4 o f 18 Next


2. Tables Tutorial exercises

FIGURE 2.3: Create a new table.

 create
Click the New button to
a new table.

 the
Select “design view” (avoid using
table wizard at this point).

Home Previous 5 o f 18 Next


2. Tables Tutorial exercises

FIGURE 2.4: Use the table design window to enter the field properties for the Employees table.

 data
Enter the field names and
types for the five fields.

The “description” column allows


you to enter a short comment
about the field (this information
is not processed in any way by
Access).

The “field properties” section


allows you to enter information
about the field and constraints on
the values for the field.

Home Previous 6 o f 18 Next


2. Tables Tutorial exercises

2.3.3 Specifying the primary key


FIGURE 2.6: Set the field properties for the
Tables normally have a primary key that uniquely EmployeeID field.
identifies the records in the table. When you desig-
nate a field as the primary key, Access will not allow
you to enter duplicate values into the field.
• Follow the steps in Figure 2.5 to set the primary
key of the table to EmployeeID.

2.3.4 Setting field properties


In this section, you will specify a number of field
properties for the EmployeeID field, as shown in
Figure 2.6.
• Since we are going to use the employees’ Social
Insurance Number (S.I.N.) to uniquely identify
them, set the Field Size property to 11 characters
(9 for numbers and 2 for separating spaces)
• Set the Input Mask property to the following:
000\ 000\ 000;0
• Set the Caption property to Employee ID

Home Previous 7 o f 18 Next


2. Tables Tutorial exercises

FIGURE 2.5: Set the primary key for the Employees table.

 fields)
Click on the grey box beside the field (or
that form the primary key.

To select more than one field for use as the


primary key, hold down the Control key
while clicking on the grey boxes.

 select
Either click the key-shaped icon in the tool bar or
Edit > Primary Key from the menu.

Home Previous 8 o f 18 Next


2. Tables Discussion

• Select View > Datasheet from the main menu to three small dots ( ) to invoke the input mask
switch to datasheet mode as shown in Figure 2.7. wizard.
Enter your own S.I.N. and observe the effect of • Follow the instructions provided by the wizard as
the input mask and caption on the EmployeeID shown in Figure 2.8.
field. • Press F1 while the cursor is still in the input mask
• Select View > Table Design from the main menu property. Scroll down the help window to find the
to return to design mode. meaning of the “0”, “9”, “>” and “L” input mask
• Set the field properties for FName and LName symbols.
(note that Length and Caption are the only two
properties that are relevant for these two fields) 2.4 Discussion
2.3.5 Using the input mask wizard 2.4.1 Key terminology
In this section, you will use the input mask wizard to A key is one or more fields that uniquely determine
create a complex input mask for a standard field the identity of the real-world object that the record is
type. You will also use the help system to learn more meant to represent. For example, there is a record in
about the meaning of the symbols used to create the student information system that contains infor-
input masks. mation about you as a student. To ensure that the
• Select the Phone field, move the cursor to the record is associated with you and only you, it con-
input mask property, and click the button with

Home Previous 9 o f 18 Next


2. Tables Discussion

FIGURE 2.7: Observe the effect of the input mask and caption properties on the behavior of the
EmployeeID field during data entry

 numbers
Try entering various characters and
into the EmployeeID
field.
If a caption is specified, it replaces the
 done
Press the Escape key when you are
to clear the changes to the record.
field name in the field selector.

Note that the input mask will not let you


type any characters other than numbers
from 0-9. In addition, the spaces between
the groups of numbers are added
automatically.

Input masks provide a relatively easy way to


avoid certain basic data input errors without
having to write complex error checking
programs. Note, however, that it is possible to
over-constrain a field so that users are unable to
enter legitimate values.

Home Previous 10 o f 18 Next


2. Tables Discussion

FIGURE 2.8: Use the input mask wizard to create an input mask.

 number”
Select “phone
from the
list of commonly-
used field types.

The items in this


list depend on the
“international
settings” specified
for Windows (e.g.,
“Zip Code” may
show instead of Since the input mask controls how
“Postal Code”). the information in the field looks, it
is possible to save some disk space
by storing the data without the
 edit
In Step 2, you may
the input mask
extras symbols, spaces, etc. For the
size of system we are building,
(e.g., remove the however, this savings is negligible.
area code section).

Home Previous 11 o f 18 Next


2. Tables Discussion

tains a field called “student number” that is guaran- nated key is made by joining together two or
teed to be unique. more fields. Course numbers at UBC provide a
The advantage of using student number as a key good example of a concatenated key made by
instead of some other field—like “student name”—is joining together two fields: DeptCode and
that there may be more than one person with the CrsNum. For example, department alone cannot
same first and last name. The combination of stu- be the primary key since there are many courses
dent name and address is probably unique (it is in each department (e.g., COMM 335, COMM
improbable that two people with the same name will 391). Similarly, course number cannot be used as
at the same address) but using these two fields as a a key since there are many courses with the
key would be cumbersome. same number in different departments (e.g.,
COMM 335, HIST 335, MATH 335). However,
Since the terminology of keys can be confusing, the
department and course number together form a
important terms are summarized below.
concatenated key (there is only one COMM 335).
1. Primary key — The terms “key” and “primary 3. Foreign key: In a one-to-many relationship, a
key” are often used interchangeably. Since there foreign key is a field (or fields) in the “child”
may be more than one candidate key for an record that uniquely identifies the correct “parent”
application, the designer has to select one: this is record. For example, DeptCode and CrsNum in
the primary key. the Sections table are foreign keys since these
2. Concatenated key: The verb “concatenate” two keys taken together are the primary key of
means to join together in a series. A concate-

Home Previous 12 o f 18 Next


2. Tables Discussion

the Courses table. Foreign keys are identified in In addition, you can use the comment field in the
Access by creating relationships (see Tutorial 3). table design window to document the meaning of
field names.
2.4.2 Fields and field properties
It is strongly recommended that you avoid all
2.4.2.1 Field names
non-alphanumeric characters whenever you
Access places relatively few restrictions on field name a field or database object. Although
names and thus it is possible to create long, descrip- Access will permit you to use names such as
tive names for your fields. The problem is that you Customer#, non-alphanumeric characters
have to type these field names when building que- (such as #, /, $, %, ~, @, etc.) may cause
ries, macros, and programs. As such, a balance undocumented problems later on.
should be struck between readability and ease of
typing. You are advised to use short-but-descriptive 2.4.2.2 Data types
field names with no spaces. The field's data type tells Access how to handle the
For example, in Section 2.3.2 you created a field information in the field. For instance, if the data type
with name FName. However, you can use the caption is date/time, then Access can perform date/time
property to provide a longer, more descriptive label arithmetic on information stored in the field. If the
such as First name. The net result is a field name same date is stored as text, however, Access treats
that is easy to type when programming and a field it just like any other string of characters. Normally,
caption that is easy to read when the data is viewed.

Home Previous 13 o f 18 Next


2. Tables Discussion

the choice of data type is straightforward. However, for use as a primary key when no other key is
the following guidelines should be kept in mind: provided or is immediately obvious.
1. Do not use a numeric data type unless you are
Since an autonumber is really Long Integer
going to treat the field as a number (i.e., perform
and since relationships can only be created
mathematical operations on it). For instance, you
between fields with the same data type, it is
might be tempted to store a person's student
important to remember that if an autonumber
number as an integer. However, if the student
is used on the “one” side of a relationship, a
number starts with a zero, then the first digit is
long integer must be used for the “many” side.
dropped and you have to coerce Access into dis-
playing it. Similarly, a UBC course number (e.g., 2.4.2.3 “Disappearing” numbers in
335) might be considered a number; however, autonumber fields
since courses like 439B have to accommodated,
If, during the process of testing your application, you
a numeric data type for the course number field is
add and delete records from a table with an auto-
clearly inappropriate.
number key, you will notice that the deleted keys are
2. Access provides a special data type called Auto
not reclaimed.
Number (Counter in version 2.0). An autonum-
ber/counter is really a number of type Long Inte- For instance, if you add records to your Customer
ger that gets incremented by Access every time table (assuming that CustID is an autonumber), you
a new record is added. As such, it is convenient will have a series of CustID values: 1, 2, 3… If you

Home Previous 14 o f 18 Next


2. Tables Discussion

later delete customer 1 and 2, you will notice that lutely no difference whether the first customer in your
your list of customers now starts at 3. customers table is CustID = 1 or 534.
Clearly, it would be impossible for Access to renum- 2.4.2.4 Input masks
ber all the customers so the list started at 1. What An input mask is a means of restricting what the user
would happen, for instance, to all the printed can type into the field. It provides a “template” which
invoices with CustID = 2 on them? Would they refer
tells Access what kind of information should be in
to the original customer 2 or the newly renumbered each space. For example, the input mask >LLLL
customer 2? consists of two parts:
The bottom line is this: once a key is 1. The right brace > ensures that every character
assigned, it should never be reused, even if the user types is converted into upper case.
the entity to which it is assigned is subse- Thus, if the user types comm, it is automatically
quently deleted. Thus, as far as you are con- converted to COMM.
cerned, there is no way to get your customers 2. The characters LLLL are place holders for letters
table to renumber from CustID = 1. from A to Z with blank spaces not allowed. What
this means is that the user has to type in exactly
Of course, there is a long and complicated way to do four letters. If she types in fewer than four or
it, but since used an autonumber in the first place, types a character that is not within the A to Z
you do not care about the actual value of the key— scope (e.g., &, 7, %), Access will display an error
you just want it to be unique. In short, it makes abso- message.

Home Previous 15 o f 18 Next


2. Tables Discussion

There are a large number of special symbols used The semicolon and zero at the end of this input mask
for the input mask templates. Since the meaning of are important because, as the on-line help system
many of the symbols is not immediately obvious, points out, an input mask value actually consists of
there is no requirement to remember the character three parts (or “arguments”), each separated by a
codes. Instead, simply place the cursor on the input semicolon:
mask property and press F1 to get help. In addition, • the actual template (e.g., 000\-0000),
the wizard can be used to provide a basic input mask • a value (0 or 1) that tells Access how to deal with
which can later be modified. literal characters, and
2.4.2.5 Input masks and literal values • the character to use as a place holder (showing
the user how many characters to enter).
To have the input mask automatically insert a char-
acter (such as a space or a dash) in a field, use a When you use a literal character in an input mask,
the second argument determines whether the literal
slash to indicate that the character following it is a lit-
eral. value is simply displayed or displayed and stored in
the table as part of the data.
For example, to create an input mask for local tele-
For example, if you use the input mask 000\-
phone numbers (e.g., 822-6109), you would use the
0000;1, Access will not store the dash with the tele-
following template: 000\-0000;0 (the dash is a lit-
phone number. Thus, although the input mask will
eral value and appears automatically as the user
always display the number as “822-6109”, the num-
enters the telephone number).
ber is actually stored as “8226109”. By using the

Home Previous 16 o f 18 Next


2. Tables Application to the assignment

input mask 000\-0000;0, however, you are telling 2.5 Application to the assignment
Access to store the dash with the rest of the data.
You now have the skills necessary to implement your
If you use the wizard to create an input mask, tables.
it asks you a simple question about storing lit- • Create all the tables required for the assignment.
eral values (as shown in Figure 2.8) and fills • Use the autonumber data type (counter in version
in the second argument accordingly. How- 2.0) for your primary keys where appropriate.
ever, if you create the input mask manually, • Specify field properties such as captions, input
you should be aware that by default, Access mask, and defaults where appropriate.
does not store literal values. In other words,
If you create an input mask for ProductID,
the input mask 000\-0000 is identical to the
ensure you understand the implications of
input mask 000\-0000;1. This has impor-
Section 2.4.2.5.
tant consequences if the field in question is
subject to referential integrity constraints (the • Set the Default property of the OrderDate field
value “822-6109” is not the same as so that the current date is automatically inserted
“8226109”). into the field when a new order is created (hint:
see the Date() function in the on-line help sys-
tem).

Home Previous 17 o f 18 Next


2. Tables Application to the assignment

• Do not forget to modify your Products table (the


data types, lengths, and field properties of
imported tables normally need to be fine tuned)
• Populate (enter data into) your master tables. Do
not populate your transaction tables.

For the purpose of the assignment, the term


“transaction” tables refers to tables that con-
tain information about individual transactions
(e.g., Orders, OrderDetails, Ship-
ments, ShipmentDetails). “Master”
tables, in contrast, are tables that either do
not contain information about transactions
(e.g., Customers) or contain only summary
or status information about transactions (e.g.,
BackOrders).

Home Previous 18 o f 18 Next

You might also like