Dbmslab PDF
Dbmslab PDF
Dbmslab PDF
INDEX
S. No
Contents
1
Lab Objective
Guidelines to Students
4
5
Background Theory
PL-Sql Programs
10
References
Page. no
3
4
6
7
8
9
14
20
50
88
94
LAB OBJECTIVE
Upon successful completion of this Lab the student will be able to:
Creating database objects
Modifying database objects
Manipulating the data
Retrieving the data from the database server
Performing database operations in a procedural manner using pl/sql
Performing database operations (create, update, modify, retrieve, etc.,) using front-end
tools like D2K.
Design and Develop applications like banking, reservation system, etc.,
There are 54 systems (Intel core to duo dx7400 and dx2300) installed in
this Lab. Their configurations are as follows:
Processor
RAM
1 GB
Hard Disk
150 GB
Mouse
Optical Mouse
Present
Software
All systems are configured in DUAL BOOT mode i.e., Students can boot from
Windows XP or Linux as per their lab requirement. This is very useful for students
because they are familiar with different Operating Systems so that they can
execute their programs in different programming environments.
Latest Technologies like J2EE are installed in the systems. Before submitting their
final project, they can start doing mini project from 3nd year onwards.
25 min.
f)
g)
h)
i)
Equipment in the lab for the use of student community. Students need to maintain a
proper decorum in the computer lab. Students must use the equipment with care.
Any damage is caused is punishable.
Students are required to carry their observation / programs book with completed
exercises while entering the lab.
Students are supposed to occupy the machines allotted to them and are not
supposed to talk or make noise in the lab. The allocation is put up on the lab notice
board.
Lab can be used in free time / lunch hours by the students who need to use the
systems should take prior permission from the lab in-charge.
Lab records need to be submitted on or before date of submission.
Students are not supposed to use floppy disks
item_id: integer,
integrity
constraints
Give a count of how many products have been bought by each customer
g)
h)
i)
Create a view which lists out the bill_no, bill_date, cust_id, item_id,
price, qty_sold, amount
Create a view which lists the daily sales date wise for the last one week
2
List the details of students who borrowed book whose author is CJDATE
Give a count of how many books have been bought by each student
Give a list of books taken by student with stud_no as 5
List the book details which are issued as of today
Create a view which lists out the iss_no, iss _date, stud_name, book name
Create a view which lists the daily issues-date wise for the last one week
10
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Additional Programs
S. No
1
2
3
4
12
SQL is structure query language.SQL contains different data types those are
1. char(size)
2. varchar2(size)
3. date
4. number(p,s)
5. long
6. raw/long raw
DDL commands:
1. The Create Table Command: - it defines each column of the table uniquely. Each column
has minimum of three attributes, a name , data type and size.
Syntax:
Create table <table name> (<col1> <datatype>(<size>),<col2> <datatype><size>));
Ex:
create table emp(empno number(4) primary key, ename char(10));
2. Modifying the structure of tables.
a)add new columns
Syntax:
Alter table <tablename> add(<new col><datatype(size),<new col>datatype(size));
13
7. Destroying tables.
Syntax:
Drop table <tablename>;
Ex:
drop table emp;
DML commands:
14
set
<col>=<exp>,<col>=<exp>
15
16
13. Filtering table data: - while viewing data from a table, it is rare that all the data from table
will be required each time. Hence, sql must give us a method of filtering out data that is not
required data.
a) Selected columns and all rows:
Syntax:
select <col1>,<col2> from <tablename>;
b) selected rows and all columns:
Syntax:
select * from <tablename> where <condition>;
c) selected columns and selected rows
Syntax:
select <col1>,<col2> from <tablename> where<condition>;
14. Sorting data in a table.
Syntax:
Select * from <tablename> order by <col1>,<col2> <[sortorder]>;
DCL commands:
Oracle provides extensive feature in order to safeguard information stored in its tables from
unauthoraised viewing and damage.The rights that allow the user of some or all oracle resources
on the server are called privileges.
a) Grant privileges using the GRANT statement
The grant statement provides various types of access to database objects such as tables,views and
sequences and so on.
Syntax:
GRANT <object privileges>
ON <objectname>
TO<username>
17
18
19
HW/SW requirements:
Processor
RAM
1 GB
Hard Disk
150 GB
Software
ORACLE 10G
SQL> create table customer1 (cust_id number(5) primary key, cust_name varchar2(15));
Output: Table created.
SQL> desc customer1;
Output:
Name
Null?
Type
----------------------------------------- -------- ---------------CUST_ID
NOT NULL NUMBER(5)
CUST_NAME
VARCHAR2(15)
Valid Test Data
b)
Cust_id
Item_name
PRICE
NOT NULL
NUMBER(4)
VARCHAR2(15)
NUMBER(6,2)
20
SQL>dsec sale
Output:
Name
Null?
Type
..
BILL_NO
NOT NULL
NUMBER(4)
BILL_DATE
DATE
CUST_ID
NUMBER(5)
ITEM_ID
NUMBER(4)
QTY_SOLD
NUMBER(4)
SQL>insert into Sale values(&bill_no, &bill_date,
&cust_id, &item_id, &qty_sold);
SQL>select * from sale;
Output:
BILL_NO
BILL_DATE CUST_ID
ITEM_ID
QTY_SOLD
...
1450
04-JAN-06
100
2124
2
1451
04-JAN-06
101
2319
1
1452
04-JAN-06
103
4531
2
1453
04-JAN-06
102
2334
3
1454
04-JAN-06
104
4532
3
c) List all the bills for the current date with the customer names and item numbers
SQL> select c.custname, i.itemid, s.billno from customer c, item I, sale s
where c.custid=s.custid and
21
ITEMID
BILLNO
-----------------5001
332
d) List the total Bill details with the quantity sold, price of the item and the final amount
SQL> select i.price, s.qty,(i.price*s.qty) total from item I, sale s where i.itemid=s.itemid;
PRICE QTY
------- ----120
20
5
10
350
2
3
2
1
4
TOTAL
-------240
60
10
10
1400
e) List the details of the customer who have bought a product which has a price>200
SQL> select c.custid, c.custname from customer c, sale s, item i where i.price>200 and
c.custid=s.custid and i.itemid=s.itemid;
CUSTID
CUSTNAME
----------------------4
duffy
f) Give a count of how many products have been bought by each customer
SQL> select custid, count(itemid) from sale group by custid;
CUSTID
---------1
3
4
5
COUNT(ITEMID)
--------------------2
1
1
1
22
3432
12-JAN-06
3
3244
120
2
4424
20-FEB-06
1
3456
20
3
332
13-MAR-06
1
1234
5
2
2343
10-MAR
5
5001
10
1
1331
11-MAR-06
4
76776 350
4
j) Create a view which lists the daily sales date wise for the last one week
Viva-Voce:
Q1. What is SQL?
Ans: Structured Query Language
2. What is database?
A database is a logically coherent collection of data with some inherent meaning,
representing some aspect of real world and which is designed, built and populated with data
for a specific purpose.
3. What is DBMS?
It is a collection of programs that enables user to create and maintain a database. In other
words it is general-purpose software that provides the users with the processes of defining,
constructing and manipulating the database for various applications.
4. What is a Database system?
The database and DBMS software together is called as Database system.
23
24
25
HW/SW requirements:
Processor
RAM
1 GB
Hard Disk
150 GB
Software
ORACLE 10G
HARISH
BALAJI
RAKESH
PAVAN
JOYCE
ISS_NO
NOT NULL
NUMBER
27
DATE
NUMBER(5)
NUMBER(5)
43
05-JAN-06
5443
4523
81
28-DEC-05
5441
8723
22
08-DEC-05
5440
7821
53
07-JAN-06
5442
9123
35
06-JAN-06
5444
2342
c) List all the student names with their membership numbers
SQL> select s.studname, m.memno from student s, membership m where m.studno=s.studno;
STUDNAME
------------abhijeet
arun
arvind
ashish
ashwin
MEMNO
-------1001
1002
1003
1004
1005
d) List all the issues for the current date with student and Book names
SQL> select i.issno, s.studname, b.bookname from iss_rec I, membership m, student s, book b
2 where i.memno=m.memno and m.studno=s.studno and
i.issdate=to_char(sysdate);
ISSNO STUDNAME BOOKNAME
------- -------------------------13
arvind
P&S
e) List the details of students who borrowed book whose author is CJDATE
SQL> select * from student where studno in(select studno from membership where memno in
2 (select memno from iss_rec where bookno in(select bookno from book where
author=CJDATE)));
STUDNO
---------505
STUDNAME
------------ashwin
28
COUNT(I.BOOKNO)
----------------------5
5
5
5
5
j) Create a view which lists the daily issues-date wise for the last one week
Viva-Vice:
1. Describe the three levels of data abstraction?
29
2.
30
31
32
HW/SW requirements:
Processor
RAM
1 GB
Hard Disk
150 GB
Software
ORACLE 10G
sales
accounts
administration
production
supervisor
..
10
101
25023.12 43.09
71.23 08-JAN-93
21
100
10500.29 23.98
40.9
01-JAN-06
30
102
6500.5
30.54
15
06-JUL-97
39
103
9700.45
32.78
65.09 08-AUG-03
87
104
15000
97.66
154.8 24-SEP-04
SQL>create table payroll(emp_id number(5)references employee(emp_id),pay_date date);
SQL>desc payroll;
Name
Null? Type
..
EMP_ID
NUMBER(5)
PAY_DATE
DATE
SQL>insert into payroll values(&emp_id,&date);
34
401
500
402
200
403
600
404
400
405
1200
d) List all the employee names who joined after particular date
SQL>select e,empname from employee e,paydet p where e.empid=p.empid and p.doj>=05-mar06;
EMPNAME
AVINASH
NITIN
PHALGUN
e) List the details of employees whose basic salary is between 10,000 and 20,000
sqL> Select empid,empname from employee where salary between 10000 and 20000;
EMPID EMPNAME
.
402
AKHILA
403
aaaaaaaa
EMPID EMPNAME
.
AKHILA
f) Give a count of how many employees are working in each department
SQL>select count(empid),deptid from paydet group by deptid;
35
1
200
1
400
1
500
1
600
1
1200
AVINASH
AKHILA
HARISH
NITIN
PHALGUN
h) List the details for an employee_id=5
SQL> select * from employee where empid=5;
EMPID
EMPNAME
-----------------------------------------5
Coulthard
i)
Create a view which lists out the emp_name, department, basic, dedeuctions, netsalary
j)
Viva-Vice:
13. What is Data Model?
A collection of conceptual tools for describing data, data relationships data semantics
and constraints.
36
37
38
HW/SW requirements:
Processor
RAM
1 GB
Hard Disk
150 GB
Software
ORACLE 10G
..
CUST_NO
NOT NULL
NUMBER(5)
CUST_NAME
VARCHAR2(20)
Valid Test Data:
SQL>insert into customer values(&cust_no,&cust_name);
SQL>select * from customer;
CUST_NO
CUST_NAME
.
50
scott
51
pandey
52
varshney
53
naidu
54
bhimbra
SQL>create table membership(mem_no number(5) primary key,cust_no number(5) references
customer(cust_no));
SQL>dsec membership;
Name
Null?
Type
...
MEM_NO
NOT NULL
NUMBER(5)
CUST_NO
NUMBER(5)
SQL>insert into memship values(&mem_no,&cust_no);
39
920
50
981
51
897
52
820
53
928
54
SQL>create table cassette(cass_no
number(5) primary key,
Cass_name varchar2(15),language varchar2(15));
SQL>desc cassette;
Name
Null?
Type
..
CASS_NO
NOT NULL
NUMBER(5)
CASS_NAME
VARCHAR2(15)
LANGUAGE
VARCHAR2(15)
SQL>insert into cassette values(&cass_no,&cass_name,&language);
SQL>select * from cassette;
CASS_NO CASS_NAME
LANGUAGE
1
tagore
telugu
2
the lion king
English
3
anniyan
tamil
4
indra
telugu
5
lord of rings
English
SQL>create table issu_rec(iss_no number(5) primary key,iss_date date,mem_no
number(5)references memship(mem_no),cass_no number(5) references cassette(cass_no));
SQL>desc issu_rec;
Name
Null?
Type
...
ISS_NO
NOT NULL
NUMBER(5)
ISS_DATE
DATE
MEM_NO
NUMBER(5)
CASS_NO
NUMBER(5)
SQL>select * from issu_rec;
40
22
07-JAN-06
920
1
23
10-JAN-00
981
2
26
10-JAN-06
897
5
3
01-JAN-06
820
4
34
31-DEC-05
928
3
c) List all the customer names with their membership numbers
SQL>select c.custname,m.memno from customer1 c,membership1 m where
c.custno=m.custno;
CUSTNAME MEMNO
.. ..
NIKHIL
51
VIVEK
52
SHRAVAN
58
VAMSI
57
SHIVA
56
d) List all the issues for the current date with the customer names and cassette names
SQL>select i.issno,c.custname,cc.cassettename from customer1 c,membership1 m,cassette
cc,issrec1 I where i.issdate=to_char(sysdate) and c.custno=m.custno and
i.cassno=cc.cassno and i.memno=m.memno;
OutPut:
no rows selected.
e) List the details of the customer who has borrowed the cassette whose title is The
Legend
f) Give a count of how many cassettes have been borrowed by each customer
g) Give a list of book which has been taken by the student with mem_no as 5
h) List the cassettes issues for today
i) Create a view which lists outs the iss_no, iss_date, cust_name, cass_name
j) Create a view which lists issues-date wise for the last one week
Viva-Vice:
19. What is an Extension of entity type?
The collections of entities of a particular entity type are grouped together into an entity
set.
41
42
43
HW/SW requirements:
Processor
RAM
1 GB
Hard Disk
150 GB
Software
ORACLE 10G
null?
NOT NULL
Type
NUMBER(5)
VARCHAR2(20)
VARCHAR2(20)
STUD_NAM
CLASS
LEON
CSE
VIKAS
CSIT
MATHEW
ECE
HANSEN
MECH
ALEXIS
EEE
null
type
VARCHAR2(10)
VARCHAR2(20)
44
null
MACH_NO
LAB_NO
DESCRIPTION
type
NOT NULL
NUMBER(5)
NUMBER(5)
VARCHAR2(20)
Null?
------- ---------
Type
NUMBER(5)
NUMBER(5)
VARCHAR2(20)
MACH_NO
-- ----------------------23
87
78
12
12
DOWEEK
sat
mon
tue
wed
thu
c) List all the machine allotments with the student names, lab and machine numbers
SQL>select s.studname,l.machno from student1 s,lab l,allotment a where
a.machno=l.machno and a.studno=s.studno;
45
1
UNIX
MONDAY
22
UNIX
TUESDAY
3
XP
WEDNESDAY
4
WINDOWS
THRUSDAY
5
ME
FRIDAY
e) Give a count of how many machines have been allocated to the CSIT class
SQL>select count(machno)from allotment where studno in(select studno from student1
where class=CSIT);
COUNT (MACHNO)
..
1
f) Give a machine allotment etails of the stud_no 5 with his personal and class details
SQL>select a.studno,a.machno,s.studname,s.class from allotment a,student1 s where
a.studno=s.studno and a.studno=503;
STUDNO
MACHNO
STUDNAME
CLASS
503
5
ARVIND
CSE
g) Count for how many machines have been allocatedin Lab_no 1 for the day of the week
as Monday
h) How many students class wise have allocated machines in the labs
46
2
CSE
1
ECE
1
EEE
1
IT
i) Create a view which lists out the stud_no, stud_name, mach_no, lab_no, dayofweek
j) Create a view which lists the machine allotment details for Thursday
Viva-Vice:
26. What is Relationship type?
Relationship type defines a set of associations or a relationship set among a given set of
entity types.
27. What is degree of Relationship type?
It is the number of entity type participating.
25. What is DDL (Data Definition Language)?
A data base schema is specifies by a set of definitions expressed by a special language
called DDL.
26. What is VDL (View Definition Language)?
It specifies user views and their mappings to the conceptual schema.
27. What is SDL (Storage Definition Language)?
This language is to specify the internal schema. This language may specify the mapping
between two schemas.
28. What is Data Storage - Definition Language?
The storage structures and access methods used by database system are specified by a set
of definition in a special type of DDL called data storage-definition language.
29. What is DML (Data Manipulation Language)?
This language that enable user to access or manipulate data as organised by appropriate
data model.
47
48
HW/SW requirements:
Processor
RAM
1 GB
Hard Disk
150 GB
Software
ORACLE 10G
Algorithm:
Step 1: Declare the variable A, B, and C.
Step 2: Store the valid data.
Step 3: Compare variable A with B and A with C
Step 4: If the value stored in variable A is big, it displays A is Big. (IF conditional
statement should be used)
Step 5: Compare variable B with C
Step 6: If the value stored in variable B is big, it displays B is Big.
Step 7: other wise it displays C is Big
Declare
A number;
B number;
C number;
Begin
A:=&a;
B:=&b;
C:=&c;
If a > b && a> c then
Dbms_output.put_line( A is big );
49
50
51
HW/SW requirements:
Processor
RAM
1 GB
Hard Disk
150 GB
Software
ORACLE 10G
Algorithm:
Step 1: Declare the variable I.
Step 2: Store the valid data 1 in I.
Step 3: Use LOOP statement
Step 4: Display the first value.
Step 5: Increment the value of I by 1 value.
Step 6: check the value up to 10 no. and repeat the loop
Step 7: If condition exceeds the given value 10, the loop will be
terminated.
52
53
54
55
HW/SW requirements:
Processor
RAM
1 GB
Hard Disk
150 GB
Software
ORACLE 10G
Algorithm:
Step 1: Declare the variable N, S, D and DUP.
Step 2: Store the value in var. N and var. DUP..
Step 3: check for the value of N, which is not equal to 0.
Step 4: divide value stored in N by 10 and store it var. D. (D=n%10).
Step 5: the reminder will be multiply 3 times and store it in Var. S.
Step 6: The coefficient will be calculated using FLOOR function. And store it in var. N.
Step 7: repeat the Steps 3, 4, 5, and 6 till loop will be terminated.
Step 8: Check whether the stored value and calculated values are same
Step 9: if both the values are same, then display The given number is
Armstrong
Step 10: Otherwise display it is not Armstrong and terminate the
loop.
Declare
N number;
S number;
D number;
Begin
N:=&n;
S:=0;
While(n!=0)
Loop
D=n%10;
S:=s+(D*D*D);
N:=floor(n/10);
56
57
58
HW/SW requirements:
Processor
RAM
1 GB
Hard Disk
150 GB
Software
ORACLE 10G
Declare
I number;
J number;
C number;
Begin
While(i<=100)
Loop
C:=0;
J:=1;
While(j<=i)
Loop
If(floor(i%j)=0) then
C:= C+1;
End if;
J:=j+1;
End loop;
If(c=2) then
Dbms_output.put_line(i);
59
60
61
HW/SW requirements:
Processor
RAM
1 GB
Hard Disk
150 GB
Software
ORACLE 10G
Declare
I number;
Begin
I:=1;
If(i>=0) then
GOTO here;
Else
Dbms_output.put_line( I is negative);
End if;
<<here>>
Dbms_output.put_line( I is positive);
End;
Valid Test Data
OUTPUT:
I is positive
Viva-Vice:
62
63
HW/SW requirements:
Processor
RAM
1 GB
Hard Disk
150 GB
Software
ORACLE 10G
Declare
My_Empno emp.empno%type;
My_Ename emp.ename%type;
My_Emprow emp%rowtype;
No number;
Begin
No:=&no;
Select empno,ename into my_empno,my_ename from emp where empno=no;
If(SQl%rowcount=1) then
Dbms_output.put_line(empno is || my_empno || ename is || my_ename);
Else
Dbms_output.put_line( error);
End if;
Select * into my_emprow from emp where empno=no;
If(SQl%rowcount=1) then
Dbms_output.put_line(empno
my_emprow.ename);
is
||
my_emprow.empno
||
ename
is
||
Else
Dbms_output.put_line( error);
64
65
66
HW/SW requirements:
Processor
RAM
1 GB
Hard Disk
150 GB
Software
ORACLE 10G
Declare
A number
B number;
C number;
Begin
A:=&a;
B:=&b;
C:=a/b;
Dbms_output.put_line(division is || C);
Exception
If (ZERO_DIVIDE) then
Dbms_output.put_line(b could not be zero);
End if;
End;
Valid Test Data:
Enter the value for a:
10
67
68
HW/SW requirements:
Processor
RAM
1 GB
Hard Disk
150 GB
Software
ORACLE 10G
Declare
A number
B number;
C number;
Mydivide_zero EXCEPTION;
Begin
A:=&a;
B:=&b;
If(B=0) then
Raise Mydivide_zero;
else
C:=a/b;
Dbms_output.put_line(division is || C);
End if;
Exception
If (mydivide_zero) then
Dbms_output.put_line(b could not be zero);
69
Database files
Control files
Redo logs
The most important of these are the database files where the actual data resides. The
control files and the redo logs support the functioning of the architecture itself.
All three sets of files must be present, open, and available to Oracle for any data on the database to be useable. Without these files,
you cannot access the database, and the database administrator might have to recover some or all of the database using a backup, if there is one.
70
71
HW/SW requirements:
Processor
RAM
1 GB
Hard Disk
150 GB
Software
ORACLE 10G
Declare
Cursor my_cur is select empno,sal from emp;
Xno emp.empno%type;
Xsal emp.sal%type;
C number;
Begin
Open my_cur;
C:=0;
Loop
Fetch my_cur into xno,xsal;
If(xsal<1000) then
Update emp set sal=3000 where empno=xno;
C:=c+1;
Else if(xsal>=2000) and xsa<3000) then
72
End loop;
Close my_cur;
Dbma_output.put_line(c||records have been successfully updated);
End;
Sql>@a.sql;
records have been successfully updated
pl/sql procedure successfully completed.
Valid Test Data
Before executing the cursor, the records in emp table as follows
Sql>select * from emp;
OUTPUT:
EMPNO ENAME JOB MGR HIREDATE SAL COMMD EPTNO
----------------------------------------------------------------7369 SMITH
CLERK
7902 17-DEC-80
7499 ALLEN
SALESMAN
7698 20-FEB-81
1600
300
30
7521 WARD
SALESMAN
7698 22-FEB-81
1250
500
30
2000
20
73
MANAGER
MANAGER
7839 02-APR-81
7698 28-SEP-81
2975
1250 1400
7839 01-MAY-81
2850
20
30
30
14 rows selected.
Viva-Vice:
81. What are database files, control files and log files. How many of these files should a
database have at least? Why?
Database Files
The database files hold the actual data and are typically the largest in size.
Depending on their sizes, the tables (and other objects) for all the user accounts can go in one
database filebut that's not an ideal situation because it does not make the database structure
very flexible for controlling access to storage for different users, putting the database on different
disk drives, or backing up and restoring just part of the database.
You must have at least one database file but usually, more than one files are used.
In terms of accessing and using the data in the tables and other objects, the number (or location)
of the files is immaterial.
The database files are fixed in size and never grow bigger than the size at which
they were created
Control Files
The control files and redo logs support the rest of the architecture. Any
database must have at least one control file, although you typically have more than one to
guard against loss. The control file records the name of the database, the date and time it was
created, the location of the database and redo logs, and the synchronization information to
ensure that all three sets of files are always in step. Every time you add a new database or
redo log file to the database, the information is recorded in the control files.
Redo Logs
Any database must have at least two redo logs. These are the journals for the
database; the redo logs record all changes to the user objects or system objects. If any type of
failure occurs, the changes recorded in the redo logs can be used to bring the database to a
74
75
76
RAM
1 GB
Hard Disk
150 GB
Software
ORACLE 10G
77
78
79
80
HW/SW requirements:
Processor
RAM
1 GB
Hard Disk
150 GB
Software
ORACLE 10G
81
82
/*add_fun specification*/
Declare
Result number;
Begin
Result:=add_fun(10,20);
Dbms_output.put_line(the sum of 10 and 20 is||result);
End;
Sql>/
The sum of 10 and 20 is 30
Pl/sql procedure successfully completed.
/*create a function which count total no.of employees having salary less than 6000.*/
/*function body*/
Create or replace function count_emp(esal number)return number as
Cursor vin_cur as Select empno,sal from emp;
Xno emp.empno%type;
Xsal emp.sal%type;
C number;
Begin
Open vin_cur;
83
84
85
What are Armstrong rules? How do we say that they are complete and/or sound
The well-known inference rules for FDs
Reflexive rule :
86
104.
world .
Retroactive Update:
87
88
89
20. Which date function is used to find the difference between two dates?
MONTHS_BETWEEN
21. Why does the following command give a compilation error?
DROP TABLE &TABLE_NAME;
Variable names should start with an alphabet. Here the table name starts with an '&'
symbol.
22. What is the advantage of specifying WITH GRANT OPTION in the GRANT command?
90
91
REFERENCES:
1)Write a pl/sql program if emp salary is greater than 25000/- display the emp name & salary
else add the 5000 for eid is 3.
declare
v_sal emp.sal%type;
v_ename emp.ename%type;
begin
select sal,ename into v_sal,v_ename from emp where eid=2;
if (v_sal > 25000)
then
DBMS_OUTPUT.PUT_LINE('empname is' || v_ename);
DBMS_OUTPUT.PUT_LINE('emp sal is' || v_sal);
else
v_sal := v_sal+5000;
DBMS_OUTPUT.PUT_LINE('empname is' || v_ename);
DBMS_OUTPUT.PUT_LINE('emp sal is' || v_sal);
end if;
end;
/
Write the pl/sql program to find ename,sal,dno who have the eid is 1
declare
v_ename emp.ename%type;
v_sal emp.sal%type;
92
3)
Write pl/sql program by using emp to select eid age,sal where name is read from the
keyboard.
declare
v_eid emp.eid%type;
v_age emp.age%type;
v_sal emp.sal%type;
v_ename emp.ename%type:= '&name';
begin
select eid,age,sal into v_eid,v_age,v_sal from emp where ename= '&name';
DBMS_OUTPUT.PUT_LINE('empid is' || v_eid);
DBMS_OUTPUT.PUT_LINE('esal is' || v_sal);
DBMS_OUTPUT.PUT_LINE('age is'|| v_age);
end;
/
o/p: enter the name:
4. insert the values into student table and save the each time and rol back to 2 records.
Sql>
begin
insert into std values(1,'x','90');
savepoint a;
insert into std values(2,'y','80');
savepoint b;
insert into std values(3,'z','97');
93
5. Write the pl/sql program whether the given number is odd or even
declare
n number:= &a;
begin
if (n mod 2=0) then
DBMS_OUTPUT.PUT_LINE(n || 'is even');
else
DBMS_OUTPUT.PUT_LINE(n || 'is odd');
end if;
end;
/
o/p: enter the value for n:6
94
95
FUNCTIONS:
Function is a set of sql and pl/sql statements to perform a particular task.
syntax:
cretae or replace procedure function function name(arguments in datatype)
96
declare
a number:= '&c';
b number:= '&d';
s number;
begin
s:=addition(a,b);
dbms_output.put_line('addition of 2 nos is' || s);
end;
/
o/P:
-----------------------------------------10. FIND THE FACTORIAL OF GIVEN NUMBER BY USING FUNCTIONS
97
declare
n number:= '&a';
k number;
begin
k:= fact(n);
dbms_output.put_line('the factorial of given no is' || k);
end;
/
--------------------------------------------------TRIGGERS
Trigger is a procedure i.e automatically invoked from rthe database. when any change occurs to
the database.
98
declare
2 a number;
3 b number;
4 c number;
5 begin
6 a:= &a;
7 b:= &b;
8 c:= a+b;
9 dbms_output.put_line('Sum of '|| a ||' and ' || b ||' is ' || c);
10* end;
SQL> /
Enter value for a: 20
old 6: a:= &a;
99
Sum of 20 and 30 is 50
1 declare
2 a number;
3 s number default 0;
4 begin
5 a:=1;
6 loop
7
s:=s+a;
8
exit when(a=100);
9
a:=a+1;
10 end loop;
11 dbms_output.put_line('Sum of 100 numbers = '||s);
12* end;
SQL> /
100
1 declare
2 n number;
3 sum1 number default 0;
4 endvalue number;
5 begin
6 endvalue:=&endvalue;
7 n:=1;
8 for n in 1..endvalue
9 loop
10
if mod(n,2)=1
11
then
12
sum1:=sum1+n;
13
end if;
101
SQL> /
Enter value for endvalue: 30
old 6: endvalue:=&endvalue;
new 6: endvalue:=30;
sum = 225
1 declare
2 n number;
3 endvalue number;
4 sum1 number default 0;
5 begin
6 endvalue:=&endvalue;
7 n:=1;
8 while(n<endvalue)
9 loop
10
sum1:=sum1+n;
11
n:=n+2;
12 end loop;
102
1 declare
2 a number;
3 b number;
4 c number;
5 d number;
6 begin
7 a:=&a;
8 b:=&b;
9 c:=&c;
10 if(a>b) and (a>c) then
11
dbms_output.put_line('A is greater ');
12 elsif (b>a) and (b>c) then
13 dbms_output.put_line(' B is Greater ');
14 else
15 dbms_output.put_line('C is Greater');
16 end if;
17* end;
103
A is greater
1 declare
2 ename varchar2(15);
3 basic number;
4 da number;
5 hra number;
6 pf number;
7 netsalary number;
8 begin
9 ename:=&ename;
10 basic:=&basic;
11 da:=basic*(65/100);
12 hra:=basic*(15/100);
13 if(basic<3000)
14
then
15
pf:=basic*(5/100);
16 elsif(basic >=3000 and basic <=5000)
17
then
104
7. Factorial of a Number
1 declare
2 n number(2):=&dn;
3 fact number:=1;
4 begin
5 for i in 1..n
6 loop
7
fact:=fact*i;
105
SQL> /
Enter value for dn: 5
old 2: n number(2):=&dn;
new 2: n number(2):=5;
Factorial of 5 is 120
8. Fibonacci Series
1 declare
2
n number(2):=&dn;
3
a number:=-1;
4
b number:=1;
5
c number:=0;
6 begin
7 dbms_output.put_line('Fibonacci Series is : ');
8 for i in 1..n
9 loop
10
c:=a+b;
11
a:=b;
106
SQL> /
Enter value for dn: 5
old 2: n number(2):=&dn;
new 2: n number(2):=5;
Fibonacci Series is :
0
1
1
2
3
1 declare
2 num number:=&dn;
3 r number;
4 begin
5 r:=mod(num,2);
6 case r
7 when 0 then
8
dbms_output.put_line(num||' is EVEN number');
9 else
10
dbms_output.put_line(num||' is ODD number');
11 end case;
12* end;
SQL> /
Enter value for dn: 7
old 2: num number:=&dn;
107
SQL> /
Enter value for dn: 2
old 2: num number:=&dn;
new 2: num number:=2;
2 is EVEN number
1 declare
2
num number:=&dn;
3
r number;
4 begin
5 case
6
when mod(num,2)=0 then
7
dbms_output.put_line(num||' is EVEN number');
8
else
9
dbms_output.put_line(num||' is ODD number');
10 end case;
11* end;
SQL> /
Enter value for dn: 4
108
SQL> /
Enter value for dn: 5
old 2: num number:=&dn;
new 2: num number:=5;
5is ODD number
1 declare
2
a number:=&a;
3
b number:=&b;
4
c number;
5
choice number:=&ch;
6
wrong_choice exception;
7 begin
8
case choice
9
when 1 then c:=a+b;
10
dbms_output.put_line('Addition of two numbers is '||c);
11
when 2 then c:=a-b;
12
dbms_output.put_line('Subtraction of two numbers is '||c);
13
when 3 then c:=a*b;
14
dbms_output.put_line('Multiplication is '||c);
15
when 4 then c:=a/b;
16
dbms_output.put_line('Division is '||c);
17
else
109
SQL> /
Enter value for a: 1
old 2: a number:=&a;
new 2: a number:=1;
Enter value for b: 2
old 3: b number:=&b;
new 3: b number:=2;
Enter value for ch: 3
old 5: choice number:=&ch;
new 5: choice number:=3;
Multiplication is 2
SQL> /
Enter value for a: 4
old 2: a number:=&a;
new 2: a number:=4;
Enter value for b: 5
old 3: b number:=&b;
new 3: b number:=5;
Enter value for ch: 4
old 5: choice number:=&ch;
new 5: choice number:=4;
Division is 8
SQL> /
Enter value for a: 20
110
cursor:
----------1) program to illustrate the use of attribute sql%found in implicit cursor.The program is to find
out the salary of an employee table whose fields are empsalary and emp number
declare
esalary number;
enumber number;
begin
select esal into esalary from emp where eno='&eno';
if sql%found then
DBMS_OUTPUT.PUT_LINE('record found');
DBMS_OUTPUT.PUT_LINE('esalary' || esalary);
end if;
exception when
no_data_found then
DBMS_OUTPUT.PUT_LINE('record not found');
end;
/
2) program to illustrate the use of attribute sql%rowcount in implicit cursor.The program is to
update the salary of each employee by 1000
begin
update emp set esal=esal+1000;
dbms_output.put_line(sql%rowcount || 'records updated');
end;
/
111
112
113
Annexure - 1
114