3. LEIT116
SQL> alter table student add(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));
Table altered.
SQL>desc student
Name
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)
SQL> create table staffincharge as select issuers_id from libissue;
Table created.
SQL> alter table staffincharge add(staff_id number(7),name char(25),qualification
char(25),designation char(25),date_of_joiningdate,contact_no number(7),address
varchar(50));
Table altered.
SQL>descstaffincharge;
Name
Null?
Type
----------------------------------------- -------- ---------------------------ISSUERS_ID
NUMBER(7)
NAME
CHAR(25)
QUALIFICATION
CHAR(25)
DESIGNATION
CHAR(25)
4. LEIT116
DATE_OF_JOINING
DATE
CONTACT_NO
NUMBER(7)
ADDRESS
VARCHAR2(50)
SQL> insert into library values(1,100,'computerscience','english','java',200,50,500,'samba');
1 row created.
SQL> select * from library;
BOOK_ID
ISBN_NO
----------------
-----------------
1
SUBJECT
--------------
100
COPIES
200
-------------------
--------
English
COST
java
AUTHOR
-----------------
50
NAME
-------------------
computer science
AVAILABLE
----------------
LANGUAGE
--------------500
samba
SQL> insert into student values(12,'ram',123,97,'it',100,5,15);
1 row created.
SQL> select * from student;
STUDENT_ID
---------------12
STUDENT_NAME
------------------------ram
TOTAL_LIMIT
-----------
ACC_NO
REG_NO DEPARTMENT
----------
----------
123
BOOKS_RETURNED
----------------97
it
BOOKS_PENDING
--------------
-------------
5
15
100
SQL> insert into libauthorvalues('samba',1,10,'samba','samba publish',2013,'anna
nagarchennai');
1 row created.
SQL> select * from libauthor;
AUTHOR
----------------
AUTHOR_ID
----------
EDITION
----------
PUBLISHER_NAME
----------------
5. LEIT116
samba
1
10
PUBLICATIONS
YR_OF_PUB
----------------------
-----------------
samba publish
2013
samba
ADDRESS
----------annanagarchennai
SQL> insert into staffincharge values('2','sita','mba','librarian','222222','anna nagarmadurai');
1 row created.
SQL> select * from staffincharge;
ISSUERS_ID
-----------
NAME
QUALIFICATION
------------
2
--------------
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
Null?
---------2
SQL> truncate table libissue;
Type
---------------------------------- -------- ---------------------------BOOK_ID
NUMBER(7)
STUDENT_ID
NUMBER(7)
ISSUE_DATE
200713
6. LEIT116
FINE
NUMBER(7)
ISSUERS_ID
NUMBER(7)
ISSUE_DATE
NUMBER(7)
SQL> select *from libissue;
no rows selected
SQL> delete from libissue where book_id=1;
0 rows deleted.
SQL> alter table libissue drop column issue_date;
Table altered.
SQL> alter table libissue drop column due_date;
Table altered.
SQL> alter table libissue drop column return_date;
Table altered.
SQL>desclibissue
Name
Null?
Type
----------------------------------------- -------- ---------------------------BOOK_ID
NUMBER(7)
STUDENT_ID
NUMBER(7)
FINE
NUMBER
ISSUERS_ID
NUMBER(7)
SQL> alter table libissue add issue_datenumber(7);
Table altered.
10. LEIT116
Table created.
SQL>insert into student3(rollno,regno) values(456,'12it71');
1 row(s) inserted.
SQL>select * from student3;
ROLLNO
REGNO
DEPARTMENT
-----------------------------------456
12it71
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")
11. LEIT116
Cascade:
SQL>create table book02(bid number(10),bnamevarchar(20),authnamevarchar(20),pid
number(10),constraint book02_pid_fk foreign key(pid) references publisher02(pid) on
delete cascade);
Table created.
SQL>insert into book02 values(453,'Java','IraPohl',134);
1 row(s) inserted.
SQL>insert into book02 values(456,'dsd','Morrismano',135);
1 row(s) inserted.
Set :
SQL>create table book03(bid number(10),bnamevarchar(20),authnamevarchar(20),pid
number(10),constraint book03_pid_fk foreign key(pid) references publisher03(pid) on
delete set null);
Table created.
SQL>insert into book03 values(862,'Surveying','Punmia',637);
1 row(s) inserted.
SQL>insert into book03 values(852,'CO','Zvonko',737);
1 row(s) inserted.
Result:
Thus the constraint was successfully completed and output was verified
12. LEIT116
Exp:3
Simple SQL Queries
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 manager’s 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;
13. LEIT116
PNUMBER
DNUMBER
----------
------------
21
6
LNAME
DOB
--------------
------------
Smith
ADDRESS
--------------------
18-AUG-90
Pudur Madurai
Query 4
To retrieve the first and last name of all employees along with their Manager’s 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
5
10
John
Smith
2
4
Ram
Kumar
1
9
Query 5
To select all employee’s name and SSN
SQL> select fname||’ ‘||lname as Name,ssn from employee;
NAME
SSN
----------
--------
Ram Kumar
1
John Smith
2
Raj Kumar
5
Query 6:
To select all employee’s name, SSN and the department’s name.
14. LEIT116
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
SSN
----------
----------
Research
3
Raj Kumar
5
Technical
6
John Smith
2
Accounts
5
Ram Kumar
1
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;
SALARY
---------14000
10500
15. LEIT116
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.
SQL> select p.pname,e.fname,w.essn,e.salary+0.1*e.salary as salary from
employee e,projectp,workson w where p.pname='Web Design' and
w.pno=p.pnumber and w.essn=e.ssn;
PNAME
FNAME
---------------
----------
----------
Web Design
John
2
Query 12
ESSN
SALARY
---------11550
16. LEIT116
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
Web Design
3
35
John
DNO
5
5
21
6
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
---------------------
PNUMBER
--------------
DNO
----------
18. LEIT116
Exp:4
Basic simple SQL queries
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
2. Write SQL update query to increase 10% salary to all instructors.
SALARY
65000
90000
40000
195000
60000
87000
175000
62000
80000
72000
update instructor set salary=salary+(0.1*salary);
14 row(s) updated.
Select salary from instructor;
SALARY
19. LEIT116
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 Tanaka
Biology
TOT_CRED
102
32
80
110
56
46
54
38
0
58
60
98
120
update student120 set tot_cred=tot_cred+(select credits from course where
course.title='Genetics') where id in(select student120.id from student120,course where
student120.dept_name=course.dept_name and course.title='Genetics');
1 row(s) updated.
ID
NAME
00128
Zhang
12345
Shankar
19991 Brandt
History
DEPT_NAME
Comp. Sci.
Comp. Sci.
80
TOT_CRED
102
32
20. LEIT116
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 Tanaka
Biology
110
56
46
54
38
0
58
60
98
122
4.Find the names of students who have not taken any biology department courses.
select name from student120 where dept_name!='Biology';
NAME
Zhang
Shankar
Brandt
Chavez
Peltier
Levy
Williams
Sanchez
Snow
Brown
5. Write SQL update query to list all the instructors who are advisor of atleast two
students,increase the salary by 50000.
Srinivasan
Wu
71500
99000
Mozart
44000
Einstein
104500
Elsaid
66000
Gold
95700
Katz
82500
Califeri
68200
21. LEIT116
Singh
88000
Crick
79200
Brandt 101200
Sara
55000
Sandy
44000
Kim
193000
select count(advisor.s_id),instructor.name from advisor,instructor,student where
student.id=advisor.s_id and instructor.id=advisor.i_id group by instructor.name;
COUNT(ADVISOR.S_ID)
NAME
2
Einstein
1
Crick
1
Srinivasan
2
Kim
1
Singh
2
Katz
update instructor set salary=salary+50000 where name in (select name from
instructor,advisor where instructor.id=advisor.i_id group by instructor.name having
count(s_id) > 1);
3 row(s) updated.
NAME
SALARY
Srinivasan 71500
Wu
99000
Mozart
44000
Einstein
214500
El Said
66000
Gold
95700
Katz
192500
Califieri
68200
Singh
88000
Crick
79200
Brandt
101200
22. LEIT116
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
4
Biology
3
Comp. Sci.
4
CS-190
Game Design
Comp. Sci.
4
CS-315
Robotics
Comp. Sci.
3
CS-319
Image Processing
Comp. Sci.
CS-347
Database System Concepts Comp. Sci.
EE-181
Intro.to Digital Systems
3
FIN-201
Investment Banking
HIS-351
World History
MU-199
Music Video Production
PHY-101
Physical Principles
3
Elec. Eng.
3
Finance
3
History
3
Music
3
Physics
4
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
TITLE
DEPT_NAME
CREDITS
BIO-301
Genetics
Biology
BIO-399
Computational Biology
Biology
CS-101
Intro.to Computer Science Comp. Sci.
CS-190
CS-315
Game Design
Robotics
Comp. Sci.
Comp. Sci.
2
3
4
2
2
23. LEIT116
CS-319
Image Processing
CS-347
Database System Concepts
EE-181
Intro.to Digital Systems
FIN-201
Investment Banking
HIS-351
World History
MU-199
Music Video Production
Comp. Sci.
Comp. Sci.
2
2
Elec. Eng.
Finance
2
2
History
Music
2
2
Result;
Thus the basic simple sql queries are successfully executed and the output was
verified.
24. LEIT116
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
----------------------------------------- -------- ---------------------------ID
NUMBER(7)
NAME
VARCHAR2(20)
DEPT_NAME
VARCHAR2(20)
TOT_CREDIT
NUMBER(3)
SQL> insert into stuvalues(116,'maha','it',60);
1 row created.
SQL> insert into stuvalues(97,'subha','it',90);
1 row created.
SQL> insert into stuvalues(116,'sne','it',50);
1 row created.
SQL> create table tak(id number(7),cource_id number(7),sec_id number(7),semester
number(3),year number(7),grade char(3));
Table created.
SQL>desctak
Name
Null?
Type
----------------------------------------- -------- ---------------
25. LEIT116
ID
NUMBER(7)
COURCE_ID
NUMBER(7)
SEC_ID
NUMBER(7)
SEMESTER
NUMBER(3)
YEAR
NUMBER(7)
GRADE
CHAR(3)
SQL> insert into takvalues(45,'32',5,3,2013,'A');
1 row created.
SQL> insert into takvalues(52,31,7,3,2012,'C');
1 row created.
SQL> insert into tak values(47,35,8,4,2010,'A');
1 row created.
SQL> insert into tak(id,sec_id,semester,year,grade) values(78,9,4,2009,'C');
1 row created.
SQL> insert into takvalues(116,45,6,3,2007,'B' );
1 row created.
SQL> create table ins(id number(7),name varchar(20),dept_namevarchar(20),salary
number(10));
Table created.
SQL>descins;
Name
Null?
Type
----------------------------------------- -------- ---------------------------ID
NAME
DEPT_NAME
SALARY
NUMBER(7)
VARCHAR2(20)
VARCHAR2(20)
NUMBER(10)
SQL> insert into ins values(78,'susila','cse',40000);
26. LEIT116
1 row created.
SQL> insert into ins values(95,'chells','maths',45000);
1 row created.
SQL> insert into ins values(64,'sankar','it',48000);
1 row created.
SQL> create table teach(id number(7),cource_id number(7),sec_id number(7),semester
number(3),year number(7));
Table created.
SQL>desc teach
Name
Null?
Type
----------------------------------------- -------- ------------------ID
NUMBER(7)
COURCE_ID
NUMBER(7)
SEC_ID
NUMBER(7)
SEMESTER
NUMBER(3)
YEAR
NUMBER(7)
SQL> insert into teach values(118,33,7,3,2009);
1 row created.
SQL> insert into teach values(114,34,8,3,2009);
1 row created.
SQL> insert into teach values(113,36,9,3,2010);
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
-------------------- ----------
27. LEIT116
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
---------- -------------------- -------------------- ---------- ---------COURCE_ID
SEC_ID SEMESTER
YEAR GRA
---------- ---------- ---------- ---------- --97 subha
it
90
116 sne
it
50
Rigtht outer join:
SQL> select * from stu right outer join tak on stu.id=tak.id;
ID NAME
DEPT_NAME
TOT_CREDIT
---------- -------------------- -------------------- ---------- ---------COURCE_ID
SEC_ID SEMESTER
YEAR GRA
---------- ---------- ---------- ---------- --78
9
4
2009 C
52
31
7
3
2012 C
ID
28. LEIT116
45
32
5
3
2013 A
ID NAME
DEPT_NAME
TOT_CREDIT
ID
---------- -------------------- -------------------- ---------- ---------COURCE_ID
SEC_ID SEMESTER
YEAR GRA
---------- ---------- ---------- ---------- --47
35
8
4
2010 A
116 maha
it
60
Full outer join:
SQL> select * from stu full outer join tak on stu.id=tak.id;
ID NAME
DEPT_NAME
TOT_CREDIT
---------- -------------------- -------------------- ---------- ---------COURCE_ID
SEC_ID SEMESTER
YEAR GRA
---------- ---------- ---------- ---------- --97 subha
it
90
116 sne
it
50
116 maha
it
60
ID
29. LEIT116
ID NAME
DEPT_NAME
TOT_CREDIT
ID
---------- -------------------- -------------------- ---------- ---------COURCE_ID
SEC_ID SEMESTER
YEAR GRA
---------- ---------- ---------- ---------- --78
9
4
2009 C
52
31
7
3
2012 C
45
32
5
3
ID NAME
2013 A
DEPT_NAME
TOT_CREDIT
---------- -------------------- -------------------- ---------- ---------COURCE_ID
SEC_ID SEMESTER
YEAR GRA
---------- ---------- ---------- ---------- --47
35
8
7 rows selected.
Interact:
4
2010 A
ID
30. LEIT116
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
3
31. LEIT116
Result;
Thus the joins queries are successfully executed by using the library management
application application.
32. LEIT116
EXP:6
creation and updation of views
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
------------------------- ------------------------computer science
balagursamy
SQL>desclib_view;
Name
Null? Type
----------------------------------------- -------- -------------SUBJECT
CHAR(25)
AUTHOR
CHAR(25)
SQL> select author from lib_view where subject='computer science';
AUTHOR
------------------------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
----------------------------------------- -------- ---------------------------
33. LEIT116
NAME
VARCHAR2(5)
DOB
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
----------------------------------------- -------- ----------------NAME
DOB
CELLNO
Drop view:
SQL> drop view lib_view;
View dropped.
SQL> select * from lib_view;
no rows selected.
VARCHAR2(5)
NUMBER(10)
NUMBER(10)
38. LEIT116
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:
SQL> declare
2 v_num number:=&any_num;
3 v_res number;
4 begin
5 v_res:=v_num
39. LEIT116
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;
10 end;
11 /
Enter value for any_num: 4
old 2: v_num number:=&any_num;
new 2: v_num number:=4;
40. LEIT116
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;
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 /
41. LEIT116
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;
8. employee:=112;
9. end if;
10. end;
11./
PL/SQL procedure successfully completed.
42. LEIT116
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
6 grade:='good';
7 elseif m>=170 and m<=190;
8 grade:='excellent';
9 elseif m>=140 and m<=170;
10 grade:='fair';
43. LEIT116
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;
12 /
5.5
PL/SQL procedure successfully completed.
While loop:
SQL> set serveroutput on
45. LEIT116
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;
Enter value for emp_id: 3
old 6: where employeeif=&emp_id;
new 6: where employeeif=3;
PL/SQL procedure successfully completed.
Insert:
46. LEIT116
SQL> declare
2 v_nameemployee.name%type;
3 begin
4 insert into employee(name,id,salary) values('gayu',112,1000);
5 commit;
6 end;
7 /
PL/SQL procedure successfully completed.
Delete:
SQL> declare
2 v_nameemployee.name%type;
3 begin
4 select id from employee where name='gayu';
5 delete from employee
6 where name=v_name;
7 commit;
8 end;
9 /
PL/SQL procedure successfully completed.
Comment line:
Singleline:
SQL> set serveroutput on
SQL> variable a number
SQL> declare
2 v_num number(10);
3 v_num2 v_num%type;
49. LEIT116
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
--declare the cursor
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;
15
loop
16 --fetch the rows from the cursor
17 fetch student_cur
18 into c_name,c_tot_cred;
19
exit when not student_cur%found;
50. LEIT116
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.
Cursor for loop:
SQL> declare
2 cursorstudent_cur is
3 selectname,tot_cred
4 from
5 student;
6 begin
7 forstu_rec in student_cur loop
8 ifstu_rec.tot_cred>90 then
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;
51. LEIT116
14 /
Zhang
102
Chavez
110
Bourikas
98
Tanaka
120
PL/SQL procedure successfully completed.
Create a cursor for update:
SQL> declare
2
cursor student_cur is
3
select * from student
4
for update of tot_cred;
5 begin
6 for stu_rec in student_cur
7 loop
8
update student
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;
52. LEIT116
14 /
PL/SQL procedure successfully completed.
Cursor for loop using a subquery:
begin
forlib_rec in
(selectname,reg_no,dept,no_of_book,fine
from library
wheredept='it')loop
dbms_output.put_line
(lib_rec.name||''||lib_rec.reg_no||'$'||to_char(lib_rec.no_of
_book+NVL(lib_rec.fine,0)));
end loop;
end;
/
PL/SQL procedure successfully completed.
Cursor with parameter
SQL> declare
2 cursorc_lib is select * from library;
3 begin
4 forlib_no in c_lib loop
5 dbms_output.put_line('lib_no.id:'||lib_no.id);
6 end loop;
7 commit;
8 end;
9 /
53. LEIT116
lib_no.id:112
lib_no.id:114
lib_no.id:115
Cursor with REF:
SQL> declare
2 typebook_type is ref cursor return book%rowtype;
3 v_bookbook_type;
4 n_bookbook%rowtype;
5 begin
6 openv_book for select * from library where id=112;
7 fetchv_book into n_book;
8 dbms_output.put_line(v_book.number||' is '||n_book.name);
9 closev_book;
10 end;
11 /
java
112
Exception:
TOO_MANY_ROWS:
SQL> set serveroutput on
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;
54. LEIT116
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;
10 elseifv_name is null then
11 raisev_id;
12 else
13 dbms_output.put_line(to_char(v_name);
14 endif;
55. 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
Result:
Thus the cursor was successfully executed and output was verified.
56. LEIT116
EXP:9
PROCEDURE,FUNCTION AND PACKAGE
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;
3 v_firstwow.category%type;
4 v_idwow.id%type:=&mp_id;
5 begin
6 search_emp(v_id,v_last,v_first);
57. LEIT116
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;
12 /
Function created.
SQL> declare
58. LEIT116
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
7 dbms_output.put_line('success');
8 else
9 dbms_output.put_line('not');
59. LEIT116
10 end if;
11 exception
12 when NO_DATA_FOUND then
13 dbms_output.put_line('medicene not found');
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;
11 /
60. LEIT116
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,
61. 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 /
62. LEIT116
Package Body created.
SQL> declare
2
v_descriptionbook.description%type;
3
v_costbook.cost%type;
4
v_mednobook.bkno%type;
5 begin
6 book.findbook(v_bkno,v_description,v_cost);
7 dbms_output.put_line('the book is found');
8 EXCEPTION
9 WHEN OTHERS THEN
10 DBMS_OUTPUT.PUT_LINE('cannot find book');
11 end;
12 /
the book is found
PL/SQL procedure successfully completed.
Result:
Thus the pl/sql in procedure,function and packages was successfully executed and the
output was verified.
63. LEIT116
EXP:10
TRIGGERS
22.10.2013
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
64. LEIT116
9 V_LIBNAME:='UPDATE';
10 END IF;
11 INSERT INTO LIB VALUES('AVIL',78,'RASES');
12 END;
13 /
Trigger created.
DELETE:
SQL> DELETE FROM LIB1 WHERE MEDNAME='PARACIT';
1 row deleted.
SQL> SELECT * FROM LIB1;
BKNAME BKTYPE
BKID
--------- --------- ---------JAVA ENGLISH
80
UPDATE:
SQL> UPDATE LIB1 SET BKID=BKID+10 WHERE BKNAME='JAVA';
1 row updated.
SQL> SELECT * FROM LIB1;
BKNAME BKTYPE
LIBID
--------- --------- ---------JAVA ENGLISH
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.
65. LEIT116
SQL> INSERT INTO DOC1 VALUES('GUNA',779,9878967251);
1 row created.
SQL> SELECT * FROM DOC1;
DOCNAME
DOCID
CELLNO
---------- ---------- ---------SIVA
GUNA
789 9876543456
779 9878967251
SQL> CREATE OR REPLACE VIEW HOST ASSELECT DOCNAME,CELLNO
FROM DOC1 WHERE DOCID=779;
View created.
DELETE:
SQL> DELETE FROM HOST WHERE DOCNAME='GUNA';
1 row deleted.
SQL> SELECT * FROM HOST;
no rows selected
SQL> SELECT * FROM DOc1;
DOCNAME
DOCID
CELLNO
---------- ---------- ---------SIVA
789 9876543456
DATA MANIPULATION AND THE INSTEAD OF TRIGGER:
SQL> CREATE TABLE HOS4(DOCNAME VARCHAR(9),DOCID
NUMBER(9),CELLNO NUMBER(10));
Table created.
SQL> INSERT INTO HOS4 VALUES('RAVI',897,9876543212);
1 row created.
SQL> INSERT INTO HOS4 VALUES('KAVI',899,9876543218);
1 row created.
66. LEIT116
SQL> CREATE OR REPLACE VIEW DOCHOS AS
2 SELECT DOCNAME,CELLNO FROM HOS4
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 CAN’T HAVE A SALARY HIGHER THAN HIS/HER
DEPT MANAGER:
B.) AN EMPLOYEE CAN’T 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);
67. LEIT116
5 s number(10);
6 begin
7 select salary into s from employee e,department d
8 wheree.dno=d.dno;
9 selectmgrssn into p from employee e,department d
10 wheree.dno=d.dno;
11 if p>s then
12 raise_application_error(-20078,'salary of the employee shld not be greater
than the manager');
13 end if;
14 end;
15 /
Trigger created.
SQL> create or replace trigger pro1 before insert or update on project
2 for each row
3 declare
4 no number(10);
5 begin
6 select count(pname) into no from project where dnum=:new.dnum;
7 if no>2 then
8 raise_application_error(-20011,'an employee cant work on more than two projects');
9 end if;
10 end;
11 /
Trigger created.
68. LEIT116
SQL> insert into project values('p1',15,'ngl',13);
1 row created.
SQL> insert into project values('p1',16,'ngl',13);
1 row created.
RESULT:
Thus the queries are implemented by using triggers.
69. LEIT116
EXP:11
CLASSIFICATION AND CLUSTERING USING WEKA
29.10.13
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
=== Classifier model (full training set) ===
ZeroR predicts class value: yes
Time taken to build model: 0 seconds
=== Stratified cross-validation ===
=== Summary ===
Correctly Classified Instances
9
64.2857 %
70. LEIT116
Incorrectly Classified Instances
Kappa statistic
5
35.7143 %
0
Mean absolute error
0.4762
Root mean squared error
0.4934
Relative absolute error
100
Root relative squared error
100
Total Number of Instances
%
%
14
=== Detailed Accuracy By Class ===
TP Rate FP Rate Precision Recall F-Measure ROC Area Class
1
1
0.643
0
0
0
Weighted Avg. 0.643
0
0.643
=== Confusion Matrix ===
a b <-- classified as
9 0 | a = yes
5 0 | b = no
1
0.783
0
0.413
0.178 yes
0.178 no
0.643
0.503
0.178
71. LEIT116
@relation weather
@attribute outlook {sunny, overcast, rainy}
@attribute temperature real
@attribute humidity real
@attribute windy {TRUE, FALSE}
@attribute play {yes, no}
@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
sunny,72,95,FALSE,no
73. LEIT116
==
Number of clusters selected by cross validation: 1
Cluster
Attribute
0
(1)
======================
outlook
sunny
6
overcast
5
rainy
6
[total]
17
temperature
mean
73.5714
std. dev.
6.3326
humidity
mean
81.6429
std. dev.
9.9111
windy
TRUE
7
FALSE
9
[total]
16
play
yes
10
74. LEIT116
no
6
[total]
16
=== Model and evaluation on test split ===
EM
==
Number of clusters selected by cross validation: 1
Cluster
Attribute
0
(1)
======================
outlook
sunny
3
overcast
3
rainy
4
[total]
10
temperature
mean
std. dev.
72.2857
6.227
humidity
mean
79.5714
std. dev.
11.4998