Sunteți pe pagina 1din 7

Advanced Relational Features Function, Procedure and Triggers

Person ID dob firstName lastName {Disjoint, mandatory}

CampusClub CID Name location phone

Student status

member 1..* 1..*

0..*

advisor

1..1

FacultyStaff rank

1..1 1..* StudentClub dateJoined 1..1 majorsIn Department Code name 1..1 worksIn

1..* 1..1 chair

Relational schema Person (pId, dob, firstName, lastName) Student (pId, status, major) Foreign key (pId) references Person Foreign key (major) references Department FacultyStaff (pId, rank, dept) Foreign key (pId) references Person Foreign key (dept) references Department CampusClub (cId, name, location, phone, advisor) Foreign key (advisor) references FacultyStaff Department (code, name, chair) Foreign key (chair) references FacultyStaff StudentClub (studentId,cId, dateJoined) Foreign key (studentID) references Student Foreign key (cId) references CampusClub Create or replace view facultyStaffView as Select p.pId, dob, firstName, lastName, rank, dept From person p, facultyStaff f Where p.pid = f.pid; Create or replace view studentView as Select p.pId, dob, firstName, lastName, status, major From person p, student s Where p.pid = s.pid;

SBB 3013 Advanced Database Systems

SQL statement for creating tables drop table person cascade constraints; drop table department cascade constraints; drop table facultyStaff cascade constraints; drop table student cascade constraints; drop table campusClub cascade constraints; drop table studentClub cascade constraints; create table department ( code varchar2(3) primary key, name varchar2(40) not null, chair number(11) ); create table person ( pId number(5) primary key, dob date not null, firstName varchar2(20) not null, lastName varchar2(20) not null); create table facultyStaff ( pId number(5) primary key, rank varchar2(10) not null, dept varchar2(3) not null, constraint rankValue check (rank in ('Assistant', 'Associate', 'Full', 'Emeritus')), constraint facultyPIdFk foreign key (pId) references person (pId), constraint facultyDeptFk foreign key (dept) references department (code)); alter table department add constraint deptChairFk foreign key(chair) references facultyStaff(pId) on delete set null; create table student ( pId number(5) primary key, status varchar(10) not null, major varchar(3) not null, constraint statusValue check (status in ('Freshman', 'Sophomore', 'Junior', 'Senior', 'Graduate')), constraint studentPIdFk foreign key (pId) references person(pId), constraint studentMajorFk foreign key (major) references department (code)); create table campusClub ( cId varchar(5) primary key, name varchar(50) not null, phone varchar(12), location varchar(40), advisor number(5), constraint AdvisorFk foreign key(advisor) references facultyStaff(pId) on delete set null); create table studentclub ( studentId number(5) not null, cId varchar(5) not null, dateJoined date, constraint clubPk primary key(studentId, cId), constraint clubPIdFk foreign key (studentId) references student(pId) on delete cascade, constraint clubCIdFk foreign key (cId) references campusClub(cId) on delete cascade); insert into department values ('cs', 'Computer Science', null);
SBB 3013 Advanced Database Systems

insert into insert into insert into insert into insert into insert into insert into

department values ('ee', 'Electrical Engineering', null); person values (11, '29-Jan-1965', 'Ameen', 'Ahmad'); person values (12, '29-Jun-1990', 'Sarah', 'Azami'); person values (13, '18-Aug-1970', 'Raihana', 'Muhammad'); person values (14, '17-Dec-1989', 'Aliff', 'Aiman'); person values (15, '1-May-1990', 'Husna', 'Hassan'); person values (16, '5-Nov-1991', 'Ahmad', 'Iman');

insert into facultystaff values (11, 'Full', 'cs'); insert into facultystaff values (13, 'Assistant', 'ee'); insert into insert into insert into insert into student values (12, 'Freshman', 'cs'); student values (14, 'Junior', 'cs'); student values (15, 'Freshman', 'cs'); student values (16, 'Freshman', 'cs');

insert into campusClub values ('c1', 'Entrepreneur Club', '013336565', 'Room 23, Bussiness School', 11); insert into campusClub values ('c2', 'Media Club', '0131212121', 'Room 02, CIS School', 13); insert into insert into insert into insert into studentClub values(12,'c1', '29-Jan-2010'); studentClub values(14,'c1', '30-Jan-2010'); studentClub values(15,'c1', '30-Jan-2010'); studentClub values(16,'c2', '30-Jan-2010');

update department set chair = 11 where code = 'cs'; update department set chair = 13 where code = 'ee';

SQL-invoked Routines

1. Procedure
A statement to delete all members of a specific club can be embedded in a procedure. create or replace procedure deleteStudentsInClub (clubID IN varchar2) is begin delete from studentClub where cId = clubID; end; / call deleteStudentsInClub('c1'); or execute deleteStudentsInClub('c1');

SBB 3013 Advanced Database Systems

Procedure to enforce the total specialization on the Person hierarchy: create or replace procedure insertStudent (pId IN number, dob IN date, fname IN varchar, lname IN varchar, status IN varchar, major IN varchar) is begin insert into person values(pId, dob, fname, lname); insert into student values(pId, status, major); end; / call insertStudent(17, '25-Feb-1990', 'Husna', 'Hassan', 'Senior', 'cs');

Procedure to enforce the ISA constraint when a student is deleted: create or replace procedure deleteStudent (Id IN number) is begin delete from student S where pId = Id; delete from person P where pId = Id; end; / call deleteStudent (15);

Procedure to update department name create or replace procedure updateDept (dCode IN varchar2) is deptName varchar2(50); begin select name into deptName from department where code = dCode; update department set name = deptName || ' and Engineering' where code = dCode; end; / call updateDept ('cs');

Procedure to list all clubs advise by an advisor. create or replace procedure getClubByAdvisor (Id in varchar) is cursor c is select cm.*, p.firstname, p.pid from campusClub cm, person p where cm.advisor = Id and p.pid = cm. advisor; begin dbms_output.put_line('Advisor: ' || Id); for rec in c loop dbms_output.put_line(rec.cId || ' ' || rec.name || ' rec.phone); end loop; end; / call getClubByAdvisor(11); call getClubByAdvisor(13);

' || rec.location || ' ' ||

SBB 3013 Advanced Database Systems

2. Function Returning the Name of a Person create or replace function getName(Id IN number) return varchar2 IS name varchar2(25); BEGIN select firstname || ' ' || lastName into name from person where pId = Id; return name; END; / The statement below uses the function to insert the name of the student into a temporary table: insert into tempTable(studentName) values(getName(12)); Or call the function using dummy table: Select getName(11) from dual; Function to return the club name with exception handling. create or replace function getClubName(cCode IN varchar2) return varchar2 is answer varchar2(20); begin select name into answer from campusClub where cId = cCode; return answer; exception when NO_DATA_FOUND then dbms_output.put_line('No data'); return 'No data'; end; / The statement below calls the function from a select statement select getClubName ('c1') from dual;

3. Using Cursors - A pointer that is used to examine each row in a result set
Function and Procedure to return the Name of a Person
create or replace procedure getPersonNameP(Id IN number) is name varchar2(30); cursor namecursor is select lastName ||' '|| firstName from person where person.pId = Id; begin open namecursor; fetch namecursor into name; close namecursor; dbms_output.put_line(name); call getPersonName('s15'); end; /

create or replace function getName(Id IN number) return varchar2 is name varchar2(30); cursor name_cursor is select lastName ||' '|| firstName from person where person.pId = Id; begin open name_cursor; fetch name_cursor into name; close name_cursor; return name; end; select getName('s15') from dual; /

SBB 3013 Advanced Database Systems

The FOR LOOP statement To delete a club together with the members of a club create or replace procedure delClub(clubId IN varchar2) is cursor clubCursor is select * from studentClub where cid = clubId; begin for club_rec IN clubCursor LOOP delete from studentClub where cid = clubId; end LOOP; delete from campusClub where cId = clubId; end; /

The CASE Statement - Case Statement to Update Student Status create or replace procedure updateStudentStatus is sStatus varchar2(10); cursor studentC is select * from student; begin for rec IN studentC LOOP select status into sStatus from student where pId = rec.pId; case sStatus when 'Freshman' then update student set status ='Sophomore' where pId = rec.pId; when 'Junior' then update student set status ='Senior' where pId = rec.pId; when 'Sophomore' then update student set status ='Junior' where pId = rec.pId; when 'Senior' then delete from student where pId = rec.pId; end case; end LOOP; end; /

The IF-THEN-ELSEIF Statement - to Update Student Status create or replace procedure updateStudentStatus is begin for rec IN (select * from student) LOOP if rec.Status = 'Senior' then delete from student where pId = rec.pId; elsif rec.Status = 'Junior' then update student set status = 'Senior' where pId = rec.pId; elsif rec.Status = 'Sophomore' then update student set status = 'Junior' where pId = rec.pId; elsif rec.Status = 'Freshman' then update student set status = 'Sophomore' where pId = rec.pId; end if; end LOOP; end; /

SBB 3013 Advanced Database Systems

Calling a procedure from an anonymous block Procedure with paremeters create or replace procedure searchStaff (id IN NUMBER, first OUT VARCHAR2, last OUT VARCHAR2) is begin

SELECT firstname, lastname INTO first, last FROM facultyStaffView WHERE pid = id;

exception WHEN OTHERS THEN dbms_output.put_line ('Staff ID ' || id || ' does not exist'); end searchStaff; / Anonymous block with call to procedure Declare firstN lastN SID person.firstname%TYPE; person.lastname%TYPE; person.pid%TYPE :=&StaffID;

Begin

searchStaff(SID, firstN, lastN); IF firstN IS NOT NULL THEN dbms_output.put_line ('Staff ID ' || SID); dbms_output.put_line ('Name ' || firstN || ' ' || lastN); END IF; END; /

SBB 3013 Advanced Database Systems

S-ar putea să vă placă și