Dbms Lab Manual (R16)
Dbms Lab Manual (R16)
C
III Year – I Semester
0 0 3
2
OBJECTIVES:
• To provide a sound introduction to the discipline of database management as a subject in its own
right, rather than as a compendium of techniques and product-specific tools.
List of Experiments:
SQL
1. Queries to facilitate acquaintance of Built-In Functions, String Functions, Numeric Functions, Date
Functions and Conversion Functions.
3. Queries to Retrieve and Change Data: Select, Insert, Delete, and Update
7. Queries for Creating, Dropping, and Altering Tables, Views, and Constraints
10. Write a PL/SQL Code using Basic Variable, Anchored Declarations, and Usage of
Assignment Operation
11. Write a PL/SQL Code Bind and Substitution Variables. Printing in PL/SQL
12. Write a PL/SQL block using SQL and Control Structures in PL/SQL
13. Write a PL/SQL Code using Cursors, Exceptions and Composite Data Types
14. Write a PL/SQL Code using Procedures, Functions, and Packages FORMS
15. Write a PL/SQL Code Creation of forms for any Information System such as Student Information
System, Employee Information System etc.
OUTCOMES:
•Understand, appreciate and effectively explain the underlying concepts of database technologies
• Design and implement a database schema for a given problem-domain
• Normalize a database
• Populate and query a database using SQL DML/DDL commands.
• Declare and enforce integrity constraints on a database using a state-of-the-artRDBMS
• Programming PL/SQL including stored procedures, stored functions, cursors, packages.
• Design and build a GUI application using a 4GL
Note: The creation of sample database for the purpose of the experiments is expected to be predecided
by the instructor.
DEPT TABLE
DEPTNO DNAME LOC
------ ---------- ----------
10 accounting newyork
20 research dallas
30 sales chicago
40 operations boston
SALGRADE TABLE
1 700 1200
2 1201 1400
3 1401 2000
4 2001 3000
5 3001 9999
create table dept( deptno number(2) primary key,
2 dname varchar2(10) not null,
3 loc varchar2(8));
Table created.
SQL> desc dept
Name Null? Type
----------------------------------------- --------
DEPTNO NOT NULL NUMBER(2)
DNAME NOT NULL VARCHAR2(10)
LOC VARCHAR2(8)
SQL> create table emp (empno number(5) primary key,ename varchar2(10) not
null,job varchar2(10),mgr number(4),hiredate date,sal number(7,2)
check(sal>=500 and sal<=10000),comm number(7,2),deptno number(2), foreign
key(deptno) references dept);
Table created.
14 rows selected.
1. Queries to facilitate acquaintance of Built-In Functions, String Functions, Numeric Functions,
Date Functions and Conversion Functions.
CEIL(88.9)
----------
3000
5000
3000
SQL> select FLOOR(SAL),CEIL(88.9) from emp
where sal BETWEEN 3000 AND 5000;
FLOOR(SAL) CEIL(88.9)
---------- ----------
3000 89
5000 89
3000 89
MOD(200,300)
------------
200
SAL POWER(SAL,2)
---------- ------------
2450 6002500
5000 25000000
1300 1690000
COMM-SAL SIGN(COMM-SAL)
---------- --------------
-1300 -1
-750 -1
150 1
-1500 -1
SQL> select sal,SQRT(sal) from emp where deptno=10;
SAL SQRT(SAL)
---------- ----------
2450 49.4974747
5000 70.7106781
1300 36.0555128
SQL> select TRUNC(90.723,1),TRUNC(90.723,-1),TRUNC(90.723) from dual;
SAL TRUNC(SQRT(SAL),2)
---------- ------------------
2450 49.49
5000 70.71
1300 36.05
SAL ROUND(SQRT(SAL),2)
---------- ------------------
2450 49.5
5000 70.71
1300 36.06
EXP(4)
----------
54.59815
A B C
- - -
% d e
UPPER(DNAME) UPP
-------------------- ---
accounting abc
sales abc
research abc
operations abc
LPAD(DNAME,15,'$')
------------------------------------------------------------
LPAD(DNAME,15,'')
------------------------------------------------------------
$$$$$accounting
$$$$$$$$$$sales
$$$$$$$research
LPAD(DNAME,15,'$')
------------------------------------------------------------
LPAD(DNAME,15,'')
------------------------------------------------------------
$$$$$operations
RPAD(DNAME,15,'$')
------------------------------------------------------------
RPAD(DNAME,15,'')
------------------------------------------------------------
accounting$$$$$
sales$$$$$$$$$$
research$$$$$$$
RPAD(DNAME,15,'$')
------------------------------------------------------------
RPAD(DNAME,15,'')
------------------------------------------------------------
operations$$$$$
First
-----------
Bis and Bat
Second
---------
is and at
DNAME SUBSTR(DNAME,2,4
-------------------- ----------------
SUBSTR(DNAME,4)
--------------------------------------------------------------------
accounting ccouounting
sales aleses
research eseaearch
DNAME SUBSTR(DNAME,2,4
-------------------- ----------------
SUBSTR(DNAME,4)
--------------------------------------------------------------------
operations perarations
TRANSLATE(
----------
123456ghij
DNAME INSTR(DNAME,'E')
-------------------- ----------------
accounting 0
sales 4
research 2
operations 3
DNAME LENGTH(DNAME)
-------------------- -------------
accounting 10
sales 5
research 8
operations 10
14 rows selected.
SQL> select
ename,INSTR(ename,'a'),SUBSTR(job,1,3),LPAD(ename,10,'.'),RPAD(ename,10,'.')
2 from emp;
allen 1 sal
.....allen
allen.....
ward 2 sal
......ward
ward......
jones 0 man
.....jones
martin 2 sal
....martin
martin....
blake 3 man
clark 3 man
.....clark
clark.....
king 0 pre
......king
king......
turner 0 sal
....turner
turner....
adams 1 cle
.....adams
james 2 cle
.....james
james.....
ford 0 ana
miller 0 cle
....miller
miller....
14 rows selected.
SYSDATE
------------------
13-AUG-15
MONTHS_BETWEEN('05-JAN-98','05-MAR-98') MONTHS_BETWEEN('05-MAR-98','05-JAN-
98')
--------------------------------------- ----------------------------------
-2 2
SYSDATE NEXT_DAY(SYSDATE,'
------------------ ------------------
13-AUG-15 19-AUG-15
SYSDATE TO_CHAR(SYSDATE,'DAY')
------------------ ------------------------------------
13-AUG-15 THURSDAY
GREATEST(10,'7',-1)
-------------------
10
Leas
----
ABCD
ENAME HIREDDAT
---------- --------
smith 17/12/80
jones 02/04/81
scott 19/04/87
adams 23/05/87
ford 03/12/81
14 rows selected.
YY COUNT(*)
-- ----------
87 2
81 10
82 1
80 1
ENAME HIREDDATE
---------- ---------------
jones APR 81
martin SEP 81
blake MAY 81
clark JUN 81
king NOV 81
turner SEP 81
james DEC 81
ford DEC 81
miller JAN 82
9 rows selected.
SQL> select ename,sal+TO_NUMBER('100.00') as salary
2 from emp;
ENAME SALARY
---------- ----------
smith 900
allen 1700
ward 1350
jones 3075
martin 1350
blake 2950
clark 2550
scott 3100
king 5100
turner 1600
adams 1200
ENAME SALARY
---------- ----------
james 1050
ford 3100
miller 1400
14 rows selected.
CONCATENATEDSTRING
----------------------------------------------------------------------------
----
Department number10with nameAccountingis situated inNewyork
Department number20with nameSalesis situated inChicago
Department number30with nameResearchis situated inDallas
Department number40with nameOperationsis situated inBoston
LPAD(ENAME,9,'*')
------------------------------------
****smith
****allen
*****ward
****jones
***martin
****blake
****clark
****scott
*****king
***turner
****adams
LPAD(ENAME,9,'*')
------------------------------------
****james
*****ford
***miller
14 rows selected.
RPAD(ENAME,9,'*')
------------------------------------
smith****
allen****
ward*****
jones****
martin***
blake****
clark****
scott****
king*****
turner***
adams****
RPAD(ENAME,9,'*')
------------------------------------
james****
ford*****
miller***
14 rows selected.
LTRIM(ENAM
----------
mith
jones
cott
adams
ford
ENAME LENGTH(ENAME)
---------- -------------
ward 4
allen 5
james 5
blake 5
turner 6
martin 6
6 rows selected.
JOB
----------------
anal
cler
pres
sale
mana
ENAME INSTR(ENAME,'S')
---------- ----------------
smith 1
jones 5
scott 1
adams 5
ford 0
HOLIDAY
------------------
21-DEC-80
22-FEB-81
01-MAR-81
05-APR-81
04-OCT-81
03-MAY-81
14-JUN-81
26-APR-87
22-NOV-81
13-SEP-81
24-MAY-87
HOLIDAY
------------------
06-DEC-81
06-DEC-81
24-JAN-82
14 rows selected.
HIREDDATE REVIEWDATE
------------------ ------------------
17-DEC-80 17-DEC-81
02-APR-81 02-APR-82
19-APR-87 19-APR-88
23-MAY-87 23-MAY-88
03-DEC-81 03-DEC-82
HIREDDATE LASTDAY
------------------ ------------------
09-JUN-81 30-JUN-81
17-NOV-81 30-NOV-81
23-JAN-82 31-JAN-82
LOWEST
----------
-2
HIGHEST
----------
89
SQL> select TRUNC(567,23165613)
2 from dual;
TRUNC(567,23165613)
-------------------
567
TRUNC(567.231651,3)
-------------------
567.231
Arithmetic operators:
14 rows selected.
14 rows selected.
14 rows selected.
14 rows selected.
Concatenation operator:
'NAMEIS'||ENAME
------------------
Name is smith
Name is allen
Name is ward
Name is jones
Name is martin
Name is blake
Name is clark
Name is scott
Name is king
Name is turner
Name is adems
Name is james
Name is ford
Name is miller
14 rows selected.
Comparison operators:
10 rows selected.
12 rows selected.
SQL> select *from emp where sal!=3000;(we can use <>or ^= also)
12 rows selected.
6 rows selected.
8 rows selected.
select ename,job from emp e where not exists(select mgr from emp where
mgr=e.empno);
ENAME JOB
--------- ---------
turner salesman
ward salesman
martin salesman
allen salesman
miller clerk
smith clerk
adems clerk
james clerk
8 rows selected.
SQL> select ename from emp where sal =any(select sal from emp where deptno=20);
ENAME
----------
smith
jones
ford
scott
adems
SQL> select ename from emp where sal <any(select sal from emp where deptno=20);
ENAME
----------
smith
james
adems
ward
martin
miller
allen
clark
blake
jones
10 rows selected.
SQL> select ename from emp where sal >any(select sal from emp where deptno=20);
ENAME
----------
turner
king
ford
scott
jones
blake
clark
allen
miller
martin
ward
adems
james
13 rows selected.
SQL> select ename from emp where sal =all(select sal from emp where deptno=30)
no rows selected
SQL> select ename from emp where sal <all(select sal from emp where deptno=30)
ENAME
----------
smith
SQL> select ename from emp where sal >all(select sal from emp where deptno=20)
ENAME
----------
king
turner
SQL> select e.ename from emp e,dept d
where e.deptno=d.deptno and d.loc='dallas'
union
select e.ename from emp e,dept d
where e.deptno=d.deptno and d.loc='newyork';
ENAME
---------
clark
king
miller
SQL> select e.deptno from emp e INTERSECT select d.deptno from dept d;
DEPTNO
----------
10
20
30
SQL> select deptno from dept minus select deptno from emp;
DEPTNO
ENAME SAL
--------- ----------
miller 1300
smith 800
james 950
14 rows selected.
10 rows selected.
SQL> select ename,empno,sal from emp where sal between 1000 and 3000;
10 rows selected.
SQL> select ename,empno,sal from emp where sal not between 1000 and 3000;
SQL> select ename, sal from emp where ename not like's%';
ENAME SAL
---------- ----------
allen 1600
ward 1250
jones 2975
martin 1250
blake 2850
clark 2450
king 5000
turner 10000
adems 1100
james 950
ford 3000
miller 1300
12 rows selected.
SQL> select ename, sal from emp where ename like '%s';
ENAME SAL
---------- ----------
jones 2975
adems 1100
james 950
SQL> select ename, sal from emp where ename like 'smit_';
ENAME SAL
---------- ----------
smith 800
SQL> select ename, sal from emp where ename like '__nes';
ENAME SAL
---------- ----------
jones 2975
SQL> select ename,sal from emp where deptno=20 and job='clerk';
ENAME SAL
---------- ----------
smith 800
adems 1100
ENAME SAL
---------- ----------
smith 800
jones 2975
scott 3000
adems 1100
james 950
ford 3000
miller 1300
7 rows selected.
10 ows selected.
3. Queries to Retrieve and Change Data: Select, Insert, Delete, and Update
SQL> create table college(cid varchar2(4) primary key,cname varchar2(10) not nul
l,cplace varchar2(10));
Table created.
1 row created.
1 row created.
SQL> commit;
Commit complete.
SQL> create table student(sid varchar2(4) primary key,sname varchar2(10) not nul
l,cid varchar2(10) references college(cid));
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
SQL> commit;
Commit complete.
1 row deleted.
1 row updated.
1 row updated.
Table altered.
Table altered.
Table altered.
Table altered.
SQL> select job,count(*) from emp group by job order by count(*) desc;
JOB COUNT(*)
--------- ----------
salesman 4
clerk 4
manager 3
analyst 2
president 1
SQL> select ename from emp where sal=(select max(sal) from emp);
ENAME
---------
turner
SQL> select job,avg(sal) from emp group by job having avg(sal)=(select
max(avg(sal)) from emp group by job);
JOB AVG(SAL)
--------- ----------
president 5000
SQL> select * from emp where deptno=10 and exists(select count(*) from emp
where deptno=10 group by deptno having count(*) >10);
no rows selected
Table created.
1 row created.
1 row created.
SQL> select *from student;
1 row deleted.
SQL> rollback;
Rollback complete.
1 row deleted.
SQL> commit;
Commit complete.
SQL> rollback;
Rollback complete.
Savepoint created.
1 row created.
Savepoint created.
1 row created.
Savepoint created.
1 row created.
Rollback complete.
Rollback complete.
salary commission
---------- ----------
800
1600 300
1250 500
2975
1250 1400
2850
2450
3000
5000
1000 0
1100
950
3000
1300
14 rows selected.
monthly monthly
salary comm
---------- -------
800
1600 300
1250 500
2975
1250 1400
2850
2450
3000
5000
1000 0
1100
950
3000
1300
14 rows selected.
monthly monthly
salary comm
========== =======
800
1600 300
1250 500
2975
1250 1400
2850
2450
3000
5000
1000 0
1100
950
3000
1300
14 rows selected.
monthly monthly
salary comm
########## #######
800
1600 300
1250 500
2975
1250 1400
2850
2450
3000
5000
1000 0
1100
950
3000
1300
14 rows selected.
monthly
salary
#######
$800
$1,600
$1,250
$2,975
$1,250
$2,850
$2,450
$3,000
$5,000
$1,000
$1,100
$950
$3,000
$1,300
14 rows selected.
SAL
----------
800
1600
1250
2975
1250
2850
2450
3000
5000
1000
1100
950
3000
1300
14 rows selected.
ENAM
----
smit
h
alle
n
ward
jone
s
mart
in
blak
e
clar
k
scot
t
king
turn
er
adem
s
jame
s
ford
mill
er
14 rows selected.
SQL> spool of
<br>
SQL> spool off
<br>
SQL> set markup html off
<br>
SQL>
two.html
Printing line at characters after wrapped column values:
Output:
LOC
----
newy
ork
dall
as
chic
ago
gost
on
LOC
----
newy
ork
-------------------------------------------------------------------------------
dall
as
-------------------------------------------------------------------------------
chic
ago
-------------------------------------------------------------------------------
gost
on
7. Queries for Creating, Dropping, and Altering Tables, Views, and Constraints
Table:
Table created.
Table dropped.
Table created.
Table altered.
SID NUMBER(4)
SNAME VARCHAR2(10)
SQL> alter table studdd add marks number(3);
Table altered.
SID NUMBER(4)
SNAME VARCHAR2(10)
MARKS NUMBER(3)
Table altered.
SID NUMBER(4)
SNAME VARCHAR2(10)
MARKS VARCHAR2(10)
Table altered.
SID NUMBER(4)
SNAME VARCHAR2(10)
Table altered.
SID NUMBER(4)
STUDNAME VARCHAR2(10)
Views:
View created.
14 rows selected.
View created.
14 rows selected.
View dropped.
Constraints:
Not null
5* Age int
SQL> create table stud1(sid number(4)not null,fname varchar2(10)not null,lname v
archar2(10)not null,age number(3));
Table created.
Table dropped.
Unique
Table created.
SQL> desc stud1;
Name Null? Type
----------------------------------------- -------- ----------------------------
Table dropped.
Primary key
Table created.
Table dropped.
Foreign key
Table created.
1 row created.
1 row created.
1 row created.
SQL> commit;
Commit complete.
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
SQL> commit;
Commit complete.
check
Table created.
Add constraint
Table altered.
Drop constraint
Table altered.
SID NUMBER(4)
STUDNAME VARCHAR2(10)
Joins:
Inner join
14 rows selected.
Outer joins
SQL> select e.ename,e.sal,d.dname from emp e left outer join dept d on e.deptn
d.deptno;
14 rows selected.
SQL> select e.ename,e.sal,d.dname from emp e right outer join dept d on e.dept
=d.deptno;
15 rows selected.
SQL> select e.ename,e.sal,d.dname from emp e full outer join dept d on e.deptn
d.deptno;
15 rows selected.
Equi join
14 rows selected.
Self join
6 rows selected.
Cross join
20 rows selected.
Sub quries:
SQL> select ename,job,sal from emp where sal=(select min(sal) from emp);
SQL> select ename,job,sal from emp where sal=(select max(sal) from emp);
SQL> select ename,job,sal from emp where sal >any(select sal from emp where dept
no=30);
12 rows selected.
SQL> select ename,job,sal from emp where sal <any(select sal from emp where dept
no=30);
9 rows selected.
SQL> select ename,job,sal from emp where sal >all(select sal from emp where dept
no=30);
SQL> select ename,job,sal from emp where sal <all(select sal from emp where dept
no=30);
SQL> select ename,job,sal from emp where sal =any(select sal from emp where dept
no=30);
6 rows selected.
SQL> select ename,job,sal from emp e where sal >(select min(losal) from salgrade
s where e.sal>s.losal);
14 rows selected.
11 rows selected.
Index created.
6 rows selected.
Index dropped.
Sequences:
Sequence created.
NEXTVAL
----------
1
SQL> select sq_fst.currval from dual;
CURRVAL
----------
1
Sequence dropped.
Synonym:
Synonym created.
Synonym created.
Synonym created.
Synonym dropped.
Access control:
User created.
Grant succeeded.
User altered.
USERNAME
------------------------------
ABC
User dropped.
10. Write a PL/SQL Code using Basic Variable, Anchored Declarations, and Usage of
Assignment Operation
declare
a integer := 10;
b integer := 20;
c integer;
f real;
v_ename emp.ename%type;
v_sal emp.sal%type;
begin
c := a + b;
dbms_output.put_line('Value of c: ' || c);
f := 70.0/3.0;
dbms_output.put_line('Value of f: ' || f);
select ename into v_ename from emp where empno=7844;
dbms_output.put_line(v_ename);
end;
/
Output:
Value of c: 30
Value of f: 23.33333333333333333333333333333333333333
turner
12. Write a PL/SQL Code Bind and Substitution Variables. Printing in PL/SQL
/
Enter value for rno: 41
old 4: x.rno:=&rno;
new 4: x.rno:=41;
Enter value for name:kba
old 5: x.name:='&name';
new 5: x.name:='kba';
Enter value for s1:67
old 6: x.s1:=&s1;
new 6: x.s1:=67;
Enter value for s2:89
old 7: x.s2:=&s2;
new 7: x.s2:=89;
Enter value for s3:56
old 8: x.s3:=&s3;
new 8: x.s3:=56;
Enter value for s4:90
old 9: x.s4:=&s4;
new 9: x.s4:=90;
SQL> /
Enter value for rno:56
old 4: x.rno:=&rno;
new 4: x.rno:=56;
Enter value for name:knsl
old 5: x.name:='&name';
new 5: x.name:='knsl';
Enter value for s1:89
old 6: x.s1:=&s1;
new 6: x.s1:=89;
Enter value for s2:90
old 7: x.s2:=&s2;
new 7: x.s2:=90;
Enter value for s3:78
old 8: x.s3:=&s3;
new 8: x.s3:=78;
Enter value for s4:88
old 9: x.s4:=&s4;
new 9: x.s4:=88;
SQL> /
Enter value for rno:36
old 4: x.rno:=&rno;
new 4: x.rno:=36;
Enter value for name:ks
old 5: x.name:='&name';
new 5: x.name:='ks';
Enter value for s1:65
old 6: x.s1:=&s1;
new 6: x.s1:=65;
Enter value for s2:77
old 7: x.s2:=&s2;
new 7: x.s2:=77;
Enter value for s3:82
old 8: x.s3:=&s3;
new 8: x.s3:=82;
Enter value for s4:92
old 9: x.s4:=&s4;
new 9: x.s4:=92;
SQL>/
12. Write a PL/SQL block using SQL and Control Structures in PL/SQL
Table created.
SQL> declare
2 x e1.sal%type;
3 y e1.eno%type;
4 bonus number(7,2);
5 begin
6 select sal,eno into x,y from e1
7 where eno=&eno;
8 if x between 500 and 1000 then
9 bonus:=500;
10 elsif x between 1001 and 2000 then
11 bonus:=1000;
12 elsif x between 2001 and 3000 then
13 bonus:=2000;
14 else
15 bonus:=3000;
16 end if;
17 update e1 set sal=sal+bonus
18 where eno=y;
19 end;
20 /
Enter value for eno: 7369
old 7: where eno=&eno;
new 7: where eno=7369;
SQL> /
Enter value for eno: 7499
old 7: where eno=&eno;
new 7: where eno=7499;
SQL> /
Enter value for eno: 7521
old 7: where eno=&eno;
new 7: where eno=7521;
SQL> /
Enter value for eno: 7566
old 7: where eno=&eno;
new 7: where eno=7566;
SQL> /
Enter value for eno: 7654
old 7: where eno=&eno;
new 7: where eno=7654;
SQL> /
Enter value for eno: 7698
old 7: where eno=&eno;
new 7: where eno=7698;
SQL> /
Enter value for eno: 7782
old 7: where eno=&eno;
new 7: where eno=7782;
SQL> /
Enter value for eno: 7839
old 7: where eno=&eno;
new 7: where eno=7839;
SQL> /
Enter value for eno: 7844
old 7: where eno=&eno;
new 7: where eno=7844;
SQL> /
Enter value for eno: 7876
old 7: where eno=&eno;
new 7: where eno=7876;
SQL> /
Enter value for eno: 7900
old 7: where eno=&eno;
new 7: where eno=7900;
SQL> /
Enter value for eno: 7902
old 7: where eno=&eno;
new 7: where eno=7902;
SQL> /
Enter value for eno: 7934
old 7: where eno=&eno;
new 7: where eno=7934;
SQL> declare
2 x dept.Deptno%type;
3 begin
4 select Deptno into x from dept
5 where Deptno=&Deptno;
6 case x
7 when 10 then
8 dbms_output.put_line('Accounting');
9 when 20 then
10 dbms_output.put_line('Research');
11 when 30 then
12 dbms_output.put_line('Sales');
13 when 40 then
14 dbms_output.put_line('Operations');
15 else
16 dbms_output.put_line('Unknown Dept');
17 end case;
18 end;
19 /
Enter value for deptno: 10
old 5: where Deptno=&Deptno;
new 5: where Deptno=10;
Accounting
SQL> /
Enter value for deptno: 20
old 5: where Deptno=&Deptno;
new 5: where Deptno=20;
Research
SQL> /
Enter value for deptno: 30
old 5: where Deptno=&Deptno;
new 5: where Deptno=30;
Sales
SQL> /
Enter value for deptno: 60
old 5: where Deptno=&Deptno;
new 5: where Deptno=60;
declare
*
ERROR at line 1:
ORA-01403: no data found
ORA-06512: at line 4
13. Write a PL/SQL Code using Cursors, Exceptions and Composite Data Types
Cursors:
SQL> declare
2 veno e1.eno%type;
3 vename e1.ename%type;
4 vsal e1.sal%type;
5 vDeptno e1.Deptno%type;
6 cursor c1 is select eno,ename,sal,Deptno from e1
7 where sal+NVL(comm,0)>1500;
8 begin
9 open c1;
10 loop
11 fetch c1 into veno,vename,vsal,vDeptno;
12 if c1%found then
13 dbms_output.put_line(veno||' '||vename||' '||vsal||' '||vDeptno);
14 else
15 exit;
16 end if;
17 end loop;
18 close c1;
19 end;
20 /
7499 allen 1600 30
7521 ward 2524 30
7566 jones 3570 20
7654 martin 1250 30
7698 blake 2850 30
7782 clark 2450 10
7788 scott 3000 20
7839 king 5000 10
7902 ford 3000 20
7934 miller 2246.4 10
PL/SQL procedure successfully completed.
SQL> declare
2 salary e1.sal%type;
3 s number:=0;
4 cursor c1 is select sal from e1;
5 begin
6 open c1;
7 loop
8 fetch c1 into salary;
9 s:=s+salary;
10 exit when c1%notfound;
11 update emp set sal=sal+sal*10/100
12 where Deptno=20 and Deptno=40;
13 end loop;
14 dbms_output.put_line('Total is: ');
15 dbms_output.put_line(to_char(s));
16 dbms_output.put_line('Rows Fetched'||c1%rowcount);
17 close c1;
18 end;
19 /
Total is:
34276.8
Rows Fetched14
SQL> declare
2 i number;
3 cursor c2 is
4 select * from e1
5 where Deptno=20
6 for update of sal;
7 begin
8 for i in c2
9 loop
10 update e1 set sal=10000
11 where current of c2;
12 end loop;
13 end;
14 /
Exceptions:
SQL> /
Enter value for job: president
old 4: select job into x from e1 where job='&job';
new 4: select job into x from e1 where job='president';
presidentJob type appeared only once
Procedure created.
SQL> /
Procedure created.
Procedure created.
SQL> select eno,sal from e1 where eno=7369;
ENO SAL
---------- ----------
7369 960
.
SQL> select eno,sal from emp where eno=7934;
ENO SAL
---------- ----------
7934 1300
Functions:
a.
1 create or replace function fact(n number,f in out number) return number
IS
2 i number;
3 begin
4 if n<0 then
5 return -1;
6 end if;
7 for i in 1..n
8 loop
9 f:=f*i;
10 end loop;
11 return f;
12* end fact;
SQL> /
Function created.
1 declare
2 n number:=&n;
3 f number:=1;
4 k number;
5 begin
6 k:=fact(n,f);
7 if k<0 then
8 dbms_output.put_line('factorial for negative numbers donot exist');
9 else
10 dbms_output.put_line('factorial is'||k);
11 end if;
12* end;
13 /
Enter value for n: 4
old 2: n number:=&n;
new 2: n number:=4;
Factorial is24
PL/SQL procedure successfully completed.
SQL> /
Enter value for n: 6
old 2: n number:=&n;
new 2: n number:=6;
Factorial is720
PL/SQL procedure successfully completed.
SQL> /
Enter value for n: -6
old 2: n number:=&n;
new 2: n number:=-6;
Factorial for Negative Numbers not exist
PL/SQL procedure successfully completed.
b.
1 create or replace function a1(eid number) return number IS
2 incr e1.sal%type;
3 net e1.sal%type;
4 veno e1.eno%type;
5 vsal e1.sal%type;
6 vcomm e1.comm%type;
7 begin
8 select eno,sal,NVL(comm,0) into veno,vsal,vcomm from e1
9 where eno=eid;
10 net:=vsal+vcomm;
11 if vsal<=3000 then
12 incr:=0.20*net;
13 elsif vsal>=3000 and vsal<=6000 then
14 incr:=0.30*net;
15 else
16 incr:=0.40*net;
17 end if;
18 return incr;
19* end a1;
20 /
Function created.
SQL> ed;
Wrote file afiedt.buf
1 declare
2 eid number:=&eid;
3 k e1.sal%type;
4 begin
5 k:=a1(eid);
6 update e1 set sal=sal+k where eno=eid;
7* end;
8 /
Enter value for eid: 7369
old 2: eid number:=&eid;
new 2: eid number:=7369;
ENO SAL
---------- ----------
7369 1990.66
Packages:
a.
1 create or replace package p1 is
2 procedure p_empdept1(dn NUMBER);
3* end p1;
SQL> /
Package created.
SQL> ed;
Wrote file afiedt.buf
Package created.
17. Write a PL/SQL Code Creation of forms for any Information System such as Student Information
System, Employee Information System etc.
Click on login
Click on create application in above and give in Name field as ‘app1’ and click on next below
Select Form check box and select any table from the list of tables like EMP in Table Name field and click add
page in below
Click emp link
16.Demonstration of database connectivity
import java.sql.*;
class ConnectToOraDB {
public static void main(String args[])throws Exception{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","scott","tiger");
System.out.println("..........Connected to Oracle Database......"+con.getClass());
}
}
Output:
E:\acedamic\f\j2ee\jdbc>javac ConnectToOraDB.java
E:\acedamic\f\j2ee\jdbc>java ConnectToOraDB
..........Connected to Oracle Database......class oracle.jdbc.driver.OracleConne
Ction
Java program to create table in oracle DB:
import java.sql.*;
class CreateTable {
public static void main(String args[])throws Exception{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","scott","tiger");
System.out.println("..........Connected to Oracle Database...........");
Statement stmt=con.createStatement();
String sql="create table std(sid number(4),sname varchar2(10),marks number(3))";
stmt.executeUpdate(sql);
}
}
Output:
E:\acedamic\f\j2ee\jdbc>javac CreateTable.java
E:\acedamic\f\j2ee\jdbc>java CreateTable
..........Connected to Oracle Database...........
E:\acedamic\f\j2ee\jdbc>
Output:
SID NUMBER(4)
SNAME VARCHAR2(10)
MARKS NUMBER(3)
SQL>
Java program to insert rows into a table:
import java.sql.*;
class InsertUsingStatement {
public static void main(String args[])throws Exception{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","scott","tiger");
System.out.println("..........Connected to Oracle Database...........");
Statement stmt=con.createStatement();
String sql1="insert into std values(1001,'sone',60)";
String sql2="insert into std values(1002,'stwo',70)";
String sql3="insert into std values(1003,'sthr',80)";
stmt.executeUpdate(sql1);
stmt.executeUpdate(sql2);
stmt.executeUpdate(sql3);
}
}
Output:
E:\acedamic\f\j2ee\jdbc>java InsertUsingStatement
..........Connected to Oracle Database...........
SQL>
Java program to select rows from table:
import java.sql.*;
class SelectingRowsUsingResultSet {
public static void main(String args[])throws Exception{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","scott","tiger");
System.out.println("..........Connected to Oracle Database...........");
String sql="select *from std";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
System.out.println("row......>"+rs.getRow());
System.out.print(rs.getInt(1)+" ");
System.out.print(rs.getString(2)+" ");
System.out.println(rs.getInt(3)+" ");
}
}
}
Output:
E:\acedamic\f\j2ee\jdbc>java SelectingRowsUsingResultSet
..........Connected to Oracle Database...........
row......>1
1001 sone 60
row......>2
1002 stwo 70
row......>3
1003 sthr 80
E:\acedamic\f\j2ee\jdbc>