Seminar week 3 (with solutions)

advertisement
Seminar week 3
To do this seminar you need to run the script that is on BB (makeuniv.sql) by
following the instructions on BB.
The database that is generated follows the structure below:
In order to determine the attributes of the tables type DESCRIBE <table>; at the SQL
prompt, for example:
Describe marks;
Name
Null?
Type
------------------------------------------------------------------STUDENTID
NOT NULL NUMBER(7)
SUBJECTID
NOT NULL CHAR(8)
PERIOD
NOT NULL CHAR(4)
MARK
NUMBER(3)
This gives you the name of the column, the data type stored and whether the column
is permitted to be null or if there has to be a value entered into the row.
Once you have run the script, working in groups of 2 or 3 carry out the following
tasks.
You have been asked to write some SQL that will indicate the day and time that the
various members of staff teach, you also need to identify where they are teaching.
1. Identify what data you need to retrieve (this is the ‘select’ columns)
2. Where can that data be found? (this will give the ‘from’ clause)
3. How many different tables do you need to relate
4. Is there a clear path to relate the tables(this is the ‘where’ clause)
5. Generate the code
-1-
Solutions
1. Identify what data you need to retrieve (this is the ‘select’ columns)
the students should identify that they only need the name of the staff member,
the day they teach the time of the class and the room, they should describe the
tables to match these needs to the columns
2. Where can that data be found? (this will give the ‘from’ clause)
Class and staffmember tables
3. How many different tables do you need to relate
2
4. Is there a clear path to relate the tables(this is the ‘where’ clause)
There is not clear link based on the column names but if they describe the
tables they should spot that the teacher and staff id have the same data types
and meaning
5. Generate the code
SQL> select name, day, classtime, room
2 from staffmember, class
3 where teacher = staffid
4 order by name, day, classtime;
NAME
DAY
CLASSTIME ROOM
-------------------- --------------- ---------- --------------Akhtar Ali
Wednesday
11am
Elec Eng G25
Andrew Turnbull Monday
9am
Pandon S3
Ben Wightman
Wednesday
3pm
Webster B
Emma-Jane Phillips Friday
11am
Pandon S2
Emma-Jane Phillips Monday
11am
Pandon F4
Emma-Jane Phillips Monday
2pm
Pandon G1
Emma-Jane Phillips Tuesday
11am
Pandon G5
Emma-Jane Phillips Tuesday
9am
Pandon s2
Emma-Jane Phillips Wednesday
9am
Pandon F2
Gareth Phillips Tuesday
6pm
Elec Eng LG1
Gareth Price
Tuesday
11am
Mathews A
Grant Smith
Monday
11am
Mathews A
Grant Smith
friday
11am
Pandon G2
John Tait
Monday
2pm
Elec Eng LG1
John Tait
Monday
9am
Pandon F1
John Tait
Thursday
1pm
Pandon F1
John Tait
Wednesday
3pm
Pandon G4
Mark Hurrell;
Thursday
11am
Elec Eng LG1
Mark Hurrell;
Thursday
4pm
Elec Eng 408
Neil Thompson
Tuesday
2pm
Webster B
Paul Samson
Friday
9am
Pandon S1
Paul Samson
Tuesday
11am
Pandon S4
Paul Samson
Tuesday
2pm
Elec Eng LG1
Tim Rose
Friday
11am
Elec Eng G25
Tim Rose
Friday
11am
Elec Eng G25
Tim Rose
Tuesday
11am
Elec Eng G25
Tim Rose
Tuesday
11am
Elec Eng G25
If the student returns hundreds of rows then they have forgotten to join the tables in the where
clause.
-2-
Download