Archive for the ‘Fields’ Category
>Creating Tables
>
In the last installment we explored the many to many relationships that exist between our various table. At that point we had not yet created the tables. This article will deal with the nuts and bolts of actually creating a table.
A word about names
The names you use when you create your database objects are important and should follow certain guidelines. Access reserves certain words for its own use. Logically these words are known as reserved words. Unfortunately some of these words are the first that comes to mind when you are trying to name fields for example. In this article I discussed at a conceptual lever, the tables we need in the Ice Cream Parlour database. If you look back to that article you will see names like ‘Name”, “Description”, and “Type”. These words all have special meaning within Access and should not be used to name Access objects.
So you will notice that the field names in the relationship diagrams in my last article are different from those I used in the conceptual diagrams. So, for example I have a field ‘strSundaeName’ rather than simply ‘Name’. What about the ‘str.’ That part of the name comes from a convention I follow to add a prefix to the name that indicates the datatype of the field. Many Access developers do not use prefixes for field names. So SundaeName would be perfectly acceptable and, in fact, preferred by many developers.
On the other hand, many developers do follow a naming convention that prefixes the names of Access objects like tables, queries, forms and reports, with three characters to signify the type of object to which the name pertains. So, for example, the Sundaes table is actually called ‘tblSundaes’.
Notice also that tables are usually plural. That is because the table contains records where each record is one example (or instance) of the subject of the table. So, tblSundaes has fields with names like strSundaeName (or SundaeName, if you prefer.)
Creating a table
There are several ways to create a table in Access. Although I don’t recommend it, you can design a table by entering data into a datasheet. Access will determine the data type for each field based on the data you enter. And unless you are careful to name each field, Access will assign such helpful field names as Field1, Field2, etc.
In Access versions prior to 2007 there are table creation wizards and in 2007 there are table templates to help you out. Unfortunately, tables created by wizards or from Access 2007 templates have field names that do not follow accepted naming standards. They include spaces in the field names. Now, while this may seem perfectly natural to you if you are an Access beginner, those spaces will come back to haunt you when you decide to enhance your work using advanced features like Visual Basic for Applications (VBA).
I prefer to create all tables using the Table Design View. Before we go there, however, we should map out the fields we will need and determine what their respective data types should be. For the sake of illustration I am going to deal with creating one of the tables we have decided we need. If you are following along and creating your own ice cream parlour database, the best way for you to learn will be for you to create the other tables on your own. I will be providing a link to my version of the database with all tables created.
Before we look at the table designer, let’s summarize the information we will need for each table. I am indicating two possible names for each field. The first name follows the naming convention I prefer to use. The second is a valid name in a format that omits the data type prefix. Whichever style you choose, it is important to be consistent throughout the database.
Sundaes – tblSundaes
|
attribute |
field name |
alternate name |
data type |
data size |
notes |
| Primary Key | idsSundae | SundaeID | Autonumber | long integer |
|
| Name | strSundaeName | SundaeName | Text | 25 |
Dishes – tblDishes
|
attribute |
field name |
alternate name |
data type |
data size |
notes |
| Primary Key | idsDish | DishID | Autonumber | long integer |
|
| Name | strDishName | DishName | Text | 25 | |
| Type | lngDishType | DishType | Number | long integer |
foreign key – used to find the dish type description in the types lookup table |
Sizes – tblSizes
|
attribute |
field name |
alternate name |
data type |
data size |
notes |
| Primary Key | idsSize | SizeID | Autonumber | long integer |
|
| Name | strSizeName | SizeName | Text | 25 | |
| Type | lngSizeType | SizeType | Number | long integer |
foreign key – used to find the dish type description in the types lookup table |
Flavors – tblFlavors
|
attribute |
field name |
alternate name |
data type |
data size |
notes |
| Primary Key | idsFlavor | FlavorID | Autonumber | long integer |
|
| Name | strFlavorName | FlavorName | Text | 25 |
Ingredients – tblIngredients
|
attribute |
field name |
alternate name |
data type |
data size |
notes |
| Primary Key | idsIngredient | IngredientID | Autonumber | long integer |
|
| Name | strIngredientName | IngredientName | Text | 25 | |
| Type | lngIngredientType | IngredientType | Number | long integer |
foreign key – used to find the dish type description in the types lookup table |
Those are the basic definitions of our five central table. As already noted, we will need additional tables in the database to allow us to connect these central tables together so that the database reflects the one to many relationships between them. We will also need a lookup table for types the type descriptions used by tblDishes, tblSizes, and tblIngredients.
Coming next: Using the table designer.
>Tables for the Ice Cream Parlor
>
Just as a real ice cream parlor needs tables where our patrons can sit and enjoy out products, our database model of ice cream sundaes will need tables in which to store information about our products.
In the previous installment of this series, we fleshed out the list of tables database will need. These are our main tables:
- ingredients
- dishes (containers)
- sizes
- sundaes
- flavors
Because we will be combining ingredients in various ways with various container sizes and ingredients actually come in several flavors, we will need some additional tables to help us model the combinations. These tables will include:
- sundae ingredients
- ingredient flavors
- sundae sizes
- container sizes
- sundae containers
Now it is time to determine what fields to include in each table. You may be starting to find this list just a little overwhelming. Try not to panic. It is really not all that bad as long as you tackle things a little bit at a time. So we will move ahead, slowly but confidently, one table at a time. For now, let’s focus on the five central tables mentioned above.
Table definition refers to identifying the fields that are needed to describe the subject (or domain) of each table. So when I think about a table, I start to think about the kinds of information I need to describe the subject of the table.
We will keep these basic guidelines in mind:
- each table describes only a single subject (entity)
- the fields included in each table should describe the subject of the table (attributes).
- data will be subdivided across multiple tables in order to
- avoid duplication of data, and
- avoid repeating groups of columns
I find that, in a database as focused on a single business solution as the ice cream sundae database is, a single table emerges as the central table at the heart of the application. Other tables in the database are needed only because they will have information that pertains to the central table.
So, for us, the central table appears to be the sundaes table. Let’s start there. What information do I need to store to describe each sundae? Obviously each sundae needs a name. It will also need ingredients and, since I plan to offer each sundae type in a variety of sizes, it will need size information.
However, storing size and ingredient data in the main sundaes table will lead to immediate design problems. While I might be certain ahead of time exactly how many sizes I will be offering and the maximum number of flavors might be used in a sundae, including that information directly in the sundaes table will break one of our basic guidelines. I would have to have fields like flavor1, flavor2, flavor3, ingredient1, ingredient2, ingredient3. In database terms these would be examples of repeating columns.
So, what’s the problem? Although not apparent at the outset of planning, repeating columns make retrieving useful information from stored data, unnecessarily complex. Even more important, repeating columns put serious limits on the future use of the database. No matter how certain you are ahead of time that you are describing how something is now and will ever remain the same, it is just not that easy to predict the future. If I "know" that I will only ever offer three sizes of sundae, small, medium, and large, the database (if I have included three size fields in the sundaes table) would not be able to handle changes I might have to make in order to remain competitive. Say, for example, that upstart competitor down the street starts stealing my customers because she has added a fourth or fifth size.
I have tried to describe the process by which I have determined what fields belong in the sundaes table. The process for deciding what fields to include each of the other main tables is the same. The following table shows the results of this analysis:
| sundaes | dishes | sizes | flavors | ingredients |
| Name | Name | Name | Name | Name |
| Type | Type | Type | ||
| Description | Description |
You may find that the list of fields in each table is somewhat short. Each table includes a name field. The sundaes and flavors tables don’t have any other fields. That’s because most of how we describe sundaes, for example, is stored in other tables. Flavor names, on the other hand are pretty much self-descriptive.
The dishes, sizes and ingredients tables have type fields. We will use these fields to categorize dishes, sizes and ingredients, respectively. Dishes may be either edible, disposable (plastic for takeout) or reusable (glass or metal for eat-in.) Sizes may refer, for example to quantity when referring to ingredients, to capacity when referring to dishes, or to the end-product size description (small, medium, large, for example) when referring to sundaes. Ingredients may be principal ingredients, toppings, or garnishes, for example.
Now we have a working definition of our main tables. In the next article we will define the additional tables we will need to flesh out the connections needed between the main tables.
>Let’s Build Some Tables
>
In the last article we looked at the relationships that exist between the various products we will be using to create ice cream sundaes. These ‘real world’ relationships dictate the relationships we will need to establish between the tables of the database.
In this article we are going to look at what information we should be including in each table. Before doing that however, there is at least one more table that we should include in the database, flavors. Since each flavor can apply to many ice creams and some ice creams have multiple flavors, the
relationship between ingredients and flavors is many to many. It is possible that some other ingredients may be multi-flavored as well but one multi-flavored ingredient is enough to make the relationship many to many.
It’s important to define the relationships before finalizing table design because one to many and many to many relationships require different table structures. For a one to many relationship, you include one field in the ‘many’ table to identify the ‘one’ table record to which each ‘many’ record belongs. Many to many relationship require this information to be stored in an separate table with one field for each of the partner tables.
Guess what!! We have just have just identified the need for several additional tables in our ‘simple’ ice cream sundae database. We will need one table for each of the many to many relationships. One reason for doing detailed planning and design before actually beginning to build the actual database is to try to uncover and resolve any possible data management problems and to ensure that all the database will include all information necessary to fulfill the database’s stated purpose.
In Know Your Data (Part 3) we defined the purpose of our ice cream sundae database:
This database stores information about the quantity of contents used to make ice cream sundaes. It assists with the design of new sundaes and provides reports of the ingredients required to make each type of sundae.
We have also defined a list of tables that we will need to manage the data:
- ingredients
- dishes (containers)
- sizes
- sundaes
- flavors
Now it is time to decide what data needs to be included in each table. Before we do that we should review some terms and principles required for good relational table design.
- Each table should contain information about only one topic or subject (formally known as an entity.
- All examples of a topic should be included in one table (if you have are dealing with people and occupations, you would have one people table, not separate tables for each occupational group, for example.)
- Each table has one or more fields that describe the subjects or topics stored in the table.
- Each table should have a primary key that uniquely identifies each record in the table. (I am going to leave for another time the discussion of the alternative types of primary keys.)
- A record is the collection of fields that describe one of the topics of the table.
- Data should be stored only once.
There is a set of rules known as the forms of normalization that are the guiding principles for relational database design. If you a new to database design, your initial efforts will most likely run afoul of normalization so don’t be surprised when the response to questions you may ask an experienced developer about designing forms or reports refers you back to normalization issues in your basic database design.
In case you haven’t noticed, we have yet to do anything with Access itself. That is because knowing how to use Access is only part of the story in building a successful database for personal or small enterprise use. We will be using Access to build and run the database but we can’t build it until we have a solid design.
In the next article we will (finally) design our the tables that we will have Access manage for us.
