Creating database tables
To work with tables, click the Tables icon in the Database list, or use Alt+a. The three tasks that you can perform on a table are in the Task list (see Figure 2).
Using the Wizard to create a table
Since none of the fields we need for our Automobile database are contained in any of the wizard tables, we will create a simple table using the wizard that has nothing to do with our database. This section is an exercise in explaining how the Wizard works.
The Wizard permits the fields of the table to come from more than one suggested table. We will create a table with fields from three different suggested tables in the Wizard.
Click Use Wizard to Create Table. This opens the Table Wizard.
Step 1: Select fields.
You have a choice of two categories of suggested tables: Business and Personal. Each category contains its own suggested tables from which to choose. Each table has a list of available fields. We will use the CD-Collection Sample table in the Personal category to select the fields we need.
- Category: Select Personal. The Sample Tables drop down list changes to a list of personal sample tables.
- Sample Tables: Select CD-Collection. The Available fields window changes to a list of available fields for this table.
- Selected Fields: Using the > button, move these fields from the Available fields window to the Selected fields window in this order: CollectionID, AlbumTitle, Artist, DatePurchased, Format, Notes, and NumberofTracks.
- Selected Fields from another sample table. Click Business as the Category. Select Employees from the dropdown list of sample tables. Use the > button to move the Photo field from the Available fields window to the Selected fields window. It will be at the bottom of the list directly below the NumberofTracks field.
- If a mistake is made in the order as listed above, click on the field name that is in the wrong order to highlight it. Use the Up or Down arrow on the right side of the Selected Fields list (see Figure 3) to move the field name to the correct position. Click Next.
Step 2: Set field types and formats.
In this step you give the fields their properties. When you click a field, the information on the right changes. You can then make changes to meet your needs. (See Figure 4.) Click each field, one at a time, and make the changes listed below.
- CollectionID: Change AutoValue from No to Yes.
- AlbumTitle:
- Entry required: If all of your music is in albums, change Entry required to Yes. Otherwise, leave Entry required as No.
- Length: Unless you have an album title that exceeds 100 characters in length counting the spaces, do not change the length.
- Artist: Use the Default setting. And since music has authors, set Entry Required to Yes.
- Date Purchased: Length: default setting. Entry required should be No. (You may not know the date.)
- Format: Only change the Entry Required setting: from No o Yes.
- Notes: No changes are required.
- NumberofTracks: Change the Field Type to Tiny Integer[TINYINT]. Your allowable number of tracks will be 999.
- Photo: Use the default settings. When you have finished, click Next.
Step 3: Set primary key.
- Create a primary key should be checked.
- Select option Use an existing field as a primary key.
- In Fieldname drop down list, select CollectionID.
- Check Auto value if it is not already checked.
- Click Next.
Step 4: Create the table.
- If desired, rename the table at this point. If you rename it, make the name meaningful to you. For this example, make no changes.
- Leave the option Insert data immediately checked.
- Click Finish to complete the table wizard. Close the window created by the table wizard. You are now back to the main window of the database with the listing of the tables, queries, forms, and reports.
Creating a table by copying an existing table
If you have a large collection of music, you might want to create a table for each type of music you have. Rather than creating each table from the wizard, you can make a copy of the original table. Each table can be named according to the type of music contained in it. Possible names could include Classical, Pop, Country and Western, and Rock among others.
- Click on the Tables icon in the Database pane to see the existing tables.
- Right-click on the CD-Collection table icon. Select Copy from the context menu.
- Move the mouse pointer below this table, right-click, and select Paste from the context menu. The Copy table window opens.
- Change the table name to Pop and click Next.
- Click the >> button to move all the Fields from the left window to the right window and click Next.
- Since all the Fields already have the proper File Type formating, no changes should be needed. However, this is the time and place to make these changes if they are needed. (See Caution below for the reason why.) Click Create. The new table is created.
Creating tables in Design View
Design View is a more advanced method for creating a new table. It allows you to directly enter information about each field in the table. We will use this method for the tables of our database.
While the Field type and formatting are different in Design View, the concepts are the same as in the Wizard. |
The first table to be created is Fuel. Its fields are FuelID, Date, FuelCost, FuelQuantity, Odometer, and PaymentType. FuelCost uses currency and two decimal places. FuelQuantity and Odometer uses the number format with 3 decimal places and 1 decimal place respectively. PaymentType uses the text format.
- Click Create Table in Design View.
- FuelID entries:
- Enter FuelID as the first Field Name.
- Select Integer[INTEGER] as the Field Type from the dropdown list. (The default setting is Text[VARCHAR].)
- Change the Field Properties in the bottom section. Change AutoValue from No to Yes (Figure 5).
- Set FuelID as the Primary key. Right-click on the green triangle to the left of FuelID (Figure 6).
- All other entries:
- Enter the next field name in the first column (Field Name column).
- Select the Field Type for each field.
- For Date use Date[DATE]. (Use the D key once to select it.)
- PaymentType uses Text[VARCHAR], the default setting.
- All other fields use Number[NUMERIC]. (Use the N key once to select it.)
- Select the Field Properties (Figure 7).
- FuelCost, FuelQuantity, and Odometer need changes in the Field Properties section (Figure 7).
- FuelQuantity: Change Length to 6 and Decimal places to 3. (Many fuel pumps measure fuel to thousands of a gallon in the USA where I live.)
- Odometer: Change the Length to 10 and the Decimal places to 1.
- FuelCost: Change the Length to 5 and Decimal places to 2. Click the Format example button. This opens the Field Format window (Figure 8).
- Use Currency as the Category and anything in the Format list with two decimal places.
- Repeat these steps for each field in the table. To access additional formatting options, click the button to the right of the Format example panel (Format example button).
- Description can be anything, or can be left blank. (Figure 9 is an example of this.)
- To save and close the table, select File > Close. Name the table Fuel.
Follow the same steps to create the Vacations table. The fields and their field types are listed in Figure 9. Make sure you make Date field the primary key before closing, naming the table Vacations, and saving it.
Creating tables for the list box
When the same information can be used in several fields, design a table for each type of information. Each table will contain two fields: the information field, and ID in this order.
- Follow the directions in Creating tables in Design View. In the table we will create, the two fields can be Type and PaymentID. Make sure that the AutoValue is set to Yes for the PaymentID field. Set the PaymentID field as the primary key. (See Figure 10.)
- Save the table using the name Payment Type.
If you have several tables to create with the same fields, design one table and produce the other tables by cutting and pasting. (See Creating a table by copying an existing table.) |
Adding data to the list table
List tables do not require a form. Instead, add their data directly to the table. In this example, use the names of the two people with a bank card and cash for cash purchases.
- In the main database window, click on the Tables icon (Figure 2). Right-click on Payment Type and select Open from the context menu.
- Enter Dan in the first row. Use the tab key to move to the second row.
- Enter Kevin in the second row.
- Enter Cash in the third row.
- Save and close the table window.
Content on this page is licensed under the Creative Common Attribution 3.0 license (CC-BY). |