Vous êtes sur la page 1sur 4

>>CREATION OF TABLES

create table lib ( bookname varchar(15),author varchar(15), publication varchar(


10), noofcopies number);
create table student ( rollno number, name varchar(20), no_card number );
create table books ( bookno number, bookname varchar(15),
available varchar(10),subscribed_to number );
create table subscription ( bookno number, rollno number, do_sub date,
do_return date, fineamount number,status varchar(10) );

>>INSERTION OF VALUES
>STUDENT
begin
insert into student values(:rollno,:name,2);
end;
>Violation in inserting rollno.
create or replace trigger firsttrig before insert or update on student for each
row
when (new.rollno<1001)
begin
raise_application_error (-20001,'invalid rollno');
end;

>BOOK
declare
bookno number;
bname varchar(25);
noc number;
noc1 number;
author varchar(15);
publication varchar(10);
nobooks number;
lib_rec lib%rowtype;
begin
bookno:= :bookno;
bname:= :bookname;
author:= :author;
publication:= :publication;
noc:= :noofcopies;
noc1:= noc;
select count(*) into nobooks from lib where bookname=bname;
if nobooks=0 then
insert into lib values(bname,author,publication,noc);
else
update lib set noofcopies=noofcopies+noc where bookname=bname;
end if;
while noc!=0
loop
insert into books values(bookno,bname,'yes',0);

noc:=noc-1;
bookno:=bookno+1;
end loop;
end;

>>DISPLAYING DATABASE
select * from lib;
select * from books;
select * from student;

>>DISPLAYING TOTAL NUMBER OF BOOKS USING CURSOR


declare
cursor c1 is select * from lib;
count1 number(7);
r lib%rowtype;
begin
count1:=0;
open c1;
loop
fetch c1 into r;
if c1%notfound then
exit;
end if;
count1:=count1+ r.noofcopies;
end loop;
close c1;
dbms_output.put_line('total no. of books available in library = '||count1);
end;

>>PROCEDURE TO TAKE A BOOK


create or replace procedure sub(bname char,roll_no number)as
lib_rec lib%rowtype;
book_rec books%rowtype;
stud_rec student%rowtype;
sub_rec subscription%rowtype;
book_no number;
no_of_books number;
begin
select * into stud_rec from student where rollno=roll_no;
if stud_rec.no_card=0 then
dbms_output.put_line('***************');
dbms_output.put_line('no card available');
dbms_output.put_line('***************');
else
select count(*) into no_of_books from books where bookname=bname and available='
yes';
if no_of_books=0 then
dbms_output.put_line('***************');
dbms_output.put_line('book not available');

dbms_output.put_line('***************');
else
select min(bookno) into book_no from books where bookname=bname and available='y
es';
insert into subscription values(book_no,roll_no,sysdate,sysdate+7,0,'ntreturned'
);
update student set no_card=no_card-1 where rollno=roll_no;
update books set available='no' where bookno=book_no;
update books set subscribed_to=roll_no where bookno=book_no;
end if;
end if;
exception
when no_data_found then
dbms_output.put_line('***************');
dbms_output.put_line('u are not a user');
dbms_output.put_line('***************');
end;
>> SUBSCRIBE
declare
begin
sub('bookname',rollno);
end;
select * from books;
select * from subscription;
select * from student;

>>PROCEDURE FOR RETURNING A BOOK


create or replace procedure ret(bno number)as
lib_rec lib%rowtype;
book_rec books%rowtype;
stud_rec student%rowtype;
sub_rec subscription%rowtype;
book_no number;
no_of_books number;
fine number;
begin
select * into book_rec from books where bookno=bno;
if book_rec.available='yes' then
dbms_output.put_line('$$$$$$$$$$$$');
dbms_output.put_line('book available');
dbms_output.put_line('$$$$$$$$$$$$');
else
update subscription set do_return=sysdate where bookno=bno and status='ntreturne
d';
select do_return-do_sub into fine from subscription where bookno=bno and status=
'ntreturned';
if fine>7 then
update subscription set fineamount=trunc(fine,0) where bookno=bno and status='nt
returned';
dbms_output.put_line('$$$$$$$$$$$$');
dbms_output.put_line('u have to pay a fine of rs'||trunc(fine,0));

dbms_output.put_line('$$$$$$$$$$$$');
end if;
update subscription set status='returned' where bookno=bno;
update student set no_card=no_card+1 where rollno=book_rec.subscribed_to;
update books set available='yes' where bookno=bno;
update books set subscribed_to=0 where bookno=bno;
end if;
exception
when no_data_found then
dbms_output.put_line('$$$$$$$$$$$$');
dbms_output.put_line('book doesnot belong to library');
dbms_output.put_line('$$$$$$$$$$$$');
end;

>>RETURN
declare
begin
ret (bookno);
end;
select * from subscription;
select * from books;
select * from student;
>>UPDATION OF PARTICULARS OF STUDENT
CREATE OR REPLACE TRIGGER studentUPDATE
AFTER UPDATE OF NAME ON student FOR EACH ROW
BEGIN
DBMS_OUTPUT.PUT_LINE('OLD NAME: ' ||:OLD.NAME);
DBMS_OUTPUT.PUT_LINE('NEW NAME:' ||:NEW.NAME);
END;
-- update student set name = 'vijay' where rollno = 1009;
CREATE OR REPLACE TRIGGER
AFTER UPDATE OF ROLLNO ON
BEGIN
DBMS_OUTPUT.PUT_LINE('OLD
DBMS_OUTPUT.PUT_LINE('NEW
END;

studentUPDATE2
student FOR EACH ROW
ROLLNO: ' ||:OLD.ROLLNO);
ROLLNO:' ||:NEW.ROLLNO);

--update student set rollno= 1021 where rollno = 1009;

Vous aimerez peut-être aussi