Lesson 2
Lesson 2
When the Design View icon is clicked, the display changes: the icon
becomes a Datasheet View icon and the display changes to reveal design
information .
Table design view
Generally we want data in a database to be reasonable and correct. We can
use datatypes and properties to achieve certain types of correctness.
Consider the following integrity rules as rules we would like to enforce:
• Call numbers, titles, and authors are alphanumeric. Any text you can
type on the keyboard is acceptable.
• Each call number must be unique (there can be no duplicates)
• Each book must have a title
• A value for call number must be no more than 50 characters long
• A value for title must be no more than 255 characters long
• A value for author must be no more than 255 characters long
• The author field can be left out (it can be null).
We discuss how these integrity rules are obtained in Table Design View
Table design view
Table design view
• Beside the callNo field you can see the key icon. This means the
callNo field is the primary key. A primary key is a unique identifier –
every row in the table must have a unique value in that field. Every
table should have a PK specified and there can be only one PK for a
table. When a field is defined as the PK then a value must be provided
in each and every row.
• The callNo field has a datatype of Text and a field size of 50. Any value you can
type on the keyboard is acceptable but the overall length, number of
characters, is restricted to at most 50.
• The callNo field is indexed and in this case no duplicates are allowed. The
index constructed by MS Access is similar in purpose to the index at the back
of any book: the index allows MS Access to quickly locate a specified row.
However, this index is different from that at the back of a book because it
allows only one entry per indexed value (No Duplicates is specified for the
Indexed property). Each call number is unique.
Table design view
For title:
• The title field has a datatype of Text with a field size of 255. A
text field can comprise any combination of letters, digits, and
punctuation. Any value entered by a user cannot exceed 255
characters in length.
• A value is required. When entering data for some book, the
user cannot omit the title.
• There is no index on title.
For author:
• The author field has a datatype of Text with a field size of 255.
A text field can comprise any combination of letters, digits,
and punctuation. Any value entered by a user cannot exceed
255 characters in length.
• A value is not required. When entering data for some student,
the user can omit the author.
• There is no index on author.
Activity
• Open the Customers table.
• Add a new record to the table. Be sure to
enter data for every field.
• Find the record with the name Sula Smart,
and replace it with a name of your choice.
• Hide a field, then unhide it.
• Change the alternate row color.
Activity
• Open the Member table and then the Loan table
in design view. Examine the properties of each
field.
• Use Design View to add fields to the Member table as
indicated:
gender: Text field of length 6 to accommodate the values
male and female. Make this a required field that is not
indexed.
birthDate: a Date/Time field; required; not indexed.
Switch back to Datasheet View (You must reply yes to the
system prompt to save your changes). You should notice
there are no values for gender nor birthDate.
Activity
• 2. Now enter values you deem appropriate in the
gender and birthDate fields for each member. Close
the table and reopen it. You will see the values you
entered are still there.
• 3. When new members join the library information
about them must be entered into the Member table.
Each member is given an id value automatically. Add
new members to the library and note how MS Access
will not let you enter id values; instead, MS Access
generates those values for you – id values are
generated sequentially. Close the table and then
reopen the table to confirm your additions worked.
Activity
4. Typically a library assesses a fine the user must pay if they
keep a book out past the due date. As well the library needs to
track the amount, if any, the member has paid. In this exercise
we add two fields to the Loan table so we can keep track of fines
that are assessed and the amount the member has paid.
a) Open the Loan table in design view and add two new fields
named fineAssessed and finePaid. These fields must have a
datatype of Currency.
b) Save the Loan table and then view the rows of the table.
There are no amounts for these fields.
c) Choose some row(s) in the Loan table and enter values for
the fineAssessed and finePaid fields. Note the values you
enter will appear as dollars and cents.
Activity
5. In exercise 3 you added a new member and
in exercise 4 you added fields to the Loan
table. Consider that the person you added
now borrows a book and so a row must be
entered into the Loan table. Enter such a row.
Activity
6. After successfully entering data for exercises 3, 4, and 5 you are
aware of a member and a book for which there are references in the
Loan table.
a) View the Member table and try to delete that member, and
then view the Book table and try to delete that book. These
deletion attempts are unsuccessful because of the
references to the Loan table.
b) Now open the Loan table and find the loan record you
entered in exercise 5. If you delete this row you will find that
you are able to delete the member (provided you did not
enter more loans for this person). These actions mirror the
way in which data would typically be deleted from a
database: if you want to delete a row you must first delete (or
modify appropriately) any rows that reference it.
Table: Data type
Text
If you specify that a field has the Text data type
then Access will permit any characters to be
placed in that field in a row of the table. This is a
common choice when the data will not be used in
calculations. The Text data type provides for
values that have fewer than 256 characters. If you
know that a maximum length less than 255 would
be appropriate then you could use the Field Size
property (discussed in the next section) to limit the
maximum length of a text string.
Memo
A designer selects Memo if the field will have
character data but the length might be longer than
255. Memo allows for a maximum length of 63,999
characters. Consider the description field of the
Course table: could these be longer than 255
characters?
Table: Data type
Number
If a field is used for storing values that are
used in numerical calculations (e.g. quantity
ordered)
then Number is appropriate. The Field Size
property (properties are discussed later) can
be usedto limit the number of storage
locations used per value.
Date/Time
If a field contains date and or time values,
then the Date/Time data type should be
chosen. The
Format property (discussed later) allows you
to control how these values will appear to the
user.
Table: Data type
Currency
If a field will contain monetary values then the
Currency data type should be chosen. This data
type provides for numeric calculations that are
accurate to 15 digits to the left of the decimal and
4 digits to the right of the decimal.
AutoNumber
If you choose AutoNumber MS Access will
generate a value for you when a row is inserted
into the table. You can, via the New Values
property, arrange the numbers to be generated
sequentially or randomly. Often control numbers
for things like orders, invoices, registrations, etc.
are numeric and we can leave it to the system to
generate a next value for us .
Table: Data type
Yes/No
This data type restricts possible values to yes
or no.
Lookup Wizard
Sometimes you need to restrict values to a list
of known values (e.g. a list of genders: Male,
Female), or to values appearing as primary
key values elsewhere in the database.
Consider the creditHours field – a suitable list
could be (1, 2, 3, 6). The Lookup Wizard is a
suitable data type for these situations; when
selected the system steps you through a
series of windows where you can make the
appropriate choices.
Activity
These exercises refer to the Library database.
1. Consider the Member table. The id field was defined with the
AutoNumber datatype. Experiment by adding new members and
you will note that id values increase by 1. Now try deleting the last
two members that you added. If you add those members back in
what id values do they get? Are id values reused?
2. Consider the Book table. Add a field, paperback, that can be used
to indicate whether or not a book is a paperback. Choose the
YES/NO datatype. Save the design and switch to datasheet view.
Now you will see how to enter such values – MS Access provides a
box that is to be checked, or not. You can select (a ‘Yes’) using the
mouse or by using the space bar. You should experiment with this.
Activity
These exercises refer to the Library database.
3. Consider the Member table. Previously you added a gender field.
Open the Member table in design view and change the datatype for
gender to be Lookup Wizard. The wizard will automatically present 3
successive popup windows where you will:
Save the table and enter datasheet view so you can test out the
datatype you have just created. You will notice the user sees a drop
down list containing Male and Female, and so the user cannot
enter/select an inappropriate value.
Table: Properties
Each field must have a data type as discussed above. According to the
data type, MS Access will present to you a set of field properties that
you can tailor for your table.
• Field Size : Data integrity is a serious issue for databases. Setting
Field Size for Text data and Number data is a common thing to do.
Often organizations will limit the data they collect for fields such as
last name and first name (for example, 30 characters). If the data
type is Number then values selected for Field Size are values such
as Byte, Integer, Long Integer, etc. These kinds of values are
associated with increasing number of memory locations used per
value. A selection of Byte restricts storage to 1 byte of memory (8
bits), and since the largest positive integer that can be stored in a
byte is 255, the values stored in the field are forced to be in the
range from 0 to 255.
Table: Properties
• Format: The Format property is used to customize the way text,
number, dates, and times are displayed to the end user. For
instance, selecting Medium Date causes values like January 14,
2023 to be displayed as 14-Jan-23; selecting Long Date results in
the display January-14-23. If you have Text data such as
department code then you could force the display to be in capital
letters by specifying > as the format code. An interesting Format
specification is @;None. If this is used and if there is no value at all
to display the word None will be displayed to the user.
Table: Properties
• Input Mask: The Input Mask property is used to force the user to add
data according to some pattern. This is another nice feature to help
improve the overall quality of data added to a database. When the
cursor is in the Input Mask area a ‘builder button’ appears. When you
click this button you will see a list of popular controls. If you were to
choose the mask for phone number you will see the control !(999) 000-
0000 appear. As a result of this choice, the user must enter a 7-digit
phone number with an optional 3-digit area code).
• New Values: If the data type is AutoNumber you can use New
Values to specify whether the next value for the field will be the next
highest integer, or if it will be a random integer.
Activity
In the next two exercises you are working with your
University database.
1. Consider the Department table. In design view, set the
deptCode field to have a length of 4 and use > as the display
format. Set the length of the deptPhone field to be 10, and
choose the Phone Number input mask. Save the table and
switch to datasheet view. Use the table below as a guide and
enter data into the Department table.
Activity
In the next two exercises you are working with your University
database.
2. Create a Course table with attributes for department code, course number,
title, short description, and credit hours. The credit hours field should be
numeric with no decimal places, and the other fields are Text fields. Set the
deptCode field to be Text with a length of 4 so that it matches the properties of
deptCode in Department. Later it will be important that the deptCode field in
both Department and Course are defined the same. Use the table below as a
guide and enter data into the Course table.
Activity
In the next two exercises you are working with your University
database.
2. Create a Course table with attributes for department code, course number,
title, short description, and credit hours. The credit hours field should be
numeric with no decimal places, and the other fields are Text fields. Set the
deptCode field to be Text with a length of 4 so that it matches the properties of
deptCode in Department. Later it will be important that the deptCode field in
both Department and Course are defined the same. Use the table below as a
guide and enter data into the Course table.
Activity
These exercises refer to the Library database.
4. The Loan table has fields that are defined with the DateTime
datatype. Experiment with different formats for these dates.
5. Consider the id field in the Member table. In design view change the
increment property of the id field to be random instead of increment.
This is a nonreversible action (but you can first do a copy of the
database to get a fresh copy). Now add some new members. What can
you say about the id values that are assigned?
Activity
These exercises refer to the Library database.
1. Set the primary key for the Department table. With the
Department table in Design View, select the deptCode field and
then click on the Primary Key button. When done successfully you
will see the deptCode field with a key icon beside it. If MS Access
rejects your primary key then you must examine the values you
previously entered for deptCode, there must be some duplicated
value. If this happens you must view the table in Datasheet View
and find the duplicated value and make necessary changes. Once
MS Access has accepted your primary key you should open the
table in datasheet view and experiment: How does MS Access
respond if you try to create a new row with an existing primary
key value?
Activity
These exercises refer to the University database.