Adbms Lab
Adbms Lab
EPLOYEE DLOCATION
Name Constraint Name Constraint
SSN Primary Key DNo Foreign Key refers Department, Part of
EName Not Null Primary Key
Address Not Null DLoc Not Null, Part of Primary Key
Gender Must be in (f, m)
Salary Not Null, must be >0 Note: A department can be in many locations
SuperSSn Foreign key refers to Employee
DName Foreign Key refers
Department(DName)
DEPARTMENT PROJECT
Name Constraint Name Constraint
DNo Primary Key PNo Primary Key, must begin with P
DName Unique PName Unique
MgrSSN Foreign Key refers to Employee PLocation Not Null
MgrStartDate Not Null DNo Foreign Key refers to Department
WORKS_ON
Name Constraint
SSN Part of Primary Key, Foreign Key
refers to Employee
PNo Part of Primary Key, Foreign Key
refers to Project
Hours Not Null, must be >0
8 DName varchar(10));
Table created.
Table created.
SQL> ALTER TABLE emp ADD CONSTRAINT DName_emp foreign key(DName)references dept(DName);
Table created.
4 primary key(dno,dloc));
Table created.
SQL> desc DLocation;
Table created.
5 primary key(SSN,PNo));
Table created.
1 row created.
7 rows selected.
1 row updated.
11 rows selected.
1 row updated.
1 row updated.
1 row updated.
1 row updated.
1 row updated.
15 rows selected.
DNO DLOC
---------- --------------------
d1 manipal
d2 udupi
1 row created.
DNO DLOC
---------- --------------------
d1 manipal
d2 udupi
d3 manglore
Query:
1)SQL> select ssn,ename,pno,'worker' from(select ssn from emp minus select mgrssn from dept)natural join
emp natural join workson where ename like'%scott';
SQL> select ssn,pno,pname,'manager' from dept natural join projectt join emp on(ssn=mgrssn)where ename
like'%scott';
SQL> select ssn,ename,pno,'worker' from(select ssn from emp minus select mgrssn from dept)natural join
emp natural join workson where ename like'%scott' union select ssn,pno,pname,'manager' from dept natural
join projectt join emp on(ssn=mgrssn)where ename like'%scott';
or
SQL> select ssn,ename,pno, 'manager' from dept natural join projectt join emp on(ssn=mgrssn)where ename
like'%scott';
SSN ENAME PNO 'MANAGE
4)SQL> select ssn,ename,pno,'worker' from(select ssn from emp minus select mgrssn from dept)natural join
emp natural join workson where ename like'%scott' union select ssn,ename,pno,'manager' from dept natural
join projectt join emp on(ssn=mgrssn)where ename like'%scott';
2)SQL>select SSN,SALARY+(SALARY*0.10)"raised"from emp natural join workson natural join projectt where
PNAME='IOT';
SSN raised
---------- ----------
e116 495000
e118 671000
e119 616000
e120 495000
4) SQL> select ename from emp where exists(select * from(select pno from projectt where dno='d5')natural
join workson "A" where emp.ssn=A.ssn);
ENAME
----------
sonali
ram
ramya
chaya
je scott
anush
6 rows selected.
5)select dname,count(*),sum(salary) from emp group by dname having count(*)>5 and sum(salary)>600000;
IT 8 49890000
Pl/sql:
SET SERVEROUTPUT ON
declare
eno emp.ssn%type;
da number(10,2);
hra number(10,2);
net number(10,2);
tax number(10,2);
name emp.ename%type;
basic emp.salary%type;
begin
open emp_cur;
loop
da:=basic*0.12;
hra:=basic*0.10;
tax:=basic*0.10;
net:=basic+da+hra-tax;
end loop;
close emp_cur;
end;
SQL> @d:emp.sql
tax=74000
2) SET SERVEROUTPUT ON
declare
Cursor emp_cur is select w.ename,m.ename from emp w left join emp m on(w.superssn=m.ssn);
Name varchar(10);
SuperName varchar(10);
begin
open emp_cur;
loop
if(SuperName!='')then
DBMS_OUTPUT.PUT_LINE('Name='||Name||' '||'SuperName='||SuperName);
else
SuperName:='SuperBoss';
DBMS_OUTPUT.PUT_LINE('Name='||Name||' '||'SuperName='||SuperName);
end if;
end loop;
close emp_cur;
end;
SQL> @ d:emp1.sql
Name=allen SuperName=SuperBoss
Name=sammy SuperName=SuperBoss
Name=rocky SuperName=SuperBoss
Name=aana SuperName=SuperBoss
Name=ankitha SuperName=SuperBoss
Name=mahesh SuperName=SuperBoss
Name=ramya SuperName=SuperBoss
Name=ram SuperName=SuperBoss
Name=chaya SuperName=SuperBoss
Name=anush SuperName=SuperBoss
Name=sonali SuperName=SuperBoss
Name=amrutha SuperName=SuperBoss
Name=marie SuperName=SuperBoss
PL/SQL procedure successfully completed.
(managerid number(3),
managername varchar(10),
deptid number(3),
deptname varchar(10),
startdate date,
enddate date);
sql>@:manager.sql
table created.
SET SERVEROUTPUT ON
declare
nameC varchar2(10);
begin
end;
/
Sql>@ d:trigger.sql
Trigger created.
STUDENT CLASS
Name Constraint Name Constraint
USN Primary Key, must begin with M USN Part of primary key, Foreign Key
SName Not Null references Student
Address Not Null SSID Must be in ISem_MSc,
Phone Unique IISem_MSc, IIISem_MSc,
Gender Must be in(‘F’, ‘M’) IVSem_MSc
SUBJECT
Name Constraint IAMARKS
SubCode Primary Key Name Constraint
Title Not Null USN Foreign Key references
SSID Must be in ISem_MSc, Student
IISem_MSc, IIISem_MSc, SSID Must be in ISem_MSc,
IVSem_MSc IISem_MSc, IIISem_MSc,
Credits Not Null IVSem_MSc
SubCode Foreign Key references Subject
Test1 Not Null must be <=30
Test2 Not Null must be <=30
Test3 Not Null must be <=30
FinalA Not Null must be <=30
PL/SQL
1. Write the PL/SQL code to list all the subjects of a particular course.
2. Write a procedure to return total number of male and female students.
3. Write a trigger to insert student information such as USN, name, marks into new table
Average_Students who got less marks (<17) in FinalA.
Table created.
4 primary key(USN,SSID));
Table created.
Table created.
2 (USN varchar(20),
3 SSID varchar(10),
9 primary key(USN,SSID,SubCode),
10 foreign key(USN,SSID)references class);
Table created.
1 row created.
7 rows selected.
1 row created.
USN SSID
-------------------- ----------
M2020-100 1Sem_MSc
M2020-101 2Sem_MSc
M2020-102 3Sem_MSc
M2020-103 4Sem_MSc
M2020-104 4Sem_MSc
M2020-105 4Sem_MSc
M2020-106 2Sem_MSc
7 rows selected.
MSCCSH103 CG 1Sem_MSc 8
MSCCSS105 DS 3Sem_MSc 8
1 row created.
FINALA
----------
M2020-100 1Sem_MSc MSCCSH103 23 21 21
1 row created.
MSCCSH103 CG 1Sem_MSc 8
MSCCSD104 DBMS 2Sem_MSc 7
MSCCSS105 DS 3Sem_MSc 8
6 rows selected.
USN SSID
-------------------- ----------
M2020-100 1Sem_MSc
M2020-101 2Sem_MSc
M2020-102 2Sem_MSc
M2020-102 3Sem_MSc
M2020-103 4Sem_MSc
M2020-104 2Sem_MSc
M2020-104 4Sem_MSc
M2020-105 4Sem_MSc
M2020-106 2Sem_MSc
9 rows selected.
FINALA
----------
QUERY
1> SQL> select *from student natural join class where ssid='4Sem_MSc';
SSID
----------
4Sem_MSc
4Sem_MSc
4Sem_MSc
2>SQL> select ssid,gender,count(*)from student natural join class group by ssid,gender order by ssid;
SSID G COUNT(*)
---------- - ----------
1Sem_MSc F 1
2Sem_MSc F 2
2Sem_MSc M 2
3Sem_MSc M 1
4Sem_MSc F 2
4Sem_MSc M 1
6 rows selected.
3> SQL> create view v as select subcode,test1,test2,test3,finala from iamarks where usn='M2020-101';
View created.
FINALA
----------
1
M2020-102 3Sem_MSc MSCCSS105 24 2 5
4> SQL> update iamarks set finala=(case when test1>=test2 and test3>=test2 then (test1+test3)/2 when
test2>=test3 and test1>=test3 then (test2+test1)/2 when test2>=test1 and test3>=test1 then(test2+test3)/2
end)where USN in iamarks.usn;
5 rows updated.
FINALA
----------
26
25
15
FINALA
----------
23
25
5> SQL> select usn,finala,case when finala>17 and finala<=30 then 'outstanding' when finala>12 and
finala<=17 then'average' else 'weak' end as cat from iamarks where ssid='3Sem_MSc';
M2020-102 15 average
PLSQL
1) set serveroutput on
declare
sc subject.subcode%type;
ttl subject.title%type;
course subject.ssid%type;
begin
open sub_cur;
loop
close sub_cur;
end;
SQL> @d:subject.sql
1Sem_MSc MSCCSH103 CG
3Sem_MSc MSCCSS105 DS
2) set serveroutput on
declare
g student.gender%type;
fcount number(3);
mcount number(3);
begin
mcount:=0;
fcount:=0;
open gen_cur;
loop
if(g='M')then
mcount:=mcount+1;
else
fcount:=fcount+1;
end if;
end loop;
close gen_cur;
end;
Output:
SQL> @d:gender.sql
name varchar(10),
test1 number(3),
test2 number(3),
test3 number(3),
finala number(3));
trigger:
set serveroutput on
create or replace trigger trig_sub after update of finala on iamarks for each row
declare
nameV varchar(10);
begin
if(:new.finala<17)then
end if;
end;
SQL> update iamarks set finala=(case when test1>=test2 and test3>=test2 then (test1+test3)/2 when
test2>=test3 and test1>=test3 then (test2+test1)/2 when test2>=test1 and test3>=test1 then(test2+test3)/2
end)where USN in iamarks.usn;
5 rows updated.
M2020-102 kiran 24 2 5 15
3. Consider the schema for Movie
ACTOR Director
Name Constraint Name Constraint
Act_Id Primary key Dir_Id Primary key
Act_Name Not Null Dir_Name Not Null
Act_Gender ‘M’ or ‘F’ Dir_Phone unique
MOVIES Movie_Cast
Name Constraint Name Constraint
Mov_Id Primary key Act_Id Foreign key references
Mov_Title Not Null Actor, Part of Primary Key
Mov_Year Not Null Mov_Id Foreign key references
Mov_Lang Not Null Movie, Part of Primary
Dir_Id Foreign Key references Key
Director Role Must be in HRO, HRN,
SPCHR or CMDN
RATING
Name Constraint
Mov_Id Primary Key, Foreign Key references Movie
Rev_Stars Must be in 1, 2, 3, 4, 5
PL/SQL
1. Write procedure to return total number of movies acted as hero/heroine and total number of movies in
supporting character by an actor/actress. (send the actor id through application program)
2. Write the PL/SQL program to display all cast names and director name of a movie.
3. Write PL/SQL program to input any two actor names display all movie names in which they acted
together. If no movies then raise user defined exception and handle it.
2 act_id varchar(10),
4 act_gender char(1));
Table created.
Table altered.
Table created.
Table created.
Table altered.
Table altered.
Table altered.
SQL> create table movie_cast(
5 primary key(act_id,mov_id));
Table created.
Table created.
1 row created.
ACT_ID ACT_NAME A
---------- ---------- -
a771 yash M
a772 sudeep M
a773 shanvi F
a774 priyamani F
a775 puneeth M
a776 radika F
a778 siddarth M
8 rows selected.
1 row created.
1 row created.
6 rows selected.
1 row created.
a776 m1 HRN
a772 m6 HRO
a773 m6 HRN
a773 m3 SPCHR
a774 m3 HRN
a775 m3 HRO
a774 m4 HRN
a777 m4 HRO
a778 m5 HRO
a774 m5 HRN
a773 m2 HRN
a772 m2 HRO
a778 m4 SPCHR
a772 m3 CMDN
15 rows selected.
1 row created.
SQL> select *from ratings;
MOV_ID REV_STARS
---------- ----------
m1 4
m2 5
m3 3
m4 5
m5 4
m6 3
6 rows selected.
Query
1> SQL> select mov_title,dir_name from movies natural join actor natural join director natural join movie_cast
where act_name='yash' and role='HRO' and(dir_name='anup'or dir_name='prashaanth');
MOV_TITLE DIR_NAME
---------- ----------
drama prashaanth
googly anup
2) SQL> select act_name,mov_title from actor natural join movie_cast natural join movies where act_id
in(select act_id from movie_cast having count(act_id)>=3 group by act_id);
ACT_NAME MOV_TITLE
---------- ----------
yash drama
yash tharak
yash googly
sudeep drama
sudeep ram
sudeep hebbuli
priyamani ram
priyamani aarya
priyamani my friend
9 rows selected.
3) SQL> select act_id,act_name,'act_name2000' from actor where act_id in(select distinct act_id from
movie_cast where mov_id in(select mov_id from movies where mov_year<=2000))union select
act_id,act_name,'act_name2015' from actor where act_id in(select distinct act_id from movie_cast where
mov_id in(select mov_id from movies where mov_year>=2015));
8 rows selected.
4) SQL> select mov_title,rev_stars from movies natural join ratings order by mov_title;
MOV_TITLE REV_STARS
---------- ----------
aarya 5
drama 4
hebbuli 3
my friend 4
ram 3
tharak 5
6 rows selected.
5) SQL> update ratings set rev_stars=5 where mov_id in(select mov_id from movies where dir_id=(select
dir_id from director where dir_name='anup'));
2 rows updated.
MOV_ID REV_STARS
---------- ----------
m1 4
m2 5
m3 5
m4 5
m5 4
m6 5
6 rows selected.
Pl/sql
1)set serveroutput on
create or replace procedure pro(A1 in varchar,A2 in varchar,A3 out varchar)is countv number(3);
begin
if(A2=1)then
Select count(*) into countv from movie_cast where act_id=A1 and (role='HRO' or role='HRN');
A3:=countv;
else
select count(*)into countv from movie_cast where act_id=A1 and (role='SPCHR' or role='CMDN');
A3:=countv;
end if;
end;
Ed d:Movie1.sql
set serveroutput on
declare
actv varchar(5);
exe number(5);
actn varchar(10);
hron varchar(10);
spch varchar(10);
begin
actv:=&actv;
if(exe>0)then
pro(actv,1,hron);
pro(actv,2,spch);
else
end if;
end;
old 8: actv:=&actv;
new 8: actv:='a773';
2)set serveroutput on
declare
movidv varchar(4);
actidv varchar(4);
movidv2 varchar(4);
movnv varchar(10);
dirv varchar(10);
actnv varchar(10);
rolev3 varchar(10);
begin
open c;
loop
dbms_output.put_line('----------------');
dbms_output.put_line('Movie id is'||movidv);
select mov_title into movnv from movies where mov_id=movidv;
dbms_output.put_line('moviename is'||movnv);
select dir_name into dirv from director where dir_id=(select dir_id from movies where mov_id=movidv);
dbms_output.put_line('director is'||dirv);
open c1;
loop
if(movidv2=movidv)then
end if;
end loop;
close c1;
dbms_output.put_line('----------------');
end loop;
close c;
end;
SQL> ed d:director.sql
SQL> @ d:director.sql
----------------
Movie id is m1
director Is prashaanth
actor id is sudeeprole is
----------------
----------------
Movie id is m2
director is pavan
----------------
----------------
Movie id is m3
director is anup
----------------
----------------
Movie id ism4
director is pavan
----------------
----------------
Movie id ism5
director isprashaanth
----------------
----------------
Movie id ism6
moviename ishebbuli
director isanup
actor id issudeeprole is
----------------
----------------
Movie id ism7
moviename isgoogly
director isanup
----------------
3) set serveroutput on
declare
exp1 exception;
exist number(2);
act1 varchar2(20);
act2 varchar2(20);
id1 varchar2(10);
id2 varchar2(10);
titv varchar2(20);
f number(1);
begin
f:=0;
act1:=&act1;
act2:=&act2;
if(exist>0) then
if(exist>0) then
for c in(select mov_title into titv from movies where mov_id in(select mov_id from movie_cast where
act_id=id1
intersect
loop
f:=1;
titv:=c.mov_title;
dbms_output.put_line(titv);
end loop;
if(f=0) then
raise exp1;
end if;
else
end if;
else
end if;
exception
end;
SQL> @ d:exception.sql
drama
tharak
SALESMAN
Name Type Constraint
S_ID Varchar(5) must start from S, Primary Key
S_Name Varchar(20) Not Null
City Varchar(20) Not Null
CUSTOMER
Name Type Constraint
C_ID Varchar(5) must start from C, Primary Key
Cust_Name Varchar(20) Not Null
City Varchar(20) Not Null
ORDERS
Name Type Constraint
O_NO Varchar(5) must start with O, Primary Key
C_ID Varchar(5) Not Null, Foreign Key refers to Customer
S_ID Varchar(5) Not Null, Foreign Key refers to Salesman
Purchase_Am Number(15, 2) Not Null
t
Grade Number(3) Not Null, Grade must be in(100, 200, 300, 400)
Commission Number(10, 2) must be >10% default 10
Order_date date
S_COMMISSION
Name Type Constraint
S_ID Varchar(5) Foreign Key refers to salesman
Month Varchar(10) Not Null
Year Number(4) Not Null
Commission Number(10, 2) Default 1000
Table created.
Table created.
8 order_date date);
Table created.
SQL> alter table orders add commission number(10,2)default 0.10 constraint cmc check(commission>=0.10);
Table altered.
9 rows selected.
1 row created.
1 row created.
9 rows selected.
1 row created.
Query
1.SQL> select count(distinct sid)from orders natural join salesman where grade>(select avg(grade)
COUNT(DISTINCTSID)
------------------
SNAME COUNT(*)
-------------------- ----------
kiran 2
anush 2
suresh 2
hari 3
manipal suresh 2
banglore anush 2
manglore hari 2
udupi kiran 3
4.SQL> create view salesman_view as select sid,sname from salesman where sid in(select sid from orders
where order_date='30-aug-22' group by sid having count(cid)=(select max(count(cid))from orders where
order_date='30-aug-22'group by(cid)));
View created.
SID SNAME
----- --------------------
S103 suresh
Table altered.
SQL> alter table orders add constraint sid_fk foreign key(sid) references salesman on delete cascade;
Table altered
Table altered.
SQL> alter table s_commission add constraint sid_cm foreign key(sid)references salesman on delete cascade;
Table altered.
1 row deleted.
SQL> rollback;
Rollback complete.
1)set serveroutput on
declare
sm_id salesman.sid%type;
sm_name salesman.sname%type;
sm_city salesman.city%type;
cursor sm_cur is
select sid,sname,city from orders natural join salesman group by sid,sname,city order by sum(grade)desc;
begin
open sm_cur;
for i in 1..3
loop
dbms_output.put_line(sm_id||''||sm_name||''||sm_city);
end loop;
close sm_cur;
end;
SQL> @ d:salesman.sql
S102harimanglore
S104kiranudupi
S103sureshmanipal
PL/SQL procedure successfully completed.
2) set serveroutput on
declare
sm_id salesman.sid%type;
sm_month varchar2(10);
sm_year number(10);
comm number(10,2);
cursor comm_cur is
begin
sm_month:='&month';
sm_year:='&year';
open comm_cur;
loop
end loop;
close comm_cur;
end;
Output:
SQL> @ d:commission;
new 9: sm_month:='DECEMBER';
3)Procedure:
create or replace procedure s_exists (s_id in varchar, n out number, M IN VARCHAR, Y IN NUMBER)
is
s_id1 varchar(10);
begin
n:=0;
select sid into s_id1 from s_commission where sid=S_ID and month=M and year =Y;
n:=1;
exception
dbms_output.put_line('');
end;
declare
comm number(10,2):=00.00;
s_id varchar(5);
mid varchar(10);
n integer(1):=0;
Y NUMBER(10);
M VARCHAR(20);
begin
comm:=0.4* :new.purchase_amt;
if(comm<1000.00)then
comm:=1000.00;
end if;
if trim(to_CHAR(:NEW.ORDER_DATE,'MONTH'))='JANUARY' then
mid:='JANUARY';
elsif trim(tO_CHAR(:NEW.ORDER_DATE,'MONTH'))='FEBRUARY'
then
mid:='FEBRUARY';
mid:='MARCH';
mid:='APRIL';
mid:='JUNE';
mid:='JULY';
mid:='AUGUST';
mid:='SEPTEMBER';
mid:='OCTOBER';
mid:='NOVEMBER';
mid:='DECEMBER';
end if;
M:=MID;
s_exists(:new.sid, n, M, Y);
if(n=1) then
update s_commission set commission=commission+comm where sid=:new.sid and month=mid
and year=extract(year from :new.order_date);
END IF;
if (n=0) then
END IF;
end;
1 row created.
No records Found
1 row created.
1 row created.
4)set serveroutput on
name varchar(10);
total number(20,2);
amount number(15,2);
cursor cust_cur is select cust_name,purchase_amt from customer natural join orders where cid=cust_id and
extract(year from order_date)=year and extract(month from order_date)=month;
begin
total:=0;
open cust_cur;
loop
total:=total+amount;
end loop;
dbms_output.put_line('Total purchase amount is :'||total);
close cust_cur;
end;
SQL> @ d:purchase.sql
Procedure created.
set serveroutput on
declare
cust_id varchar(5);
month varchar(10);
year number(4);
id varchar(5);
begin
cust_id:='&customer_id';
month:=&month_in_number;
year:=&year;
cust_proc(cust_id,month,year);
exception
end;
/
SQL> @ d:purchase1.sql
old 7: cust_id:='&customer_id';
new 7: cust_id:='C101';
old 9: month:=&month_in_number;
new 9: month:=2;
BOOK_RETURN
Name Constraint BOOK_AUTHORS
Card_No Foreign Key references Name Constraint
Book_Lending Book_id Foreign Key references
Return_Date Not Null Book, Part of Primary Key
Fine Not Null, Default 0 Author_Name Not Null, Part of Primary
Key
BOOK
Name Constraint
Book_id Primary Key, must begin with B LIBRARY_BRANCH
Title Not Null Name Constraint
Publisher_Name Foreign Key references Publisher Branch_id Primary Key
Publishing_Year Not Null Branch_Nam Not Null
e
Address Not Null
BOOK_LENDING BOOK_COPIES
Name Constraint Name Constraint
Book_id Part of Foreign Key references Book_id Foreign Key references
Book_copies Book, Part of Primary Key
Branch_id Part of Foreign Key references Branch_id Foreign Key references
Book_copies Branch, Part of Primary Key
Card_No Primary Key No_of_Copies Not Null
Date_Out Not Null Avlbl_Copies Not Null
Due_Date Not Null
Bid F.K refers borrower
PL/SQL
1. Write PL/SQL to display total books in each branch.
2. Write a procedure to calculate the total number of books published by a publisher in the current year and last
two years.
3. Display the borrowed book names and their due_dates which are not yet returned (use set operation minus).
Create table publisher(
Table created.
Table created.
Table created.
4 primary key(book_id,author_name));
Table created.
SQL> create table library_branch(
Table created.
6 primary key(book_id,branch_id));
Table created.
2 book_id varchar(5),
3 branch_id varchar(5),
Table created.
1 row created.
1 row created.
1 row created.
B1 amrutha barkur
B2 anisha udupi
B3 bavya banglore
B4 kavya manglore
1 row created.
BOOK_ AUTHOR_NAM
----- ----------
Bk01 Toni
Bk02 chethan
Bk03 Peter
Bk04 chethan
1 row created.
SQL> select *from library_branch;
1 row created.
Bk01 br1 10 10
Bk02 br2 20 20
Bk01 br2 10 10
Bk02 br3 15 15
Bk03 br3 15 15
Bk03 br4 17 17
Bk04 br4 17 17
Bk04 br2 17 17
Bk04 br1 32 32
9 rows selected.
SQL> insert into book_lending values('Bk02','br3','c8','09-sep-2017','12-oct-2017','B4');
1 row created.
8 rows selected.
1 row created.
c1 20-FEB-17 20
c2 10-FEB-17 0
c3 10-APR-17 0
c4 20-APR-17 10
Query:
AUTHOR_NAM BOOK_ BRANC CARDN DATE_OUT DUE_DATE BID BOOK_ BRANC NO_OF_COPIES
---------- ----- ----- ----- --------- --------- ----- ----- ----- ------------
AUTHOR_NAM BOOK_ BRANC CARDN DATE_OUT DUE_DATE BID BOOK_ BRANC NO_OF_COPIES
---------- ----- ----- ----- --------- --------- ----- ----- ----- ------------
AUTHOR_NAM BOOK_ BRANC CARDN DATE_OUT DUE_DATE BID BOOK_ BRANC NO_OF_COPIES
---------- ----- ----- ----- --------- --------- ----- ----- ----- ------------
AUTHOR_NAM BOOK_ BRANC CARDN DATE_OUT DUE_DATE BID BOOK_ BRANC NO_OF_COPIES
---------- ----- ----- ----- --------- --------- ----- ----- ----- ------------
AUTHOR_NAM BOOK_ BRANC CARDN DATE_OUT DUE_DATE BID BOOK_ BRANC NO_OF_COPIES
---------- ----- ----- ----- --------- --------- ----- ----- ----- ------------
AUTHOR_NAM BOOK_ BRANC CARDN DATE_OUT DUE_DATE BID BOOK_ BRANC NO_OF_COPIES
---------- ----- ----- ----- --------- --------- ----- ----- ----- ------------
2 on(borrower.bid=book_lending.bid)
BID
-----
B1
on (book.book_id=book_copies.book_id);
View created.
1 row deleted.
----- ----------
Bk02 chethan
Bk03 Peter
Bk04 chethan
Bk02 br2 20 20
Bk02 br3 15 15
Bk03 br3 15 15
Bk03 br4 17 17
Bk04 br4 17 17
Bk04 br2 17 17
Bk04 br1 32 32
7 rows selected.
6 rows selected.
PL/sql
1) set serveroutput on
declare
total_books number(5);
branch_name varchar(10);
begin
open book_cur;
dbms_output.put_line('branch_id total_books');
loop
dbms_output.put_line(branch_name||' '||total_books);
end loop;
close book_cur;
end;
SQL> @ d:book.sql
branch_id total_books
br3 30
br2 47
br1 42
br4 34
2)
set serveroutput on
create or replace procedure proc_book(pbl_name varchar)is
total number(5);
cursor proc_cur is select sum(no_of_copies)from book_copies natural join
book where(pub_year=extract(year from sysdate)or pub_year=(extract(year
from sysdate)-1)or pub_year=(extract(year from sysdate)-2))and
pub_name=pbl_name;
begin
open proc_cur;
loop
fetch proc_cur into total;
exit when proc_cur%notfound;
dbms_output.put_line(pbl_name||' '||total);
end loop;
close proc_cur;
end;
/
sql>procedure2.sql
set serveroutput on
declare
p_name varchar(10);
begin
p_name:='&pub_name';
dbms_output.put_line('publisher_name total_books');
proc_book(p_name);
end;
/
Output:
SQL> ed d:procedure1.sql
SQL> @ d:procedure2.sql
Enter value for pub_name: bharath
old 4: p_name:='&pub_name';
new 4: p_name:='bharath';
publisher_name total_books
bharath 20
declare
name varchar(10);
due_date date;
begin
open due_cur;
dbms_output.put_line('Title Due_date');
loop
dbms_output.put_line(name||' '||due_date);
end loop;
end;
SQL> @ d:bookreturn.sql
Title Due_date
Perfect us 12-MAY-17
Note:available copies:
4) set serveroutput on
create or replace trigger tr_blending
before insert on book_lending
referencing OLD as o New as n
for each row
begin
update book_copies set avlbl_copies=avlbl_copies-1 where
book_id=:n.book_id;
end;
/
SQL> @ d:bktrigger.sql
Trigger created.
1 row created.
9 rows selected.
Bk01 br1 10 9
Bk02 br2 20 20
Bk01 br2 10 9
Bk02 br3 15 15
Bk03 br3 15 15
Bk03 br4 17 17
Bk04 br4 17 17
Bk04 br2 17 17
Bk04 br1 32 32
2) set serveroutput on
declare
b_id varchar(10);
begin
end;
SQL> @ d:bktrigger1.sql
Trigger created.
1 row created.
SQL> insert into book_return values('c6','10-may-2017',0);
1 row created.
Bk01 br1 10 9
Bk02 br2 20 20
Bk01 br2 10 9
Bk02 br3 15 15
Bk03 br3 15 16
Bk03 br4 17 18
Bk04 br4 17 18
Bk04 br2 17 18
Bk04 br1 32 33
9 rows selected.