CSCI 5931A
Advanced Database Development
Fall 1998
Suggested Solution To Homework #3

(1)    For example:

PROMPT Creating the sequence H3_MajorsSequence...
DROP SEQUENCE H3_MajorsSequence;
CREATE SEQUENCE H3_MajorsSequence
  START WITH 100
  INCREMENT BY 1;
PROMPT H3_MajorsSequence created.

PROMPT creating the table H3_Majors...
drop view H3_Raw_Enrol;
drop table H3_Enrol;
drop table H3_Students;
drop table H3_Courses;
drop table H3_Majors;

create table H3_Majors (
    Major       CHAR(4)    PRIMARY KEY,
    MajorDesc   VARCHAR2(50)
);

INSERT INTO H3_Majors (Major, MajorDesc)
   VALUES ('CSCI', 'Computer Science');
INSERT INTO H3_Majors (Major, MajorDesc)
   VALUES ('SWEN', 'Software Engineering');
INSERT INTO H3_Majors (Major, MajorDesc)
   VALUES ('CIS', 'Computer Infrormation Systems');
INSERT INTO H3_Majors (Major, MajorDesc)
   VALUES ('CENG', 'Computer Engineering');
PROMPT table H3_Majors created....
 

PROMPT Creating the sequence H3_StudentsSequence...
DROP SEQUENCE H3_StudentsSequence;
CREATE SEQUENCE H3_StudentsSequence
  START WITH 100
  INCREMENT BY 1;
PROMPT H3_StudentsSequence created.

PROMPT creating the table H3_Students...
create table H3_Students (
    StudentID     NUMBER(6)   PRIMARY KEY,
    SLastName     VARCHAR2(30),
    SFirstName    VARCHAR2(30),
    SMajor        CHAR(4),
    STotalCredit  NUMBER(3),
    STotalGP      NUMBER(8,4),
    SGPA          NUMBER(4,3),
    CONSTRAINT H3_Students_CMajor
       FOREIGN KEY (SMajor) REFERENCES H3_Majors(Major)
);

INSERT INTO H3_Students (StudentID, SLastName, SFirstName, SMajor, STotalCredit, STotalGP, SGPA)
   VALUES (H3_StudentsSequence.NEXTVAL, 'Bun', 'Yue', 'CSCI', 0, 0, 0);
INSERT INTO H3_Students (StudentID, SLastName, SFirstName, SMajor, STotalCredit, STotalGP, SGPA)
   VALUES (H3_StudentsSequence.NEXTVAL, 'Joe', 'Giarratano', 'CSCI', 0, 0, 0);
INSERT INTO H3_Students (StudentID, SLastName, SFirstName, SMajor, STotalCredit, STotalGP, SGPA)
   VALUES (H3_StudentsSequence.NEXTVAL, 'Sadegh', 'Davari', 'CSCI', 0, 0, 0);
INSERT INTO H3_Students (StudentID, SLastName, SFirstName, SMajor, STotalCredit, STotalGP, SGPA)
   VALUES (H3_StudentsSequence.NEXTVAL, 'Sharon', 'White', 'SWEN', 0, 0, 0);
INSERT INTO H3_Students (StudentID, SLastName, SFirstName, SMajor, STotalCredit, STotalGP, SGPA)
   VALUES (H3_StudentsSequence.NEXTVAL, 'Jim', 'Helm', 'SWEN', 0, 0, 0);
INSERT INTO H3_Students (StudentID, SLastName, SFirstName, SMajor, STotalCredit, STotalGP, SGPA)
   VALUES (H3_StudentsSequence.NEXTVAL, 'Sharon', 'Perkins', 'CIS', 0, 0, 0);
PROMPT Table H3_Students created...

PROMPT Creating the sequence H3_CoursesSequence...
DROP SEQUENCE H3_CoursesSequence;
CREATE SEQUENCE H3_CoursesSequence
  START WITH 100
  INCREMENT BY 1;
PROMPT H3_CoursesSequence created.

PROMPT creating the table H3_Students...
create table H3_Courses (
    CourseID    NUMBER(5)   PRIMARY KEY,
    CMajor      CHAR(4),
    CNumber     CHAR(4),
    CCredit     NUMBER(2),
    CTitle      VARCHAR2(60),
    CDesc       VARCHAR2(200),
    CONSTRAINT H3_Courses_CMajor
       FOREIGN KEY (CMajor) REFERENCES H3_Majors(Major)
);

INSERT INTO H3_Courses (CourseID, CMajor, CNumber, CCredit, CTitle, CDesc)
   VALUES (H3_CoursesSequence.NEXTVAL, 'CSCI', '3333', 3, 'Data Structures', 'Data Structures');
INSERT INTO H3_Courses (CourseID, CMajor, CNumber, CCredit, CTitle, CDesc)
   VALUES (H3_CoursesSequence.NEXTVAL, 'CSCI', '3532', 3, 'Advance Data and File Structures', 'Advance Data and File Structures');
INSERT INTO H3_Courses (CourseID, CMajor, CNumber, CCredit, CTitle, CDesc)
   VALUES (H3_CoursesSequence.NEXTVAL, 'CSCI', '4333', 3, 'Design of Data Systems', 'Design of Data Systemss');
INSERT INTO H3_Courses (CourseID, CMajor, CNumber, CCredit, CTitle, CDesc)
   VALUES (H3_CoursesSequence.NEXTVAL, 'CSCI', '4230', 3, 'Software Tools', 'Software Tools');
INSERT INTO H3_Courses (CourseID, CMajor, CNumber, CCredit, CTitle, CDesc)
   VALUES (H3_CoursesSequence.NEXTVAL, 'CSCI', '3133', 3, 'Programming with C', 'Programming with the C Programming Language.  Lab.');

INSERT INTO H3_Courses (CourseID, CMajor, CNumber, CCredit, CTitle, CDesc)
   VALUES (H3_CoursesSequence.NEXTVAL, 'CSCI', '3134', 3, 'Programming with Java', 'Programming with Java');
INSERT INTO H3_Courses (CourseID, CMajor, CNumber, CCredit, CTitle, CDesc)
   VALUES (H3_CoursesSequence.NEXTVAL, 'SWEN', '4432', 3, 'Software Engineering', 'Software Engineering');
INSERT INTO H3_Courses (CourseID, CMajor, CNumber, CCredit, CTitle, CDesc)
   VALUES (H3_CoursesSequence.NEXTVAL, 'SWEN', '4434', 3, 'Personal Software Processes', 'Personal Software Processes');
INSERT INTO H3_Courses (CourseID, CMajor, CNumber, CCredit, CTitle, CDesc)
   VALUES (H3_CoursesSequence.NEXTVAL, 'CENG', '3331', 3, 'Telecommunication and Networks', 'Telecommunication and Networks');
INSERT INTO H3_Courses (CourseID, CMajor, CNumber, CCredit, CTitle, CDesc)
   VALUES (H3_CoursesSequence.NEXTVAL, 'CENG', '3311', 3, 'Lab for Telecommunication and Networks', ' Lab for Telecommunication and Networks');

PROMPT creating the table H3_Enrols...
create table H3_Enrol (
    EnrolID     NUMBER(8)   PRIMARY KEY,
    StudentID   NUMBER(6),
    CourseID    NUMBER(5),
    Score       NUMBER(5,2),
    Grade       CHAR(2),
    CONSTRAINT H3_Enrol_StudentID
       FOREIGN KEY (StudentID) REFERENCES H3_Students(StudentID),
    CONSTRAINT H3_Enrol_CourseID
       FOREIGN KEY (CourseID) REFERENCES H3_Courses(CourseID),
    CONSTRAINT H3_EnrolCandidateKey UNIQUE (StudentID, CourseID)
);

create view H3_Raw_Enrol (EnrolId, StudentID, CourseID, Score) AS
    SELECT EnrolID, StudentID, CourseID, Score
    FROM H3_Enrol;

(2)    For example:

create or replace function ConvertToGradePoint(grade CHAR) return NUMBER is
    Result NUMBER(5,4) := 0;
BEGIN
    if (grade IS NULL) then
        Result := NULL;
    elsif (grade = 'A') then
        Result := 4.0;
    elsif (grade = 'A-') then
        Result := 3.6667;
    elsif (grade = 'B+') then
        Result := 3.3333;
    elsif (grade = 'B') then
        Result := 3.0;
    elsif (grade = 'B-') then
        Result := 2.6667;
    elsif (grade = 'C+') then
        Result := 2.3333;
    elsif (grade = 'C') then
        Result := 2.0;
    elsif (grade = 'C-') then
        Result := 1.6667;
    elsif (grade = 'D+') then
        Result := 1.3333;
    elsif (grade = 'D') then
        Result := 1.0;
    elsif (grade = 'D-') then
        Result := 0.6667;
    else   --  grade is F
        Result := 0;
    end if;
    return Result;
end;
/

(3)    For example:

drop trigger H3_Raw_Enrol_Insert;
create trigger H3_Raw_Enrol_Insert
    instead of insert on H3_Raw_Enrol
    for each row
declare
    student_rec H3_Students%ROWTYPE;
    course_rec H3_Courses%ROWTYPE;
    grade H3_Enrol.Grade%TYPE;
    grade_point H3_Students.STotalGP%TYPE;
begin
    if (:new.score IS NULL) THEN
        insert into H3_Enrol (EnrolID, StudentID, CourseID, Score, Grade)
        values (:new.EnrolId, :new.StudentID, :new.CourseID, null, null);
    else
        --  Insert the Enrol record.
        grade := ConvertToGrade(:new.score);
        grade_point := ConvertToGradePoint(grade);

        insert into H3_Enrol (EnrolID, StudentID, CourseID, Score, Grade)
        values (:new.EnrolId, :new.StudentID, :new.CourseID, :new.Score, grade);

        select * into course_rec
        from H3_Courses
        where CourseID = :new.CourseID;

        select * into student_rec
        from H3_Students
        where StudentID = :new.StudentID;

        student_rec.STotalCredit := student_rec.STotalCredit + course_rec.CCredit;
        student_rec.STotalGP := student_rec.STotalGP + course_rec.CCredit * grade_point;
        student_rec.SGPA := student_rec.STotalGP / student_rec.STotalCredit;

        update H3_Students
        set STotalCredit = student_rec.STotalCredit,
            STotalGP = student_rec.STotalGP,
            SGPA = student_rec.SGPA
        where StudentID = student_rec.StudentID;
    end if;
end;
/