Project: Setting Up Your First Database
We’ll be setting up a database in eight easy steps:
- Getting started
- Creating tables
- Creating fields
- Creating relationships
- Creating a layout
- Testing your solution
- Sharing FileMaker databases on a network
- Converting and normalizing a spreadsheet
Step 1: Getting Started
To begin, choose New Database from the File menu. Give it a name that makes sense.
Each of the projects in Part II has its own database requirements, so you need to know your way around the basics of the Define Database dialog (accessible from the File menu). It opens as soon as you have named your database.
A FileMaker database (starting with FileMaker 7) can contain a number of database tables. As with other relational database managers, FileMaker stores its data in tables that have rows and columns (sometimes called records and fields). A FileMaker database file can contain all of your solution’s tables or none of them. In addition to tables, a database file contains scripts, layouts, and security controls. These can apply to and be used by any of the tables in the database file. In addition, you can share database files and their tables.
Step 2: Creating Tables
By default, a single table is created in your new database file. Its name is the name of the database file. You can delete this table or rename it if you want to create a table with another name. The database file used in the first three chapters of this book contains two tables, shown in Figure 3.1. (The figures in this section show FileMaker Advanced. The buttons that enable you to copy, paste, and import tables are not available in FileMaker Pro.)
Figure 3.1 Create tables in the Define Database dialog.
Step 3: Creating Fields
To start creating fields, click the Fields tab in the Define Database dialog and select the table in which you want to create fields from the drop-down menu at the upper left of the window. Each field should have a meaningful name, and if there is any ambiguity, add a comment to the field name at the bottom of the window. Figure 3.2 shows the Fields tab in the Define Database dialog.
Figure 3.2 Define fields in your table.
Choose the type of field you want to use. A good rule of thumb is to choose the most specific type of field you can. If you are storing numbers in a field, FileMaker is able to convert the contents of a text field to numbers as needed, but you can’t apply numeric formatting to the text field. In order to use numeric formatting (as well as numeric validation for data entry), define numeric fields as numeric. Likewise, define dates as dates and times as times.
As shown in Figure 3.3, you can switch between the options for fields and their comments by clicking the header of the right column. You also can change the order in which fields are shown.
You will see how to use the types of fields in the later projects.
Step 4: Creating Relationships
Each table you have created is shown in the Relationships tab of the Define Database dialog, as you can see in Figure 3.4.
Figure 3.3 Add comments to field names where appropriate.
Figure 3.4 Use the Relationships tab to manage tables in your database.
Relationships are defined to link records in one table with records in another table; they are drawn as lines on the relationship graph shown in Figure 3.5. One of the most frequent reasons to do this is if the number of records in one table for a given entity differs from the number in another table. For example, you might have a table containing categories of items in a collection. Each category can have several inventory items associated with it. If you try to squeeze all of the inventory information into the categories table (perhaps as item1, item2, and so forth), you will encounter problems. Instead, the preferred method is to assign a unique ID to each category and to use it as the related field for a table containing inventory items. Each inventory item has a Category ID field that is related to the zID field in the category table. (The use of the name zID for ID fields is common; because the field is used internally, it is prefixed with z so it appears at the bottom of alphabetized field listings, below the more visible fields such as name, price, address, and so forth.)
By using this relationship, you are able to get from any given inventory item to the related category information (Category Name, for example). Relationships go in the other direction, too. From a given category you can get to that category’s inventory items. This is precisely the situation that will be explored in the Collections Database project in Chapter 5, "Managing One-to-Many Relationships." Figure 3.5 shows its relationship graph; as you can see, in addition to the tables in the relationship graph, there are now lines indicating the relationship.
Figure 3.5 Lines indicate relationships.
This structure can be replicated in many levels. For example, a category may have many inventory items, and an inventory item may belong to many categories. To implement this, you might have three tables that are related to one another:
- zID in the Inventory table is related to Category ID in the Categories table as shown in Figure 3.5.
- ID in the Categories table is related to Inventory ID in a new table called InventoriesAndCategories. This structure allows you to have many categories assigned to a single inventory item and many inventory items assigned to a category. This type of relationship is called a many-to-many relationship; it is the basis of the Boutique Manager project in Chapter 6, "Managing Many-to-Many Relationships."
As you will see, you implement these relationships by drawing lines from one table to another. Relationships can be quite complex; they need not be equal. You can have a relationship of all orders with balances over a certain amount; you can use dates as the basis of relationships. The projects in this book show a wide variety of relationships.
The process of organizing data into a set of related database tables is called normalization. Rules are available that can guide you through the process, but as you will see later, you can be guided just as easily by common sense and by making certain that your data is not duplicated. (The process of normalizing a spreadsheet is described in the "Converting and Normalizing a Spreadsheet" section later in this chapter.)
For now, you just need to know that you can create tables, fields, and relationships with the Define Database dialog and that you will use it in each of the projects of in this book.
Step 5: Creating a Layout
Your first layout needs to have data entry fields for the fields in your table. It is not hard to create the layout: FileMaker will do it by default. As soon as you have finished entering the names of your fields into the Define Database dialog, you will have a layout you can use to enter and display data.
Step 6: Testing your Solution
At every step of the development process, test your solution. Test it to see that it does what you want it to do. Then go back and test it to see if it fails properly. This is one of the most common problems: Your solution works right if you enter the right data, but sometimes you (or someone else) enter something that is wrong. Do you accommodate human frailty and data errors?
One tip for testing any software is to have someone else do the testing (just as you have someone else proofread what you have written).
Step 7: Sharing FileMaker Databases on a Network
Along with security, it is hard to retrofit networking and data sharing after you have designed a solution (this applies to any software, not just FileMaker solutions). If you are certain that your data will never be shared, you do not have to worry about networking. However, it is safest to consider the possibility of networking and data sharing so that, even if you do not need it now, you can use it in the future.
Three ways of sharing FileMaker databases on networks are
- FileMaker sharing
- FileMaker Server
- Web sharing
Using FileMaker Sharing
If you have a network, you can share FileMaker databases over it. You can turn on sharing from one copy of FileMaker and connect to it from another copy of FileMaker on that network. Because you can locate FileMaker by using a network IP address, this sharing can even happen over the Internet, provided that you know the IP address of the computer on which FileMaker is running.
In order to connect to a shared database, use the Open Remote command from the File menu; you will see the copies of FileMaker that are sharing databases on your local network. You can also type in the IP address of a FileMaker computer that is not on your network.
This type of sharing is appropriate for a small number of users; the software limits that number to five.
Using FileMaker Server
For more than five users, you need to use FileMaker Server. This is software that runs on a computer (it need not be a specific server computer, but that is often preferred), and it does nothing except share FileMaker databases. It manages users, can control backups, and enables you to open and close databases. However, FileMaker Server does not enable you to create or edit databases as FileMaker Pro does.
FileMaker Server has no visible interface; you use FileMaker Server Admin to manage the FileMaker Server software. It is often desirable to install FileMaker Server Admin on one or more computers on a network other than the computer where FileMaker Server itself is running. That gives you access to FileMaker Server itself without needing to physically touch the server computer which might be locked away in a secure location.
Using the Web
You can share databases using Web technology. FileMaker supports Instant Web Publishing that puts your databases on the Web so you can use a web browser to access the databases. With Instant Web Publishing, what you see in a browser is very close to what people see using FileMaker itself as a client.
You can exert additional control over your web databases by using Custom Web Publishing with FileMaker Server Advanced, which relies on XML/XSLT, two industry-wide standards for web publication. Instant Web Publishing is discussed in Chapter 11, "Implementing a Simple Web Publishing Solution"; Custom Web Publishing is not.
Step 8: Converting and Normalizing a Spreadsheet
Here is a taste of a very small project that can get you started using the tools described in the three chapters of this part of the book. The process of building tables and relationships that is summarized here is explored in detail in the next three chapters.
Convert the Spreadsheet to FileMaker
As described in Chapter 1, a spreadsheet can be downloaded from the U.S. Census Bureau website; it contains population estimates through 2030 by age and sex for each state. It also contains 2000 actual population numbers by state for each age and sex.
To convert it to a FileMaker database, you can simply drag it onto the FileMaker icon. What you get is a database that contains the spreadsheet data. It looks like a spreadsheet, and, in Table view, it behaves like a spreadsheet.
Rename the Fields
In fact, the table that is created in this way is table File4 of the file File4 normalized.fp7 that is available on this book’s website. There is only one difference between table File4 and the table that is generated by the automatic conversion: The field names in the automatic conversion are named Field 1, Field 2, and so forth.
Open the Define Database dialog and change the names of the fields to more meaningful names. You can tell what they should be because the first few records of the converted file have headings that describe the columns. File4 normalized.fp7 has renamed fields in it. These are names such as Age, Census 2000, Projection 2004, Projection 2005, Region, Sex, and State.
Eliminate Unneeded Data
This step starts to clean up the data. Some blank lines are in the spreadsheet. In addition, there are totals. Those can be calculated easily by FileMaker. Using Find mode, find lines where Age is blank or contains the string Total. Delete these found records.
Normalize the Data
If you start to manipulate the data, you will see that it is awkward to work with the string of fields for each projected year. A lot of repeating data is in the database. Normalizing it as much as possible makes it easier to work with. Instead of putting all of the projections for a state, age, and sex together in one record, you can split up the record. Figure 3.4, shown previously in this chapter, shows the fields of a new table, Data4, that are normalized.
It is worth looking at each of these fields so you understand how normalization works. The idea is that each data element is a separate record. Thus, each record needs to be identified by the Age, Sex, and State fields. Another field, DataName, is used to indicate the year of the projection. Together, these four fields uniquely identify every number on the spreadsheet. (The table Data4 also contains some census identification—FIPS, Division, and Region—that are not needed in the example but have been preserved in the database.) In addition to the fields that identify a data value, you need the value itself—DataValue. This field is a numeric field, not a text field.
The last important field is a summary field, sDataValue. It is a summary that represents the total of all DataValue fields currently displayed. If you experiment with the database, you will see that selecting the records where DataValue contains the string 2000 gives you all of the actual 2000 census records, and the sDataValue field displays the total population of the United States according to the 2000 census.
Finally, there are five fields that many FileMaker database designers automatically add to every database table: Their names start with z (so they appear at the bottom of an alphabetized field list), and they contain timestamp and other information about the record. They can help with debugging. Throughout this book these are referred to as administrative fields, and they appear in almost every table. All are auto-entered by FileMaker, and the option is set to prevent modification by users. As a result, you can rely on these fields containing accurate data:
- zIDUnique serial number generated by FileMaker
- zCreatorUser or FileMaker account name of the creator of the record
- zCreationTSTimestamp of creation
- zModiferUser or FileMaker account name of the previous modifier
- zModificationTSTimestamp of previous modification
After you have the Data4 table created with these fields, you now need to create scripts to normalize the data. The scripts that do this were described in the preceding chapter, and they are provided in the file that you download from the website. You can now go back and see what happens as a long, unnormalized record containing projections for 2004 to 2030 is split into its component data values.
Make a copy of File4 normalized.fp7, and go to the layout that displays Data4 (the new table). From the Records menu, select Show All Records, and then, from the same menu, choose Delete All Records. Data4 is now empty, and you can run the script to convert all of the data. It takes a few minutes (but not many), and all the data is converted.
Experiment in finding all the data for a certain year, state, or sex. Look at the sDataValue display: If the numbers are strange, look for extraneous records. (For example, if they are too big, you have left fields with the total number of records in the database.)
This part of the book has reviewed the basics of FileMaker that you need for the later projects. You might need to look at some of the documentation again or explore the online help if you are uncertain about some areas. If you can work your way through the normalization process described in this section, you are ready to move on to the rest of the projects.