Documente Academic
Documente Profesional
Documente Cultură
Introduction
University of Wolverhampton
School of Technology
Assessment 1
In this assignment you will build a small database to support the needs of a training company called Trainset.
To do this you will need to create tables to support the following scenario.
Trainset run professional short courses. These courses have a title, a duration of 1,2,3,4,or 5 days, and a cost
that varies between £300 and £2000. The courses are repeatedly run at various locations. The start date of
each offering, its location and its instructor are recorded. Students attend the courses and Trainset need to
record the student name, gender, telephone number, and company name. Each time a student attends a
course they will give an evaluation of the course (the evaluation is a number from 0 to 4), and attempt an
examination. Trainset need to record the examination result of each student in each examination. It is not a
requirement that all offerings of courses have attendees registered, and it is also not required that every
student has to have attended a course.
Contents:
Contents - Page 2
Constraints -
View definition -
Trigger definitions -
Query Output-
Entity Model
( Offering_offering_id, Student_sNo );
ON DELETE CASCADE;
-- CREATE TABLE 5
-- CREATE INDEX 0
-- ALTER TABLE 9
-- CREATE VIEW 0
-- CREATE PACKAGE 0
-- CREATE PROCEDURE 0
-- CREATE FUNCTION 0
-- CREATE TRIGGER 0
-- ALTER TRIGGER 0
-- CREATE CLUSTER 0
-- CREATE CONTEXT 0
-- CREATE DATABASE 0
-- CREATE DIMENSION 0
-- CREATE DIRECTORY 0
-- CREATE ROLE 0
-- CREATE SEQUENCE 0
-- CREATE SYNONYM 0
-- CREATE TABLESPACE 0
-- CREATE USER 0
-- DROP TABLESPACE 0
-- DROP DATABASE 0
-- ERRORS 0
-- WARNINGS 0
Test Data
Course
SQL> SELECT * FROM COURSE;
6 rows selected.
Offering
SQL> SELECT * FROM OFFERING;
11 rows selected.
Instructor
SQL> SELECT * FROM INSTRUCTOR;
2000001 Joe Sullivan United Kingdom London 14 MiddleCross Road DY11 SW3
2000003 Mark Lannister United Kingdom Birmingham 29 Forest Grove DY13 EA2
2000009 Liam Williams United Kingdom Cambridge 1 Dunhampton Drive DY19 OU7
2000012 Frederick Krause Germany Hamburg 9 Frankfort avenue 23843 Bad Oldesloe
10 rows selected.
Student
SQL> SELECT * FROM STUDENT;
10 rows selected.
Student Details
SQL> SELECT * FROM STUDENT_DETAILS;
3000005 4000004 2 E
3000011 4000007 3 C
3000011 4000002 2 E
3000011 4000005 3 A
3000001 4000001 1 D
3000002 4000006 3 B
3000004 4000003 2 C
3000005 4000010 4 A
3000006 4000008 0 E
3000008 4000005 3 C
3000009 4000002 2 D
3000011 4000004 3 A
12 rows selected.
Constraints
ALTER TABLE Course
( Offering_offering_id, Student_sNo );
ON DELETE CASCADE;
(Loc IN ('London', 'Rome', 'Birmingham', 'Paris', 'Berlin', 'Milan', 'Lille', 'Cambridge', 'Naples',
'Hamburg'));
View Definition
SQL> create or replace view courses as
2 select course.cNo, cTitle, duration, cost, offering_id, startdate, enddate, loc, instructorno
View created.
11 rows selected.
VIEW TESTS
1) UPDATES
UPDATE ON CNO
SQL> update courses set cNo = 101 where cNo = 1000001;
ERROR at line 1:
The error occurs as the column cNo is the primary key of one of the tables therefore it is not updateable.
UPDATE ON CTITLE
SQL> update courses set cTitle = 'Computers' where cTitle = 'Computer Science';
ERROR at line 1:
The error occurs because the course table this column is mapped to is not key-preserved, this is because in
the joined offering table there is no primary key on a column in the table that is referenced in the course
table.
UPDATE ON DURATION
SQL> update courses set duration = 1 where duration = 3;
ERROR at line 1:
The error occurs because the course table this column is mapped to is not key-preserved, this is because in
the joined offering table there is no primary key on a column in the table that is referenced in the course
table.
UPDATE ON COST
SQL> update courses set cost = 300 where cost = 730;
ERROR at line 1:
The error occurs because the course table this column is mapped to is not key-preserved, this is because in
the joined offering table there is no primary key on a column in the table that is referenced in the course
table.
UPDATE ON OFFERING_ID
SQL> update courses set offering_id = 312 where offering_id = 3000012;
1 row updated.
The update was successful because the column being updated is mapped to a key-preserved table this is due
to the primary key cNo being referenced in the offering table.
UPDATE ON STARTDATE
SQL> update courses set startdate = '23-APR-08' where startdate = '24-APR-08';
1 row updated.
The update was successful because the column being updated is mapped to a key-preserved table this is due
to the primary key cNo being referenced in the offering table.
UPDATE ON ENDDATE
SQL> update courses set enddate = '26-APR-08' where enddate = '27-APR-08';
1 row updated.
The update was successful because the column being updated is mapped to a key-preserved table this is due
to the primary key cNo being referenced in the offering table.
UPDATE ON LOC
SQL> update courses set loc = 'Naples' where loc = 'London';
3 rows updated.
The update was successful because the column being updated is mapped to a key-preserved table this is due
to the primary key cNo being referenced in the offering table.
UPDATE ON INSTRUCTORNO
SQL> update courses set instructorno = 2000011 where instructorno = 2000012;
1 row updated.
The update was successful because the column being updated is mapped to a key-preserved table this is due
to the primary key cNo being referenced in the offering table.
11 rows selected.
2) INSERTS
SQL> INSERT INTO COURSES VALUES
ERROR at line 1:
The error occurs because the courses table is not key-preserved due to the lack of a primary key from the
offering table being referenced in the courses table, therefore the values cannot be inserted.
ERROR at line 1:
The error occurs because the courses table is not key-preserved due to the lack of a primary key from the
offering table being referenced in the courses table, therefore the values cannot be inserted.
3) DELETES
SQL> delete from courses where cNo = 1000006;
2 rows deleted.
9 rows selected.
The delete was successful because the view contains a primary key on the non key preserved table as well as
a key preserved table due to the primary key cNo being referenced in the offering table.
a)
SQL> create materialized view offerings_mv
2 build immediate
10 from offering O1
12 group by cNo);
b)
SQL> select * from offerings_mv;
6 from offering O1
8 group by cNo);
Execution Plan
----------------------------------------------------------
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
Note
-----
Statistics
----------------------------------------------------------
19 recursive calls
0 db block gets
12 consistent gets
1 physical reads
0 redo size
0 sorts (memory)
0 sorts (disk)
5 rows processed
c)
SQL> update course set cTitle = 'Computers' where cTitle = 'Computer Science';
1 row updated.
6 from offering O1
8 group by cNo);
Execution Plan
----------------------------------------------------------
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
|* 1 | FILTER | | | | | |
--------------------------------------------------------------------------------------------
---------------------------------------------------
GROUP BY "CNO"))
6 - access("C"."CNO"="O"."CNO")
filter("C"."CNO"="O"."CNO")
10 - filter("LOC"=:B1)
Statistics
----------------------------------------------------------
0 db block gets
18 physical reads
0 redo size
7 sorts (memory)
0 sorts (disk)
5 rows processed
6 from offering O1
8 group by cNo);
Execution Plan
----------------------------------------------------------
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
Note
-----
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
44 consistent gets
7 physical reads
0 redo size
6 sorts (memory)
0 sorts (disk)
5 rows processed
d)
There are several ways a materialised view can be refreshed, either refreshing on commit which keeps the
materialised view up to date but can affect the performance on base table(s) transactions or refreshing on
demand which gives the user freedom to refresh at their discretion but it does require a procedure to be
manually invoked each time the user wishes to refresh instead of the DBMS handling this. Whether on
commit or demand refreshing can be specified to be fast which is where only the rows in the material view
that are affected by changes to the base table(s) which makes this quicker than a complete refresh however
it requires the presence or materialized view logs. Refreshing can also be specified to be complete which is
where the materialized view is completely rebuilt which is slower than a fast refresh but it does not require
materialized view logs. Also refreshing can be specified to be forced which is where the refresh will be fast if
there are materialized logs present but if not it forces a complete refresh.
Trigger Definitions
a)
b)
c)
SQL> create or replace trigger course_date
2 before delete
3 on offering
5 begin
7 raise_application_error(-20100,
9 end if;
10 end;
11 /
Trigger created.
ERROR at line 1:
1 row deleted.
http://msdn.microsoft.com/en-us/library/aa258254%28v=sql.80%29.aspx
Query Output
a)
SQL> select C.cNo, C.cTitle, C.Cost
b)
SQL> select loc, cNo, count(*) counts
2 from offering O
5 from offering O1
7 group by cNo);
Rome 1000004 2
Paris 1000005 2
London 1000001 3
Naples 1000003 2
Berlin 1000006 2
c)
SQL> select O.cNo, O.offering_id, S.student_sno
4 order by cNo;
12 rows selected.
d)
SQL> select offering_offering_id, student_sno, case evaluation
7 ELSE 'OTHER'
8 END
9 FROM STUDENT_DETAILS;
12 rows selected.
e)
SQL> select S.sno, S.sfname, S.slname, C.cTitle
S.sno = S1.student_sno;
12 rows selected.
f)
SQL> select cTitle, cost, (select avg(cost) from course) as difference
2 from course
6 rows selected.
g)
SQL> select S.sno, S.sfname, S.slname, C.cTitle
10 rows selected.
h)
i)
SQL> select c.cTitle, c.cNO, O.startdate, O.enddate, O.loc
j)
SQL> select S.sfname, S.slname
DEC-07';
SFNAME SLNAME
------------------------------ ------------------------------
Elizabeth Newey
k)