Tables
Tables
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).
create
Click the New button to
a new table.
the
Select “design view” (avoid using
table wizard at this point).
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.
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.
select
Either click the key-shaped icon in the tool bar or
Edit > Primary Key from the menu.
• 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
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.
FIGURE 2.8: Use the input mask wizard to create an input mask.
number”
Select “phone
from the
list of commonly-
used field types.
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-
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.
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
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.
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
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).