Beruflich Dokumente
Kultur Dokumente
Exp:1
Date: 30/7/2013
Aim:
To write a query on creation and modification of relations using library management
system..
Queries:
SQL> create table library(book_id number(7),isbn_no number(7),subject char(25),language
char(25),name char(25),copies number(7),available number(7),cost number(7),author
char(25));
Table created.
SQL>desc library;
Name
Null?
Type
NUMBER(7)
ISBN_NO
NUMBER(7)
SUBJECT
CHAR(25)
LANGUAGE
CHAR(25)
NAME
CHAR(25)
COPIES
NUMBER(7)
AVAILABLE
COST
AUTHOR
NUMBER(7)
NUMBER(7)
CHAR(25)
LEIT116
Name
Null? Type
NUMBER(7)
STUDENT_ID
NUMBER(7)
ISSUE_DATE
DATE
DUE_DATE
DATE
RETURN_DATE
DATE
FINE
NUMBER(7)
ISSUERS_ID
NUMBER(7)
Null?
Type
CHAR(25)
AUTHOR_ID
NUMBER(7)
EDITION
NUMBER(7)
PUBLISHER_NAME
CHAR(25)
PUBLICATIONS
CHAR(25)
YR_OF_PUB
NUMBER(7)
ADDRESS
VARCHAR2(50)
LEIT116
Null?
------------------------------------- --------
Type
------------------------
STUDENT_ID
NUMBER(7)
STUDENT_NAME
CHAR(25)
ACC_NO
NUMBER(7)
REG_NO
NUMBER(7)
DEPARTMENT
CHAR(25)
TOTAL_LIMIT
NUMBER(7)
BOOKS_RETURNED
NUMBER(7)
BOOKS_PENDING
NUMBER(7)
Null? Type
NUMBER(7)
NAME
CHAR(25)
QUALIFICATION
CHAR(25)
DESIGNATION
CHAR(25)
DATE_OF_JOINING
DATE
LEIT116
CONTACT_NO
NUMBER(7)
ADDRESS
VARCHAR2(50)
ISBN_NO
----------------
-----------------
SUBJECT
--------------
100
COPIES
-------------------
200
NAME
-------------------
computer science
AVAILABLE
----------------
LANGUAGE
English
COST
java
AUTHOR
-----------------
50
--------
--------------500
samba
STUDENT_NAME
------------------------ram
TOTAL_LIMIT
ACC_NO
----------
----------
123
it
BOOKS_PENDING
--------------
-------------
15
100
----------------97
BOOKS_RETURNED
-----------
REG_NO DEPARTMENT
AUTHOR_ID
---------1
EDITION
---------10
PUBLISHER_NAME
---------------samba
LEIT116
PUBLICATIONS
YR_OF_PUB
ADDRESS
----------------------
-----------------
-----------
samba publish
2013
annanagarchennai
NAME
QUALIFICATION
------------
--------------
sita
DESIGNATION CONTACT_NO
---------------------
mba
librarian
---------222222
ADDRESS
----------annanagarmadurai
SQL> insert into libissue values('1','123','100','2','200713');
1 row created.
SQL> select * from libissue;
BOOK_ID
STUDENT_ID
FINE
----------
----------
---------1
123
ISSUERS_ID
----------
100
Table truncated.
SQL>desclibissue;
Name
---------2
Null? Type
NUMBER(7)
STUDENT_ID
NUMBER(7)
FINE
NUMBER(7)
ISSUE_DATE
200713
LEIT116
ISSUERS_ID
NUMBER(7)
ISSUE_DATE
NUMBER(7)
Null? Type
NUMBER(7)
STUDENT_ID
NUMBER(7)
FINE
NUMBER
ISSUERS_ID
NUMBER(7)
LEIT116
Result:
Thus the creation and modification of relation are done and successfully output was
verified.
Exp:2
Integrity Constraint
LEIT116
Aim:
To create and implement the usage of queries using constraint.
Queries:
Primary key :
Column level:
SQL> create table book2(bid number(10),bnamevarchar(10),authnamevarchar(20),co
nstraint book2_bid_pk primary key(bid));
Table created.
SQL>desc book1;
Name
-----------------------
Null?
--------
BOOKNAME
PRICE
Type
---------------------------CHAR(20)
NOT NULL
NUMBER(18)
Table level:
SQL> create table book2(bid number(10),bnamevarchar(10),authnamevarchar(20),co
nstraint book2_bid_pk primary key(bid));
Table created.
SQL>desc book2;
Name
-----------------BID
Null?
Type
--------
--------
NOT NULL
NUMBER(10)
BNAME
VARCHAR2(10)
AUTHNAME
VARCHAR2(20)
BID BNAME
PID
999
998
LEIT116
Column level:
SQL> create table bbbc(bid number(10) constraint
bbbc_bid_ukunique,bnamevarchar(20),pid number(10));
Table created.
SQL> insert into bbbcvalues(435,'maths',999);
1 row created.
SQL> insert into bbbcvalues(123,'social',998);
1 row created.
SQL> insert into bbbcvalues(123,'science',997);
insert into bbbc values(123,'science',997)
*ERROR at line 1:
ORA-00001: unique constraint (SCOTT.BBBC_BID_UK) violated
SQL> select * from bbbc;
BID BNAME
PID
999
998
LEIT116
1 row(s) inserted.
SQL>select * from student3;
ROLLNO
REGNO
----------------456
12it71
DEPARTMENT
-------------------IT
Foreign key
Table level:
SQL>create table publisher(pid number(10),pnamevarchar(20),phno
number(10),constraint publisher_pid_pk primary key(pid));
Table created
SQL>insert into book6 values(1232,'Linux',435,768,31245);
ORA-02291: integrity constraint (SYSTEM.BOOK6_PID_FK) violated - parent key not
found
SQL>insert into publisher values(111,'grewal',991123459)
1 row(s) inserted.
SQL>select * from publisher;
PID
PNAME
PHNO
------ -------------------111 Grewal
991123459
SQL>insert into book6 values(1241,'Linux',610,111,6122);
1 row(s) inserted.
Column level:
SQL>create table book01(bid number(10),bnamevarchar(20),authnamevarchar(20),pid
number(10),constraint book02_pid_fk foreign key(pid) references publisher01(pid) );
Table created.
SQL>insert into book01 values(11,'Maths','Grewall',190);
1 row(s) inserted.
SQL>insert into book01 values(124,'CO','Zvonko',191);
1 row(s) inserted.
Not null:
SQL>alter table book6 add isbnnumber(10) constraint book6_isbn_nn NOT NULL;
Table altered.
SQL>insert into book6(bid,bname,price,pid) values(465,'Java2',99.50,65);
ORA-01400: cannot insert NULL into ("SYSTEM"."BOOK6"."ISBN")
Cascade:
LEIT116
Result:
Thus the constraint was successfully completed and output was verified
Exp:3
LEIT116
Aim:
To execute the simple sql queries.
Queries:
Query 1
To display the DOB and address of the employee John Smith.
SQL> select bdate,address from employee where fname='John' and lname='Smith';
BDATE
ADDRESS
---------
-------------------
18-AUG-90
Pudur, Madurai
Query 2
To select the name and address of the employee who works in Research
department
SQL> select fname||' '||lname as Name,address from employee where ssn=(select
mgrssn from department where dname='Research');
NAME
ADDRESS
--------------------
--------------------
Raj Kumar
Kodambakkam, Chennai
Query 3
To display the Project No, Department No. And Department managers last
name,DOB and address for the projects located in Chennai.
SQL> select p.pnumber,p.dnumber,e.lname,e.bdate as DOB,e.address from
employee e,projectp,department d where d.dnum=p.dnumber and
p.plocation='Chennai' and d.mgrssn=e.ssn;
LEIT116
PNUMBER
DNUMBER
----------
------------
21
LNAME
DOB
--------------
------------
Smith
18-AUG-90
ADDRESS
-------------------Pudur Madurai
Query 4
To retrieve the first and last name of all employees along with their Managers no.
SQL> select e.fname,e.lname,e.ssn,d.mgrssn from employee e,department d where
e.ssn!=d.mgrssn and e.dno=d.dnum;
FNAME
LNAME
SSN
----------
----------------
----------
MGRSSN
----------
Raj
Kumar
10
John
Smith
Ram
Kumar
Query 5
To select all employees name and SSN
SQL> select fname|| ||lname as Name,ssn from employee;
NAME
SSN
----------
--------
Ram Kumar
John Smith
Raj Kumar
LEIT116
Query 6:
To select all employees name, SSN and the departments name.
SQL> select d.dname,d.dnum,e.fname||||e.lname as name,e.ssn from employee
e,department d where d.mgrssn=e.ssn;
DNAME
----------
DNUM
----------
NAME
----------
----------
Research
Raj Kumar
Technical
John Smith
Accounts
Ram Kumar
SSN
Query 7:
Retrieving the salary of all employees
SQL> select fname||' '||lname as Name_Of_Employee,salary from employee;
NAME_OF_EMPLOYEE
-----------------------------Ram Kumar
SALARY
---------14000
John Smith
10500
Raj Kumar
10500
Query 8
To retrieve the distinct salary values.
SQL> select distinct salary from employee;
LEIT116
SALARY
---------14000
10500
Query 9
To retrieve the name of the employee who is from Chennai.
SQL> select fname||' '||lname as name from employee where address like
'%Theni';
NAME
------------------------------Ram Kumar
Query 10
Employees who were born in 1990.
SQL> select fname|| ||lname as name from employee where bdate like '%90';
NAME
---------------------------John Smith
Query 11
To show the resulting salary and the name of the employee who is working on the
project Web Design and is given 10% rise in salary.
LEIT116
FNAME
ESSN
---------------
----------
----------
Web Design
John
SALARY
---------11550
Query 12
To retrieve all employees in the department 3 whose salary is in between 13K and
15K.
SQL> select fname,salary from employee where dno=3 and salary between 13000
and 15000;
FNAME
SALARY
----------
------------
Ram
14500
Query 13
To retrieve the list of employees and the project they are working on ordered by
the department number.
SQL> select distinct e.fname,p.pname,p.pnumber,e.dno from employee e,project p
where e.dno=p.dnumber order by e.dno;
FNAME
PNAME
----------
---------------
Raj
PNUMBER
--------------
Cryptography
--------
42
Ram
yyy
41
Ram
zzz
35
DNO
3
5
5
LEIT116
John
Web Design
21
Query 14
To retrieve the name of employee whose project is being controlled by department
number 6
SQL> select e.fname||' '||e.lname as name,p.pnumber,e.dno from project
p,employe e e,workson w where w.essn=e.ssn and w.pno=p.pnumber and
p.dnumber=6;
NAME
--------------------John Smith
PNUMBER
-------------21
DNO
---------6
LEIT116
Result:
Thus the simple sqlqueries was successfully executed and output was verified.
Exp:4
20/8/2013
Aim:
To write a basic simple sql queries in library management system
Queries:
1. Find the id and title of all courses which do not require any prerequisities.
selectcourse_id,title from course where course.course_id not in (select course_id from
prereq);
COURSE_ID
BIO-301
BIO-399
CS-101
FIN-201
HIS-351
MU-199
PHY-101
TITLE
Genetics
Computational Biology
Intro.to Computer Science
Investment Banking
World History
Music Video Production
Physical Principles
LEIT116
195000
60000
87000
175000
62000
80000
72000
update instructor set salary=salary+(0.1*salary);
14 row(s) updated.
Select salary from instructor;
SALARY
71500
99000
44000
104500
66000
95700
82500
68200
88000
79200
3.Write SQL update query to increase the total credits of all students who have taken
the course title Genetics by the no. Of credits associated with the course.
select * from student;
ID
NAME DEPT_NAME
00128
Zhang
Comp. Sci.
12345
Shankar Comp. Sci.
19991
Brandt
History
23121
Chavez
Finance
44553
Peltier
Physics
45678
Levy
Physics
54321
Williams Comp. Sci.
55739
Sanchez
Music
70557
Snow
Physics
76543
Brown
Comp. Sci.
76653 Aoi
Elec. Eng.
98765 Bourikas
Elec. Eng.
98988 TanakaBiology
120
TOT_CRED
102
32
80
110
56
46
54
38
0
58
60
98
LEIT116
71500
99000
Mozart
44000
Einstein
104500
LEIT116
Elsaid
66000
Gold
95700
Katz
82500
Califeri
68200
Singh
88000
Crick
79200
Brandt 101200
Sara
55000
Sandy
44000
Kim
193000
NAME
Einstein
Crick
Srinivasan
Kim
Singh
Katz
2
1
1
2
1
2
SALARY
71500
99000
44000
214500
66000
95700
LEIT116
Katz
192500
Califieri
68200
Singh
88000
Crick
79200
Brandt
101200
Kim
198000
Sara
55000
Sandy
44000
6.Write SQL update query to set the credits to 2 for all courses which have less than 5
students taking them COURSE_ID
TITLE
DEPT_NAME
CREDITS
BIO-301
BIO-399
CS-101
Genetics
Biology
ComputationalBiology
Intro.to Computer Science
Biology
Comp. Sci.
CS-190
Game Design
Comp. Sci.
CS-315
Robotics
Comp. Sci.
CS-319
Image Processing
Comp. Sci.
CS-347
EE-181
FIN-201
Investment Banking
HIS-351
World History
MU-199
PHY-101
Physical Principles
3
3
Elec. Eng.
Finance
3
3
History
Music
3
3
Physics
update course set credits=2 where course_id in (select course.course_id from course,takes
where course.course_id=takes.course_id group by course.course_id having
count(takes.id)<5);
COURSE_ID
BIO-301
TITLE
Genetics
DEPT_NAME
CREDITS
Biology
LEIT116
BIO-399
Computational Biology
CS-101
CS-190
Game Design
CS-315
Robotics
CS-319
Image Processing
CS-347
EE-181
FIN-201
Investment Banking
HIS-351
World History
MU-199
Biology
Comp. Sci.
3
4
2
Comp. Sci.
Comp. Sci.
Comp. Sci.
2
2
2
Elec. Eng.
Finance
2
2
History
Music
2
2
LEIT116
Result;
Thus the basic simple sql queries are successfully executed and the output was
verified.
EXP:5
JOINS
27/8/13
Aim:
To write a simple sql queries on joins using our application.
Queries:
SQL> create table stu(id number(7),name varchar(20),dept_namevarchar(20),tot_credit
number(3));
Table created.
SQL>descstu
Name
Null? Type
NUMBER(7)
VARCHAR2(20)
DEPT_NAME
VARCHAR2(20)
TOT_CREDIT
NUMBER(3)
LEIT116
Null? Type
NUMBER(7)
NUMBER(7)
NUMBER(7)
NUMBER(3)
NUMBER(7)
CHAR(3)
Null? Type
LEIT116
NUMBER(7)
VARCHAR2(20)
VARCHAR2(20)
NUMBER(10)
Null? Type
NUMBER(7)
NUMBER(7)
NUMBER(7)
NUMBER(3)
NUMBER(7)
LEIT116
1 row created.
Joins queries:
INNER JOINS:
SQL> select stu.name,tak.id from stu inner join tak on stu.id=tak.id;
NAME
ID
-------------------- ---------maha
116
sne
116
OUTER JOINS:
Left outer join:
SQL> select * from stu left outer join tak on stu.id=tak.id;
ID NAME
DEPT_NAME
TOT_CREDIT
ID
SEC_ID SEMESTER
YEAR GRA
it
90
116 sne
it
50
ID NAME
DEPT_NAME
TOT_CREDIT
SEC_ID SEMESTER
YEAR GRA
ID
LEIT116
78
9
2009 C
52
31
2012 C
45
32
2013 A
ID NAME
DEPT_NAME
TOT_CREDIT
ID
SEC_ID SEMESTER
YEAR GRA
116 maha
2010 A
it
60
ID NAME
DEPT_NAME
TOT_CREDIT
SEC_ID SEMESTER
YEAR GRA
it
90
ID
LEIT116
116 sne
it
116 maha
50
it
ID NAME
60
DEPT_NAME
TOT_CREDIT
ID
SEC_ID SEMESTER
YEAR GRA
2009 C
52
31
2012 C
45
32
ID NAME
2013 A
DEPT_NAME
TOT_CREDIT
SEC_ID SEMESTER
YEAR GRA
ID
LEIT116
2010 A
7 rows selected.
Interact:
SQL>select name from tak interact select name from stu;
NAME
----------Maha
Subha
Sne
Union:
SQL> select namedept_name from stu union select semester from tak where
tot_cred=90;
DEPT_NAME
SEMESTER
Subha
LEIT116
LEIT116
Result;
Thus the joins queries are successfully executed by using the library management
application application.
EXP:6
10/9/13
Aim:
To write a simple queries by using views
Queries:
Create view:
SQL> create view lib_view as select subject,author from library;
View created.
Display:
SQL> select * from lib_view;
SUBJECT
AUTHOR
balagursamy
SQL>desclib_view;
Name
Null?
Type
CHAR(25)
AUTHOR
CHAR(25)
LEIT116
Balagursamy
SQL> create view lib_view1(name,dob,cellno) as select docname,docid,cellno
from docdet where docid=12 ;
View created.
SQL>desc lib_view1;
Name
Null?
Type
VARCHAR2(5)
NUMBER(10)
CELLNO
NUMBER(10)
Check option:
SQL> create view lib_view3(name,address,wardno) as select
docname,docid,cellno from docdet where docid=112 with check option;
View created.
Update:
SQL>update lib_view3 libno=116 where name=main;
O Rows selected.
SQL> alter view lib_view3 compile;
View altered.
Read only option:
SQL> create view lib_view4(name,dob,cellno) as select docname,docid,cellno
from docdet where docid=1WITH READ ONLY;
View created.
SQL>desc lib_view4;
Name
Null?
Type
VARCHAR2(5)
NUMBER(10)
NUMBER(10)
LEIT116
Drop view:
SQL> drop view lib_view;
View dropped.
SQL> select * from lib_view;
no rows selected.
LEIT116
Result:
Thus the creation and updation of view was successfully executed and
output was verified.
Expno:7
EXERCISES IN PL/SQL
17/09/2013
Aim:
To implement an query on pl/sql statement.
Pl/sql blocks:
Bind variable:
SQL> set serveroutput on
SQL> VARIABLE bindvar NUMBER
SQL> declare
2 v_num number(2);
3 BEGIN
4 v_num := 5;
5 :bindvar := v_num*2;
6 END;
7 /
PL/SQL procedure successfully completed.
SQL> PRINT bindvar;
G_DOUBLE
---------10
SQL> VARIABLE g_double NUMBER
LEIT116
SQL> declare
2 v_num number(2);
3 begin
4 v_num := &p_num;
5 :g_double := v_num*2;
6 end;
7 /
Enter value for p_num: 20
old 4: v_num := &p_num;
new 4: v_num := 20;
PL/SQL procedure successfully completed.
SQL> print g_double;
G_DOUBLE
---------40
SQL> variable num number
SQL> set serveroutput on
SQL> declare
2 double number;
3 begin
4 :num := 5;
5 double := :num*2;
6 dbms_output.put_line(('double of'||to_char(:num) || 'is' || to_char(double)
));
7 end;
8 /
LEIT116
double of5is10
PL/SQL procedure successfully completed.
Anchor declaration
SQL> variable b number
SQL> declare
2 v_num number(2);
3 v_num2 v_num%type;
4 begin
5 v_num:=4;
6 v_num2:=8;
7 :b:=v_num*v_num2;
8 end;
9 /
PL/SQL procedure successfully completed.
SQL> print b
B
---------32
Control statements
Decode function
SQL> select fname,lname,
2 decode(101,1000,
3 102,2000,
4 103,3000)from emp;
LEIT116
FNAME
LNAME
DECODE(101,1000,102,2000,103,3000)
3000
nathpriy
3000
kumararun
3000
if..then..elseif..endif
SQL> declare
2 m number:=&mark;
3 grade char;
4 begin
5 if m>=190 and m<=200then
6 grade:='a';
7 elseif m>=160 and m<=190 then
8 grade:='b';
9 elseif m>=120 and n<=160 then
10 grade:='u';
11 end if;
12 dbms_output.put_line(grade);
13 end;
14 /
Enter value for mark: 199
old 2: m number:=&mark;
new 2: m number:=199;
a
PL/SQL procedure successfully completed.
Searched case:
LEIT116
SQL> declare
2 v_num number:=&any_num;
3 v_res number;
4 begin
5 v_res:=v_num
6 casev_res
7 when 0 then dbms_output.put_line(v_num||'is even');
8 elsedbms_output.put_line(v_num||'is odd');
9 end case;
10 end;
11./
Enter value for any_num: 3
old 2: v_num number:=&any_num;
new 2: v_num number:=3;
3is odd
PL/SQL procedure successfully completed.
Case structure
SQL> declare
2 v_num number:=&any_num;
3 v_res number;
4 begin
5 v_res:=v_num
6 casev_res
7 when 0 then dbms_output.put_line(v_num||'is even');
8 elsedbms_output.put_line(v_num||'is odd');
9 end case;
LEIT116
10 end;
11 /
Enter value for any_num: 4
old 2: v_num number:=&any_num;
new 2: v_num number:=4;
4is even
PL/SQL procedure successfully completed.
Nested if
SQL> set serveroutput on
SQL> declare
2 v_name char:='&name';
3 v_id number(2):='&id';
4 v_age number(2):='&age';
5 begin
6 if(v_name='gayu' and v_id=11)then
7 v_age:=23;
8 end if;
9 if(v_name='anu' and v_id=12)then
10 v_age:=24;
11 end if;
12 if(v_name='priya' and v_id=13)then
13 v_age:=25;
14 end if;
15 if(v_name='kris' and v_id=14)then
16 v_age:=26;
17 end if;
LEIT116
18 dbms_output.put_line(v_name);
19 dbms_output.put_line(to_char(v_id));
20 dbms_output.put_line(to_char(v_age));
21 en
2 /
Enter value for name: anu
old 2: v_name char:='&name';
new 2: v_name char:='anu';
Enter value for id: 12
old 3: v_id number(2):='&id';
new 3: v_id number(2):='12';
Enter value for age: 24
old 4: v_age number(2):='&age';
new 4: v_age number(2):='24';
anu
12
24
PL/SQL procedure successfully completed.
If..then.end..if
SQL> declare
2 v_num number(5):=112;
3 v_id number(5):=115;
4 employee number(5);
5 begin
6 ifv_num>113 then
7. employee:=114;
LEIT116
8. employee:=112;
9. end if;
10. end;
11./
PL/SQL procedure successfully completed.
If..then..else..endif
SQL> declare
2 v_num number(5):=112;
3 v_id number(5):=115;
4 employee number(5);
5 begin
6 ifv_num>113 then
7 employee:=114;
8 else
9 employee:=112;
10 end if;
11 end;
12 /
PL/SQL procedure successfully completed.
If..then..elseif..endif
SQL> declare
2 m number(3):=&mark;
3 grade char;
4 begin
5 if m>=190 and m<=200then
LEIT116
6 grade:='good';
7 elseif m>=170 and m<=190;
8 grade:='excellent';
9 elseif m>=140 and m<=170;
10 grade:='fair';
11 endif;
12 dbms_output.put_line(grade);
13 end;
14 /
Enter value for mark: 170
old 2: m number(3):=&mark;
new 2: m number(3):=170;
Looping statements
for loop
SQL> declare
2 v_countnumber(2);
3 v_sumnumber(2):=0;
4 v_avgnumber(3,1);
5 begin
6 for v_count in 1..10 loop
7 v_sum:=v_sum+v_count;
8 end loop;
9 v_avg:=v_sum/10;
10 dbms_output.put_line(to_char(v_avg));
11 end;
LEIT116
12 /
5.5
PL/SQL procedure successfully completed.
While loop:
SQL> set serveroutput on
SQL> declare
2 v_count number(2);
3 v_sum number(2):=0;
4 v_avg number(3,1);
5 begin
6 v_count:=1;
7 whilev_count<=10 loop
8 v_sum:=v_sum+v_count;
9 v_count:=v_count+1;
10 end loop;
11 v_sum:=v_sum+v_count;
12 v_avg:=v_sum/(v_count-1);
13 dbms_output.put_line(to_char(v_avg));
14 end;
15 /
6.6
PL/SQL procedure successfully completed.
Basic loop:
SQL> setserveroutput on
SQL> declare
2 numnumber(5);
LEIT116
3 id number(5);
4 begin
5 loop
6 num:=num+1;
7 id:=id+1;
8 exit when num=2;
9 end loop;
10 end;
11 /
10
2
PL/SQL procedure successfully completed.
Update:
SQL> set serveroutput on
SQL> declare
2 cnumber number:=&value;
3 begin
4 update employee
5 set salary=salary*(1+value)
6 whereemployeeif=&emp_id;
7 commit;
8 end;
9 /
Enter value for value: 3
old 2: cnumber number:=&value;
new 2: cnumber number:=3;
LEIT116
LEIT116
LEIT116
LEIT116
Result:
Thus the pl/sql program was executed successfully.
EXP:08
CURSOR MANAGEMENT
01/10/13
Aim:
To implement the cursor management on library management system.
Queries:
Explicit cursor attributes:
SQL> set serveroutput on;
SQL> declare
2 -- declare the variables
3 c_namestudent.name%type;
4 c_tot_credstudent.tot_cred%type;
5
6 cursor student_cur is
7 select name,tot_cred
8 from student
9 where id=76543;
10 begin
11 if not student_cur%isopen then
12 --open the cursor
13
open student_cur;
14
end if;
LEIT116
15
loop
20 dbms_output.put_line(c_name||' '||c_tot_cred);
21 end loop;
22 dbms_output.put_line(student_cur%rowcount||'student(s) found');
23 --close the cursor
24 closestudent_cur;
25 end;
26 /
Brown 58
1student(s) found
PL/SQL procedure successfully completed.
LEIT116
9 dbms_output.put_line(stu_rec.name||' ');
10 dbms_output.put_line(stu_rec.tot_cred||' ');
11 end if;
12 end loop;
13 end;
14 /
Zhang
102
Chavez
110
Bourikas
98
Tanaka
120
update student
LEIT116
9 set tot_cred=(stu_rec.tot_cred/10)
10 --Where current of clause
11 where current of student_cur;
12
end loop;
13
end;
14 /
LEIT116
5 dbms_output.put_line('lib_no.id:'||lib_no.id);
6 end loop;
7 commit;
8 end;
9 /
lib_no.id:112
lib_no.id:114
lib_no.id:115
LEIT116
SQL> declare
2 v_namelibrary.id%type;
3 begin
4 dbms_output.put_line('first'||v_name);
5 select id into v_name from library;
6 dbms_output.put_line('second'||v_name);
7 exception
8 whentoo_many_rows then
9 dbms_output.put_line('third'||v_name);
10 dbms_output.put_line('exception'||v_name);
11 end;
12 /
first
third112
exception112
PL/SQL procedure successfully completed.
User defined exception:
SQL> declare
2 v_name exception;
3 v_no exception;
4 v_namelibrary.name%type;
5 begin
6 select name into v_name from libaray
7 wherelibraryid=&v_id;
8 ifv_name<0 then
9 raisev_name;
LEIT116
15 exception
16 whenno_data_found
17 dbms_output.put_line('no_data_found');
18 end;
19 /
Enter value for v_id: 2
old 7: where libraryeid=&v_id;
new 7: where libraryid=2
LEIT116
Result:
Thus the cursor was successfully executed and output was verified.
EXP:9
DATE:15/10/13
PL/SQL BLOCKS:
Procedures:
SQL> create or replace procedure search_lib
2 (i_libid in number,
3 o_last out varchar,
4 o_first out varchar)
5 is
6 begin
7 selectid,name
8 intoo_last,o_first
9 from wow
10 where wow.id=i_empid;
11 exception
12 when others then
13 dbms_output.put_line('name is'||o_last);
14 endsearch_emp;
15 /
Procedure created.
SQL> set serveroutput on;
SQL> declare
2 v_lastwow.name%type;
LEIT116
3 v_firstwow.category%type;
4 v_idwow.id%type:=&mp_id;
5 begin
6 search_emp(v_id,v_last,v_first);
7 ifv_last is not null then
8 dbms_output.put_line('library'||v_id);
9 dbms_output.put_line('name'||v_last||','||v_first);
10 end if;
11 end;
12 /
Enter value for mp_id: 12
old 4: v_idwow.id%type:=&mp_id;
new 4: v_idwow.id%type:=12;
library 12
name12,maha
PL/SQL procedure successfully completed.
Function:
SQL> create or replace function val_get
2 (i_id in number)
3 returnvarchar
4 is
5 v_namevarchar(10);
6 begin
7 select name into v_name
8 from wow
9 where id=i_id;
10 returnv_name;
11 endval_get;
LEIT116
12 /
Function created.
SQL> declare
2 v_idwow.id%type:=&id;
3 v_namewow.name%type;
4 begin
5 select id
6 intov_id from wow
7 where wow.id=v_id;
8 v_name:=val_get(v_id);
9 dbms_output.put_line('the name is'||v_name);
10 exception
11 when others then
12 dbms_output.put_line(v_id||'not found');
13 end;
14 /
Enter value for id: 12
old 2: v_idwow.id%type:=&id;
new 2: v_idwow.id%type:=12;
the name is maha
PL/SQL procedure successfully completed.
SQL> create or replace procedure displayquantity is
2
temp_quantitynumber(10);
3 begin
4 select quantity into temp_quantity from book
5 where bookno=12;
6
if temp_quantity>100 then
dbms_output.put_line('success');
LEIT116
8
else
dbms_output.put_line('not');
10
11
end if;
exception
12
13
14
end displayquantity;
15 /
Procedure created.
Execute:
SQL> execute displayquantity
PL/SQL procedure successfully completed.
SQL> set serveroutput on;
SQL> execute displayquantity
success
PL/SQL procedure successfully completed.
SQL>drop procedure displayquantity;
Procedure dropped.
SQL> create or replace function retrievequantity
2 return number
3 is
4 v_quantity number(10);
5 begin
6 select quantity into v_quantity
7 from book
8 wherebookno=12;
9 returnv_quantity;
10 endretrievequantity;
LEIT116
11 /
Function created.
SQL>varv_qty number;
SQL>EXEC :v_qty := retrievequantity;
PL/SQL procedure successfully completed.
SQL> print v_qty;
V_QTY
---------150
Drop a function:
SQL>drop function retrievequantity;
Function dropped.
Package:
SQL> create or replace PACKAGE book AS
2 PROCEDUREfindbook(
3 medno IN book.bkno%type,
4 description IN book.description%type,
5 cost IN book.cost%type);
6 v_bknoNOTFOUND EXCEPTION;
7 FUNCTIONgoodidentifier(
8 bkno IN book.bkno%type)
9 return BOOLEAN;
10 end book;
11 /
Package created.
SQL> create or replace PACKAGE BODY book AS
2
PROCEDURE findbook(
3 v_medno IN book.bkno%type,
LEIT116
4 v_description OUT book.description%type,v_cost OUT book.cost%type) IS
5 begin
6 select description,cost
7 into v_description,v_cost
8 from
9 book where medno=v_medno;
10 if SQL%ROWCOUNT = 0 then
11 RAISE v_bkno NOTFOUND;
12 end if;
13 end findbook;
14 FUNCTION goodidentifier(
15 v_medno IN book.bkno%type)
16 return BOOLEAN
17 IS
18 v_id_count number;
19 begin
20 select count(*) into v_id_count
21 from book
22 where bkno=v_bkno;
23 return (1=v_id_count);
24 EXCEPTION
25 WHEN OTHERS THEN
26 RETURN FALSE;
27 END goodidentifier;
28 end med;
29 /
LEIT116
SQL> declare
2
v_descriptionbook.description%type;
v_costbook.cost%type;
v_mednobook.bkno%type;
begin
book.findbook(v_bkno,v_description,v_cost);
Result:
Thus the pl/sql in procedure,function and packages was successfully
executed and the output was verified.
EXP:10
22.10.2013
TRIGGERS
LEIT116
BEFORE TRIGGERS:
SQL> SET SERVEROUTPUT ON
SQL> CREATE OR REPLACE TRIGGER LIB_BI_TRIGGER
2 BEFORE INSERT ON LIB
3 FOR EACH ROW
4 DECLARE
5 V_BKID NUMBER(9);
6 V_BKNAME VARCHAR(12);
7 BEGIN
8 SELECT BKID,BKNAME INTO V_BKID,V_BKNAME FROM LIB;
9 :NEW.BKID:=V_BKID;
10 :NEW.BKNAME:=V_BKNAME;
11 END;
12 /
Trigger created.
AFTER TRIGGERS:
SQL> CREATE OR REPLACE TRIGGER LIB_ADU_TRIGGER
2 AFTER DELETE OR UPDATE ON LIB
3 DECLARE
4 V_LIBNAME VARCHAR(9);
5 BEGIN
6 IF DELETING THEN
7 V_LIBNAME:='DELETE';
8 ELSIF UPDATING THEN
9 V_LIBNAME:='UPDATE';
10 END IF;
LEIT116
BKID
80
UPDATE:
SQL> UPDATE LIB1 SET BKID=BKID+10 WHERE BKNAME='JAVA';
1 row updated.
SQL> SELECT * FROM LIB1;
BKNAME BKTYPE
LIBID
90
INSTEAD OF TRIGGERS:
NO DATA MANIPULATION THROUGH COMPLEX VIEW:
SQL> CREATE TABLE DOC1(DOCNAME VARCHAR(10),DOCID
NUMBER(9),CELLNO NUMBER(10));
Table created.
SQL> INSERT INTO DOC1 VALUES('SIVA',789,9876543456);
1 row created.
SQL> INSERT INTO DOC1 VALUES('GUNA',779,9878967251);
1 row created.
LEIT116
DOCID
CELLNO
789 9876543456
GUNA
779 9878967251
DOCID
CELLNO
789 9876543456
LEIT116
3 WHERE DOCID=899;
View created.
SQL> CREATE OR REPLACE TRIGGER GOVTHOS_DELETE_IOD
2 INSTEAD OF DELETE ON DOCHOS
3 FOR EACH ROW
4 BEGIN
5 DELETE FROM HOS4
6 WHERE DOCID=899;
7 END;
8 /
Trigger created.
DELETE:
SQL> DELETE FROM DOCHOS WHERE DOCNAME='RAVI';
0 rows deleted.
QUERIES:
WRITE A PL/SQL TRIGGER TO ENFORCE THE FOLLOWING RULES :
A.) AN EMPLOYEE CANT HAVE A SALARY HIGHER THAN HIS/HER DEPT
MANAGER:
B.) AN EMPLOYEE CANT WORK ON MORE THAN TWO PROJECTS
CONTOLLED BY A SINGLE DEPT:
SQL> create or replace trigger tri1 before insert on employee
2 for each row
3 declare
4 p number(20);
5 s number(10);
6 begin
LEIT116
LEIT116
1 row created.
SQL> insert into project values('p1',16,'ngl',13);
1 row created.
RESULT:
Thus the queries are implemented by using triggers.
EXP:11
29.10.13
LEIT116
Aim :
To implement clustering and classification by using weka.
CLASSIFICATION:
=== Run information ===
Scheme:
weka.classifiers.rules.ZeroR
Relation:
weather
Instances:
14
Attributes: 5
outlook
temperature
humidity
windy
play
Test mode:
10-fold cross-validation
64.2857 %
35.7143 %
Kappa statistic
LEIT116
Mean absolute error
0.4762
0.4934
100
100
14
%
%
0.643
Weighted Avg.
0.643
0.643
a b <-- classified as
9 0 | a = yes
5 0 | b = no
1
0
0.783
0
0.413
0.178 yes
0.178 no
0.643
0.503
0.178
LEIT116
@relation weather
@data
sunny,85,85,FALSE,no
sunny,80,90,TRUE,no
overcast,83,86,FALSE,yes
rainy,70,96,FALSE,yes
rainy,68,80,FALSE,yes
rainy,65,70,TRUE,no
overcast,64,65,TRUE,yes
LEIT116
sunny,72,95,FALSE,no
sunny,69,70,FALSE,yes
rainy,75,80,FALSE,yes
sunny,75,70,TRUE,yes
overcast,72,90,TRUE,yes
overcast,81,75,FALSE,yes
rainy,71,91,TRUE,no
CLUSTERING:
=== Run information ===
Scheme:
Relation:
weather
Instances:
14
Attributes: 5
outlook
temperature
humidity
windy
play
Test mode:
LEIT116
EM
==
Cluster
Attribute
0
(1)
======================
outlook
sunny
overcast
rainy
[total]
17
temperature
mean
73.5714
std. dev.
6.3326
humidity
mean
81.6429
std. dev.
9.9111
windy
TRUE
FALSE
[total]
play
16
LEIT116
yes
10
no
[total]
16
EM
==
Cluster
Attribute
0
(1)
======================
outlook
sunny
overcast
rainy
[total]
10
temperature
mean
std. dev.
72.2857
6.227
humidity
mean
79.5714
LEIT116
windy
TRUE
FALSE
[total]
play
yes
no
[total]
Clustered Instances
7 (100%)
LEIT116
LEIT116
Result:
Thus the implementation of classification and clustering was successfully executed.