Sei sulla pagina 1di 33

ODL225 Database Systems

06A2 Team A Coursework 2

HOSPITAL INFORMATION SYSTEM


ODL225 Database Systems

Team A - Deliverable 2
Contents
1
2
3

Introduction ................................................................................................................... 2
Original Relational Schema.......................................................................................... 2
Changes to Relational Schema.................................................................................... 2
3.1 Blood Type............................................................................................................... 2
3.2 Primary Keys ........................................................................................................... 3
3.3 Patient Notes Relation............................................................................................. 4
3.3.1 Attribute Description.......................................................................................... 4
3.3.2 First Normal Form ............................................................................................. 5
3.3.3 Second Normal Form ........................................................................................ 5
3.3.4 Third Normal Form ............................................................................................ 5
3.3.5 Boyce-Codd Normal Form ................................................................................ 5
3.3.6 Fourth Normal Form.......................................................................................... 5
3.4 Revised Relational Schema .................................................................................... 6
4 Implementing the schema ............................................................................................ 6
5 Sample Data................................................................................................................. 7
6 Views ............................................................................................................................ 7
6.1 Blood Type (View 1) ................................................................................................ 7
6.2 Wards (View 2) ........................................................................................................ 8
6.3 Doctors Appointments (View 3) .............................................................................. 9
6.4 Occupied Bed (View 4)............................................................................................ 9
7 SQL Queries ............................................................................................................... 10
7.1 Patient Details (SQL Query 1) ............................................................................... 10
7.2 Patient Occupyind a Bed (SQL Query 2) .............................................................. 11
7.3 Bed Occupation (SQL Query 3)............................................................................. 11
7.4 Daily Patients on a Ward (SQL Query 4) .............................................................. 12
7.5 Speciality Nurse (SQL Query 5) ............................................................................ 12
7.6 Unoccupied beds (SQL Query 6) .......................................................................... 13
7.7 Nurse Specialities (SQL Query 7) ......................................................................... 13
7.8 Timed Clinic Appointments (SQL Query 8) ........................................................... 14
7.9 Change Appointment (SQL Query 9) .................................................................... 14
7.10 Ward Nurses (SQL Query 10) ............................................................................... 15
7.11 Doctors Patient List (SQL Query 11) .................................................................... 16
7.12 Nurses on Duty (SQL Query 12) ........................................................................... 17
8 References ................................................................................................................. 17
APPENDIX I Test Data .................................................................................................. 18
CREATE TABLE statements ......................................................................................... 18
Sample Data .................................................................................................................. 20
APPENDIX II - Example Screen Shots ............................................................................. 26
APPENDIX III - Data Dictionary ........................................................................................ 30

Page 1 of 1

ODL225 Database Systems

06A2 Team A Coursework 2

Introduction

This system has been designed to facilitate the accessibility of hospital appointment records and in-patient
stays. Included are details of patients and staff combining to create a full medical history.
This deliverable is concerned with the implementation of the relational schema using PostgreSQL ORDBMS.
The implementation of the schema uses the design produced in deliverable 1.

Original Relational Schema

The relations in 4NF from deliverable 1 are shown in the following table. Primary Keys are shown in red, foreign
keys are bordered with blue.
Entity

Attributes

Appointment

( appointmentNo.

Bed

bedNo.

wardID

Doctor

doctorID

GMCno

employeeID )

Employee

employeeID,

firstName,

InpatientStay ( admittanceNo.

date

time

surname,

admitDate,

Nurse

nurseID

UKCCPIN,

Patient

patientID,

firstName,

Qualification

( qualificationID

Shift

ShiftID

Treatment

Ward
NurseQual.

location

doctorID

address,

telNo,

treatmentNo )

admitTime, dischargeDate, dischargeTime,

dOB

jobType,

natInsNo,

doctorID

bedID,

treatmentNo )

nHSNo.,

DOB,

bloodType )

employeeID, )
surname,

address,

telNo,

nurseID

date

shift

wardID

treatmentNo,

notes,

patientID

wardID

wardName,

floor,

nurseID

qualification )
)

)
qualificationID )

qualificationID )

Fig 1. Relations normalised to 4NF.

Changes to Relational Schema

3.1

Blood Type

This value was originally designed as a char(2) to cater for the A, B & O blood types with their +/- Rh marker
suffix. Unfortunately this did not take account of the AB+ & AB- blood types so the column was amended to a
Varchar(3)

Page 2 of 2

ODL225 Database Systems

3.2

06A2 Team A Coursework 2

Primary Keys

The original schema defined the majority of Primary Keys as unique, not null, CHAR(x) data types with a leading
alpha character for identification as follows:
Data
Type

Nulls?

Domains

Char(5)

E0000-E9999

Uniquely identifies a doctor

Char(5)

D0000-D9999

nurseID

Uniquely identifies a nurse

char(5)

N0000-N9999

qualification

qualificationID

Uniquely
qualification

char(5)

Q0000-Q9999

patient

patientID

Uniquely identifies a patient

char(6)

P00000-P99999

treatment

treatmentID

Uniquely identifies a treatment

char(7)

T000000T999999

appointment

appointmentID

Uniquely
identifies
appointment

char(6)

A00000-A99999

ward

wardID

Uniquely identifies a ward

char(2)

00-99

bed

bedID

Uniquely identifies a bed

char(3)

000-999

inpatientStay

inpatienStayID

Uniquely identifies a doctor

char(6)

I00000-I99999

shift

shiftID

Uniquely identifies a shift

char(6)

S0000-S9999

Entity

Attribute

Description

employee

employeeID

Uniquely
employee

doctor

doctirID

nurse

identifies

an

identifies

an

Fig 2. Primary Keys from Original Relations


Displaying these attributes in the proposed alpha-numeric form is useful for the human eye but is not so useful
for the number-crunching database. SQL does not have an Auto_Increment function to provide the next unique
key so requires a number of steps to be performed to find the maximum value already used and increment it.
The following demonstrates an example of the steps required to calculate the next doctorID value:

1. Find the maximum value used as an ID in the doctors table


maxID = SELECT max(doctorID) FROM doctor = D0012

2. Capture the numeric part of the expression by left trimming the D and leading zeros
numberPart = ltrim(maxID,D0) = 12
3. Convert this to an integer using the cast function
intNumber = CAST(numberPart AS integer) = 12
4. Increment the integer
intNumber = intNumber +1 = 13
5. Cast the integer back to a VARCHAR. VARCHAR is used as a CHAR appends unwanted spaces at
the end of the string.
strNumber = CAST(incNumber AS varchar(4)) = 13
6. Convert the string to a four digit string with leading zeros by left padding with 0
digitString = lpad(strNumber, 4, 0) = 0013
7. Prefix the digit string by concatenating with the original identifier prefix
nextKey = D || digitString = D0013
Page 3 of 3

ODL225 Database Systems

06A2 Team A Coursework 2

As a single function this becomes


SELECT ('D' || lpad( CAST( CAST(ltrim(max(doctorID),'D0') AS integer) +1 AS varchar(4)), 4, '0') ) FROM
doctor;
Although this solution is feasible for automatically incrementing the primary key, the build may be simplified by
using an integer for the primary key in the first place, in which case the query to find the next available Primary
Key becomes
(SELECT max(doctorID) FROM doctor) + 1
and this can be used in an insert command as follows:
INSERT INTO doctor VALUES (
((SELECT max(doctorID) FROM doctor) + 1),123456,56);
The management of the Primary Key insertion can be simplified even further by making the integer column of
SERIAL data type. This is more akin with an Auto-Increment structure found in other databases.
To insert the next value of the sequence into the serial column, the serial column should be assigned its default
value. This is achieved either by excluding the column from the list of columns in the INSERT statement,
INSERT INTO doctor (GMCNo, employeeID) VALUES (123456,56);
or through using the DEFAULT key word.
INSERT INTO doctor VALUES (DEFAULT,123456,56);
This method was therefore adopted for the sequential Primary Keys listed above.

3.3

Patient Notes Relation

Testing of the database highlighted that, although there is a field in the treatment table for recording a note
(which may be sufficient for a one off appointment), there is not much scope for making several notes, especially
when the patient may stay as an inpatient for a longer period of time, or where a treatment may involve several
appointments. The original design meant that a single treatment can have many appointments / inpatient stays,
but only a single note per treatment. A new relation, patientNotes, was added to facilitate this.

3.3.1
Entity

Attribute Description
Attribute

Data type

Nulls?

Domains

series

Integer

date

Todays date

int

Integer

treatmentID Foreign Key to identify the treatment that the patient is


receiving

int

Integer

doctorsNote Field for writing the doctors notes

text

Free text

PatientNotes NotesID

Description
Uniquely identifies a single note written by a doctor

noteDate

Date that the note was written

doctorID

Foreign Key to identify the doctor writing the note

Fig 3. Data dictionary entry for PatientNotes entity

Page 4 of 4

ODL225 Database Systems

3.3.2

06A2 Team A Coursework 2

First Normal Form

The PatientNotes table is in 1NF as, for each relation, the intersection of each row and column contains one and
only one value. There are no empty cells within the table.
notesid

notedate

doctorid

treatmentid

doctorsnote

01/01/2000

203

90

BM test showed negative for diabetes

05/09/2005

203

400

Referred by GP. Diagnosed with Type 2 Diabetes. Treatment by diet


and tables

06/12/2005

203

400

Responding well to diet and medication treatment. Continue 3


monthly check ups

06/12/2005

203

400

Responding well to diet and medication treatment. Continue 3


monthly check ups

01/02/2006

215

401

Admitted to A&E. Fell @ home. X-Ray confirmed fractured L Neck


of Femur. Admitted to Barclay Ward

01/02/2006

216

401

Initial examination: Referred for emergency L hip replacement.


Analgesia prescribed

Fig 4. Sample data for PatientNotes entity

3.3.3

Second Normal Form

The PatientNotes table is in 2NF as the only candidate key is the Primary key so every non-candidate-key
attribute is fully functionally dependent on the primary key.

3.3.4

Third Normal Form

The PatientNotes table, which is in 1NF & 2NF, is in 3NF as no non-primary-key attribute is transitively
dependent on the primary key.
fd1
notesid -> notedate, doctorid, treatmentid, doctorsnote

3.3.5

Boyce-Codd Normal Form

The only determinate is the notesid attribute which is the only candidate key and therefore the primary key.
Therefore this table is in BCNF as all determinants are a candidate key for the relation.

3.3.6

Fourth Normal Form

This is also in 4NF as it is in BCNF and does not contain any non-trivial multi-valued dependencies.

Page 5 of 5

ODL225 Database Systems

3.4

06A2 Team A Coursework 2

Revised Relational Schema

The revised relations in 4NF are shown in the following table. Primary Keys are shown in red, foreign keys are
bordered with blue.
Entity

Attributes

Appointment

( appointmentNo.

aptdate

Bed

bedNo.

wardID

Doctor

doctorID

GMCno

employeeID )

Employee

employeeID,

firstName,

InpatientStay ( admittanceNo.

apttime

surname,

admitDate,

Nurse

nurseID

UKCCPIN,

Patient

patientID,

firstName,

Qualification

( qualificationID

Shift

ShiftID

Treatment

Ward

location

doctorID

address,

telNo,

treatmentID )

admitTime, dischargeDate, dischargeTime,

dOB

jobType,

natInsNo,

doctorID

bedID,

treatmentID )

nHSNo.,

DOB,

bloodType )

employeeID, )
surname,

address,

telNo,

nurseID

shiftDate

shift

wardID

treatmentID,

notes,

patientID

wardID

wardName,

floor,

NurseQual.

nurseID

PatientNotes

notesID

doctorsNote

qualification )

)
qualificationID )

qualificationID )
notesDate

doctorID

treatmentID

Fig 5. Revised relations normalised to 4NF.

Implementing the schema

In order to implement the relational schema, Structured Query Language (SQL) was used. SQL is a database
language that allows databases to be created, data to be inserted, modified and deleted from the database, and
allows queries on the data within the database to be performed.
PostgreSQL database management system was used to implement the relational schema. This was accessed
using DBVisualiser. A database management system (DBMS) is used to build, manage and control access to a
database.
The SQL Data Definition Language (DDL) allows databases to be created. Using SQL, a schema (collection of
database objects) is created, and the relations derived in deliverable 1 (shown in section 2), are converted into
tables.
The schema is created using the statement:
CREATE SCHEMA [Name | AUTHORISATION CreatorIdentifier]
Due to authorisation restrictions with PostgreSQL, default schemas are allocated to users. The name of the
schema is the same as the user's username, and authorisation is restricted solely to that user.
To create tables for the relations, the following statement was used:
CREATE TABLE TableName (
columnName1 dataType [NOT NULL],
...
columnNameN dataType [NOT NULL],
[PRIMARY KEY (listOfColumns),]
{[UNIQUE (listOfColumns)]}
{[FOREIGN KEY (listOfForeignKeyColumns) REFERENCES ParentTableName,]}

Page 6 of 6

ODL225 Database Systems

06A2 Team A Coursework 2

The 'CREATE TABLE' statement creates a table called 'TableName'. For the implementation of this
database, the table names are the same as the relation names given in Section 3.
Columns are created within the table that correspond to the attributes of the relation. The columns are
assigned the data types as given in the fig32 in Appendix II.
'NOT NULL' is used to indicate that data must always be entered in each row of the column - cannot be
nudl.
For each table, a primary key must be specified. The primary keys are those identified in fig 5in section
3.
The UNIQUE statement is used to ensure the uniqueness of for any alternate keys used within the table.
The FOREIGN KEY statement is used to show that a foreign key has a relationship with another table.
Foreign keys were identified in deliverable 1 and are shown in fig 5 in section 3.
'Serial' is just for setting up unique primary keys

A listing of all the 'Create Table' commands that were used to set up the database is shown in Appendix I. The
'Create Table' statements were executed in the order shown. This is because certain tables had to be created
first due to the foreign key assignments. In order to maintain referential integrity, a table with foreign keys cannot
be created unless the table that the foreign key references already exists.

Sample Data

A sample of the data used for populating the database is shoen in Appendix I. The actual data is included as text
files as part of the submission. Sufficient data has been generated to allow queries to be tests to see if they will
give the correct result, regardless of data values.
To add data to the database, the INSERT statement was used as it was not possible to use the COPY command
for inserting bulk data into the dabase on the student server due to super user constraints:
INSERT INTO TableName VALUES (dataValueList)

This statement allows a single row of data to be added to a table 'TableName'.

'dataValueList' is the list of data that is to be added to the table. The number of items in the list must
match the number of columns in the table. The data type of each item must also match with that
specified for each column.
The complete list of commands for loading the data is contained in the directory that accompanies this report.
Each set of INSERT commands are contained in a text file and, again to the foreign key assignments, must be
loaded in the correct numerical sequence as defined by the filenames.
The sample data for the PatientNotes was loaded as a scenario following the progress of a single patient
through the hospital system. This is documented in Patient Treatment.pdf, which is also contained within the
accompanying directory .

Views

Task:

Define specialist views which are appropriate to various sub-groups or individual users
A set of 3-4 view definitions (create 'view' commands), with copies of the output you obtain, when you
list these views. A view definition, in this context, consists of both the English explanation of what the
view is intended to do AND the SQL command itself.
Views are used for various reasons, such as used to hide parts of the database from certain users or allowing
the presentation of the data within the database to be customised to specific users. A view looks like a table,
with columns and data, but does not necessarily exist as a relation within the relational schema. A view is a
query on one or more tables, or other views. The CREATE VIEW statement is used to create a view.

6.1

Blood Type (View 1)

The first view is PatientBloodtype which shows only the columns containing the patients first and last name and
their blood type. From this view I have selected only patients with a blood type of B+
Create a view of patients' names and bloodtypes:
Page 7 of 7

ODL225 Database Systems

06A2 Team A Coursework 2

CREATE VIEW PatientBloodType


AS SELECT firstname,surname,bloodtype
FROM Patient;
Using view 'PatientBloodType', choose only patients with a blood type of B+
SELECT *
FROM PatientBloodType
WHERE bloodtype = 'B+';
Output:
firstname

surname

bloodtype

Jessica
Williams
Ryan
Paterson
Jacob
Robertson
Jose
Smith
Michael
Ross
Joseph
Wilson
Kyle
Morrison
Nicole
Anderson
Adam
Jones
Fig 6. View 1 output

6.2

B+
B+
B+
B+
B+
B+
B+
B+
B+

Wards (View 2)

The view WardSpecialism returns the specialism associated with each ward and the floor that the ward is on.
Note that the specialism is referred to as qualification in the database, which links it to nurse qualifications. All
wards are selected in the sample output.
Create a view of specialisms, ward names and floors:
CREATE VIEW WardSpecialism
AS SELECT q.qualification,w.wardname, w.floor
FROM Qualification q, Ward w
WHERE q.qualificationID = w.qualificationID
ORDER BY q.qualification;
Using view 'WardSpecialism', show all ward names, their specialisms and the floor that the ward is on:
SELECT * FROM WardSpecialism;
Output:
qualification

wardname

floor

Cardiology
Dermatology
Endocinology
General
General
General
General
General
General
General
General
General
General
Gynaecology
Haematology

Coronary Care Unit


Dunston Ward
Elsing Ward
Barclay Ward
Coltishall Ward
Davidson Unit
Dilham Ward
Docking Ward
Earsham ward
Easton Ward
Edgefield Ward
Jack Pryor Unit
Rheumatology Day Wd
Cley Ward
Mulbarton Ward

2
1
2
G
1
G
2
G
2
G
1
2
1
G
G

Page 8 of 8

ODL225 Database Systems

06A2 Team A Coursework 2


Maternity
Oncology
Orthopaedics
Paediatrics
Renal

Blakeney Ward
Heydon Ward
Denton ward
Buxton Ward
Hethel Ward

1
1
1
2
G

Fig 7. View 2 output

6.3

Doctors Appointments (View 3)

The view DoctorAppt displays the Doctor ID, Appointment number, Date, Time and location of scheduled patient
appointments. The sample output data is for the Doctor ID 205 on 9th Feb 2006.
NB Date format must be set to European (set datestyle to 'European';)
Create a view of doctorID, appointmentNo, aptdate, apttime and location:
CREATE VIEW DoctorAppt
AS SELECT d.doctorID,a.appointmentNo,a.aptdate,a.apttime,a.location
FROM doctor d, appointment a
WHERE d.doctorID=a.doctorID
ORDER BY a.aptdate;
Using view 'DoctorAppt', show appointment details for doctor 205 on 09/02/2006:
SELECT * FROM DoctorAppt
WHERE doctorID=5 AND aptdate = '09/02/2006';
Output:
doctorid appointmentno
205
205

6.4

aptdate

apttime

115
09/02/2006 14:00:00
116
09/02/2006 16:00:00
Fig 8. View 3 output

location
clinic 5
clinic 1

Occupied Bed (View 4)

View 4 shows which bed and ward a patient is or has been on during an inpatient stay. The SELECT query
below searches for all patients with the surname Brown who have had an inpatient stay.
Create a view of patient-ward allocations:
CREATE VIEW PatientWard
AS SELECT p.patientID,p.firstname,p.surname, b.bedID,w.wardname
FROM Patient p, Treatment t, InPatientStay i, Bed b, Ward w
WHERE p.patientID=t.patientID AND t.treatmentID = i.treatmentID AND i.bedID = b.bedID
AND b.wardID = w.wardID
ORDER BY p.patientID;
Using view 'PatientWard', show patient-ward allocations for patients with the surname 'Brown':
SELECT * FROM PatientWard
WHERE surname = Smith;
Output:
patientid
20
58
58
77
77
96
96

firstname

surname

bedid

wardname

Stephanie
Smith
44
Dilham Ward
David
Smith
10
Blakeney Ward
David
Smith
37
Denton ward
Jacob
Smith
14
Buxton Ward
Jacob
Smith
26
Coronary Care Unit
Courtney
Smith
30
Coronary Care Unit
Courtney
Smith
42
Dilham Ward
Fig 9. View 4 output
Page 9 of 9

ODL225 Database Systems

06A2 Team A Coursework 2

To only select patients admitted on a particular day, an extra clause with the date would be needed as shown
below.
SELECT * FROM PatientWard
WHERE surname = 'Smith' AND patientID IN (
SELECT patientID FROM Treatment
WHERE treatmentID IN (
SELECT treatmentID FROM InPatientStay
WHERE admitDate = '08/01/2006'));
Output:
patientid firstname surname bedid
20

wardname

Stephanie Smith
44
Dilham Ward
Fig 10. View 4 output with extra clause
th

Therefore Stephanie Smith was the only patient with the surname Smith admitted on the 8 January 2006.

SQL Queries

This section defines SQL queries which could be used as pre-programmed queries by users. Included are a set
of 12 SQL queries, of varying complexity, with the results of the output obtained when you run the queries were
run. Both the English explanation of what the query is intended to do AND the SQL query itself have been given.
An SQL query is often in the form:
SELECT {*|[columnExpression [AS newNAME]],[...]}
FROM TableName [alias] [...]
WHERE condition
The select statement is used to find and show data from one or more database tables. Following 'SELECT', the
columns that you want to be displayed can be specified. An asterisk can be used to select all columns. 'FROM' is
used to specify the table(s) from which the data will be returned. 'WHERE' is used as a filter to return only those
rows that satisfy a certain condition. There are other key words that are used with the select statement; these will
be defined when used in the following queries.

7.1

Patient Details (SQL Query 1)

Show patient details for treatmentNo 244:


SELECT p.patientID, p.firstName, p.surname
FROM patient p, treatment t
WHERE (t.patientID=p.patientID) AND (t.treatmentID='54');
The query uses two tables, Patient and Treatment. The query returns the necessary details for all patients who
have their patientIDs associated with a treatment, and the treatmentID for that treatment is '244'.
Output:
patientid firstname surname
44
Amber
Wilson
Fig 11. SQL query 1 output

Page 10 of 10

ODL225 Database Systems

7.2

06A2 Team A Coursework 2

Patient Occupyind a Bed (SQL Query 2)

Show all details of the patient who was admitted to bed 220 on 26th Jan 2006.
SELECT *
FROM patient
WHERE patientid =
(SELECT patientid
FROM treatment
WHERE treatmentid =
(SELECT treatmentid
FROM inpatientstay
WHERE bedid ='20' AND admitdate='26/01/2006'));
This query uses three tables, Patient, Treatment and InPatientStay. A search of the InpatientStay table is carried
out for all rows where the bedID is equal to '220' and the admitdate is equal to '26/01/2006'. Only the
treatmentIDs from these rows are returned. The Treatment table is then searched for all rows where the
treatmentIDs found in the InpatientStay table match a treatmentID in the Treatment table. Only the patientIDs
from the Treatment table are returned. A search is then carried out of the Patient table for all rows where the
patientIIDs found in the Treatment table match a patientID in the Patient table. All of the columns from any
matching rows are then shown.
Output:
patientid firstname surname
60

Andrew

Williams

address

telno

nhsno

dob

bloodtype

The Pines, Patterson Road,


Burnham Market

01263 648383

1948638105

17/08/1957

O+

Fig 12. SQL query 2 output

7.3

Bed Occupation (SQL Query 3)

Show names and patient IDs of patients who have stayed in bed 220:
SELECT firstname,surname,patientid
FROM patient
WHERE patientid IN
(SELECT patientid
FROM treatment
WHERE treatmentid IN
(SELECT treatmentid
FROM inpatientstay
WHERE bedid ='20' ));
This query looks at three tables, Patient, Treatment and InPatientStay. 'IN' is used to test a data value for
membership. A search of the InPatientStay table is performed for all treatments with a bedID of '220'. The
treatmentID for each matching row is returned. Each row of the Treatment table is then tested to see if the
treatmentID is a member of the set of results returned from the InpatientStay table. The patientIDs from all
matching rows are returned. Each row of the Patient table is then tested to see if the patientID is a member of
the set of results returned from the Treatment table. The firstname, surname and patientID of all matching rows
are returned.
Output:
firstname surname patientid
Jacob Robertson
27
Andrew Williams
60
Brian
Walker
90
Fig 13. SQL query 3 output

Page 11 of 11

ODL225 Database Systems

7.4

06A2 Team A Coursework 2

Daily Patients on a Ward (SQL Query 4)

ID's, names and addresses of patients admitted to the Buxton Ward on the 27th Jan 2006.
SELECT patientid,firstname,surname,address
FROM patient
WHERE patientid IN
(SELECT patientid
FROM treatment
WHERE treatmentid in
(SELECT treatmentid
FROM inpatientstay
WHERE admitdate ='27/01/2006' AND bedid IN
(SELECT bedid
FROM ward
WHERE wardid =
(SELECT wardid
FROM ward
WHERE wardname= 'Buxton Ward'))));
This query looks at four tables, Patient, Treatment, InPatientStay and WARD. The wardID of Buxton Ward is
found first of all. All rows in the Bed table are then searched for wardIDs that match the wardID of Buxton ward.
The InpatientStay table is then searched for rows where admitDate is 27/01/2006 and where the assigned bed is
in Buxton Ward. The treatmentIDs of all matching rows are returned. The Treatment table is searched next for all
rows where the treatmentID is also a member of the set of previously returned treatmentIDs. The patientIDs of
all matching rows are returned. Finally, the Patient table is searched for all rows where the patientID is also in
the set of previously returned patientIDs. The patientID, firstname, surname and address of all matching rows
are then displayed.

Output:
patientid firstname surname
65
66
67
68

7.5

address

Ryan Robertson
98 Norwich Road, New Costessey
Robert Campbell
1 Tickle Avenue, Lourd
Nicholas Stewart Fordham House, Cheese Road, Walsingham
Sarah Anderson
2The Croft, Guist
Fig 14. SQL query 4 output

Speciality Nurse (SQL Query 5)

Lists the names and employee ID of all nurses qualified in orthopaedics.


SELECT firstname,surname,employeeid
FROM employee
WHERE employeeid IN
(SELECT employeeid
FROM nurse
WHERE nurseid IN
(SELECT nurseid
FROM nursequal
WHERE qualificationid IN
(SELECT qualificationid
FROM qualification
WHERE qualification ='Orthopaedics')));
This query looks at four tables, Qualification, NurseQual, Nurse and Employee. The qualificationID of
Orthopaedics is found first of all. All rows in the NurseQual are then searched for qualificationIDs that match the
qualificationID of Orthopaedics. The Nurse table is then searched for rows where the nurseID is also a member
of the set of previously returned nurseIDs. The employeeIDs of matching rows are returned. Finally, the
Employee table is searched for all rows where the employeeID is also in the set of previously returned
employeeIDs. The firstname, surname and employeeID of all matching rows are then displayed.
Page 12 of 12

ODL225 Database Systems

06A2 Team A Coursework 2

Output:
firstname surname employeeid
William
Moon
53
Xander
Nappy
54
John
Lennon
83
Paul McCartney
84
Fig 15. SQL query 5 output

7.6

Unoccupied beds (SQL Query 6)

How many beds were un-occupied on the on 5th January 2006


SELECT d.bedCount - d.bedOccupied AS unoccupiedbeds
FROM (SELECT COUNT(DISTINCT b.bedID) AS bedCount, COUNT(DISTINCT i.bedID) AS
bedOccupied
FROM bed b, inpatientstay i
WHERE (i.admitdate<='08/01/2006') AND (i.discharge>='08/01/2006')) d;
This query uses 'COUNT' and 'DISTINCT' as part of the select statement. 'COUNT' is used to return the number
of matching rows from a query, rather than the rows themselves. 'DISTINCT' is used to eliminate duplicate rows
from the result of a query. Aliases are also used in this query. Aliases are used to avoid confusion when using
multi-table queries, to show which column name that a table is associated with.
This query looks at two tables, Bed and InPatientStay. The InPatientStay table is checked for all rows where a
bed is occupied on the 8th January 2006. At the same time, the number of beds in the hospital is calculated by
returning the number of rows in the Bed table. These two results are temporarily put into a new table, aliased 'd'.
Table 'd' is given column headings 'bedCount' and 'bedOccupied'. The value of 'bedOccupied' is then subtracted
from 'bedCount', and the result returned in a new table with a single column titled 'unoccupiedbeds'.
Output:
unoccupiedbeds
93
Fig 16. SQL query 6 output

7.7

Nurse Specialities (SQL Query 7)

List of qualifications the nurse Roger Hill possesses.


SELECT qualification
FROM qualification
WHERE qualificationid IN
( SELECT qualificationid
FROM nursequal
WHERE nurseid =
(SELECT nurseid
FROM nurse
WHERE employeeid =
(SELECT employeeid
FROM employee
WHERE firstname = 'Roger' AND surname = 'Hill')));
This query looks at four tables. The Employee table is searched for the employeeID of Roger Hill. The
employeeID is then used to find the nurseID of Roger Hill. The NurseQualification table is then searched for all
rows where the nurseID of Roger Hill matches a nurseID. The qualificationIDs of all matching rows are returned.
Finally, the Qualification table is searched for the qualification name of all rows where the qualificationID
matches a qualificationID from the set of qualificationIDs returned previously. A table is returned that displays
the names of all qualifications held.
Page 13 of 13

ODL225 Database Systems

06A2 Team A Coursework 2

Output:
qualification
Cardiology
Renal
Dermatology
Fig 17. SQL query 7 output

7.8

Timed Clinic Appointments (SQL Query 8)

Show all 10:00 appointments in clinic 1:


SELECT *
FROM appointment
WHERE apttime = '10:00' AND location='clinic 1'
ORDER BY appointmentNo;
This query uses a single table, Appointment. All columns of rows are displayed, where the appointment time is
10:00 and the location of the appointment is 'clinic 1'. The results are ordered by the appointmentNo. 'ORDER
BY' is a keyword used to specify an order in which the results of a query are to be displayed.
Output:
appointmentno
1
21
41
61
81
101

7.9

aptdate

apttime

location

doctorid

treatmentid

1
1
6
6
1
1

11
43
75
111
143
175

02/01/2006 10:00:00 clinic 1


09/01/2006 10:00:00 clinic 1
16/01/2006 10:00:00 clinic 1
23/01/2006 10:00:00 clinic 1
30/01/2006 10:00:00 clinic 1
06/02/2006 10:00:00 clinic 1
Fig 18. SQL query 8 output

Change Appointment (SQL Query 9)

Change location of appointments in clinic 1 between 01/02/2006 and 28/02/2006 from clinic 1 to a new 'clinic 9',
then show the names and addresses of all affected patients.
UPDATE appointment SET location = 'clinic 9'
WHERE location = 'clinic 1'
AND aptdate BETWEEN '1/2/2006' AND '28/2/2006';
SELECT surname, firstname, address
FROM patient INNER JOIN treatment USING (patientid)
WHERE treatmentid IN
(SELECT treatmentid
FROM appointment
WHERE location ='clinic 9')
ORDER BY surname;
This query uses four new keywords:

UPDATE is used to change existing data within a specified table.

INNER JOIN is used to produce the Cartesian product of subsets of two tables. Tables are joined by
forming pairs of related rows. A column is specified where matching data is used to join the tables. An
inner join omits rows with no matching data. In the above query, Patient and Treatment tables are
combined. Rows that have a matching patientID are combined. 'USING' is used to specify the column in
the tables to use as matching data.

BETWEEN is used to specify a range of data.


Page 14 of 14

ODL225 Database Systems

06A2 Team A Coursework 2

This query looks at three tables, Appointment, Patient and Treatment. The Appointment table is searched for
rows where the location of the appointment is 'clinic 1' and the appointment date is between 1st February and
28th February. The location of all affected rows is changed to 'clinic 9'. This part of the statement does not return
a table, it just changes the required data within the Appointment table.
The next part of the query searched the Appointment table for rows where the location of the appointment is
'clinic 9' and the appointment date is between 1st February and 28th February. The treatmentIDs of all matching
rows are returned. The Patient and Treatment tables are joined using patientIDs. Only those rows where the
treatmentID is in the set of treatmentIDs returned previously are displayed. Of those rows, only the first name,
surname and address are displayed. The results are ordered by the surname of the patient.
Output:
surname firstname

address

Robertson
Ryan
98 Norwich Road, New Costessey
Ross
Michael
99 Holt Road, Fakenham
Thomson Megan
High Street, Mulbarton
Watson Jennifer
The Cottage, Whitwell Street, Reepham
Williams Andrew The Pines, Patterson Road, Burnham Market
Young
Jordan
Hopfield lane
Fig 19. SQL query 9 output

7.10

Ward Nurses (SQL Query 10)

Show all nurses able to work on Buxton Ward


SELECT surname, firstname
FROM employee INNER JOIN nurse USING (employeeid)
WHERE nurseid IN
(SELECT nurseid
FROM nursequal INNER JOIN ward USING (qualificationid)
WHERE wardname = 'Buxton Ward')
ORDER BY surname, firstname;
This query looks at four tables, Employee, Nurse, NuresQual and Ward. The NurseQual and Ward tables are
joined using qualificationIDs. Only the nurseIDs of those rows of the combined table where the name of the ward
is 'Buxton Ward' are returned. The Employee and Nurse tables are joined, using employeeIDs. The first names
and surnames of those rows of the combined table, where the nurseID is in the set of previously returned
nurseIDs, are displayed. The results are ordered by surname, then by first name.
Output:
surname firstname
Brown
Laura
Crown
Mandy
Simpson Charles
Xad
Henry
Yacht
Ian
Fig 20. SQL query 10 output

Page 15 of 15

ODL225 Database Systems

7.11

06A2 Team A Coursework 2

Doctors Patient List (SQL Query 11)

Show all patients seen by doctor George Wall:


SELECT p.firstName, p.surname, p.address, p.telNo
FROM patient p,
(SELECT t.patientID
FROM treatment t,
((SELECT treatmentID
FROM appointment
WHERE doctorID =
(SELECT doctorID
FROM doctor
WHERE employeeID =
(SELECT employeeID
FROM employee
WHERE 'Belinda' = firstname AND Ram=surname)))
UNION
(SELECT treatmentID
FROM inpatientstay
WHERE doctorID =
(SELECT doctorID
FROM doctor
WHERE employeeID =
(SELECT employeeID
FROM employee
WHERE Belinda = firstname AND Rame=surname)))) z
WHERE (t.treatmentID=z.treatmentID)) p2
WHERE p.patientID=p2.patientID
ORDER BY p.surname, p.firstname;
This query uses the 'UNION' key word. UNION is used to combine the rows of two tables that are either in the
first table, the second table, or in both tables.
A doctor can see a patient either during an appointment or during an in-patient stay. The details of these are
kept in separate tables. To see all of the patients seen by a doctor, both the Appointment and InPatientStay
tables have to be searched, and the results combined into a single table.
For this query, from the name of the doctor, their employeeID is found. This is then used to get their doctorID.
The Appointment table and the InPatientStay table are both checked for occurrences of this doctorID. Each table
is searched separately, and the patientIDs of the union of these results is returned. The patientIDs are used to
return selected data from the Patient table for all matching rows.

Page 16 of 16

ODL225 Database Systems

06A2 Team A Coursework 2

Output:

7.12

firstname

surname

address

telno

Adam
Matthew
Amanda
Emily
Xavier
Samantha
Daniel
William
Nicole
Alexander
Ryan
Jose
Zachary
Zachary
Jacob
Timothy
John
Kyle
Zachary

Anderson
Brown
Campbell
Campbell
Cerveca
Clark
Gray
Jones
Morrison
Paterson
Robertson
Ross
Ross
Ross
Smith
Thomson
Walker
Wilson
Wilson

2The Croft, Guist


1 Quiddenham Road, Attleborough
1 Tickle Avenue, Lourd
1 Tickle Avenue, Lourd
2 The Croft, Guist
154 Kingswood Avenue, Thorpe Marriott
16 The Street, Somerton
3 Market Place, Cawston
6B Phillips Road, Lowestoft
78 Station Road, North Elmham
98 Norwich Road, New Costessey
99 Holt Road, Fakenham
99 Holt Road, Fakenham
99 Holt Road, Fakenham
Flat 6C, Spitfire Place, Norwich
High Street, Mulbarton
Old Bakery, Queens Road, Fakenham
The Rectory, Rectory Road, Guestwick
The Rectory, Rectory Road, Guestwick
Fig 21. SQL query 11 output

01263 748383
01263 878345
01263 548383
01263 548383
01263 748383
01463 848383
01263 848383
01508 748383
01603 848383
01263 548383
01263 865433
01953 648383
01953 648383
01953 648383
01353 448383
01263 448383
01553 548383
01263 748521
01263 748521

Nurses on Duty (SQL Query 12)

Show how many different nurses were working between 02/02/2006 and 04/02/2006?
SELECT COUNT(DISTINCT nurseID) AS nursesWorking
FROM shift
WHERE shiftdate BETWEEN '02/02/2006' AND '04/02/2006';
This query looks at the Shift table. The table is searched for all rows where the shift date is between 2nd
February and 4th February (2006). As some nurses may work more than one day between theses dates,
DISTINCT is used to return only a single occurrence of their nurseID for each nurse. The returned rows are
counted, and the result is displayed in a new table with a single column 'nursesWorking'.
Output:
nursesworking
40
Fig 22. SQL query 12 output

References
th

Connolly, T & Begg, C. (2005) Database Systems. 4 Ed. Harlow: Addison Wesley
The PostgreSQLQL Global Development Group, 2005, PostgreSQLQL 8.1.3 Documentation, Available from:
http://www.PostgreSQLql.org/docs/8.1/static/index.html [Accessed 6th April 2006]

Page 17 of 17

ODL225 Database Systems

06A2 Team A Coursework 2

APPENDIX I Test Data


CREATE TABLE statements
CREATE TABLE employee (
employeeID SERIAL,
firstName varchar(15) NOT NULL,
surname varchar(15) NOT NULL,
address varchar(50) NOT NULL,
telNo varchar(12),
jobType varchar(10) NOT NULL,
natInsNo varchar(9) NOT NULL,
DOB Date NOT NULL,
PRIMARY KEY(employeeID),
UNIQUE (natInsNo));
CREATE TABLE doctor (
doctorID SERIAL,
GMCNo varchar(7),
employeeID int NOT NULL,
PRIMARY KEY(doctorID),
FOREIGN KEY (employeeID) REFERENCES employee,
UNIQUE (employeeID));
CREATE TABLE nurse (
nurseID SERIAL,
UKCCPIN char(8),
employeeID int NOT NULL,
PRIMARY KEY(nurseID),
FOREIGN KEY (employeeID) REFERENCES employee,
UNIQUE (employeeID));
CREATE TABLE qualification (
qualificationID SERIAL,
qualification varchar (20) NOT NULL,
PRIMARY KEY(qualificationID));
CREATE TABLE nurseQual (
nurseID SERIAL,
qualificationID int NOT NULL,
PRIMARY KEY(nurseID,qualificationID),
FOREIGN KEY (nurseID) REFERENCES nurse,
FOREIGN KEY (qualificationID) REFERENCES qualification);
CREATE TABLE patient (
patientID SERIAL,
firstName varchar(15) NOT NULL,
surname varchar(15) NOT NULL,
address varchar(50),
telNo varchar(12),
NHSNo varchar(10),
DOB Date,
bloodType varchar(3),
PRIMARY KEY(patientID));

Page 18 of 18

ODL225 Database Systems

06A2 Team A Coursework 2

CREATE TABLE treatment (


treatmentID SERIAL,
notes varchar (100),
patientID int NOT NULL,
PRIMARY KEY(treatmentID),
FOREIGN KEY (patientID) REFERENCES patient);
CREATE TABLE appointment (
appointmentNo SERIAL,
aptDate date NOT NULL,
aptTime time NOT NULL,
location varchar (10) NOT NULL,
doctorID int NOT NULL,
treatmentID int NOT NULL,
PRIMARY KEY(appointmentNo),
FOREIGN KEY (doctorID) REFERENCES doctor,
FOREIGN KEY (treatmentID) REFERENCES treatment);
CREATE TABLE ward (
wardID SERIAL,
wardName varchar (20),
floor char(1),
qualificationID int NOT NULL,
PRIMARY KEY(wardID),
FOREIGN KEY (qualificationID) REFERENCES qualification);
CREATE TABLE bed (
bedID SERIAL,
wardID int NOT NULL,
PRIMARY KEY(bedID),
FOREIGN KEY (wardID) REFERENCES ward);
CREATE TABLE inpatientStay (
admittanceNo SERIAL,
admitDate Date NOT NULL,
admitTime Time NOT NULL,
discharge Date,
dischargeTime Time,
doctorID int NOT NULL,
bedID int NOT NULL,
treatmentID int NOT NULL,
PRIMARY KEY (admittanceNo),
FOREIGN KEY (doctorID) REFERENCES doctor,
FOREIGN KEY (treatmentID) REFERENCES treatment,
FOREIGN KEY (bedID) REFERENCES bed);
CREATE TABLE shift (
shiftID SERIAL,
nurseID int NOT NULL,
shiftDate Date NOT NULL,
shift char (1) NOT NULL,
wardID int NOT NULL,
PRIMARY KEY(shiftID),
FOREIGN KEY (nurseID) REFERENCES nurse,
FOREIGN KEY (wardID) REFERENCES ward);

Page 19 of 19

ODL225 Database Systems

06A2 Team A Coursework 2

CREATE TABLE patientNotes (


notesID SERIAL,
noteDate date NOT NULL,
doctorID int NOT NULL,
treatmentID int NOT NULL,
doctorsNote text,
PRIMARY KEY(notesID),
FOREIGN KEY (treatmentID) REFERENCES treatment,
FOREIGN KEY (doctorID) REFERENCES doctor);

Sample Data
The following is a sample of data taken from the database tables
Employee
employeeid

firstname

surname

address

telno

jobtype

natinsno

dob

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

June
Amber
Jade
Mandy
Jill
Amy
Sue
Ann
Dot
Bev
Gail
Jane
Kim
Carol
Laura
Lyn
Liz
Susan
Helen
Sam

Barker
Barker
Corbett
Brown
Smith
Beven
Binks
Sim
Shipp
Shad
Santos
Sayer
Drake
Seago
Sewell
Skinner
Simkin
Skipper
Smart
Snow

Bath rd
exeter st
London rd
Deal way
The alley
Wick avenue
The links
The crescent
The backs
Beach rd
Sandy lane
Holt rd
High rd
Low rd
Back lane
The alley
Beach rd
The crescent
Bath rd
The alley

01263 982034
01263 102954
01219 034254
01263 129303
01263 983029
01263 981199
01263 987654
01263 222654
01229 183454
01263 293454
01293 487654
01263 438214
01263 123934
01263 103454
01381 432654
01238 391054
01263 982943
01229 987654
01263 203454
01374 283494

Nurse
Nurse
Nurse
Nurse
Nurse
Nurse
Nurse
Nurse
Nurse
Nurse
Nurse
Nurse
Nurse
Nurse
Nurse
Nurse
Nurse
Nurse
Nurse
Nurse

HV293944E
KI209554E
FG493943E
FG453043K
FG439423L
FG453044K
FG455233H
FG455420E
FG455438E
FG455419E
FG452954E
FG455448E
FG451954E
FG458354E
FG194554E
FG455544E
FG450254E
FG494554E
FG455054E
FG452754E

02/02/1983
02/03/1979
02/02/1973
02/02/1985
02/05/1979
02/12/1983
02/11/1979
02/05/1982
02/02/1986
02/02/1977
02/10/1979
02/02/1982
02/11/1986
17/10/1979
02/10/1979
02/02/1980
02/02/1981
19/02/1979
02/02/1985
02/04/1977

Page 20 of 20

ODL225 Database Systems

06A2 Team A Coursework 2

Doctor

Nurse
doctorid

gmcno

employeeid

nurseid

ukccpin

employeeid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

77882
748484
43327
3783
1034782
953302
11209
433901
478829
43929
588828
989231
1000932
772932
3923
492929
1303933
1049393

31
34
37
40
43
46
49
52
55
58
61
64
67
70
73
76
79
82

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

23P112E
67P432A
32P864B
78P654C
12P349D
83P962E
10P842A
78P045B
61P951C
40P150D
70P942E
81P041F
92P432A
24P932B
32P627C
41P992D
50P220E
69P801F
78P471A
87P372B

32
33
35
36
38
39
41
42
44
45
47
48
50
51
53
54
56
57
59
60

Nursequal

Qualification
qualificationid
1
2
3
4
5
6
7
8
9
10
11
12
13
14

qualification
Maternity
Paediatrics
General
Gynaecology
Cardiology
Orthopaedics
Urology
Respiratory
Renal
Oncology
Haematology
Endocinology
Dermatology
Neurology

Page 21 of 21

nurseid

qualificationid

1
2
3
4
5
6
6
7
8
11
12
12
12
15
16
16
23
24
25
26
27

1
2
8
1
2
2
7
4
4
5
13
9
5
6
6
10
1
1
2
2
4

ODL225 Database Systems

06A2 Team A Coursework 2

Patient
patientid

firstname

surname

Michael

Smith

Christopher

Johnson

Jessica

Williams

Ashley

Jones

Matthew

Brown

Joshus

Wilson

Daniel

Thomson

David

Robertson

Amanda

Campbell

10

Andrew

Stewart

11

James

Anderson

12

Justin

Clark

13

Joseph

Ross

14

John

Walker

15

Ryan

Paterson

16

Robert

Young

17

Nicholas

Watson

18

Sarah

Morrison

19

Samantha

Gray

20

Stephanie

Smith

address
Flat 6C, Spitfire
Place, Norwich
22 Bill Todd Way,
Taverham
The Pines,
Patterson Road,
Burnham Market
3 Market Place,
Cawston
1 Quiddenham
Road, Attleborough
The Rectory,
Rectory Road,
Guestwick
High Street,
Mulbarton
98 Norwich Road,
New Costessey
1 Tickle Avenue,
Lourd
Fordham House,
Cheese Road,
Walsingham
2The Croft, Guist
154 Kingswood
Avenue, Thorpe
Marriott
99 Holt Road,
Fakenham
Old Bakery,
Queens Road,
Fakenham
78 Station Road,
North Elmham
Hopfield lane
The Cottage,
Whitwell Street,
Reepham
6B Phillips Road,
Lowestoft
16 The Street,
Somerton
Flat 6C, Spitfire
Place, Norwich

Page 22 of 22

telno

nhsno

dob

01353
448383
01328
448383
01263
648383

3927469256

24/01/2000

O+

1552039045

23/02/1999

A+

4688734660

18/03/1998

B+

01508
748383
01263
878345
01263
748521

176372888

17/04/1998

O+

4271403743

21/05/1999

A+

1569435502

22/06/1999

O-

01263
448383
01263
865433
01263
548383
01263
648383

5539338961

19/07/1998

O+

3961214123

08/08/1995

A+

5197177040

14/09/1997

A-

3588981421

15/09/1997

O+

01263
748383
01463
848383

9499804699

09/01/1996

A+

9695231260

04/10/1994

A-

01953
648383
01553
548383

8979483636

10/10/1996

O+

9622072702

06/11/1995

A+

01263
548383
01777
998826
01263
878383

489282484

07/12/1995

B+

7356543900

11/12/1996

O+

730919468

13/01/1997

A+

473365859

01/02/1937

O-

1039367003

04/03/1937

O+

9736605353

27/04/1943

A+

01603
848383
01263
848383
01353
448383

bloodtype

ODL225 Database Systems

06A2 Team A Coursework 2

Treatment
treatmentid

notes

patientid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Complaining of back pain


Appt req for
Referred from
Dizzy spells
Follow up app
Painful to touch
Broken leg
Irregular Heart
Broken nose
Dislocated shoulder
needs appointment in clinic 1
needs appointment in clinic 2
needs appointment in clinic 3
needs appointment in clinic 4
requires in-patient stay, then appointment in clinic 5
requires in-patient stay, then appointment in clinic 5
requires in-patient stay, then appointment in clinic 5
requires in-patient stay, then appointment in clinic 5
requires in-patient stay, then appointment in clinic 5
requires in-patient stay, then appointment in clinic 5

4
8
7
6
9
3
1
2
5
3
1
2
3
4
5
6
7
8
9
10

Appointment
appointmentno

aptdate

apttime

location

doctorid

treatmentid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

02/01/2006
02/01/2006
02/01/2006
02/01/2006
03/01/2006
03/01/2006
03/01/2006
03/01/2006
04/01/2006
04/01/2006
04/01/2006
04/01/2006
05/01/2006
05/01/2006
05/01/2006
05/01/2006
06/01/2006
06/01/2006
06/01/2006
06/01/2006

10:00:00
12:00:00
14:00:00
16:00:00
10:00:00
12:00:00
14:00:00
16:00:00
10:00:00
12:00:00
14:00:00
16:00:00
10:00:00
12:00:00
14:00:00
16:00:00
10:00:00
12:00:00
14:00:00
16:00:00

clinic 1
clinic 2
clinic 3
clinic 4
clinic 5
clinic 1
clinic 2
clinic 3
clinic 4
clinic 5
clinic 1
clinic 2
clinic 3
clinic 4
clinic 5
clinic 1
clinic 2
clinic 3
clinic 4
clinic 5

1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5

11
12
13
14
15
20
21
22
23
24
25
30
31
32
33
34
35
40
41
42

Page 23 of 23

ODL225 Database Systems

06A2 Team A Coursework 2

Ward

Bed

wardid

wardname

floor

qualificationid

bedid

wardid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Barclay Ward
Blakeney Ward
Buxton Ward
Cley Ward
Coltishall Ward
Coronary Care Unit
Davidson Unit
Denton ward
Dilham Ward
Docking Ward
Dunston Ward
Earsham ward
Easton Ward
Edgefield Ward
Elsing Ward
Hethel Ward
Heydon Ward
Jack Pryor Unit
Mulbarton Ward
Rheumatology Day
Wd

G
1
2
G
1
2
G
1
2
G
1
2
G
1
2
G
1
2
G
1

3
1
2
4
3
5
3
6
3
3
13
3
3
3
12
9
10
3
11
3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

1
1
1
1
1
2
2
2
2
2
3
3
3
3
3
4
4
4
4
4

InpatientStay
admittanceno

admitdate

admittime

discharge

dischargetime

doctorid

bedid

treatmentid

2
3
4
5
6
7
8
10
11
12
13
14
15
16
18
19
20
21

24/01/2006
24/01/2006
24/01/2006
25/01/2006
25/01/2006
25/01/2006
25/01/2006
26/01/2006
26/01/2006
26/01/2006
27/01/2006
27/01/2006
27/01/2006
27/01/2006
30/01/2006
30/01/2006
30/01/2006
31/01/2006

11:00:00
09:00:00
10:00:00
11:00:00
09:00:00
10:00:00
11:00:00
10:00:00
11:00:00
09:00:00
10:00:00
11:00:00
09:00:00
10:00:00
09:00:00
10:00:00
11:00:00
09:00:00

25/01/2006
25/01/2006
25/01/2006
26/01/2006
26/01/2006
26/01/2006
26/01/2006
27/01/2006
27/01/2006
27/01/2006
30/01/2006
30/01/2006
30/01/2006
30/01/2006
31/01/2006
31/01/2006
31/01/2006
01/02/2006

10:00:00
08:00:00
09:00:00
10:00:00
08:00:00
09:00:00
10:00:00
09:00:00
10:00:00
08:00:00
09:00:00
10:00:00
08:00:00
09:00:00
08:00:00
09:00:00
10:00:00
08:00:00

12
13
14
15
16
17
18
12
13
14
15
16
17
18
12
13
14
15

9
14
19
24
29
34
39
10
15
20
25
30
35
40
11
16
21
26

56
57
58
59
60
65
66
68
69
70
75
76
77
78
80
85
86
87

Page 24 of 24

ODL225 Database Systems

06A2 Team A Coursework 2

Shift
shiftid

nurseid

shiftdate

shift

wardid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

01/01/2006
01/01/2006
01/01/2006
01/01/2006
01/01/2006
01/01/2006
01/01/2006
01/01/2006
01/01/2006
01/01/2006
01/01/2006
01/01/2006
01/01/2006
01/01/2006
01/01/2006
01/01/2006
01/01/2006
01/01/2006
01/01/2006
01/01/2006

A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10

PatientNotes
notesid
1

notedate
05/09/2005

doctorid
3

treatmentid
212

06/12/2005

212

01/02/2006

15

213

01/02/2006

16

213

02/02/2006

16

213

04/02/2006

17

213

06/02/2006

16

213

11

06/02/2006

16

213

12

13/02/2006

213

13

07/03/2006

212

14

08/06/2006

213

doctorsnote
Referred by GP. Diagnosed with Type 2 Diabetes. Treatment
by diet and tables
Responding well to diet and medication treatment. Continue 3
monthly check ups
Admitted to A&E. Fell @ home. X-Ray confirmed fractured L
Neck of Femur. Admitted to Barclay Ward
Initial examination: Referred for emergency L hip
replacement. Analgesia prescribed
Hip replaced in theatre. Returned to ward and recovering well.
Strong analgesia prescribed
Transfer to chair. Commence physiotherapy. Analgesia
reduced to normal strength
Walking with zimmer frame. Transfer to Jack Prior Unit for
rehabilitation.
Wound healing well. Stable on feet with frame. Discharge
from ward. Referred to Fracture Clinic
Able to walk moderate distances with frame. Daily physio @
home with Community Physiotherapist
Diabetes uncontrolled by diet following hip operation. Now
Type 1. Commence insulin by injection
Wound healed. Excellent progress with frame - issued with
walking stick. Advised to use stick until confidence regained.
Discharged

Page 25 of 25

ODL225 Database Systems

06A2 Team A Coursework 2

APPENDIX II - Example Screen Shots

Fig 23 - Using CREATE TABLE statements to create the tables

Page 26 of 26

ODL225 Database Systems

06A2 Team A Coursework 2

Fig 24 - Using INSERT statements to enter data into the tables.

Fig 25 - Data within employee table.

Page 27 of 27

ODL225 Database Systems

06A2 Team A Coursework 2

Fig 26. View 1 statement and output (For blood type B+)

Fig 27. View 2 statement and output (For blood type B+)

Page 28 of 28

ODL225 Database Systems

06A2 Team A Coursework 2

Fig 28. SQL query 1 statement and output

Fig 29. SQL query 7 statement and output

Page 29 of 29

ODL225 Database Systems

06A2 Team A Coursework 2

APPENDIX III - Data Dictionary


Entity Name Description
Employee
General term describing all
employees who work in the hospital
Doctor
Term applied to all employees who
are assigned to the treatment of
patients
Nurse
Term applied to all employees who
are assigned to wards

Qualification General term describing all


qualifications held by employees
Patient

General term describing all people


who are treated at the hospital

Treatment

General term describing all


Treatments carried out at the
hospital

Aliases
Staff

Occurrence
Employees are doctors and nurses who work within the
hospital
A doctor attends appointments at a given date and time
with patients. All patients are allocated a doctor from within
the hospital.
Nurses work on a shift system. Nurses are allocated to
wards based on their qualifications. Any nurse can work on
a general ward. A nurse needs a specific qualification to
work on a specialised ward.
Specialism A nurse must have a specific qualification to work on a
specialised ward.
The hospital provides care for in-patients and out-patients.
In-patients stay in the hospital. Out-patients attend
appointments.
Patients visit the hospital for treatment. A treatment
consists of appointments and in-patient stays.

Appointment General term describing all


appointments attended at the
hospital

Out-patients attend appointments at a given date and time


with a doctor. A patient may have more than one
appointment pending at a time.

InpatientStay General term describing all inpatient stays at the hospital


General term describing all wards
within the hospital
Shift
General term describing all shifts
worked at the hospital
Bed
General term descrinbing all beds
within the hospital
PatientNotes General term describing all
appointment and inpatientstay
notes written at the hospital

Stay

Ward

In-patients stay in the hospital, occupying a bed on a ward


Wards are either general or specific.

NurseShift Nurses work on a shift system, consisting of three 8-hour


shifts per day.
A bed is assigned to a patient who stays in the hospital.
Beds are located on wards
Patient Notes are written during/after each appointment
and in-patient stay.

Fig 30. Entity Description Table


Entity
Mult.
Patient
1..1
Treatment
1..1
Nurse
1..1
Treatment
1..1
Ward
0..*
InPatientStay 1..*
Appointment
0..*
InPatientStay 0..*
Bed
1..*
Doctor
1..*
Nurse
1..*
Shift
0..*
Doctor
1..1
PatientNotes
1..*

Relationship
has
necessitates
works
t_requires
w_requires
allocated
attendedBy
assigned
locatedOn
d_isAn
n_isAn
worksOn
writes
writtenFor

Mult.
Entity
1..*
Treatment
0..* Appointment
0..*
Shift
0..* InPatientStay
0..1 Qualification
1..1
Doctor
1..1
Doctor
1..1
Bed
1..1
Ward
1..1
Employee
1..1
Employee
1..1
Ward
1..* PatientNotes
1..1
Treatment

Fig 31. Entity Relationship Table


Page 30 of 30

ODL225 Database Systems


Entity
Employee

Doctor

Nurse

nurseQual
Qualification
Patient

Appointment

06A2 Team A Coursework 2

Attribute

Description

EmployeeID
FirstName
Surname
Address
TelNo
jobType
NINo

Uniquely identifies an employee


First name of employee
Last name of employee
Address of employee
Telephone number for employee
Job title of employee
National insurance number of
employee
Date of birth of employee

DOB
DoctorID
GMCNo
EmployeeID
NurseID
UKCCPIN
EmployeeID
nurseID
qualificationID
qualificationID
qualification
PatientID
FirstName
Surname
Address
TelNo
NHSNo
DOB
bloodType

Uniquely identifies a doctor


GMC number of doctor
Employee ID of doctor
Uniquely identifies a nurse
UKCCPIN of nurse
Employee ID of nurse
Identifies nurse
Identifies qualification
Uniquely identifies a qualification
Name of qualification
Uniquely identifies a patient
First name of patient
Last name of patient
Address of patient
Telephone number for patient
NHS number of patient
Date of birth of patient
Blood type of patient

AppointmentNo Uniquely
date

identifies
appointment
Date of appointment

Time of appointment
Identifies patient attending the
appointment
Identifies the doctor attending
doctorID
the appointment
Location of appointment
Location
Identifies treatment for which
TreatmentID
appointment is needed
admittanceNo Uniquely identifies an in-patient
stay
Date at which patient to be
admitDate,
admitted
Time at which patient to be
admitTime,
admitted
dateDischarged, Date at which patient to be
discharged
timeDischarged Time at which patient to be
discharged
Identifies doctor allocated to
doctorID
patient
Identifies treatment for which intreatmentID
patient stay is needed
Identifies bed allocated for
bedID
patient stay

time
PatientID

InPatientStay

an

Page 31 of 31

Data type

Nulls?

Domains

int
varchar (10)
varchar (10)
varchar (50)
varchar (12)
varchar (10)
char (9)

N
N
N
N
Y
N
N

integer
string 1-10 long
string 1-10 long
string 1-50 long
string 1-12 long
string 1-10 long
string 9 long

Date
int
char (7)
int
int
char (8)
int
int
int
int
varchar (20)
int
varchar (15)
varchar (15)
varchar (50)
varchar (12)
char (10)
Date
char(3)

N
N
Y
N
N
Y
N
N
N
N
N
N
N
N
Y
Y
Y
Y
Y

int

any past date


integer
string 7 long
integer
integer
string 8 long
integer
integer
integer
integer
string 1-20 long
integer
string 1-15 long
string 1-15 long
string 1-50 long
string 1-12 long
string 10 long
any past date
a+,a-,b+,b-,ab+,ab,o+,ointeger

Date

Time
int

N
N

01/01/2000
31/12/9999
00:00 23:59
integer

int

integer

varchar (10)
int

N
N

string 1-10 long


integer

int

integer

Date

Time

01/01/2000
31/12/9999
00:00 23:59

Date

Time

01/01/2000
31/12/9999
00:00 23:59

int

integer

int

integer

int

integer

ODL225 Database Systems


Entity
Treatment

06A2 Team A Coursework 2

Attribute

Description

treatmentID
notes
patientID

Uniquely identifies a treatment

Data type

Nulls?

Domains

N
Y
N

integer
string 1-100 long
integer

int
varchar (20)
char (1)
int

N
Y
Y
N

00-99
string 1-20 long
0-9
integer

int
int

N
N

integer
integer

int
int

N
N

integer
integer

Date

char(1)
int

N
N

01/01/2000
31/12/9999
single character
00-99

int

integer

Date

int
varchar (100)
Identifies patient associated with
int
Field for treatment notes
the treatment

Ward

Bed

shift

PatientNote

wardID
wardName
floor
qualificationID

Uniquely identifies a ward

bedID
wardID

Uniquely identifies a bed

shiftID
nurseID

Uniquely identifies a shift

date

Date of the shift

shift
wardID

Identifies the shift period

notesID

Uniquely identifies a set of notes

noteDate

Date notes written

doctorID

Identifies author (doctor) of notes

int

01/01/2000
31/12/9999
integer

treatmentID

Identifies treatment for which


notes written

int

integer

doctorsNote

Notes written by doctoe

varchar(200)

string 1-200 long

Name of the ward


Floor on which ward is located
Qualification needed for ward
Identifies the ward on which the
bed is located
Identifies nurse working the shift

Identifies the ward on which the


shift is to be worked

Fig 32. Attribute descriptions

Page 32 of 32

ODL225 Database Systems

06A2 Team A Coursework 2

Fig 3. Entity-Relationship diagram (showing primary keys and attributes)

Page 33 of 33

Potrebbero piacerti anche