CSCI 5931A
 Advanced Database Development
 Fall 1998
 Suggested Solution to Mid-Term Examination

(1) See the file t1q1sol.mdl.

(2) For example,

declare
    cursor c_questions is
        select Question, count(*) NumChoice
        from H1_VotingProblem, H1_Choice
        where H1_VotingProblem.VotingProblemID = H1_Choice.VotingProblemID
        group by Question;
begin
    dbms_output.put_line('Questions with number of choices: ');
    for v_question in c_questions loop
        dbms_output.put_line(v_question.Question || ' : '
                             || v_question.numChoice || ' choices.');
    end loop;
end;
/

(3) For example,

create or replace function AccountPersonName (Account H1_Administrator.AccountName%TYPE)
                       return VARCHAR2 is
    PId H1_Person.PersonID%TYPE;
    LName H1_Person.LastName%TYPE;
    FName H1_Person.FirstName%TYPE;
begin -- AccountPersonName
    select PersonID into PID
    from H1_Administrator
    where H1_Administrator.AccountName = Account;

    select LastName, FirstName into LName, FName
    from H1_Person
    where PersonID = PID;

    return FName || ' ' || LName;
end AccountPersonName;
/
 
 

(4) For example,

(a)

create table H1_Vote_Stat(
    ChoiceID         NUMBER(6) PRIMARY KEY,
    VotedTimes       NUMBER(6),
    FOREIGN KEY (ChoiceID) REFERENCES H1_Choice(ChoiceID)
);

(b)

create or replace trigger Update_Vote_State
    after insert on H1_Vote
    for each row
begin
    update H1_Vote_Stat
        set VotedTimes = VotedTimes + 1
        where ChoiceID = :new.ChoiceID;
    if SQL%NOTFOUND then
        insert into H1_Vote_Stat
        values(:new.ChoiceID, 1);
    end if;
end Update_Vote_Stat;
/