HOMEWORK ASSIGNMENT 9
DHIR THACKER
002819144
1. Create a database named “AdultLiteracy” on your RDBMS environment. Using Figure 7-5 above, write DDL
commands to create table structures for each entity above. Name your tables with the following names:
Tutor, Student, MatchHistory, TutorReport.
CREATE DATABASE AdultLiteracy;
use AdultLiteracy;
-- create table Tutor
CREATE TABLE Tutor (
TutorID int not null,
CertDate date not null,
Status varchar(50) not null,
CONSTRAINT TUTOR_PK PRIMARY KEY (TutorID)
);
-- create table Student
CREATE TABLE Student (
StudentID int not null,
[Read] Float not null
CONSTRAINT Student_PK PRIMARY KEY (StudentID)
);
-- create table MatchHistory
CREATE TABLE MatchHistory (
MatchID int not null,
TutorID int not null,
StudentID int not null,
StartDate date not null,
EndDate date,
CONSTRAINT MatchHistory_PK PRIMARY KEY (MatchID),
CONSTRAINT MatchHistory_FK1 FOREIGN KEY (TutorID) REFERENCES TUTOR
(TutorID),
CONSTRAINT MatchHistory_FK2 FOREIGN KEY (StudentID) REFERENCES STUDENT
(StudentID)
);
-- create table TutorReport
CREATE TABLE TutorReport (
MatchID INT NOT NULL,
Month VARCHAR(20) NOT NULL,
Hours INT NOT NULL,
Lessons INT NOT NULL
CONSTRAINT TutorReport_PK PRIMARY KEY (MatchID, Month)
CONSTRAINT TutorReport_FK FOREIGN KEY (MatchID) REFERENCES
MatchHistory(MatchID)
);
2. Write SQL scripts to insert sample data from Fig 7-5 into the database.
-- insert values into table TUTOR
INSERT INTO TUTOR VALUES(100, '1/05/2008','Active');
INSERT INTO TUTOR VALUES(101, '1/05/2008','Temp Stop');
INSERT INTO TUTOR VALUES(102, '1/05/2008','Dropped');
INSERT INTO TUTOR VALUES(103, '5/22/2008','Active');
INSERT INTO TUTOR VALUES(104, '5/22/2008','Active');
INSERT INTO TUTOR VALUES(105, '5/22/2008','Temp Stop');
INSERT INTO TUTOR VALUES(106, '5/22/2008','Active');
SELECT * FROM TUTOR;
-- insert values into table STUDENT
INSERT INTO STUDENT VALUES (3000, 2.3);
INSERT INTO STUDENT VALUES (3001, 5.6);
INSERT INTO STUDENT VALUES (3002, 1.3);
INSERT INTO STUDENT VALUES (3003, 3.3);
INSERT INTO STUDENT VALUES (3004, 2.7);
INSERT INTO STUDENT VALUES (3005, 4.8);
INSERT INTO STUDENT VALUES (3006, 7.8);
INSERT INTO STUDENT VALUES (3007, 1.5);
SELECT * FROM STUDENT;
-- insert values into table MatchHistory
INSERT INTO MatchHistory (MatchID, TutorID, StudentID, StartDate) VALUES (1, 100,
3000, '1/10/2008');
INSERT INTO MatchHistory (MatchID, TutorID, StudentID, StartDate, EndDate) VALUES (2,
101, 3001, '1/15/2008', '5/15/2008');
INSERT INTO MatchHistory (MatchID, TutorID, StudentID, StartDate, EndDate) VALUES (3,
102, 3002, '2/10/2008', '3/01/2008');
INSERT INTO MatchHistory (MatchID, TutorID, StudentID, StartDate) VALUES (4, 106,
3003, '5/28/2008');
INSERT INTO MatchHistory (MatchID, TutorID, StudentID, StartDate, EndDate) VALUES (5,
103, 3004, '6/01/2008', '6/15/2008');
INSERT INTO MatchHistory (MatchID, TutorID, StudentID, StartDate, EndDate) VALUES (6,
104, 3005, '6/01/2008', '6/28/2008');
INSERT INTO MatchHistory (MatchID, TutorID, StudentID, StartDate) VALUES (7, 104,
3006, '6/01/2008');
SELECT * FROM MatchHistory;
-- insert values into table TutorReport
INSERT INTO TutorReport VALUES (1, '6/08', 8, 4);
INSERT INTO TutorReport VALUES (4, '6/08', 8, 6);
INSERT INTO TutorReport VALUES (5, '6/08', 4, 4);
INSERT INTO TutorReport VALUES (4, '7/08', 10, 5);
INSERT INTO TutorReport VALUES (1, '7/08', 4, 2);
3. Write the SQL command to add MATH SCORE to the STUDENT table.
-- 3. Write the SQL command to add MATH SCORE to the STUDENT table.
ALTER TABLE STUDENT ADD Math_score int;
-- adding values into column Math_score
UPDATE STUDENT SET Math_score = '90' WHERE StudentID = 3000;
UPDATE STUDENT SET Math_score = '80' WHERE StudentID = 3001;
UPDATE STUDENT SET Math_score = '74' WHERE StudentID = 3002;
UPDATE STUDENT SET Math_score = '69' WHERE StudentID = 3003;
UPDATE STUDENT SET Math_score = '85' WHERE StudentID = 3004;
UPDATE STUDENT SET Math_score = '97' WHERE StudentID = 3005;
UPDATE STUDENT SET Math_score = '83' WHERE StudentID = 3006;
UPDATE STUDENT SET Math_score = '92' WHERE StudentID = 3007;
SELECT * FROM STUDENT;
4. Write the SQL command to add SUBJECT to TUTOR. The only values allowed for SUBJECT will
be Reading, Math, and ESL.
ALTER TABLE TUTOR ADD Subject varchar(50) CHECK (Subject in ('Reading', 'Math',
'ESL'));
-- adding values into column Subject
UPDATE TUTOR SET Subject = 'Math' WHERE TutorID = 100;
UPDATE TUTOR SET Subject = 'Math' WHERE TutorID = 101;
UPDATE TUTOR SET Subject = 'Reading' WHERE TutorID = 102;
UPDATE TUTOR SET Subject = 'ESL' WHERE TutorID = 103;
UPDATE TUTOR SET Subject = 'Reading' WHERE TutorID = 104;
UPDATE TUTOR SET Subject = 'Math' WHERE TutorID = 105;
UPDATE TUTOR SET Subject = 'ESL' WHERE TutorID = 106;
SELECT * FROM TUTOR;
Running below query resulted in an error:
UPDATE TUTOR SET Subject = 'English' WHERE TutorID = 100;
5. What do you need to do if a tutor signs up and wants to tutor in both reading and math? (Don’t need to
write SQL).
These are 2 possible approaches to the problem:
1)
To do this, we can update the primary key of the Tutor table to be a composite primary key, consisting of
two columns: TutorID and Subject. Now, if a tutor wants to tutor in both math and reading, upon defining
a composite primary key (TutorID, Subject), a given TutorID can have two records in the table, for
subjects: Reading and Math respectively.
We can edit the primary key definition constraint on the Tutor table.
Old definition of Tutor table
CREATE TABLE Tutor (
TutorID int not null,
CertDate date not null,
Status varchar(50) not null,
Subject varchar(50) CHECK (Subject in ('Reading', 'Math', 'ESL'));
CONSTRAINT TUTOR_PK PRIMARY KEY (TutorID)
);
Updated definition of Tutor table
CREATE TABLE Tutor (
TutorID int not null,
CertDate date not null,
Status varchar(50) not null,
Subject varchar(50) CHECK (Subject in ('Reading', 'Math', 'ESL'));
CONSTRAINT TUTOR_PK PRIMARY KEY (TutorID, SUBJECT)
);
2)
For a tutor to be associated with multiple subjects, instead of creating a new table, we can simply redefine
the Subject column as an array or list to store multiple values. The constraint of the values to be one of
(‘Reading’, ‘Math’) or both would be in place but if the definition is of array/list, multiple values can be
stored.
6. Write the SQL command to find any tutors who have not submitted a report for July.
-- 6. Write the SQL command to find any tutors who have not submitted a report for
July.
SELECT tut.TutorID, tut.CertDate, tut.Status, tut.Subject
From Tutor tut
WHERE NOT EXISTS (
SELECT *
FROM MatchHistory MatchHis JOIN TutorReport TutRep ON MatchHis.MatchID =
TutRep.MatchID
WHERE tut.TutorID = MatchHis.TutorID AND Month='7/08'
);