Creating a Database Table
With the database created, the next step is to create one or more database tables, or just tables for short. The table is the object in the database that actually holds your information. Each table consists of one or more records, containing data stored in fields. Going back to our earlier file cabinet analogy, think of each record as a form in the file folder (one per person), and the fields as the data that someone filled in for that personthe fields are where the actual information is stored. We're creating a guest book table for our Web site that has seven fields: email address, name, comment, gender, age, an option to hide a user's email address from other users, and the date on which the comment was posted.
After you click the Create button to create your database (as described in the preceding section), Access displays what I call the "start window" (see Figure 3). From the start window you can create not only tables, but a number of other important database objects, such as queries, forms, and reports. These objects are listed under the Objects button on the left side of the start window.
Figure 3 The "start window."
To create the guest book table, follow these steps:
-
First, if your screen doesn't look like Figure 3, click the word Tables (it doesn't look like a button, but it is) to display the following icons:
Create table in Design view
Create table by using wizard
Create table by entering data
Double-click the icon labeled Create table in Design view.
This step opens the "table design window"a window with the title Table 1: Table (see Figure 4).
Figure 4 Table design window sans toolbar menu.
Figure 4 doesn't have the Table Design toolbar displayed, and we'll need it. To display the toolbar, select View, Toolbars, Table Design, if it isn't already checked on your menu (see Figure 5). The Table Design toolbar appears below the menu (see Figure 6).
Figure 5 Selecting the Table Design toolbar for display.
Figure 6 Table design window with Table Design toolbar displayed.
The table design window contains three columns: Field Name, Data Type, and Description. Field Name is where you label the different pieces of information your table will hold. Data Type is the specific type of information (more on types later). Description is a comment about the information.
Recall that we're creating a guest book table to hold the following pieces of information: email address, name, comment, gender, age, an option to hide the email address, and date posted. Each piece of information is a field that we have to add to our database table. Let's start off with the most basic fielda text field.
Text Fields: Creating the email Field
In general, creating any field in a table is a three-step process:
Enter the name of the field. A valid field name in Access is a combination of letters and numbers, with the important restriction that the first character has to be a letter. You can include spaces and other characters in the field name, but try to avoid them if possiblethey often lead to subtle bugs that are hard to find. Finally, field names are not case-sensitive; Email, email, and even eMaIl refer to the same field as far as the database is concerned.
Enter the field's data type. The data types include Text, Memo, Number, Date/Time, and Yes/No.
Set the field's properties. The properties vary by data type; for example, for a Text field, important properties include Field Size, Default Value, and Allow Zero Length.
(Optional) Enter a description for the field.
To create the email field, follow these steps:
Type email in the first blank space under Field Name.
-
After entering a field name, press Tab or click in the Data Type cell adjacent to the field name. (See Figure 7.)
You're ready to set your field's data type. Notice the down-arrow button on the right side of the Data Type box. Clicking the button displays a drop-down list of the data types you can assign to the field (see Figure 8). If the information consists of a combination of letters and numbers and the total size of that combination is less than 255 characters, select Text as the data type. (We'll explain the other data types shortly.) All email messages in our sample database will be restricted to 255 characters, so the email field is a Text data type.
Click the Text item in the drop-down list.
-
In general, you should add a comment to your fieldsort of like commenting your code. For this field, we'll enter the description This is the user's e-mail address (see Figure 9). To speed our tutorial along, we'll omit comments for the rest of the fields.
-
The final step is to set your field's properties. The bottom section of the table design window lists a number of properties you can set, which vary according to your field's data type. The default text field size is 50 characters, which may be too small for some email addresses, so change it to 255 characters by typing 255 in the text box to the right of the Field Size label, and then pressing Enter (see Figure 10).
Figure 7 Creating the email field.
Figure 8 Data types.
Figure 9 Setting a comment for the email field.
Figure 10 Setting a default value of 255 characters for the email field.
You're done setting the e-mail field! Next we'll add the comment field.
Memo Fields: Creating the comment Field
You might guess that the guest book's comment field would use the Text data type (a "text field") just like the email field, where we set the field size to the maximum of 255 characters. Honestly, for most user comments a text field would suffice. However, some users might enter comments longer than 255 characters. For textual information whose size is more than 255 characters, you need a Memo field, which can accept a maximum of approximately 64,000 characters.
Follow these steps to create the comment field:
Type comment as the field name.
-
Open the Data Type drop-down list and select Memo (see Figure 11).
That's it for the comment field!
Figure 11 Creating the comment field.
Most of the time you won't need to change any of the memo field's property values from their defaults. However, we should point out that it's common to set the Allow Zero Length property to Yes for those memo fields that are optional; for example, a "special shipping instructions" field in a customer order table. However, for our guest book we always want the user to type a comment, so we'll leave this property set to its default (No).
As you can probably guess, creating the other fields in the guest book table is not a difficult procedure. The key is to know when to use the various data types and what values you need for the field properties.
Number Fields: Creating the age Field
There are two ways to get a user's age. The first way is simply to ask the user for his or her age (in your HTML form) and the second is to get the user's birthdate and calculate the user's age relative to the current date. The first way is easier, so we'll do that, which requires that you create a field whose data type is Number. As the name implies, you use a Number field whenever you have numeric information, which includes both whole numbers (123, 1024, 7) and real numbers (6.023, 1.99, 3.14159265358979).
Note: If you're dealing with monetary information, use the Currency data type rather than the Number data type.
To create our guest book's age field as a Number data type, follow these steps:
Type age as the field name.
-
Open the Data Type drop-down list and select Number (see Figure 12).
Figure 12 Creating the age field.
Important Number field properties include Format and Decimal Places, both of which affect how the database displays numbers. Note that these properties only affect a number's display unless you change the Field Size property. However, most of the time you'll keep the default values for the Number field's properties.
Date/Time Fields: Creating the dateposted Field
It's common in Web site guest books to keep track of when a user "signed" the guest book. Whenever you have time-based information such as a date, a time, or a combination of both, you create a field whose data type is Date/Time. We'll create a Date/Time field named dateposted and set its default value so that when a user signs the guest book the current date and time are automatically inserted.
You create a Date/Time field almost the same way you create any other field:
Type dateposted as the field name.
-
Open the Data Type drop-down list and select Date/Time (see Figure 13).
-
Enter Now() as the Default Value in the Field Properties section of the table design window (see Figure 14).
Figure 13 Creating the dateposted field.
For our guest book, we really don't want the user to type the date he or she posted the message. For one thing, the user may type the wrong date. More importantly, however, you shouldn't have the user enter any information that the database can calculate automatically, such as the current date/time.
Figure 14 Setting the default value for the dateposted field.
The database will now automatically substitute the current date and time into the dateposted fieldthere's no need to have a text box in your HTML guest book form that asks the user for the current date.
Yes/No Fields: Creating the hideEmail Field
There are cases when the information you get from the user can only have two values, such as yes/no or true/false. Whenever you have such Boolean values, you use the Yes/No data type.
In our guest book example, for security reasons we want to give our users the option of hiding their email addresses from other users viewing the guest book. To implement this option in our HTML guest book form, we would have a check box labeled Hide E-mail? If a user checks the box, we don't display his or her email address; otherwise, we do. Since this check box can only have two valuesit's either checked or nota Yes/No field is appropriate for recording the user's choice.
To create a Yes/No field to store the user's choice, follow these steps:
Enter hideEmail as the field name.
Open the Data Type drop-down list and select Yes/No (see Figure 15).
In most cases, you want to at least set a default value for the Yes/No field. In the Field Properties section of the table design window, enter Yes for the Default Value property (see Figure 16).
You can also change the format of the Yes/No field so that instead of entering or displaying yes or no as values, you can instead enter or display True/False or On/Off. For our purposes, however, Yes/No is the proper format.
Figure 15 Creating the hideEmail field.
Figure 16 Setting the default value for the hideEmail field.
You're now ready to do some exercises on your own. In the following exercises you'll complete the guest book table by creating a name field and a gender field.
Exercise 1: Creating the name Field
In addition to the aforementioned fields, you also want to get the name of the user who is signing your guest book. To simplify matters, we'll use one field to hold the user's entire namefirst, middle, and last names. Your task is to do the following:
Create a field called name whose data type is Text.
Set the field size for name to the maximum 255 characters.
Make the name field optional by setting Allow Zero Length to Yes.
Set the default value for this field to Declined to Specify.
If you did this exercise correctly, your table design window should look like Figure 17.
Figure 17 Adding the name field.
We'll do one more exercise, just for fun!
Exercise 2: Creating the gender Field
The final field we'll add to our guest book table is a field to hold the user's gender. Although gender typically takes only two valuesmale or femaleand we could use a Yes/No field, we'll simply use a text field to store this information. Your task is to do the following:
Create a field called gender whose data type is Text.
Set the field size for gender to six characters, or just enough to hold the value female (which is six characters).
Set the default value for this field to female. Unlike the name field, which is optional, let's make the user supply gender information. Don't set the value of Allow Zero Length to Yes in the Field Properties section.
If you did this exercise correctly, your table design window should look like Figure 18.
Figure 18 Adding the gender field.
You're almost done. The penultimate step is to set a primary key for your guest book table.