Sie sind auf Seite 1von 17

Preface

Notes on : DATABASE Design Guidelines and Concepts Using MS Access From the author.
These notes [free of charge] have been placed on the Internet to assist anyone wishing to use Microsoft Access at the introductory and intermediate level. They have, however, been specifically prepared for students taking I.S. (Information Systems MGCR-331, a first year course at McGill University in the Faculty of Management as well as at the Centre for Continuing Education, as well as for a number of practical, hands on, courses on Access. The notes specifically refer to Access, version 2000. However for those with an earlier version, such as the Office 97, Version 7 and 2, these notes should be just as applicable. It is assumed when a newer version of Access is introduced, these notes will be just as applicable. The notes are continually being updated with improvements and or corrections. If you find that you wish to contribute to the content with suggestions for improvements and or corrections, please contact the author below. Again, your comments and suggestions are most welcome! Date Created: Author: E-mail address: February 1996 Date last modified: May 29, 2006 Leo Kerklaan - McGill University - Montreal - PQ - Canada leo.kerklaan@McGill.CA

No part of this work may be reproduced or used in any form or by any means - graphic, electronic, or mechanical, including photocopying, recording, taping, or information storage and retrieval systems without the written permission of the author unless the above is performed exclusively for use in our I.S. courses at McGill or those seminars and courses given by the author.

Notes such as these are under constant review, as such, they may contain any number of spelling errors and or other minor grammatical problems. Your understanding is appreciated. Please bring any corrections and or suggestions to my attention! Thank you!

Notes on: Database concepts and how to use Access.

Page

10/18/2013

Table of content

Table of content : Notes on : Database Concepts and Access Chapter Topic Page No.
1 2 Introduction to database concepts Tables and Database Creating a new database and table Opening an existing database and table Table structure and properties Create, Delete and Change a Primary Key Viewing the contents of a table Printing the content of a table Relationships in Access Establish a relationship Modify, delete and remove a relationship View and print a relationship Short cut keys Querying a table Bringing up the query screen and types of queries Creating a simple, single table query Sorting the data to be displayed Creating a complex query (and / or, IN, between, Like) Calculated field Group by query (includes WHERE clause) Update query User prompt - parameter query Crosstab query Reports Overview Report layouts supported by the Wizard Understanding report layout Creating a report with the Wizard Sample Preview Print Preview Print and printer setup Saving a report Text box controls - Bound and unbound Moving, sizing, copying, and deleting fields Adding new fields to a report Adding text to a report Sorting and grouping in a report Other interesting things you can do in a report Forms
Page 2 10/18/2013

3 5 5 5 6 8 8 10 11 12 13 13 13 15 15 16 17 17 19 20 22 23 24 26 26 26 26 27 29 29 29 30 30 30 31 31 31 32 33

Notes on: Database concepts and how to use Access.

Table of content

Background, how to create, table lookup and multi table Expressions, and Functions

33 to 37 38

Notes on: Database concepts and how to use Access.

Page

10/18/2013

0Chapter 1 - Database Design and Guidelines 1

Database Design Guidelines


The most common DBMS approach in use today is the relational database model. For a database to be effective, it is absolutely imperative that it be carefully and efficiently designed. The relational database approach presents entities as two-dimensional tables, with records as rows and fields as columns. Tables can be joined (relationships between tables) where there are common columns in tables. A good design should eliminate unnecessary data duplication (redundancy). For example, it is more than likely that it makes no sense to have someones home address in more than one table. On the other hand, a field like employee number is more than likely to be found in more than one table, enabling the DBMS to establish a relationship between the tables on this common field. The relational DBMS supports one-to-one, one-to-many and many-to-many relationships between entities. An entity is something you collect data about, such as people, places or things. Often an entity is thought of as a table , where a table has many rows/records with many individual fields representing individual pieces of information. Thus a record is collection of related fields within a single entity. In order to establish relationships between tables, the common field found in each of the tables is identified as either a primary key or a foreign key . The student number, in our earlier example, is more than likely the primary key in the student table and the foreign key the enrolment table (courses assigned to each student). A primary key is a column/field whose value uniquely identifies a row/record in a table. That is, you cannot have duplicates of any value (e.g. only one occurrence of a given student number). A primary key must never be a null value (i.e. does not exist). A table need not have a primary but it is recommended to have one. As well, a primary key may consist of more than one column/field, as long as the group of fields uniquely identifies a record in the table. The latter is called a combination primary key . A foreign key is a column/field whose value must match a primary key in another table . In fact, if referential integrity is enforced, the DBMS will ensure that no foreign key exists without a corresponding primary key. A table may have more than one foreign key, however, it need have a foreign key. In Access, the person creating the database structure establishes the relationship between two tables. The following are the rules for expressing associations among entities and their corresponding data structures. One-to-One One record in the primary key table may have only one matching foreign key value . Note: There need not be a matching foreign key record. E.g. some employees may be assigned to a single company car. As such, a given car is only assigned to one employee.

Additional Example: A given employee may use a single personal computer or none at all. Said another way, a given personal computer is used by only one employee and always the same employee (at least as far as the database is concerned). The relationship is 1:1.

Employee
1 One-to-Many

USES
1

Personal Computer

One record in the primary key table may have any number of matching foreign key records. Note: There need not be a matching foreign key record. E.g. an employee may have any number of skills, each identified and stored in an employee skills table (e.g. Lotus, Excel, Access, Pascal etc.). As such, many employees can hold a given skill.

Additional example: A given employee, may use one or more personal computers or the employee may not use any at all. Alternatively, no employee or only one employee can use a given personal computer. The relationship is 1:M.

Employee

USES

Personal Computer

1 M Many-to-Many Two tables are linked, however, neither column represents a primary key. Note: This does not work very well in Access. Note: There need not be matching value records between tables. Page 4 10/18/2013

Notes on: Database concepts and how to use Access.

0Chapter 1 - Database Design and Guidelines 1 e.g. A given player may participate in many games and each game has many players. Additional example: A given employee can use no computer or any number of computers or: No employees or any number of employees can use a given computer. The relationship is M:M.

Employee
M

USES
M

Personal Computer

In addition to assigning primary and foreign keys, each field in the database needs to be defined in terms of a field name, field description, data type (e.g. numeric, text etc.) and properties. The properties for a field may include a variety of things such as rules about the value, the formatting and other display characteristics and indexing.

Steps to follow for a well-designed database.


The key to a good design is to fully understand the problem to be solved. To design an efficient database model, one should go through the following key steps on a piece of paper. List the objects where an object is a single theme or subject about which you would like to track information. E.g. Employees, Departments and Hours worked. List the facts about the objects where a fact is a single piece of information about an object. e.g. The employee table may have the following facts: Employee Last, First and Middle name, Date of birth, social insurance number, home address split into street, city, province/state, postal code and country. Turn the objects and facts into tables and columns . Objects become tables and facts become columns. Employees and Departments are example of objects. Each table in turn is made up of one or more columns where a column is a specific fact. In other words the facts are individual pieces of information about a given object or table. Determine the relationships among objects. Look at the objects and determine how they might be related. For example the employee table contains information about employees, whereas the Hours worked table contains the hours worked pertaining to your employees. These two tables are related on the piece of information that identifies the employee. Determine key columns. Keys can be either primary or foreign. A Primary key in a given table uniquely identifies each value, whereas a Foreign key can have many occurrences of a given value. In establishing a primary key one may identify any number of Candidate keys. These are columns or groups of columns that meet the requirements to be a primary key. Composite keys or Concatenated keys are those that combine any number of columns in a given table to meet the uniqueness required of the primary key. Determine linking columns. The best linking columns, sometimes referred as common columns, are those that point to a unique row of date e.g. a given employee number identifying a unique individual in the base table to a matching employee number in a relation table where many occurrences of a given value can occur, like the hours worked table. Determine relationship constraints. This is sometimes referred to as referential integrity where one can set up rules that you cannot enter information in the foreign table without the primary key table having a matching value. In other words, you must set up a value in the parent table before a matching value can be added to the child table. Evaluate your design. One can ask the following questions: Does each table have a single theme? It should and furthermore each column should be a fact about the object. Does each table have at least one key column? It should. Is the table easy to add data to and retrieve data from? It should be. Are the columns unique to a table? They should be unless they are linking columns between tables. In addition, for each fact: Determine data type and size and all the properties such as rules, formatting defaults etc. Assign a meaningful name that is neither too long nor unwieldy but at the same time representative of what it is trying to describe. Spaces can be left between the words in a field name. e.g. social insurance number. Although this is meaningful it is probably too long, try: SIN

Notes on: Database concepts and how to use Access.

Page

10/18/2013

0Chapter 1 - Database Design and Guidelines 1 Additionally for a design to be successful, one must also determine how the database will be used and by whom by considering the following. Establish the types of updates and transactions that create and modify the data keeping referential integrity in mind. Determine which fields should have indexes. Suggestion: Always index key fields and those fields that are sorted. Decide on who can access and or modify the data and if required set up a security matrix. Document your design process. Establish backup procedures and off site storage. Develop a contingency plan that will cover any potential disaster.

Functional dependencies and design anomaly.


Most anomalies are caused by poor relational database design. In fact, the poor design is tied directly to the relationships that you have incorporated into your database between attributes. These relationships are known as functional dependencies. A functional dependency is defined as follows: Attribute (field) B is functionally dependent on field A , if for each unique value of field A, there exists only one value of field B in the relationship at any one time. When time permits, more on this conceptually difficult topic will be added.

Data modeling.
Tools are available for designing and then automatically creating the structure of a relational database. Often these use graphic techniques to assist with the E-R diagrams (Entity Relationship). As the theory behind a relational database is founded in a branch of mathematics called set theory. This was expanded upon to include relational algebra. There is a formal process called Normalization that helps determine which data attributes are grouped into tables, which in turn make up a database. The key here is to: Eliminate duplicate tables in tables. Accommodate future changes in tables Minimize the impact on user applications when the database structure is changed. The scope of this and other technical topics is beyond these notes. A good relational database design book should prove invaluable in this area. It should be software independent.

Notes on: Database concepts and how to use Access.

Page

10/18/2013

Chapter 2 - Using a Database and Tables

Creating and Opening, Viewing, Updating and Printing a Table Using MS Access
Loading Access.
From the screen below, either create a new blank Access database or open an existing database file.

Used to create a new database container

Used to open an existing database container. Select the file from the list below or if none is selected turn, on the next panel, select the drive, folder and database file that has to be opened.

Note: You may also use the File Open or File new command to accomplish the same. Access needs the drive, folder and database name to continue. See below.

Drive and folder selection Examine the power of these icons!

Use these icons to speed up the location selection! Database name. Note: The default is db? And the extension is always mdb

When done click Create

Once you have selected the drive, folder and database name, click on Create icon.

Notes on: Database concepts and how to use Access.

Page

10/18/2013

Chapter 2 - Using a Database and Tables

yThe following screen is presented. The Objects area indicates you are active in Tables section. To go elsewhere click on the required Object name such as Queries. As this was a new database, no existing tbales, queries, Reports and Forms will appear. Each item will have to be created. From this panel you would normally Create a new table design Modify an existing table design Open an existing table and display its content.

Creating a new Table structure.


You can create a table using the Access Wizard or on your own. The Wizard is a tool that helps you create structures from pre-defined commonly used objects. Experiment with the Wizard if you wish, however, these notes will assist with creating a table without the use of the Wizard. By selecting the New Table icon, Access will bring up the following screen. Each row in the Table dialog represents a new field in the table. Each field needs to be further identified as to Field Name, Type, Description, Size, Format, Decimal places, Caption, Index, etc.. This is all explained on the pages that follow.

Notes on: Database concepts and how to use Access.

Page

10/18/2013

Chapter 2 - Using a Database and Tables

Information on the screen above.


Field Name. Choose as descriptive a name as possible, but a good guide is to keep the name short if the field is of short length, as the field name by default becomes the column title in a Query , Form and or Report. Up to 64 characters long, made up of letters, numbers, spaces and most special symbols except a period, exclamation mark and square brackets. The field name must not start with a space. Data Type. This determines what can be entered into a field. The ten data types supported are: Text (up to 255 characters). Number (digits, + , - and .). Supports whole numbers e.g. 256 and very large and small numbers with up to 28 decimal places. Currency (stores accurate values with cents) and assigns the currency symbol ($). Date/time in MM/DD/YY (Month/Day/Year) format performs accurate date and time arithmetic. Memo Data is much like text, however it is used for long comments (64,000 characters long). Autonumber consists of a number (no decimals) that is automatically incremented by 1 in Access. This can guarantee uniqueness, for you cannot edit this field. As far as I can tell it can only start at 1 and is always incremented by 1. However a variation exists where one can specify that the autonumber is a random number in the - 2 billion to plus 2 billion number range (long integer type). Yes/No Limits the values stored to Yes or No. OLE object data allows fields created in other software (e.g. pictures, drawings, voice messages etc.) to be stored, but not modified in Access.

Two additional, rather complex but very powerful data types are possible. They are: Hyperlink (The following was copied from Access help Function key F1). Text or combinations of text and numbers stored as text and used as a hyperlink address. A hyperlink address can have up to three parts: text to display the text that appears in a field or control. address the path to a file (UNC path) or page (URL). subaddress a location within the file or page. screentip the text displayed as a tooltip. The easiest way to insert a hyperlink address in a field or control is to click Hyperlink on the Insert menu. For more information, see Type a hyperlink address in Form view or Datasheet view. Each part of the three parts of a Hyperlink data type can contain up to 2048 characters

Lookup wizard (The following was copied from Access) Lookup Wizard Creates a field that allows you to choose a value from another table or from a list of values by using a list box or combo box. Clicking this option starts the Lookup Wizard, which creates a Lookup field. After you complete the wizard, Microsoft Access sets the data type based on the values selected in the wizard. The same size as the primary key field used to perform the lookup, typically 4 bytes

Field Description. This is an optional field that can be up to 255 characters long. Use this to document your table structure. Field Size. Assigns maximum field length, but is only used for Text and Number fields. However you really only need to concern yourself with length for Text fields. For number fields: If a whole number like social insurance number, use Long Integer. For a value that has decimal places use Double. For more information turn to Data types and Field sizes under Access help. Page 9 10/18/2013

Notes on: Database concepts and how to use Access.

Chapter 2 - Using a Database and Tables Format. Enables you to specify how the data is to be displayed and or printed e.g. Currency - 2 decimal places, Short or Long Date format. Input Mask. Specifies how the data is be entered over a mask. Examples: (000)-000-0000. A letter or digit entry required. C any character or space, but nothing else. Caption. Label that will be used as column heading in a query and or report instead of the field name. Default value. Access will automatically insert whatever is specified. e.g. todays date (=Date()). Validation Rule. Data must meet the limitations as per specified rules. e.g. Value must not be less than $6.00. More examples follow. Validation Text. The message that will be displayed if validation rule is violated. Required. If yes then user must enter a value, i.e. field cannot be left blank. Allow zero length. If allowed, text with no length e.g. is permitted. Indexed. Maintains an index on all the values of this field to speed up searches and links. A good rule of thumb as to whether or not to assign an index is: It must be assigned to all primary key and linked fields in a relationship. If one does frequent searches and or sorts on this field. Note: If you index a field, you will be prompted as to whether or not duplicate keys are allowed. If no duplicate values are permitted then the field can be assigned a primary key. Additional field property notes. Acceptable data entry formats are: 5/14/94, 5/14/1994, May 14, 1994, May 4, 1994, May 14. Note: 5/14/00 is assumed to be May 14, 2000. Time is entered as 16:00:00 or 16:00. All dates and times can be formatted (displayed) with a number of short and long date and time formats. Validation rules examples: <> 0 ; 10 OR 900 ; Like Montr??? ; <= #12/31/96# etc.. Null Values. If you do not type a value, Access stores a Null value in the field. Note: To seek out a null value in a criteria simply specify Null in the criteria cell of the query. See example in the Query chapter.

Creating, Deleting and Changing a Primary Key.


To assign a primary key to a single field: select the row and click Set primary key button. Note: The Indexed field property will now read: Yes (No Duplicates). To assign a primary key to more than one field: select the first row and while holding down the Ctrl key, click Set primary key button, then select the next row and click the Primary Key button . Note: For each respective field with a primary key, duplicates are permitted (Only on the combination of the two fields are duplicates not permitted). As such, the Indexed field property for each of the respective fields can read : Yes (No Duplicates) or Yes (Duplicates OK). To delete or change a primary key, be sure that you delete any relationships first (See chapter 3). To delete a primary key, in table design, click the Indexes button. Select the entire row with the index to be deleted and press the Del key. Close this window and when you return to the table design window, the primary key should be gone. To change a primary key from one column to another, select the new primary column and click the Primary key button. Note: When leaving the table design, Access will check to see if a primary key has been created. If not, it will prompt you with: No primary key exists, Okay, Cancel the closing of the table design or let Access create a primary key for you. If you pick the latter, Access will add an autonumber field as the primary key.

Viewing the contents of a Table


From the Database - Table dialog, Open the required table. This presents the Datasheet View window. This window displays the contents of the table in spreadsheet like format. The Menu and icons displayed also take on a display to assist you with scrolling through the table. As well, you can filter the data you may wish to display. Navigation button: You will find navigation buttons at the bottom of the screen to move through the records quickly. The following symbols do the following: |< First record < Previous > Next >| Last record Scroll up and down the table. If the table has enough rows, you can utilize the scroll bars to the right to move up and down. Notes on: Database concepts and how to use Access. Page 10 10/18/2013

Chapter 2 - Using a Database and Tables Add a New Record. Use the >* icon to add a new record to the table. You can also use menu Records - Go To New. Delete a Record. There are numerous ways of deleting a record. Note: The record is not physically deleted, but flagged as having been deleted. The methods to delete are: First select the entire record (click leftmost column). Use the Cut icon From the menu select Edit - Delete Hit the Del key Using the right mouse button, select Cut. Maximize and minimize the size of your view display. Use bottom at the top right of the screen. Find or Replace information. To find a record use the binoculars icon which presents the Find dialog (see below) and complete the required information. This presents one record at a time. Note: The same thing can also use the Edit - Find to do the same. To Replace data, use the or Edit - Replace menu and follow the instructions. This can replace a single data field or all matching values in the table. Note: This can also be done in an Update Query (See Query chapter). Sort your table. Use the sort icons to rearrange the display order based on the column that you have selected. There is an icon to sort in ascending order (A:Z) and one for descending order (Z:A). To sort on multiple columns do the following. Under the Records menu, you will find a Quick Sort option. Complete the dialog to let you to sort on multiple columns. Note: The underlying order (physical order) of your table is not changed with a sort. The sort only changes the order of how the data is displayed. Filter the table to only show selected records. Use the Edit/Filter icon. This will bring up a query dialog. This dialog is completed similarly to what is explained in the query sections of these notes. Size the column width on the view datasheet. Position the mouse pointer on the heading row , on the right side of the desired column. Hold the left mouse button to expand or reduce the width. Let go when done. When you close the view you will prompted to save this display format for future use. Misc. options. Explore the Format menu to hide, freeze and apply font and height to the view. Under the records option, you can prevent the user from changing the information in the table. Select Records -Allow editing. Move a column to a new locations on the datasheet view. This cannot be done here. To do this you must create a query and within the QBE dialog, move the columns around. See explanations in the query window.

Example of a Find Dialog window.

Notes on: Database concepts and how to use Access.

Page

11

10/18/2013

Chapter 2 - Using a Database and Tables Filter icons Find icon

Print & Print preview

Sort icons Replace dialog would let you find and replace all or some of the data.

Design icon. Takes you to the table structure

More Options have been selected. This displays:

Printing the contents of a Table


With the tools described above, once you are satisfied that the data is displayed the way you wish to see it, you can then print the table. But first, to save paper, in case something goes wrong, you may wish to preview your printed output. Print Preview Click the Print preview icon (see screen above) Use this to Zoom in on the display, browse through it, set up the print display and print. Close the window when done. Hard copy Print out. You can print from within the Print Preview window or click directly on the Print button or from the File menu select Print. Note: You may want to do a Setup before printing. This will enable you to set margins and select the paper size and orientation. Page setup can be activated only after you have opened the table. The set up will be remembered. Print icon. Clicking on the print icon will immediately print the table according to the default or latest settings.

Notes on: Database concepts and how to use Access.

Page

12

10/18/2013

0Chapter 3 - Relationships in Access 1

RELATIONSHIPS in ACCESS - Create, Modify, Delete and Remove


Once you have created the necessary tables, the following are the steps to either create or update the relationship. The updating of the relationship would be used when you create a new table and or modify the structure of an existing table and or make changes to the referential integrity rules. Note: To set up a relationship between tables, the common field between the tables must have the same data type. As well the tables must belong to the same Access database.

Click on the Relationship button or from the main menu select Edit - Relationship. From the Add Table dialog, Add each of the tables (be sure not to duplicate any tables). Click the Close button on the Add Table dialog.

See Add Table Dialog below. This dialog is super imposed (this was manually placed this way for your benefit) on a relationship diagram (with 1 table address thus far). These two picture in turn are combined with the Database window. Table option. Add Table Dialog under the Relationship dialog.

Notes on: Database concepts and how to use Access.

Page

13

10/18/2013

0Chapter 3 - Relationships in Access 1

How to form a relationship between the tables.


Drag the common field from the table with the primary key to the common foreign key field in the other table. Once you release the mouse button, Access displays the Relationship dialog box shown below. In the example below, you have already established a one to many relationship on Number between the primary table Employee Master and the foreign table Hours Worked. In the dialog below you are establishing the following one to many relationship. The field Dept in the dept table (primary table key) is related to Dept in the Employee Master table (foreign table key). As well you have indicated (click check box) that you wish to enforce referential integrity. That means you cannot create an employee with a Dept that does not exist in the Dept table. Furthermore, you can check off the following two boxes: Cascade Update related fields and Cascade Delete related fields. These two check boxes, if on, will ensure that Access will ripple changes that are made in the primary table to the foreign table. For example, changing the Dept number for the Paint Shop from 10 to 11 in the Dept table will automatically mean that all Dept numbers of 10 in the Employee Master table will become 11. Establish the necessary relationships between all the tables. That is, no table should be left hanging without a relationship to another table. When done, click the Create button. To close the relationship window, double click the Relationship Control box and specify that you wish to Save the changes with a Yes response.

Notes on: Database concepts and how to use Access.

Page

14

10/18/2013

0Chapter 3 - Relationships in Access 1

How to Modify, Delete and Remove a Relationship.


To modify a relationship. Click the relationship icon, or from the Edit menu choose Relationship. Double click the line that you wish to edit. This brings up the relationship dialog. Make the required changes and when done, confirm with Yes to any confirmation prompts. When done, close the relationship dialog. To delete a relationship. From the Edit menu choose Relationship. Single click the relationship line that you wish to delete. From the Edit menu choose Delete or press the Del key. Confirm any confirmation prompts with Yes. When done, close the relationship diagram. To remove a table from the relationship. Click on the table you wish to remove. From the Relationship menu , choose Remove Table or press the Del key. Please note that this deletion only affects the Display of the relationships. The tables is not deleted. As well, the Database still maintains the relationship even if it is not displayed. To permanently remove the table and all its relationships from the relationship window. This may be necessary if you accidentally added a given table more than once. This two step operation requires that you first delete all relationships to this table (see above how it is done). Once the relationship is removed then remove the unwanted table from the relationship (see above). Be sure to save. You may even wish to exit the database and re-enter and make sure the unwanted table is gone.

To View and Print an Existing Relationship.


To view an existing relationship for a table or query. From the edit menu choose Relationship. To view all the relationships in the database, choose Show All from the Relationship window (or click the Show All relationship button on the toolbar. To view the relationship of a particular table select the table first then perform the steps described earlier. The Show Direct command adds all tables and queries that have a direct relationship with the selected table or query. To print a relationship diagram. From the menu select File and Print Definition. Access takes a few seconds to compile all the relationships and displays everything about the relationship on the screen. This information can then be printed. See sample relationship page is displayed on the next page. You can always use PrtScr to capture the screen image to the clipboard and then Paste it back into any software that supports the Edit Paste command (e.g. WORD, PC PAINT, ECXCEL etc.). Then use the software to print the relationship diagram.

Short cut keys


Instead of using the menu or the icons, Access supports short keys. To see if a short cut key exists, select the object (e.g. table or relationship line) and click the right mouse button. Select the option of your choice.

Notes on: Database concepts and how to use Access.

Page

15

10/18/2013

0Chapter 3 - Relationships in Access 1 Sample output from a relationship diagram print out. Use File - Print definition.

Notes on: Database concepts and how to use Access.

Page

16

10/18/2013

Chapter 7 - Expressions and Functions

Notes on: Database concepts and how to use Access.

Page

17

10/18/2013