Documentation/OOoAuthors User Manual/Getting Started/Getting Started with Base

From Apache OpenOffice Wiki
Jump to: navigation, search

Template:NeedsWork This page was created by converting ODT to Mediawiki using Writer2MediaWiki.

This is the first part of Chapter 10 of Getting Started with OpenOffice.org 2.x (Third edition), produced by the OOoAuthors group. A PDF of this chapter is available from the OOoAuthors Guides page at OpenOffice.org.

This chapter is continued here:

<< User Manuals page
<< Getting Started Table of Contents
<< Chapter 9 Getting Started with Impress   |   Chapter 11 Getting Started with Math >>


Introduction

A data source, or database, is a collection of pieces of information that can be accessed or managed by OpenOffice.org (OOo). For example, a list of names and addresses is a data source that could be used for producing a mail merge letter. A shop stock list could be a data source managed through OOo.

Note: OpenOffice.org uses the terms "Data Source" and "Database" to refer to the same thing, which could be a database such as MySQL or dBase or a spreadsheet or text document holding data.

This chapter covers creating a database, showing what is contained in a database and how the different parts are used by OOo. It also covers using the Base component of OOo to register other data sources. A data source can be a database, spreadsheet, or text document.

Data sources are only introduced in this chapter. For more detailed information about the use of databases, see the Database Guide (not yet written).

Note: OOo Base uses the HSQL database engine. All of the files created by this engine are kept in one zipped file. The database forms are included in this zipped file.

A database consists of a number of fields that contain the the individual pieces of data. Each table of the database is a group of fields. When creating a table, you also determine the characteristics of each field in the table. Forms are for data entry into the fields of one or more tables associated with the form. They can also be used for viewing fields from one or more tables associated with the form. A query creates a new table from the existing tables based upon how you create the query. A report organizes the information of the fields of a query in a document according to your requirements.

Caution: The database in OOo requires Java Runtime Environment (JRE). If you do not have it on your computer, you can download it from www.java.com and install it following the instructions on the site. It should be Java 5.0 or higher. In OOo, use Tools > Options > OpenOffice.org > Java to register Java. Window's version of JRE can not be used, while there are other versions that can.

Base creates relational databases. This makes it fairly easy to create a database in which the fields of the database have relationships with each other.

For example: Consider a database for a library. It will contain a field for the names of the authors and another field for the names of the books. There is an obvious relationship between the authors and the books they have written. The library may contain more than one book by the same author. This is what is known as a one-to-many relationship: one author and more than one book. Most if not all the relationships in such a database are one-to-many relationships.

Consider an employment database for the same library. One of the fields contains the names of the employees while others contain the social security numbers, and other personal data. The relationship between the names and social security is one-to-one: only one social security number for each name.

If you are acquainted with mathematical sets, a relational database can easily be explained in terms of sets: elements, subsets, unions, and intersections. The fields of a database are the elements. The tables are subsets. Relationships are defined in terms of unions and intersections of the subsets (tables).

To explain how to use a database, we will create one for automobile expenses. In the process, we will be explaining how a database work.

Creating a database

The first step in creating a database is to ask yourself many questions. Write them down, and leave some space between the questions to later write the answers. At least some of the answers should seem obvious after you take some time to think.

You may have to go through this process a few times before everything becomes clear in your mind and on paper. Using a text document for these questions and answers makes it easier to move the questions around, add additional questions, or change the answers.

Here are some of the questions and answers I developed before I created a database for automobile expenses. I had an idea of what I wanted before I started, but as I began asking questions and listing the answers, I discovered that I needed additional tables and fields.

What are the fields going to be? My expenses divided into three broad areas: fuel purchases, maintenance, and vacations. The annual cost for the car's license plate and driver's license every four years did not fit into any of these. It will be a table of its own: license fees.

What fields fit the fuel purchases area? Date purchased, odometer reading, fuel cost, fuel quantity, and payment method fit. (Fuel economy can be calculated with a query.)

What fields fit the maintenance area? Date of service, odometer reading, type of service, cost of service, and next scheduled service of this type (for example, for oil changes list when the next oil change should be). But it would be nice if there was a way to write notes. So, a field for notes was added to the list.

What fields fit the vacations area? Date, odometer reading, fuel (including all the fields of the fuel table), food (including meals and snacks), motel, total tolls, and miscellaneous. Since these purchases are made by one of two bank cards or with cash, I want a field to state which payment type was used for each item.

What fields fit into the food category? Breakfast, lunch, supper, and snacks seem to fit. Do I list all the snacks individually or list the total cost for snacks for the day? I chose to divide snacks into two fields: number of snacks and total cost of snacks. I also need a payment type for each of these: breakfast, lunch, supper, and total cost of snacks.

What are the fields that are common to more than one area? Date appears in all of the areas as does odometer reading and payment type.

How will I use this information about these three fields? While on vacation, I want the expenses for each day to be listed together. The date fields suggest a relationship between the vacation table and the dates in each of these tables: fuel and food, This means that the date fields in these tables will be linked as we create the database.

The type of payment includes two bank cards and cash. So, we will create a table with a field for the type of payment and use it in list boxes in the forms.

Tip: While we have listed fields we will create in the tables of the database, there is one more field that may be needed in a table: the field for the primary key. In some tables, the field for the primary key has already been listed. In other tables such as the payment type, an additional field for the primary key must be created.

Creating a new database

NewDB.png

To create a new database, click the arrow next to the New icon. In the drop-down menu, select Database (Figure 1). This opens the Database Wizard. You can also open the Database Wizard using File > New > Database.

The first step of the Database Wizard has one question with two choices: Create a new database or Connect to an existing database. For this example, select Create a new database and then click Next.

The second step has two questions with two choices each. The default choice for the first question is Yes, register the database for me and the default choice for the second question is Open the database for editing. Make sure these choices are selected and click Finish.

Note: If the database is not registered, it will not be accessible to the other OOo components such as Writer and Calc. If the database is registered, other components can access it.

Save the new database with the name Automobile. This opens the Automobile - OpenOffice.org Base window. Figure 2 shows part of this window.

Tip: Every time the Automobile database is opened, the Automobile - OpenOffice.org Base window opens. Changes can then be made to the database. The title for this window is always (database name) - OpenOffice.org.

Caution: As you create a database, you should save your work regularly. This means more than just saving what you have just created. You must save the whole database as well. For example, when you create your first table, you must save it before you can close it. If you look at the Save icon in the Standard toolbar at the top after closing the table, it will be active. Click the Save icon, and this icon will be grayed out again. Not only the table has been saved, but it also been made a part of the database.

Creating database tables

Note: In a database, a table stores information for a group of things we call fields. For example, a table might hold an address book, a stock list, a phone book or a price list. A database can have from one to several 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).

Figure 2
Figure 2: Creating tables.

Using the Wizard to create a table

Caution: 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.

Click Use Wizard to Create Table. This opens the Table Wizard.

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.

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 (see Figure 3) to move the field name to the correct position. Click Next.
inline:Frame11.png

Caution: 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 (Figure 3).

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.

Figure 4
Figure 4: Changing field types.

Note: If any of these fields requires an entry, set Entry required to Yes. If Entry required is set to Yes, this field must have something in it. For example if FirstName has Entry required set to Yes, having an entry with the first name missing will not be allowed. In general, only set Entry required to Yes if something must always be put in that field. By default, Entry required is set to No.

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

Note: In Base the maximum length of each field must be specified on creation. It is not easy to change this later, so if in doubt specify a greater length. Base uses VCHAR as the field format for text fields. This format only uses the actual number of characters in a field up to the limit set. So, a field containing 20 characters will only use space for 20 characters even if the limit is set at 100. So, two album titles containing 25 and 32 characters respectively will use space for 25 and 32 characters and not 100 characters.

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

Note: Each field also has a Field Type. In Base the field type must be specified. These types include text, integer, date and decimal. If the field is going to have general information in it (for example a name or a description) then you want to use text. If the field will always contain a number (for example a price) the type should be decimal or another appropriate numerical field. The wizard picks the right field type, so to get an idea of how this works, see what the wizard has chosen for different fields.

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 Fieldname drop down list, select CollectionID.
  4. Check Auto value if it is not already checked.
  5. Click Next.

Note: A primary key uniquely identifies an item (or record) in the table. For example, you might know two people called "Randy Herring" or three people living at the same address and the database needs to distinguish between them. The simplest method is to assign a unique number to each one: number the first person 1, the second 2 and so on. Each entry has one number and every number is different, so it is easy to say "record ID 172". This is the option chosen here: CollectionID is just a number assigned automatically by Base to each record of this table. There are more complex ways of doing this, all answering the question "How do I make sure that every single record in my database can be uniquely identified?"

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.

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.

Caution: Once tables have been created using the wizard, editing them is limited. The Primary key can not be changed in any way. It is possible to add new fields and remove fields. It is possible to change the field type when creating the field as well as later as long as it is not the primary key. Once data has been added to the database, deleting a field will also delete any data contained in that field. When creating a new table, it pays to create the fields with the correct names, length and format before data is added.

Caution: 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 pop up 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.

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.

Note: 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.

  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. Tip: Shortcut for selecting from the Field Type dropdown list: use the key for the first letter of the choice. This might require using the letter more than once to get the choice you want. You can cycle through the choices for a given letter by repeatedly using that letter. After typing the name of the field in the Fields column, use the Tab key to move to the Field Type column. This will enter the field name and highlight the dropdown list. You can then use the key for the first letter of your choice to select the field type. Just remember to use it the correct number of times if necessary.

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

      Figure 6
      Figure 6: Primary key field.

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

    Note: The primary key serves only one purpose. Any name can be used for this field. It is not necessary to use FuelID as the name of the primary key field. We have used it so we know to which table it belong by its name.

  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 (Figure 7).
    4. Figure 7
      Figure 7: Field Properties section.
      • 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).
        • Figure 8
          Figure 8: Field Format options.
      • Use Currency as the Category and anything in the Format list with two decimal places.
  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. (Figure 9 is an example of this.)
  6. To save and close the table, select File > Close. Name the table Fuel.
  7. Figure 9
    Figure 9: 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, 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.

  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. (See Figure 10.)
  2. Save the table using the name Payment Type.
Figure 10
Figure 10: Table in Design View.

Note: 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.

  1. In the main database window, click on the Tables icon (Figure 2). 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.

Tip: The Enter key can also be used to move from field entry to field entry. For this example, enter Jan. in the first Name field. Enter moves the cursor to the ID field. Enter then moves the cursor to the second Name field. The Down Arrow key can also be used to move from row to row.

Note: The PaymentID field contains <AutoField> until you use the Enter key to move to the second row. Then it becomes a 0. As you add the entries to each row, the rows of the PaymentID field change to consecutive whole numbers. For example the first three numbers in this field are 0,1,2.

Creating a View

A View is a query. Because of this, the details of how to create and use a View are in the Creating queries section.

Figure 11
Figure 11: View of some fields from the Vacations table.

A View is also a table. Its fields come from the fields of one or more tables of the database. It provides a way to look at a number of fields without regard to the table to which any of the fields belong. A View can consists of some of the fields on one table as in Figure 11. Or, it can consist of fields from more than one field as in Figure 12.

Figure 12
Figure 12: View of fields from the Fuel and Payment Type tables.

Caution: Data can not be entered into a View like it can be added to a table. It is strictly for viewing data which has already been added.

Defining relationships

Now that the tables have been created, what are the relationships between our tables? This is the time to define them based upon the questions we asked and answered in the beginning.

When on vacation, we want to enter all of our expenses all at one time each day. Most of these expenses are in the Vacations table, but the fuel we buy is not. So, we will relate these two tables using the Date fields. Since the Fuel table may have more than one entry per date, this relationship between the Vacations and Fuel tables is one to many. (It is designated 1:n.)

The Vacations tables also contains several fields for the type of payment used. For each field listing the payment type, there is only one entry from the Payment Type table. This is a one to one relationship: one field in one table to one entry from the other table. (It is designated 1:1.) Other tables also contain fields for the type of payment. The relationship between these fields of those tables and the Payment Type table are also 1:1.

Since the Payment Type table only provides a static list, we will not be defining a relationship between the Payment Type table and the fields of the other tables which use the entries of the Payment Type table. That will be done when the forms are created.

The Fuel and Maintenance tables do not really have a relationship even though they share similar fields: Date, and Odometer. Unless a person is in a habit of regularly getting fuel and having their vehicle serviced, the entries in these tables do not share anything in common.

Tip: As you create your own databases, you need to also determine where tables are related and how.

inline:Frame35.png

We begin defining relationships by Tools > Relationships. The Automobile - OpenOffice.org Base: Relation design window opens (Figure 13). The icons we will use are Add Tables and New Relation.

Click the Add Tables icon. The Add Tables window opens.

  1. There are two ways to add a table to the Relation design window.
    • Double-click the name of the table. In our case, do this for both Vacations and Fuel.
    • Or, click the name of the table and then click Add.
  2. Click Close when you have added the tables you want (Figure 14).
Figure 14
Figure 14: Added table lists.

Defining the relationship between the Vacations and Fuel tables

Two ways exist to do this:

  • Click and drag the Date field in the Fuel table to the Date field in the Vacations table. When you release the left mouse button, a connecting line forms between the two date fields (Figure 15).
  • Figure 15
    Figure 15: Designation for a 1:n relationship.
  • Or, click the New Relation icon. This opens the Relations window (Figure 16). Our two tables are listed in the Tables involved section.
    • In the Fields involved section, click the dropdown list under the Fuel label.
    • Figure 16
      Figure 16: Setting the relationship between tables.
    • Select Date from the Fuel table list.
    • Click in the cell to the right of this dropdown list. This opens a dropdown list for the Vacations table.
    • Select Date from the Vacations table list. It should now look like Figure 17.
    • Click OK.
    • Figure 17
      Figure 17: Selected fields in a relationship.

Modifying the Update options and Delete options section of the Relation window

  1. Right-click the line connecting the Date fields in the two table lists to open a context menu.
  2. Select Edit to open the Relation window (Figure 18).
  3. Select Update cascade.
  4. Select Delete cascade.
Figure 18
Figure 18: Update options and Delete options section.

While these options are not absolutely necessary, they do help. Having these options selected permits you to update a table that has a relationship defined with another table. It also permits you to delete a field from the table.


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