Difference between revisions of "Documentation/OOo3 User Guides/Getting Started/Creating database tables"

From Apache OpenOffice Wiki
Jump to: navigation, search
m
Line 19: Line 19:
 
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.
 
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.
+
From the '''Tasks Pane''' click '''Use Wizard to Create Table'''. This opens the Table Wizard.
  
 
{{Documentation/Note|A field in a table is one bit of information. For example, in a price list table, there might be one field for item name, one for the description and a third for the price. More fields may be added as needed.}}
 
{{Documentation/Note|A field in a table is one bit of information. For example, in a price list table, there might be one field for item name, one for the description and a third for the price. More fields may be added as needed.}}

Revision as of 18:12, 14 February 2010



Template:Documentation/Note

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.

Creating tables.

Using the Wizard to create a table

Documentation caution.png Every table requires a Primary key field. (What this field does will be explained later.) We will use this field to number our entries and want that number to automatically increase as we add each entry.

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.

From the Tasks Pane click Use Wizard to Create Table. This opens the Table Wizard.

Template:Documentation/Note

Top of page

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.

  1. Category: Select Personal. The Sample Tables drop down list changes to a list of personal sample tables.
  2. Sample Tables: Select CD-Collection. The Available fields window changes to a list of available fields for this table.
  3. 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.
  4. 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.
  5. 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 to move the field name to the correct position. Click Next.
Order of fields.
Documentation caution.png Below the Selected Fields list are two buttons: one with a +, and one with a -. These buttons are used to add or to remove fields from the Selected Fields list. Be careful when using these buttons until well acquainted with how to create tables.

Top of page

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. Click each field, one at a time, and make the changes listed below.

Changing field types.

Template:Documentation/Note

  • 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.

Template:Documentation/Note

  • 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 to Yes.
  • Notes: No changes are required.
  • NumberofTracks: Change the Field Type to Tiny Integer [TINYINT]. Your allowable number of tracks will be 999. Small Integer [SMALLINT] would allow 99999 tracks if you needed more than 999 tracks.
  • Photo: Use the default settings.

When you have finished, click Next.

Template:Documentation/Note

Top of page

Step 3: Set primary key.

  1. Create a primary key should be checked.
  2. Select option Use an existing field as a primary key.
  3. In the Fieldname drop down list, select CollectionID.
  4. Check Auto value if it is not already checked.
  5. Click Next.

Template:Documentation/Note

Top of page

Step 4: Create the table.

  1. If desired, rename the table at this point. If you rename it, make the name meaningful to you. For this example, make no changes.
  2. Leave the option Insert data immediately checked.
  3. 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.

Top of page

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.

  1. Click on the Tables icon in the Database pane to see the existing tables.
  2. Right-click on the CD-Collection table icon. Select Copy from the context menu.
  3. Move the mouse pointer below this table, right-click, and select Paste from the context menu. The Copy table window opens.
  4. Change the table name to Pop and click Next.
  5. Click the >> button to move all the Fields from the left window to the right window and click Next.
  6. 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.
Documentation caution.png Once tables have been created using the wizard, editing them should be very limited if any editing is done at all. Fields can be added or deleted. But adding a field requires taking the time to enter the data for that one field for every record having an entry for that field. Deleting a field deletes all the data once contained in that field. Changing the field type of a field can lead to data being lost either partially or completely. When creating a new table, it pays to create the fields with the correct names, length and format before data is added.
Documentation caution.png Tables can be deleted in a very simple way. But doing so removes all of the data contained in every field of the table. Unless you are sure, do not delete a table.

To delete a table, right-click it in the list of tables. Select Delete from the context menu. A popup window asks if you are sure you want to delete the table. Once you click Yes, the table and its data are gone forever unless you have a backup.

Top of page

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.

Template:Documentation/Note

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 use the number format with 3 decimal places and 1 decimal place respectively. PaymentType uses the text format.

  1. Click Create Table in Design View.
  2. FuelID entries:
    1. Enter FuelID as the first Field Name.
    2. Select Integer [INTEGER] as the Field Type from the dropdown list. (The default setting is Text[VARCHAR].)
    3. Template:Documentation/Tip

    4. Change the Field Properties in the bottom section. Change AutoValue from No to Yes.
    5. Field Properties section (AutoValue).
    6. Set FuelID as the Primary key. Right-click on the green triangle to the left of FuelID.
    7. Primary key field.

      Click Primary Key in the context menu. This places a key icon in front of FuelID.

    Template:Documentation/Note

  3. All other entries:
    1. Enter the next field name in the first column (Field Name column).
    2. 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.)
    3. Select the Field Properties.
    4. Field Properties section.
      • FuelCost, FuelQuantity, and Odometer need changes in the Field Properties section.
        • 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.
        • Field Format options.
      • Use Currency as the Category and your currency as the Format. My currency has two decimal places. Use what is appropriate for your currency.
  4. 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).
  5. Description can be anything, or can be left blank. (The figure below is an example of this.)
  6. To save and close the table, select File > Close. Name the table Fuel.
  7. Example of Description entries.

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. (Right click the Grey box to the left of Date. Select Primary key from the context menu.) Name the table Vacations, and save it.

Top of page

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.

Documentation caution.png You must create these tables with the information field listed first and the and the ID field listed last. Failure to do so will produce the wrong results. For my Payment table, I use Name and ID as my fields, with Dan, Kevin, and Cash being the Name entries. The corresponding ID entries are 0, 1, 2. When the Name field is listed first in the table, one of the three names will appear in the payment field of the Fuel table. If the ID field is listed first, 0, 1, or 2 appear in the payment field instead.
  1. 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.
  2. Save the table using the name Payment Type.
Table in Design View.

Template:Documentation/Note

Top of page

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.

  1. In the main database window, click on the Tables icon. Right-click on Payment Type and select Open from the context menu.
  2. Enter Dan in the first row. Use the tab key to move to the second row.
  3. Enter Kevin in the second row.
  4. Enter Cash in the third row.
  5. Save and close the table window.

Template:Documentation/Tip

Template:Documentation/Note

Top of page


Content on this page is licensed under the Creative Common Attribution 3.0 license (CC-BY).
Personal tools