You are on page 1of 23

Access Database Design

Technical Support Services


Office of Information Technology, West Virginia University

OIT Help Desk -- 293-4444

http://oit.wvu.edu/training/classmat/db/

Last revised: January 19, 2006

Copyright 2006 West Virginia University

Table of Contents
Course Description............................................................................................. 1 Advanced Table Utilities .................................................................................... 2 Exporting Data................................................................................................. 2 Importing Data ................................................................................................. 3 Creating Lookup fields..................................................................................... 6 Setting Validation Rule Properties .................................................................... 8 Setting validation rule and validation text properties........................................ 8 Setting the default value property .................................................................... 9 Set the format properties ................................................................................. 9 Set the input mask property............................................................................. 9 Defining Relationships ..................................................................................... 11 Relationship Window ..................................................................................... 11 Referential Integrity ....................................................................................... 12 Database Utilities .............................................................................................. 15 Compacting databases.................................................................................. 15 Repairing a database .................................................................................... 16 Analysis tools................................................................................................. 16 Sharing a database ........................................................................................... 17 Splitting a Database.......................................................................................... 19 Why would you split a database? .................................................................. 19 How to split a database ................................................................................. 19 Maintaining a database after it is split............................................................ 19 Linked Table Manager................................................................................... 20 Environment Options ....................................................................................... 21 Database Properties...................................................................................... 21

ii

Course Description
This is the fourth in a series of workshops about Microsoft Access 2003. It deals specifically with database design and maintenance. The purpose of this installment is to expose you to the possibilities involved in designing your databases. To introduce you to the various options that Access provides for importing and exporting data as well as the various utilities provided for maintaining your databases. Our goal is to assist you to learn the software, understand some basic concepts and show you some tips and techniques so you can develop your database management/programming skills over time. The six classes are: Introduction to Access Access Queries Access Reports Access Database Design Access Form Design Access Macros

Thank you, The OIT Support Services Trainers West Virginia University

Access Database Design

Advanced Table Utilities


Exporting Data
Access allows you to export entire tables or a portion of a table to an Excel spreadsheet as well as other file formats. To export an entire table, follow the steps below.

Activity 1: Exporting Access tables to an Excel spreadsheet


1. Open the SCHOOL2.mdb database file. 2. Click once on the STUDENT table. 3. Choose the Export command from the File menu. 4. Specify a storage location for the exported file. 5. Specify the file format. Select the Microsoft Excel 97-2002 (*.xls) entry in the Save as type pop-up menu at the bottom of the Export dialog box. After doing so the name of the table should appear in the File name field. 6. Modify the file name if necessary. 7. Turn On the Save Formatted checkbox to the right of the File name field. This will preserve individual field properties for the exported table. Option: Turn On the Autostart checkbox. This will automatically launch Excel and open the newly exported file. 8. Click the Export button.

Exporting specific records to an Excel Spreadsheet


If you wish to export just a specific set of records from a table follow the steps below. 1. Open the table from wish you wish to export a set of records. 2. Select the records you wish to export. 3. Follow steps 3 7 above. When exporting a subset of a table the Export All button changes its appearance and provides 2 options: Export All Exports the entire table. Save Selection Exports only the selected records in the table.

Access Database Design

Importing Data
Access allows you to import data from external files. This is accomplished by either Importing the data from a file or by Linking to a file. Should you Import or Link a foreign file? In making that decision, consider that Access works faster in its own file format and that you can customize that format to meet your needs. Therefore, you should import if you can; if you cannot import, then link. The exact procedure varies with the type of files you are using and whether you are importing or linking. In general: Research the procedure. Make backup copies of all files. Test the import or link procedure. Test the foreign files in Access thoroughly before discarding backups.

Before you import or link to external data, it is important to check that the data is arranged in an appropriate tabular format, and has the same type of data in each field (column) and the same fields in every row.

Importing data from an Excel spreadsheet


1. Open the database. 2. On the File menu, choose Get External Data, and then Click the Import entry to import the data OR Click the Link Tables entry to link to the external data source.

3. In the Import (or Link) dialog box, in the Files of type box, select Microsoft Excel (*.xls). 4. In the dialog box locate the drive and folder where the spreadsheet file is located, and then double-click its icon. 5. Follow the directions in the Import Spreadsheet Wizard dialog boxes.

Access Database Design

Notes on Importing
You can import or link all the data from a spreadsheet, or just the data from a named range of cells. You can create a new table in Access based on the data. You can append the data to an existing table as long as your spreadsheet column headings match the table's field names. If you attempt to append a table which contains a primary key field and your data for that field contains null values you will experience an error. Access attempts to assign the appropriate data type to imported fields, but you should check your fields to make sure that they are set to the data type you want. For example, in an Access database, a phone number or postal code field might be imported as a Number field, but should be changed to a Text field in Microsoft Access because it is unlikely that you will perform any calculations on these types of fields. You should also check and set field properties, such as formatting, as necessary.

Notes on Linking
If you link to a file and that file is deleted or moved from its original location (i.e., it is placed in a different folder or drive) the link to the file will be severed. You then have to use the Linked Table Manager to re-establish the link if the file has not been deleted. If you link to a file on a local area network, it is best to use a universal naming convention UNC* path (if you know it), instead of relying on the drive letter of a mapped network drive in Microsoft Windows Explorer. A drive letter can vary on a computer or may not always be defined, whereas a UNC path is a reliable and consistent way for Microsoft Access to locate the data source that contains the linked table. * UNC stands for universal naming convention. It is a naming convention for files that provides a machine-independent means of locating the file. Rather than specifying a drive letter and path, a UNC name uses the syntax \\server\share\path\filename.

Access Database Design

Activity 2: Importing an Access table


1. Open the database SCHOOL2.MDB if it is not already open. 2. Select the Get External Data command from the File menu and click the Import entry. 3. Select the data type of the external file: Access 4. Locate the file named Athletics.mdb and click on the Import button. 5. Select the table named Ping-Pong. 6. Click the OK button.

Activity 3: Link to a table in another database


1. Open the database SCHOOL2.MDB if it is not already open. 2. Select the Get External Data command from the File menu and click the Link Tables entry. 3. Select the data type of the external file: dBASE III+ . 4. Highlight the file named Pool.dbf. 5. Click the Link button. 6. Click the OK button on the Successfully Linked dialog window. 7. Click the Close button in the standard open file dialog box. Note: This table has an arrow icon to its left side indicating that it has been linked to an external file.

Access Database Design

Creating Lookup fields


The data entry process can be simplified and the number of typed errors reduced by looking up or obtaining values from a list. The source data can be obtained from: An Access table with a primary key field. A query that extracts specified columns from a table. A list of values entered into a field property.

Access contains a Lookup Wizard; listed as a field data type, that simplifies the creation of a lookup field. Note however, that using a table or query as a basis for the lookup field implies that the data source exists before starting the Lookup Wizard. Once the data source is created: Open the table in design view. Move to an empty row where the lookup field is to be placed. Note: you can leave the field name blank and the wizard will assign a field name. Select the Lookup Wizard under data type. Respond to the questions posed by the Lookup Wizard.

Access Database Design

Activity 4: Create a Lookup Field for Textbook Name


1. Open the SCHOOL2.MDB sample database if it is not already open. 2. Open the CLASS table in Design view. 3. Click in the Data Type area for the first blank row in the table. 4. Select Lookup Wizard from the drop-down menu. This will start the Lookup Wizard. 5. Screen 1 asks: How do you want your lookup column to get its values? Choose the option to look up the values in a table or query then click the Next button. 6. Screen 2 asks: Which table or query? Select the TEXTBOOK table and click Next. 7. Screen 3 asks: Which fields contain the values? Select the B-Title field, click the > button to add it to the Selected Fields list, and click Next. 8. Screen 4 asks: What sort order do you want for your list? Choose B-Title, click Next. 9. Screen 5 asks: What column width do you want? Drag the right column edge to see the text names, then click Next. 10. Screen 6 asks: What label would you like for your lookup column? Accept the default field name (B-Title) and click the Finish button. 11. Screen 7 asks: Prompts you to save? Click Yes. Notice the new field named B-Title in design view of the Class table. This field was the primary key of the textbook table. Also look at the property settings under the Lookup tab. 12. Switch to Datasheet view. Notice the new field named B-Title. Click in this field for the first record and pull down the menu. Select a text book title from the list of books and its name will be inserted in the datasheet.

Access Database Design

Setting Validation Rule Properties


Setting validation rule and validation text properties
The validation rule property specifies the criteria that the input data must meet before it can be stored in the table. It is generally a list of acceptable values or an expression. If the data being entered does not meet the criteria, then Access displays an error message and refuses to move from the field until the data is corrected. The validation text property defines an error message that will appear when the validation rule is violated. In the SCHOOL2 database the Student table contains a Gender field. 1 is entered for female or 2 for male. The validation rule would be 1 Or 2.

Example:

The validation text might be something like this: Data Entry Error: Enter 1 for Female or 2 for Male. When you enter the incorrect criteria you will get the following error message based on your validation text:

Access Database Design

Setting the default value property


A default value is the value that is automatically inserted into a field by Access. You can accept this value or replace it with a different value. Example: In the SCHOOL2 database open the Textbook table in Design view and insert a new field called Required. Under the Field Properties enter Yes as the Default Value. Save and switch to datasheet view. Enter a new Textbook and notice the default value.

Set the format properties


The format property allows you to specify the appearance of the data in the field. Example: In the SCHOOL2 database open the Teacher table in Design view. Put your cursor in the HireDate field, under field properties click the Format box. Choose a format from the list.

Set the input mask property


The input mask property allows you to define a pattern for entering data into the field. Example: In the SCHOOL2 database open the Teacher table in Design view. Put your cursor in the HireDate field, under field properties click the Input Mask box. Click on the builder button to activate the Input Mask Wizard. Scroll down the list to choose the short date format dd/mm/yyyy.

If you dont enter the exact date format it will give you the following error message:

Access Database Design

Activity 5: Setting properties in the teacher table


1. Open the SCHOOL 2 database if it is not already open. 2. Select the TEACHER table and open it in Design View. 3. Change the following field properties:
Field Name T-Phone Data Type Text Properties Field Size: 15 Input Mask Click Build button to run the Input Mask Wizard

Choose Phone Number Accept all default answers

Switch to the Datasheet View and enter a few new records to see how the changes affect the behavior of the field in the table. Close the table. 4. Open the STUDENT table in Design View.
Field Name S-Score Data Type Number Properties Format = Fixed Decimal Places = 1 Validation Rule: >=200 And <=900 Validation Text: Score must be between 200 & 900

Switch to the Datasheet View and enter a few new records to see how the changes affect the behavior of the field in the table. Close the table. 5. Open the CLASS table in Design View.
Field Name C-Day Data Type Text Properties Validation Rule: "mo" Or "tu" Or "we" Or "th" Or "fr" Or "sa" Validation Text: Must be mo, tu, we, th, fr, or sa C-Cost Currency Format: Currency

Switch to the Datasheet View and enter a few new records to see how the changes affect the behavior of the fields in the table. Close the table.

10

Access Database Design

Defining Relationships
After you've set up multiple tables in your Microsoft Access database, you need a way of telling Access how to bring that information back together again. The first step in this process is to define relationships between your tables. A relationship works by matching data in key fields - usually a field with the same name in both tables. In most cases, these matching fields are the primary key from one table, which provides a unique identifier for each record, and a foreign key in the other table. For example, teachers can be associated with the students they're responsible for by creating a relationship between the teacher's table and the student's table using the TeacherID fields. By creating relationships you prevent the duplication of information in a database.

Relationship Window
To open the relationship window go to Tools> Relationships or click on the Relationship button on the Toolbar. If your database does not have any relationships defined, the Show Table dialog box will automatically be displayed. Add the tables you want to relate. Once you have all the tables you want to relate. Define a relationship between 2 tables by dragging the field that you want to relate from one table to the related field in the other table.

Access Database Design

11

In most cases, you drag the primary key field (which is displayed in bold text) from one table to a similar field (often with the same name) called the foreign key in the other table. The related fields are not required to have the same names (but its good practice to do so since it reminds you where the relationship comes from), but they must have the same data type and contain the same kind of information. In addition, when the matching fields are Number fields, they must have the same FieldSize property setting. Once you have created the relationships, the Edit Relationships dialog box is displayed. Check the field names displayed in the two columns to ensure they are correct. You can change them if necessary.

Referential Integrity
Referential integrity is a system of rules that Microsoft Access uses to ensure that relationships between records in related tables are valid, and that you don't accidentally delete or change related data. If referential integrity is set on the relationship between the teacher table and the class table and a teacher is assigned to teach a course, Access will not allow you to delete that teachers record in the teacher table. Also, it will not allow you to assign a teacher to a class if they are not in the teacher table. If you choose to set referential integrity, then two other options are available for your use: Cascading Updates: If you make a change to the primary key in the source table, then that change is cascaded to all related foreign keys in the target table. Cascading deletes: If you delete a primary key in the source table then all related records in the target table is also deleted.

12

Access Database Design

Activity 6: Create relationships between the school table


1. Open the sample database SCHOOL2.MDB if it is not already open. 2. Link TEACHER to CLASS by creating a new field in Class. Open the CLASS table in Design View and add the following field:
Field Name C-Teach Data Type Lookup Wizard Description Link to TEACHER, T-LName

Follow through the Lookup Wizard to select the Teacher table and then select T-LName. Close the Design View of the CLASS table and Save changes if prompted. 3. Link STUDENT to CLASS by creating a new linking table ATTEND. Note: the primary key for ATTEND should be the combination of the foreign keys, ClassID and StudentID.
Field Name ClassID StudentID Status Data Type Number Number Text Properties Field Size: Long Integer Field Size: Long Integer Field Size: 1 Validation Rule: "C" Or "A" Or "W" Grade Number Field Size: Single Format: Fixed Decimal Places: 2 Description Link to CLASS, C-ID Link to STUDENT, S-ID C = Completed A = Attending W = Withdrew Students Final Grade

Close the Design View of the ATTEND table and Save changes if prompted. 4. Open the Relationships window by choosing Tools> Relationships In the Relationship window, choose the Show Table command from the Relationships menu. This displays the Show Table dialog box. Add the TEACHER, STUDENT, CLASS, TEXTBOOK, ATTEND and PingPong tables. Then: Drag T-ID from TEACHER to C-Teach in CLASS Drag S-ID from STUDENT to StudentID in ATTEND Drag C-ID from CLASS to ClassID in ATTEND Drag S-ID from STUDENT to PP-Player in PingPong Note: A relationship already exists between the CLASS and TEXTBOOK tables. This relationship was created by the Look Up Wizard Activity.
Access Database Design 13

Activity 7: Finish entering the data in the Class table


1. Update the C-Teach field in the existing records of the CLASS table by selecting Teacher name from the lookup field. CLASS table

ATTEND table After completing this activity, the data should appear identical to the database SCHOOL3.mdb. If you dont want to do the data entry, close SCHOOL2.mdb and open SCHOOL3.mdb.

14

Access Database Design

Database Utilities
Compacting databases
When you delete a database object or record, it leaves behind an empty hole where the object previously occupied space. This isnt a big deal unless, over time, you have deleted lots of database objects and records. Compacting a database rearranges how the database is stored and reduces its file size. To ensure optimal performance, you should compact and repair your Microsoft Access files on a regular basis. If you are compacting a shared Microsoft Access database that is located on a server or shared folder, make sure that no one else has it open. Note: The compacting process needs a block of free disk space that is greater than or equal to the file size in order to make a duplicate copy of the database. To avoid problems during the compacting process, check your free space before executing the compacting command. Compact a database that is open: 1. On the Tools menu, point to Database Utilities, and then click Compact and Repair Database. Compact a database that is not open: 1. On the Tools menu, point to Database Utilities, and then click Compact and Repair Database. 2. This opens a standard file dialog box. 2. Locate and select the database to be compacted. 3. A standard file dialog is displayed which prompting you to (re)name the new copy of the database. 3. Specify a new name and location for the database. 4. Click the Compact button.

Access Database Design

15

Repairing a database
Over time, normal wear and tear causes errors to appear in your database, affecting its performance. Usually these errors are very minor and can easily be fixed by repairing the database. Microsoft has combined compact and repair into a single command in Access 2003. When should you use the compact and repair command? If you have been busy adding, editing, and deleting records for a while or if your database seems buggy, seems sluggish, or is generating error messages, its a good idea to run the Compact and Repair Database command.

Analysis tools
The Performance Analyzer will examine your database and offer suggestions of ways to improve the performance of the objects. It does not analyze Access itself or the underlying operating system. To use the performance analyzer: 1. Open the database that is to be analyzed. Note: The database needs a reasonable amount of data in the tables before the analyzer can make realistic projections. 2. Select Tools> Analyze from the menu and select Performance. 3. Select the type of object and then the name of the object that is to be optimized. 4. Repeat the until all desired objects are selected or use the All Object Types tab to view a list of all objects in the database. A Select All button is provided to simplify the selection process. 4. Click the OK button to run the analysis. 5. The Analyzer examines each of the objects and offers suggestions, recommendations, and ideas.

16

Access Database Design

The Table Analyzer examines your tables and looks for design flaws. It tests your tables to see if they conform to the commonly accepted rules of data normalization and then offers suggestions for improving the tables. To use the table analyzer: 1. With the database open, Select Tools> Analyze from the menu and select Table. 2. When the Table Analyzer Wizard starts, move to the table selection screen and indicate the table that you want to analyze. 3. When you let the wizard automatically analyze your table, it will advise you of changes that need to be made (like splitting the table into multiple tables) and then it will make them for you.

Sharing a database
If your computer is connected to a network, you and others can work with one Access database at the same time. There are several ways you can share data in a multi-user environment. Share the entire Access database You can put the entire Access database on a network server or in a shared folder. This is the easiest method to implement. Everyone shares the data and uses the same forms, reports, queries, macros, and modules. Use this strategy if you want everyone to use the Access database the same way or if you can't support users creating their own objects.

Access Database Design

17

Share only the tables in the Access database You can put only the tables on a network server, and keep other database objects on users' computers. In this case, the Access database's performance is faster because only data is sent across the network. When you split a database into a back end (tables) and a front end, users can customize forms, reports, and other objects in their front-end databases without affecting other users. We cover Splitting a Database on the next page. Record Locking When you open an Access database file (.mdb) in shared mode, Microsoft Access also creates a locking information file (.ldb) with the same file name (for example, Northwind.ldb) and in the same folder as the database file. This locking information file stores the computer name (such as mypc) and security name (such as Admin) of each shared user of the database. Microsoft Access uses this information to control concurrency. In most cases, Microsoft Access automatically deletes the locking information file when the last user closes the database file. In order to setup a shared database you need to instruct Access how to handle a shared database. This is done in the Advanced tab of the Options dialog box. 1. On the menu Go to Tools> Options. 2. Click the Advanced tab in the dialog box. 3. Click the Shared radio button in the Default open mode of the dialog box. 4. Click the Edited record radio button in the Default record locking section of the dialog box. 5. Click the OK button to close the dialog box.

18

Access Database Design

Splitting a Database
For various reasons, you may decide to split your database into either a front-end application or a back-end application. The back-end database contains the tables and is stored on a file server or a shared network drive. The front-end database that links to the back-end tables contains all the forms, the queries, the reports, the macros, and the modules. The front-end database is distributed to the workstations of the users. Why would you split a database? The following are typical reasons to split a database: 1. You are sharing your database with multiple users on a network. 2. You do not want your users to be able to make design changes to tables. The most common reason to split a database is that you are sharing the database with multiple users on a network. You can put only the tables on a network server, and keep other database objects on users' computers. The database's performance is faster because only data is sent across the network. Also, users can customize their forms, reports, and other objects for their individual needs and preferences without affecting other users.

How to split a database


1. Open the database. 2. Choose Database Utilities | Database Splitter from the Tools menu. 3. Click on Split Database. 4. Enter a name for the back-end file of your database. 5. Click OK. If you click on the tables tab of your database, you will see that all of the table names now have an arrow next to them. This is a visual indicator that they are linked to data outside of this file. Maintaining a database after it is split. Once a database is split, the front end file must be kept in sync with the back end file. You must refresh the linked tables if the back end is moved or if the file name changes.

Access Database Design

19

Linked Table Manager


To update the linked tables in a split database 1. Go to Tools> Database Utilities> Linked Table Manager 2. Make sure to check the Always prompt for new location check box. 3. Choose Select All 4. Click OK

5. Select the location of your back-end database using the Select New Location of Categories dialog window.

6. Click Open. 7. You will then see the following window:

20

Access Database Design

Environment Options
The Access environment options are displayed by selecting the Options command from the Tools menu. There are 11 tabs which allow you to control how Access functions and change various default settings. For information about a specific item in the dialog box, click the question mark at the top of the dialog box, and then click the item.

Database Properties
To display database properties go to the File menu and select Database Properties. You can also right-click in the database window title bar and choose the Database Properties shortcut. There are four tabs which contain different types of information about the database. The fifth, Custom, allows you to input additional information. For information about a specific item in the dialog box, click the question mark at the top of the dialog box, and then click the item.

Access Database Design

21

You might also like