Administration: Relational Databases for Higher

advertisement
HIGHER
ADMINISTRATION
RELATIONAL
DATABASES
South Lanarkshire Council
Administration
Higher
Relational Databases
------------------------------------------------------------------------------------------------------------------------
CONTENTS
INTRODUCTION
3
CREATING RELATIONSHIPS
3
RELATIONSHIP TYPES
3
GETTING STARTED
4
SETTING A PRIMARY KEY
8
PRACTICE MATERIAL 1
13
PRACTICE MATERIAL 2
14
PRACTICE MATERIAL 3
15
Administration
Higher
Relational Databases
------------------------------------------------------------------------------------------------------------------------
INTRODUCTION
We are all able to use one table of information in a database to query, produce forms
and reports, however, we are now going to bring together separate tables with
related data using queries, reports and forms.
Firstly to allow this to happen we have to ensure that each table has a field or set of
fields that uniquely identifies each record stored in the table. This information is
called the primary key of the table. A primary key cannot have any duplication or
null values entered into it. Normally you would set the primary key when you are
designing the database.
CREATING RELATIONSHIPS
A relationship is a way of linking information between two tables. You create a
relationship by matching fields in the different tables – usually these fields will have
the same name. In most cases these matching fields are the primary key from one of
the tables. The primary key from one table is included in the second table and it is
then called the foreign key within the other table.
RELATIONSHIP TYPES
One-to-one relationship
Each record in a table relates to one record in another table.
For example in the Pupil Information table and the Library table, each pupil
only has one library card.
You can create a relationship by relating the primary key in one table to the
primary key in another.
One-to-many relationship
Each record in a table relates to one or more records in another table.
For example in the Employees and Appointments tables, each employee can
have more than one appointment but each appointment can only have one
matching record in the Employee table.
3
Administration
Higher
Relational Databases
------------------------------------------------------------------------------------------------------------------------
GETTING STARTED
1 Open up the Database called Relational Database Exercise 1 as shown below.
This is an extract from a school database. Open each table up and look at the
information held within each table.
The information appears to be related in the 3 tables due to the fact that the data
is linked. For example,
Some of the same fields
have been used in all 3 of
the tables.
4
Administration
Higher
Relational Databases
------------------------------------------------------------------------------------------------------------------------
2 With this related data we should be able to perform a query on all 3 tables. We
want to find the names of the staff who have been successful in applying for a
course and the title of the course they have applied for.
Double Click on
Create Query in
Design View
Click on the
Queries Tab.
3 A blank query grid will appear and the Show Table box as shown below:
To insert the 3 tables
into the query grid
either select each table
and click Add or select
all 3 tables together by
holding your CTRL key
and then click on Add.
5
Administration
Higher
Relational Databases
------------------------------------------------------------------------------------------------------------------------
4 Adjust the query grid to be the same as below:
5 Insert the following information into the grid:
6 Click on the run icon
to see the results of your query.
6
Administration
Higher
Relational Databases
------------------------------------------------------------------------------------------------------------------------
7 Your results should be as follows:
There are 420
matches to our
query.
This doesn’t seem right considering in the Staff table there were only 10 records.
The computer has completed our query as follows:
6 courses in the Courses Table X 7 successful candidates in the Course Applied for
X 10 records in Staff Table = 420 records.
In other words they have assumed that there were 6 courses available for every staff
member (10) and multiplies by the number of successes for application.
The computer does not understand that these tables have related data in them. In
order for the computer to understand this we must create a relationship between the
3 tables.
7
Administration
Higher
Relational Databases
------------------------------------------------------------------------------------------------------------------------
SETTING A PRIMARY KEY
As discussed earlier we must create a primary key that uniquely identifies the data in
the table.
1 Open the Courses table in design view.
2 Select the Course Code as shown below:
3 Right click at the right hand side of the selected record and select Primary Key
from the menu as shown below:
Select Primary key
from the menu.
4 The design view of the table should be the same as below:
5 Now open the Staff table and set the Employee No as the Primary Key. Once
completed the table should look like below:
8
Administration
Higher
Relational Databases
------------------------------------------------------------------------------------------------------------------------
6 We are now going to create a relationship between each table. Go to the Tools
menu and select Relationships from the menu, or you can click on the
Relationships icon on the toolbar.
Click on the tools menu and
select Relationships.
7 The following menu will appear:
Select each table
and click on add
to insert.
9
Administration
Higher
Relational Databases
------------------------------------------------------------------------------------------------------------------------
8 The relationships should look like below if not click on the box and move to the
correct position:
9 Now drag the Employee No field in the staff table to the Employee No field in the
Courses Applied For table.
An Edit Relationships window will appear as shown below:
REFERENTIAL INTEGRITY
Referential Integrity is a set of rules that prevents you from changing or
deleting a record if matching records exist in a related table.
Cascade Update Related Fields
When you change data in the
primary key in the table,
Access will automatically
update the matching data in a
related table.
Cascade Delete Related Records
When you delete a record,
Access will automatically
delete the matching records in
a related table.
10
Administration
Higher
Relational Databases
------------------------------------------------------------------------------------------------------------------------
Click on Enforce Referential Integrity, Cascade Update Related Fields and Cascade
Delete Related Records as shown below:
Click on
create.
Click on the 3
boxes as
shown.
10 Now follow the same steps from above and drag Course Code field from the
Courses Applied For table to the Course table. Select the 3 boxes in Edit
relationships as before.
Your screen will now be the same as below:
RELATIONSHIP OF ONE TO MANY
RELATIONSHIP OF ONE TO MANY
There is only ONE employee with each
employee number in the staff table
but they could have applied for MANY
courses.
Each course has a unique code i.e.
ONE code for each course but MANY
staff members could have applied to
attend the course.
11
Administration
Higher
Relational Databases
------------------------------------------------------------------------------------------------------------------------
11 Close the Relationship window and save it when prompted.
12 Now open up the query grid and add the 3 tables and the fields as previously.
Your screen should be the same as below:
You may have to adjust the tables to the appropriate positions by clicking and
dragging them to where you want them to be. This makes no difference to the
query working but helps with the understanding of what we are trying to achieve.
Key in Yes in the
criteria box under
Course Granted.
13 Click on the run icon to obtain the results of your query.
14 The results of the query are shown below:
15 Save the query as Successful applications and print one copy.
12
Administration
Higher
Relational Databases
------------------------------------------------------------------------------------------------------------------------
PRACTICAL MATERIAL 1
1 Open up the Hospital Database and have a look at the 3 tables.
2 No primary keys have been set yet but they will need to be before we can create
a relationship between the 3 tables.
3 Set the primary keys - remember they need to be unique identifiers.
4 You should have set Consultant ID as a primary key in the Doctor table and
Patient ID in the Patient table.
5 Now click on the relationships icon on the toolbar and add the 3 tables. (You can
select all 3 at once by holding your CTRL key and then click on add).
6 Rearrange the tables so that Appointments table is in the middle.
7 Drag Consultant ID from the doctor table to Consultant ID in the Appointment
table.
8 In Edit relationships window only click on the 3 boxes to enforce referential
integrity.
9 Now drag Patient ID from the Patient table to Patient ID in the Appointments
table. Again click on the 3 boxes in the Edit relationships window.
10 Close the window and save the relationship.
11 Create a query to list all the Consultant IDs and their surnames (from the Doctor
table), along with Dates and Times of Appointments (from the Appointments
table), as well as patients' Forenames and Surnames (from the Patient table).
12 If everything is correct the result will be as shown below:
13 Save the query and print one copy of it.
13
Administration
Higher
Relational Databases
------------------------------------------------------------------------------------------------------------------------
PRACTICE MATERIAL 2
1 Open the schools database and look at the 3 tables.
2 Set appropriate primary keys.
3 Create a relationship between the 3 tables.
4 Now query the database to find Mrs Potter’s pupils showing their forename and
surname as well as the Year Head for each of them.
5 If you have performed all the steps above correctly you should end up with 2
records.
6 Save the query as Mrs Potter’s pupils and print one copy of the file.
14
Administration
Higher
Relational Databases
------------------------------------------------------------------------------------------------------------------------
PRACTICE MATERIAL 3
1 Open up the Schools database.
2 Using the Report Wizard create a Report with the following fields:
From the Guidance Table

Teacher
From the Pupil Information Table




First Name
Surname
Address
Town
From the Year Head Table

Depute Name
3 Set a grouping of Depute Name.
4 Set a Grouping level of Guidance Teacher.
5 Sort on ascending order of Surname.
6 Choose the layout of Align Left 1 and landscape.
7 Select the casual style.
8 Insert Pupil Report as the name.
9 Adjust the fonts and layout so all data is visible and well presented.
10 Print one copy of the Report and submit for marking.
15
Download