Designing Databases
Data Analysis and Design
After you have analyzed and documented all the tasks involved in a system, you're ready to work on the data analysis and design phase of an application. In this phase, you must identify each piece of information needed to complete each task. You must assign these data elements to subjects, and each subject will become a separate table in the database. For example, a subject might be a client; every data element relating to that client—the name, address, phone number, credit limit, and any other pertinent information—would become fields within the client table.
You should determine the following for each data element:
-
The appropriate data type
-
The required size
-
Validation rules
You should also determine whether you will allow the user to update each data element and whether the user will enter each update or the system will calculate it. Then you can figure out whether you have properly normalized the table structures.
Database Terms Introduced
In the preceding hours we have briefly mentioned some terms that are integrally important now that you will be designing your own objects. The following are the most important of these terms and what they mean:
-
Column or field— A single piece of information about an object (for example, a company name).
-
Row or record— A collection of information about a single entity (for example, all the information about a customer).
-
Table— A collection of all the data for a specific type of entity (for example, all the information for all the customers stored in a database). It is important that each table contain information about only a single entity. In other words, you would not store information about an order in the Customers table.
-
Primary key field— A field or a combination of fields in a table that uniquely identifies each row in the table (for example, the CustomerID).
-
Natural key field— A primary key field that is naturally part of the data contained within the table (for example, a Social Security number). Generally it is better to use a contrived key field, such as an AutoNumber field, than a natural key field as the primary key field.
-
Composite key field— A primary key field comprising more than one field in a table (for example, LastName and FirstName fields). It is preferable to create a primary key based on an AutoNumber field than to use a composite key field.
-
Relationship— Two tables in a database sharing a common key value. An example is a relationship between the Customers table and the Orders table: The CustomerID field in the Customers table is related to the CustomerID field in the Orders table.
-
Foreign key field— A field on the many side of the relationship in a one-to-many relationship. Whereas the table on the one side of the relationship is related by the primary key field, the table on the many side of the relationship is related by the foreign key field. For example, one customer has multiple orders, so whereas the CustomerID field is the primary key field in the Customers table, it is the foreign key field in the Orders table.
Creating Tables
Building a New Table
There are several ways to add a new table to an Access 2003 database: You can use a wizard to help you with the design process, design the table from scratch, build the table from a datasheet (a spreadsheet-like format), import the table from another source, or link to an external table. This hour discusses the processes of building a table from a datasheet and designing a table
Building a Table from a Datasheet
Building a table from a datasheet might seem simple, but it isn't always the best way to build a table because it's easy to introduce severe design flaws into a table. Microsoft added this method of building tables as an "enhancement" to Access—and it is primarily for spreadsheet users getting their feet wet in the database world. I suggest you use one of the other methods to design tables. If you decide to use the datasheet method, though, follow these steps:
-
Select the Tables icon from the list of objects in the Database window.
-
Click the New button. The New Table dialog box appears.
-
Select Datasheet View from the New Table dialog box.
-
Rename each column by double-clicking the column heading (for example, Field1) you want to change or by right-clicking the column and selecting Rename Column from the context menu. Type a name for the field and then press Enter.
-
Enter data into the datasheet. Be sure to enter the data in a consistent format. For example, if a table includes a column for employee hire dates, make sure all entries in that column are valid dates and that you enter all dates in the same format. Access uses the contents of each column to determine the data type for each field, so inconsistent data entry confuses Access and causes unpredictable results.
-
After you have added all the columns and data you want, click the Save button on the toolbar. Access prompts you for a table name and asks whether you want to add a primary key.
-
Access assigns data types to each field based on the data you have entered. When Access is done with this, click the View button on the toolbar to look at the design of the resulting table.
-
Add a description to each field to help make the table self-documenting.
Field Type | Appropriate Uses | Storage Space |
---|---|---|
Data containing text, a combination of text and numbers, or numbers that don't need to be included in calculations. Examples are names, addresses, department codes, and phone numbers. | Based on what's actually stored in the field; ranges from 0 to 255 bytes | |
Long text and numeric strings. Examples are notes and descriptions. | Ranges from 0 to 65,536 bytes. |
The All-Important Primary Key
Creating Queries
Refining a Query by Using Criteria
So far in this hour, you have learned how to select the fields you want and how to indicate the sort order for query output. One of the important features of queries is the ability to limit output by using selection criteria. Access allows you to combine criteria by using several operators to limit the criteria for multiple fields. Table covers the operators and their meanings.
Operator | Meaning | Example | Result of Example |
---|---|---|---|
= | ="Sales" | Finds only records with "Sales" as the field value. | |
< | <100 | Finds all records with values less than 100 in that field. | |
<= | <=100 | Finds all records with or equal tovalues less than or equal to 100 in that field. | |
> | >100 | Finds all records with values greater than 100 in that field. | |
>= | >=100 | Finds all records with or equal to values greater than or equal to 100 in that field. | |
<> | <>"Sales" | Finds all records with values other than Sales in the field. | |
And | Created by adding criteria on the same line of the query design grid to more thanone field | Finds all records where the conditions in both fields are true. | |
Or | "CA" or "NY" or "UT" | Finds all records with the value "CA", "NY", or "UT" in the field. | |
Like | Like "Sales*" | Finds all records with the value "Sales" at the beginning of the field (the asterisk is a wildcard character). | |
Between | Between 5 and 10 | Finds all records with the values 5–10 (inclusive) in the field. | |
In | In("CA","NY","UT") | Finds all records with the value "CA", "NY", or "UT" in the field. | |
Not | Not "Sales" | Finds all records with values other than Sales in the field. | |
Is Null | Is Null | Finds all records where no data has been entered in the field. | |
Is Not Null | Is Not Null | Finds all records where data has been entered into thefield. |
Updating Query ResultsIf you haven't realized it yet, you can usually update the results of a query. This means that if you modify the data in the query output, Access permanently modifies the data in the tables underlying the query. To see how this works, follow these steps:
|
No comments:
Post a Comment