Introduction To Ms Access 2003
Introduction To Ms Access 2003
Contents 1 Introduction 1 2 Some Theory 1 2.1 What is a Database? 1 2.2 Where does Microsoft Access 2003 fit in? 2 2.3 Steps in Designing and Building a Database 2 3 Case Study 2 3.1 About the Educational Institutions 3 3.2 About the orders 3 3.3 Analysis 3Doc 5.131 Ver 2 Introduction to Access 2003 3.4 Data Types 4 Creating a database 5 5 Working with tables 7 5.1 Creating Tables 7 5.2 Entering Data into a table 8 5.3 Sorting Data 9 5.4 More Practice on working with tables 9
6 Queries 11 6.1 What are Queries? 11 6.2 Query Wizard 11 6.3 Adding criteria to a query 12 6.4 Joining tables in a query 12 6.5 Calculated criteria 14 6.6 More practice at working with queries 14 7 Forms 15 7.1 What are forms? 15 7.2 Create a form with Form Wizard 16 7.3 Entering Data with a form 16 7.4 Modify the form design 16 7.5 Create an object on a form 18 7.6 More practice at working with forms 19 8 Reports 19 8.1 Modifying a report 20 8.2 More practice at working with reports 21 8.3 Mail Merge 21 9 Relationships 22 9.1 Why use Relationships 22
9.2 Types of relationships 23 9.3 A sample database 23 9.4 Finding out More about Database Design 24
1 Introduction
The aims of this training material are: To introduce basic techniques for creating a database using the Microsoft Access 2003 relational database management system (RDBMS). In this introductory material we will not cover the more advanced features of Microsoft Access 2003 such as macros and modules. These topics are necessary for the development of full-blown applications To demonstrate the need for requirements analysis before attempting a database design. Before you create a document with a word-processor, you need to do some planning of the content and appearance. For a simple document you may begin typing straight away. However, a database needs much more planning before you even think of using a RDBMS such as Microsoft Access 2003 To build a simple database. You will start by learning about the topics needed to build a single table database, that is the simplest type of database, such as a list of names and addresses. Topics include what is a database, steps in designing and building a database, Tables, Forms and Reports and Queries To become familiar with using some of Wizards that are provided with Microsoft Access 2003 To introduce terminology commonly used in database design and in the Windows 2000 environment. In order to understand the on-line help and other further reading, you need to become familiar with the terminology that is used in the documents
A database is a simply structured collection of data, with the emphasis very much on structured. Everyone encounters databases in their daily lives, even if they are not in electronic form. Telephone books, diaries, and mailing order catalogues are all instances of databases. Computerised databases are created, stored, manipulated and maintained using Database Management Systems (DBMS); DBMS are the programs or groups of programs through which we gain access to databases. In the personal computer world, DBMSs are often referred to as database packages or database applications. Depending on the way data is modelled, we have relational, network, hierarchical, and object-oriented data models. The data model defines how real-world data is represented in the database. DBMSs are typically tied to one type of data model.
Microsoft Access 2003 is based on the relational model and is therefore a relational DBMS, or RDBMS. Relational databases are built up of Tables. The column headers in the table are called fields. The rows of data in the table are called records. RDBMSs are extremely fussy and insist that each record is uniquely identified by one or more fields. We will see when we design tables how a unique identifier or key is used. 2.2 Where does Microsoft Access 2003 fit in? You should consider building your database with Microsoft Access 2003 if your data structures can be represented by the relational model. This is true of most business applications, and the sample databases provided with Microsoft Access 2003 demonstrate this. Microsoft Access 2003 can be used for quite large, complex database applications. However, even if your data structures are quite simple, and could be easily held in a card index, you may want to consider using Microsoft Access 2003, as it works well with Word. If you frequently need to send out mail shots then Microsoft Access 2003 is particularly suitable for use with the mail merge tool in Word There are more specialist databases available for bibliographic databases (ITS supply Endnote in workstation rooms). Contact ITS Reception/Help Desk on our website for information on available documentation If the data contained in your tables is mostly numeric data, then you are probably better off using a spreadsheet program such as Excel. Spreadsheet programs are specifically designed to handle numeric data 2.3 Steps in Designing and Building a Database Define the problem
Decide what real world things (entities) you need to store information about. For example Employees, Products, Customers, Orders. Entities are represented by tables in the database Decide what facts (attributes) you need to know about each entity, e.g. an Employees date of birth, salary. Attributes are represented by fields in the tables Decide on the relationships between the entities in the database; i.e. what attributes do they have in common. Relationships are formed in the database between entities that have common attributes. They have common fields in the related tables. For example, customer Fred Bloggs can place an order for product eggs. So the Orders table has relationships with Customers table and Products table Decide what information you need to extract from your data. This will allow you to design the queries Design Forms that will allow users to enter data values for running queries or entering data Design Reports that will print information extracted from the database
To illustrate what this means, we will examine a sample case study, and then build a very simple database to provide the required functions. 3 Case Study
The database that you are going to build is to hold the records of the Software Made Simple company. The company sells training material to educational
institutions. They need to keep details of the Institutions that they deal with and the orders that they place. 3.1 About the Educational Institutions
The details SMS need to know about each institution are: A unique identifier for each institution The number of students in the institution this helps to know the potential size of the market for their products The educational sector whether pricing is covered by special deals for certain types of institutions The name, address, phone, fax, email of the institution. For simplicity we will include only the institution name in our sample database The company deals only with a named contact at each institution. SMS needs to know who this person is 3.2 About the orders
The training material is provided either as printed books or on a CD-ROM. For each order SMS need to know The institution code. This links each order to the institution that placed it The order number. This uniquely identifies the order If the order is for books then they need to know the number. Since all the books are the same price there is no need at this stage to break down the order into book titles
The value of the order The order type i.e. whether it is for books or CD-ROM CD Renewal date. Sites buy an annual licence to make copies of the material on the CD-ROM so SMS need to know the renewal date 3.3 Analysis
The important thing at this stage is not your knowledge of computers, but an understanding of the problem. As this is not a course in Systems Analysis, the analysis has been done for you, so that you can practise using Microsoft Access 2003 straight away. The Entities about which to keep information are Institutions and Orders, so we will start with two tables in our database. Since the institution is represented by the site contact, the table representing the institution will be called Contacts. We will call our other table Orders. Tables are structured into fields (columns) and records (rows). All records must have the same fields, though as we shall see in this example there may be instances where a field is redundant in some records but not in others. The Attributes (i.e. the details that we need to know) of a Contact are the institution code, the number of students, the name of the institution, the educational sector and the contacts forename and surname. Since the contact is representing an institution the institution code must be unique The attributes of an Order are the order number, which must be unique, the institution code, the value of the order and the order type (CD-ROM or books). For book orders we need the number of books and for CDROM orders we
need the licence renewal date. For CD-ROM orders the number of books field is redundant. For book orders the renewal date is redundant. However, we must include the field for all records to maintain a consistent structure In order to identify the Relationship between an institution (represented in the database by its Contact) and its orders we will give each institution a unique identifier in the Contacts table. We will include this identifier as a field in the Orders table. It is good practice, when designing a database, to use unique identifiers. In our example each contact is uniquely identified by the institution code. There are good reasons for this. The contact might change in the future. In order to link an institution to the orders it has placed we need to maintain consistency. Even if the contact name changes the institution code remains the same so we still know where to send the order. In addition, there may be several institutions with contacts named (say) John Smith 3.4 Data Types (SMS 2.10) Having decided what tables we need, and what fields we need in the tables we need to decide what type of data we are going to put in each field. Is it, for example, a number, a date, or some text. When you are building a database from scratch you will usually make use of the Microsoft Office 2003 autonumber to allocate a unique identifier for each record. Note however that if you are building a database to contain data that already exists with a unique identifier (e.g. Student number, National Insurance number, bank account number), you should use your existing unique number in preference to an autonumber. The field that uniquely identifies a record in a database is called the Primary Key
Table 1 Fields for Contacts table Field Name Surname Forename Edu Sector Institution Num Students Institution Code
Table 2 Fields for Orders Table Field Name Institution Code Order Num Num Books Value Order Type CD Renewal
4 Creating a database Microsoft Access 2003 also provides wizards. Wizards are software tools that help you with a programmes features. However, in this document you will learn how to create your database from scratch, so that you are better able to match the database to the requirement. SMS guide 2.4 describes how to create a database using a wizard. Exercise 1 Starting Microsoft Access 2003 on ITS workstations At the Windows log in prompt, type your username in the Username box. Press the TAB key to move to the Password box, and type your password. Then press the ENTER key or click on OK Wait a few minutes while Windows 2000 checks your username and password, and allocates your network resources Click on the Start button on the taskbar at the bottom of the screen. The Start Menu will appear Click on Programs on the Start Menu
Click on Microsoft Office on the Program menu and then on Microsoft Access 2003
Exercise 2 Create a new database On the menus, choose File New Select Blank Database from the task pane on the right-hand side of the screen In the File New Database dialogue box make sure that you have selected the My Documents folder on your N: drive, and replace db1.mdb in the Filename box with sms contacts. Click on Create
You have now created a file that will become the database. Unlike programs such as Word and Excel, Access saves the objects that you create and the data that you enter automatically to this file. When you create a new database it will look something like Figure 4-1. On the left of the window are buttons representing various database objects. Select the class of object that you wish to work on by clicking on the button. Tables are the foundation of the database; just as you start building a house by laying the foundations you start building a database by creating the tables. The first time that you use the database the table button is selected by default
On the right of the window you will see a list of tasks that you can perform on the selected class of object. If a wizard matches your requirements, then it is the simplest way to create a table (see SMS section 2.7). In this document we will learn how to create a table from scratch
In the analysis section we looked at the tables needed, what fields should appear in the tables and what data types those fields should have. It is important to realise that we are talking about the data structures in the database. We are not talking about the data values which will ultimately be contained in the tables. For example the field forename is a part of the contacts table data structure. When we enter names (Tony, Eleanor, Nurinder) we are entering data values into the table. Exercise 3 Create a table Select Create table in Design view and click on the New button In the New Table window select Design View and click on OK Enter the field names and data types that we discussed in the analysis section. (Surname, Forename, Edu Sector, Institution, Num Students, Institution Code) By default the field type is text. Use the TAB key to move between fields To change the data type display the data type menu. To do this: either click in the data type field, then click on the drop down arrow that appears in the right of the or press the ALT+DOWN ARROW keys together Choose the data type that you require. Your table structure should look like Figure 5-1 Figure 5-1 Contacts Table in Design View
Choose Save As from the File menu and save your table as Contacts When Microsoft Access 2003 asks if you want to create a primary key now click on Yes
5.2 Entering Data into a table Exercise 4 Entering data into a table Click on the datasheet view button (see right). It is situated at the right hand end of the tool bar. You do not need to enter the Institution Code as it is an autonumber Access will do it for you. Note that if you make a mistake and delete a record Microsoft Access 2003 will not reuse a number; your numbers may therefore be different to those given in the examples. You should use your numbers in your database. Enter the data values shown in Table 3. You can use the TAB key to move between fields. Use the BACKSPACE or DELETE key to correct mistakes
Edu
Institu Num
Institu
ame Tony
Sector School
tion
ts Callow 500 Gram mar Lermo uth Mawle y North Cambe r Abdul Kamee 600
Keenan
Penny
School
r Callow 1000
Parkinson
Helen
School
Schofi eld
900
13
Univer sity Colleg e Univer sity Univer sity Univer sity Univer
14 15
n Aberda 100000 16 re Bradbu 9000 ry City Folkla nds Persho re Truma n Trowb ury 17
5.3 Sorting Data (SMS Section 4.0) Each record in the contacts table is stored in the order in which it was entered. You will often want to view it in a different order for example sorted by date or alphabetic order.
Exercise 5 Sorting Data Click in the Surname field to select it Click on the Sort Ascending button (see right) to sort the records in ascending alphabetic order. What is the seventh surname in the list?
Now practice using the techniques that you have just learnt to create another table, enter and sort data. Exercise 6 Create Orders Table Make the sms contacts database window the active window (either by clicking on it or choosing it from the Window menu)
Using the techniques that you learnt in Exercise 3 create a table called Orders, with the field names and data types shown on the right Note that the date for renewing CD licences (CD Renewal) needs to be expressed in Short Date format. To change the format select the CD Renewal field, click in the Format field in the Field Properties section of the table design view
window. Click on the down arrow to display the format menu and choose the option that you want see Figure 5-2 Save the table as Orders
Exercise 7 Entering & Sorting Orders Data Using the technique that you learnt in Exercise 4 enter the data shown in Table 4 into the Orders table. As Order Num is an autonumber there is no need to enter it yourself
Table
Orders
Value
Order Type
CD Renew al
Institution Code 8
1,190. 00
Books
14 11 17 20 16 13 4 5 1 2 9 10 15 12 7 3
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
3,213. 00 170.0 0 720.0 0 720.0 0 720.0 0 170.0 0 19,04 0.00 595.0 0 315.0 0 720.0 0 170.0 0 720.0 0 720.0 0 119.0 0 720.0
Books CD CD CD CD CD Books Books Books CD CD CD CD Books CD 19/11/0 1 04/03/0 2 05/06/0 2 20/10/0 2 31/12/0 1 05/05/0 2 01/08/0 2 04/08/0 2 01/07/0 2
0 2,023. Books
6 21 19 18 12 3 1 8 12
18 19 20 21 22 23 24 25 26
0 0 0 44 50 400 352 32 70
CD CD CD
0 261.0 Books 0 298.0 Books 0 2,380 Books .00 5,095 Books .00 190.0 Books 0 416.0 Books 0
Sort the table in ascending order on the Value field. What is the fourth value?
6 Queries
6.1 What are Queries? You use queries to view, change, and analyze data in different ways. You can also use them as the source of records for forms and reports. The examples that we will build today is called a Select Query because we are selecting fields and records from the database. The steps in designing a query are: Decide on the type of query to design Add the tables that contain the fields to be displayed Add the fields which contain the data to be displayed Add any criteria you want to use. Criteria are instructions that you use to tell Access which records to display. When you come to design your own databases you should use the on-line help to search for examples of criteria expressions
A query creates a dynamic subset (called a dynaset in database terminology) of those records in the underlying tables that satisfy the criteria specified in the query.
The next task is to create a query that finds all the records in the Contacts table. We will then see how to apply criteria to the query to return dynasets. Exercise 8 Create a simple select query In the database window select the Queries button Double click on Create query by using wizard. The query wizard starts In the box labelled Tables/Queries select Table: Contacts from the drop down menu Click on >> to move all the fields from the Available fields list to the Selected fields list Click on Next > In the next step select Open the query to view information (it should be selected by default) and click on Finish. Your query will return data and should display the data that you entered into the contacts table
In the above example you moved all the available fields from one table. However, if you only need some fields Click on the field name in the Available fields list Click on > to move it to the Selected fields list Repeat this for all the fields that you need
If you need fields from another table or query select the other table or query from the Table/Query list If you change your mind about a selected field, select it in the Selected fields list and click on < to return it to the Available fields list. 6.3 Adding criteria to a query You enter criteria in the criteria cell, that is the intersection of a row and a column. You may use a text value as a selection criteria; e.g. a persons surname or you may use mathematical values e.g. >=10 Exercise 9 Select a contact In the query window switch to design view by clicking on the Design view button on the tool bar In the Criteria Row and Surname column type Parkinson. Your query should look like Figure 6-1
Run the query by clicking on the Run button on the toolbar at the top of the screen (see right) Close your query by clicking on the close button (x) at the top right corner of the query window. (When asked if you want to save changes click on No.) 6.4 Joining tables in a query You can also create a query that uses more than one table. For example, if we want to find all the orders made for institutions with more than 10,000 students we need to know the institution and number of students, which is held in the Contacts table, and the order numbers which are held in the Orders table. The institution code in
both tables allows us to link together the information. In database terminology it identifies a relationship between the tables. When working with several tables, designers often use the Table.Field notation to clarify the specification. In the following exercise you will see this notation used; for example Contacts.Institution Code refers to the Institution code field in the Contacts table. Exercise 10 Joining tables in a query In the database window double click on Create Query in Design view In the Show Table window click on contacts, then click on add to add the table to the query. Do the same for orders, then click on Close A query design window appears. Add the following fields by double clicking on the field name Contacts.Institution Code (i.e the institution code field from the contacts table) Contacts.Institution Contacts.num students Orders.Institution code Orders.Order num In the Criteria row, Num students column type >10000
Save as Large Institutions Query. Your query in design view should look like Figure 6-2
Run the query. The results should look like Figure 6-3 Figure 6-3 Results of Large Institution Query
Close Large Institutions Query If you do not want to display a field then clear the check box in the Show row in the designer view. The records are sorted in ascending order by Institution code. In design view you can choose to sort on a different field by clicking in the Sort row of the column for your chosen field. You can select a sort order from the drop down menu. 6.5 Calculated criteria
You can group records based on fields that have the same value. You can calculate mathematical functions such as totals or averages. To do this you need to add the Totals row to the design view window. Exercise 11 Calculate the sector Average In the database window open a new query in design view
Using the techniques that you learnt in Exercise 10, add the contacts table, then add the fields Edu Sector and Num Students Click on the Totals button on the toolbar (hint: it looks like a ). A totals row appears in the QBE grid In the Edu Sector field choose Group By from the totals menu In the num students field choose Avg (average) from the totals menu Run the query Save the query as Sector Average Query
7 Forms 7.1 What are forms? Forms provide an alternative and more accessible way of presenting and entering data than using a datasheet. The form permits the viewing of a single record at a time, and the design of the form may be easily customised to suit your requirements. Other advantages of forms are: they are familiar, as they are similar to a paper form it is often quicker to enter data using a form rather than the datasheet
data can be presented in a more attractive and accessible manner and can include graphs and pictures Each form is based on either a table or a query. In order to create a form that uses fields from more than one table you will need to create a query that contains all the necessary fields. The quickest way to create a form is by using the form wizard. 7.2 Create a form with Form Wizard
We will create a columnar form based on the contacts table, using the Form Wizard. The contacts table is the record source of the form, i.e. the source from which it retrieves data. Exercise 12 Create a form with Form Wizard In the database window select the Forms button Double click on Create form by using wizard. The form wizard starts In the box labelled Tables/Queries select Table: Contacts from the drop down menu Click on >> to move all the fields from the Available fields list to the Selected fields list then click on Next > Select columnar layout and click on Next > Select standard style and click on Next >
Type Contacts Form in the title box, select Modify the form design and click on Finish 7.3 Entering Data with a form
To enter data using the form you simply type the data into the boxes, using the mouse or clicking on the TAB key to move to the next field. 7.4 Modify the form design
In Access jargon everything is called an object. Tables, Forms, Queries and Reports are all Objects. The labels, boxes, text boxes and other items on a form or report are a special type of object known as a control. Before you can do anything with an object you have to select it, by clicking on it when you are in design view. On Forms and Reports you can make Multiple Selections; that is you can select several objects at once. All objects have properties that you can edit. If you cannot see the properties window then click the right mouse button and choose Properties from the menu that drops down. The properties window is displayed (see right). In the title bar is the name of the object that you have selected (the form in this example).
To change a property click in the relevant field. Some properties will display a drop down arrow, allowing you to choose a value from a menu. For other properties you will need to type a value. To select several objects located next to each other, hold down the SHIFT key and click on each object in turn. To select several objects that are not next to each other hold down the CTRL key and click on each object in turn You may have used a similar technique when selecting files with Windows Explorer. Windows applications use consistent techniques for performing tasks. The techniques explained above work equally well for selecting emails in Outlook and selecting files in Windows Explorer All Windows applications require you to select an object before you try to do anything with it. For example, you must select a block of text in Word before you can copy it
Exercise 13 Modify a form The form should now be in design view. If it is not then switch to design view by clicking on the Design view button on the tool bar Click on the label Surname. Hold down the SHIFT key and click on the other labels (forename, edu sector, Institution, Num Students and Institution code). Your form should now look like Figure 7-1
Still holding down the SHIFT key display the properties window (as explained above). The properties window should have the title Multiple Selection If the Format tab is not on top click on it to select it
Click to select Fore Color (scroll down the window if you cannot see it). A
continuation button (), appears (see right). Click on it to see the choice of colours Select a suitable shade of red and click on OK Click on the form and you will see that the text labels are now red Select Save from the file menu to save your design 7.5 Create an object on a form You can add objects such as labels to your forms. These objects are known as controls. You create controls by clicking once on an object from the Toolbox (see right) and clicking and dragging the mouse pointer to draw it on the form. When you point (do not click) to a control the name of the control appears as a small yellow label. If you cannot see the Toolbox then, in form design view, select toolbox from the view menu. We will start by creating a simple label on the form. However, some controls are more sophisticated. For example you can use a combo box to display a menu of
items that the user is allowed to select (see SMS section 5.3). You can use a calculation as the control source from which a text box takes its data.
Exercise 14 Add a control to a form Make sure that you have the Contacts form open in design view. Position the mouse pointer between the header section and the detail sections on the form (see Figure 7-2). The pointer will change to a bar with a double arrow across it
Drag the detail section down so that a gap of about 1cm appears between the form header and detail sections. Click on the label tool on the tool box to select it. To find the tool move the cursor along the tool bar without clicking on the buttons. You will see a label appear to indicate the function of the button. This is called Hover Help Click in the space that you created between the header and detail sections, then drag to draw a box. In the box type Software made Simple Contacts
In the Format section of the properties box set the Text align property to center, font size to 10 and font weight to semi bold. Set the fore colour to a colour of your choice Save your changes
8 Reports
Reporting is the way in which you choose to present the contents of your database as printed pages. Reports enable you to select which fields will be printed out and what data should be included. Reports can also summarise data in various ways Microsoft Access 2003 gives you the opportunity to design the layout of a particular report, including the position of headings, labels and the data itself along with graphics such as pictures and logos Reports can take their data from tables or existing queries or you may create a query especially for the report In order to create a report that uses fields from more than one table a query should be created which contains all the necessary fields. For example you
could create a report based on the Large Institutions Query that you created, which uses data from the contacts and orders tables The best way to create a report is by using the report wizard
We will use the Contacts table to create a report grouping the fields by Edu Sector. We will sort on Institution and Surname fields in ascending order, and display the report in landscape format. Exercise 15 Create a report In the database window select the Reports button Double click on Create report by using wizard. The report wizard starts In the box labelled Tables/Queries select Table: Contacts from the drop down menu Click on >> to move all the fields from the Available fields list to the Selected fields list Click on Next > In the grouping window select Edu sector from the list of fields & click on > Your window should now look like Figure 8-1 Click on Next >
In the sorting window select Institution in the box labelled 1 and Surname in the box labelled 2. The sort buttons are already set for ascending order. Click on next > In the layout window select Stepped layout and landscape orientation. Click on next > Select a style that you like and click on Next > Type the title Contacts Report in the title box and click on Finish
Your report will now appear as a Print Preview. You can print it by either clicking on the print button on the toolbar or by selecting print from the file menu. 8.1 Modifying a report
(SMS Section 6.2) A basic report is divided into three sections Page Contain data that will appear at the Header Page Footer top or bottom of each page of the report. You might use this to include information about the source of the report, the date it was Detail completed or the page number. The most important section since any fields added here display the actual information from the database for every record in the table or query on which the report is based.
8.3 Mail Merge Although Microsoft Access 2003 is good for producing tabulated reports, there will be occasions when you need something more flexible than Access. The Microsoft Office suite of programs allows you to combine the data management features of
Access with the formatting and layout features of Word, using a feature called Mail Merge. Exercise 16 Mail Merge with Word Make sure that the Contacts table is not open In the database window select the Contacts table by clicking on it once do not open it Move the cursor along the tool bar without clicking on the buttons. You will see a label appear to indicate the function of the button. This is called Hover Help When you have located the Office Links button click on it and select Merge it with Microsoft Office Word In the Mail Word Wizard dialogue box select Create New Document and click on OK When Microsoft Word starts type Dear followed by a space You should be able to see the mail merge toolbar if not, choose View Toolbars Mail Merge
Click on the button Insert Merge Fields. You will see a list of fields from your contacts table. Double-click on Forename (or click once and click insert). You will see a place marker <<Forename>> in your document.
Type a space, then insert the merge field surname Type the some text in your letter so that it appears as shown below: Dear Forename Surname Please find enclosed an evaluation copy of the latest version of Software Made Simple material on CD-ROM. We can also supply this material in book form. Yours Sincerely Simon Simple Use Hover Help to locate the button Merge to New Document. When you click on this button Word will generate a set of form letters, based on your Access data source. The names of the people in the contacts table have been substituted for the place markers in your master document. This document may be printed
In the first session we discussed the tables needed for the database and introduced the idea of relationships between the entities (mapped to tables) in the database.
We have seen that by using the Institution Code as the primary key in the Contacts table and as a foreign key in the Orders table, it is possible to join data from the two tables into a dynaset using the Large Institutions Query. In the small sample data base that youve worked with you may wonder why we did not combine all the data in one table SMS Orders (Table 5) Table 5 SMS Orders Field Name Order Num Institution Code Surname Forename Edu Sector Institution Num Students Num Books Value Order Type CD Renewal Value Order Type CD Renewal Data Type Autonumber Number Text Text Text Text Number Number Number Text Date: dd/mm/yy Number Text Date: dd/mm/yy
Consider what would happen if an institution placed many orders. Institutions that order books sell them on to the students and re-order each academic year. For each order the SMS Orders table would have repeated fields for all the fields shown above in italics.
Remember also that we simplified matters by omitting contact details such as address, phone etc. which would otherwise have been repeated and you will see how inefficient such a design would be. For example: We would need to re-enter the data in italics every time an institution placed an order time consuming, and with more scope for errors The table would occupy more disk space Our two table structure is more flexible. For example, if the institution contact changes we need only change it in the Contacts table once. 9.2 Types of relationships
Microsoft Access 2003 can handle two types of relationships: One to Many relationship a record in one table can be matched to many records in the second table. However, the record in the second table can only be related to one record in the first table. For example a site contact (identified uniquely by the institution code) can place many orders. But each order is associated with only one institution One to One relationship a record in one table can be matched to no more than one record in the second table, and vice versa There is a third type of relationship between entities. That is a Many to Many relationship. In a many-to-many relationship, a record in Table A can have many matching records in Table B, and a record in Table B can have many
matching records in Table A. This type of relationship is only possible by defining a third table (called a junction table) whose primary key consists of two fields the foreign keys from both Tables A and B. A many-to-many relationship is really two one-to-many relationships with a third table.
A sample database We will now use a Microsoft Access 2003 database template to build a rather more complicated database than the simple example that we have looked at so far. Exercise 17 Create a database using a wizard Open Access Close any databases that you have open and select New from the File menu In the task pane, click on under Templates Click on the Databases tab From the database templates select Event Management and click on OK In the File New Database dialogue box click on create
As the database wizard takes you through the steps click on next > until it is finished
After a few minutes Microsoft Access 2003 will create a database. From the Tools menu select relationships. You will see that the relationships are rather complicated! Let us look in more detail at the relationship between the Events, Attendees and Registration tables. Events.eventid is related to Registration.eventid by a one to many relationship (shown as 1 to ). Attendees.attendeeid is related to Registration.attendeeid by a one to many relationship (shown as 1 to ). The Registration table is the junction table used to model the many to many relationship between Events and Attendees. Attendees can attend one or more events, events are attended by one or more attendees. Registration relates an employee to an event