DBMS Lab Manual 2
DBMS Lab Manual 2
MANUAL
Subject: Database
Management System
(130703)
Department: C.S.E.
Department
Class: B.E.-II,
Semester-III
Subject
: Database Management System (130703)
Name(s) of the author(s) : Manthan. J. Surti
Checked by
:
Index
No
01
Title of content
Page No
Course Details
i. Subject Code
ii. Title of the laboratory
iii. Teaching scheme
iv. Examination Scheme
04
02
04
03
04
04
05
List of Experiments
05
07
09
11
13
available in Oracle.
5.5 To study the concept of sub-queries and the use of
GROUP BY and HAVING clause.
14
16
19
21
23
26
Appendix
i. Reference Books
Teac
hing
Universi
University
t
Sche
Credi
me ( t
Sub
Code
Subject
Name
Hour
s
)
T T
h u
e t
o o
r ri
y al
Database
Managemen
130703
t
3 0
System
us
M
Practic
Exam
y Exam Evolutio
al k
Theory Practical
n
Process
P
r
a
c
t
i
c
a
l
2
70
30
50
2.
3.
4.
5.
7.
8.
9.
10.
CURSOR is a work area that is used by the oracle engine for its
internal processing in order to execute SQL statements.
1. To study the concept of Implicit cursors.
2. To study the concept of Explicit cursors.
Benefits of DBMS:
1. The amount of Data redundancy in stored data can
be reduced
2. No more data inconsistencies
3. Data can be shared by a single or multiple users
4. Standards can be set and follows
5. Data integrity can be maintained
6. Security of data can be implemented
7. Data independence can be achieved
ORACLE: It is a one type of relational Software which
used SQL for data storing. Relational software
later came to be known as Oracle Corporation.
5.2
To study Basic DATA
TYPE in SQL and Basic SQL
Commands.
Basic DATA TYPE:
Data Types : Data types come in several forms and
sizes,allowing the programmer to create
tables suited to the scope of project.
Oracle supports many datatypes such as follows :
CHAR (size) : This Data type is used to store character
strings values of fixed length.
picture or image.
Create table stud (sname varchar2(20) not null, rollno number(10) not null,dob
date not null);
Rules:
1.
2.
3.
4.
5.
6.
7.
10
Null?
---------------------------------
EmpNo
NOT NULL
EName
Job
NOT NULL
DeptNo
NOT NULL
PHONE_NO
Type
-------number(5)
VarChar(15)
Char(10)
number(3)
number (10)
DOMAIN INTEGRITY
Example: Create table cust(custid number(6) not null, name char(10));
Alter table cust modify (name not null);
CHECK CONSTRAINT
Example: Create table student (regno number (6), mark number (3) constraint
b check (mark >=0 and mark <=100));
Alter table student add constraint b2 check (length(regno<=4));
ENTITY INTEGRITY
a) Unique key constraint
Example: Create table cust(custid number(6) constraint uni unique, name
char(10)); Alter table cust add(constraint c unique(custid));
11
Queries:
Q1. Create a table called EMP with the following structure.
Name
Type
---------- --------------------EMPNO
NUMBER(6)
ENAME
VARCHAR2(20)
JOB
VARCHAR2(10)
DEPTNO
NUMBER(3)
SAL
NUMBER(7,2)
Allow NULL for all columns except ename and job.
Solution:
1. Understand create table syntax.
2. Use the create table syntax to create the said tables.
3. Create primary key constraint for each table as understand from logical table structure.
Ans:
SQL> create table emp(empno number(6),ename varchar2(20)not null,job
varchar2(10) not null, deptno number(3),sal number(7,2));
Table created.
Q2: Add a column experience to the emp table. experience numeric null allowed.
Solution:
1. Learn alter table syntax. 2. Define the new column and its
data type. 3. Use the alter table syntax.
Ans:
SQL> alter table emp add(experience number(2));
Table altered.
Q3: Modify the column width of the job field of emp table.
Solution:
1. Use the alter table syntax.
2. Modify the column width and its data type.
12
Ans:
SQL> create table dept(deptno number(2) primary key,dname
varchar2(10),loc varchar2(10));
Table created.
Q5: create the emp1 table with ename and empno, add constraints to check
the empno value while entering (i.e) empno > 100.
Solution:
1. Learn alter table syntax. 2. Define the new constraint [columns name type]
2. Use the alter table syntax for adding constraints.
Ans:
SQL> create table emp1(ename varchar2(10),empno number(6)
constraint ch check(empno>100));
Table created.
13
14
15
1 row created.
SQL> /
Enter value for empno: 2
Enter value for ename: Arjun
Enter value for job: ASP
16
1 row created.
SQL> /
Enter value for empno: 3
Enter value for ename: Gugan
Enter value for job: ASP
Enter value for deptno: 1
Enter value for sal: 12000
old 1: insert into emp values(&empno,'&ename','&job',&deptno,&sal)
new 1: insert into emp values(3,'Gugan','ASP',1,12000)
1 row created.
Q3: Update the emp table to set the salary of all employees to Rs15000/who are working as ASP
Ans:
SQL> select * from emp;
EMPNO
---------- 1
2
3
ENAME
------------Mathi
Arjun
Gugan
JOB
----AP
ASP
ASP
DEPTNO SAL
------------ -------1
10000
2
12000
1
12000
ENAME
JOB DEPTNO
SAL
-------------------- ------------- ---------- ---------Mathi
AP
1
10000
Arjun
ASP
2
15000
Gugan
ASP
1
15000
17
Null?
-----------------NOT NULL
NOT NULL
Type
----------------NUMBER(6)
VARCHAR2(20)
VARCHAR2(13)
NUMBER(3)
NUMBER(7,2)
1
2
3
Mathi
Arjun
Gugan
18
Karthik
Akalya
suresh
Prof
AP
lect
2
1
1
30000
10000
8000
6 rows selected.
SQL> delete from emp where job='lect';
1 row deleted.
SQL> select * from emp;
EMPNO
---------1
2
3
4
5
ENAME
-----------Mathi
Arjun
Gugan
Karthik
Akalya
Q7: List the records in the emp table orderby salary in ascending order.
Ans:
SQL> select * from emp order by sal;
EMPNO ENAME
JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ---------1
Mathi
AP
1
10000
5
Akalya
AP
1
10000
2
Arjun
ASP
2
15000
3
Gugan
ASP
1
15000
4
Karthik
Prof
2
30000
Q8: List the records in the emp table orderby salary in descending order.
Ans:
SQL> select * from emp order by sal desc;
EMPNO
ENAME
---------- --------------------
4
2
3
1
Karthik
Arjun
Gugan
Mathi
JOB
DEPTNO
------------- ---------Prof
2
ASP
2
ASP
1
AP
1
SAL
---------30000
15000
15000
10000
19
Akalya
AP
10000
ENAME
JOB
DEPTNO SAL
------------ -------- ------------- ---------- - --------Mathi
AP
1
10000
Gugan
ASP
1
15000
Akalya
AP
1
10000
Q10: Display deptno from the table employee avoiding the duplicated values.
Solution:
1. Use SELECT FROM syntax.
2.Select should include distinct clause for the deptno.
Ans:
SQL> select distinct deptno from
emp; DEPTNO
---------1
2
Result:
Thus the DML commands using from where clause was performed
successfully and executed.
20
Primary key
Syntax for Column level constraints Using Primary key:
SQL:>CREATE <OBJ.TYPE> <OBJ.NAME> (COLUMN NAME.1 <DATATYPE>
(SIZE)<TYPE OF CONSTRAINTS> , COLUMN NAME.1 <DATATYPE> (SIZE)
);
QUERY
21
QUERY: 15
SQL>CREATE TABLE EMPLOYEE (EMPNO NUMBER(6),
ENAME VARCHAR2(20),
JOB VARCHAR2(6),
22
QUERY: 16
SQL>CREATE TABLE EMPLOYEE(EMPNO
NUMBER(5), ENAME VARCHAR2(6),
JOB VARCHAR2(6),
SAL NUMBER(6),
DEPTNO NUMBER(6));
SQL>ALTER TABLE EMP3 ADD CONSTRAINT EMP3_EMPNO_PK PRIMARY
KEY (EMPNO);
Reference /foreign key constraint
Column level foreign key constraint:
Q.17. Write a query to create foreign key constraints with column level
Parent Table:
Syntax for Column level constraints Using Primary key:
SQL:>CREATE <OBJ.TYPE> <OBJ.NAME> (COLUMN NAME.1 <DATATYPE>
(SIZE)<TYPE OF CONSTRAINTS> , COLUMN NAME.1 <DATATYPE> (SIZE)
);
Child Table:
Syntax for Column level constraints Using foreign key:
SQL:>CREATE <OBJ.TYPE> <OBJ.NAME> (COLUMN NAME.1 <DATATYPE>
(SIZE), COLUMN NAME2 <DATATYPE> (SIZE) REFERENCES <TABLE
NAME> (COLUMN NAME> );
QUERY: 17
23
24
Parent Table:
SQL:>CREATE <OBJ.TYPE> <OBJ.NAME> (COLUMN
NAME.1 <DATATYPE> (SIZE)<TYPE OF
CONSTRAINTS> , COLUMN NAME.1 <DATATYPE>
(SIZE)
);
Child Table:
Syntax for Table level constraints using foreign key:
SQL:>CREATE <OBJ.TYPE> <OBJ.NAME> (COLUMN
NAME.1 <DATATYPE> (SIZE), COLUMN NAME2
<DATATYPE> (SIZE), CONSTRAINT <CONST. NAME>
REFERENCES <TABLE NAME> (COLUMN NAME> );
QUERY: 19
SQL>CREATE TABLE DEPT
(DEPTNO
NUMBER(2)
PRIMARY KEY,
DNAME
VARCHAR2(20),
LOCATION VARCHAR2(15));
SQL>
CRE
ATE
TABL
E
EMP
5
(EMP
NO
NUM
BER(
3),
DEP
TNO
NUM
BER(
2),
DESIGN VARCHAR2(10)CONSTRAINT
ENP2_DEPTNO_FK FOREIGN
KEY(DEPT NO)REFERENCESDEPT(DEPTNO));
Table Level Foreign Key Constraints with Alter
command
Q.20. Write a query to create foreign key
constraints with Table level with alter command.
Parent Table:
SQL:>CREATE <OBJ.TYPE> <OBJ.NAME> (COLUMN
NAME.1 <DATATYPE> (SIZE)<TYPE OF
CONSTRAINTS> , COLUMN NAME.1 <DATATYPE>
(SIZE)
);
Child Table:
Syntax for Table level constraints using foreign key:
SQL:>CREATE <OBJ.TYPE> <OBJ.NAME> (COLUMN
NAME.1 <DATATYPE> (SIZE) , COLUMN NAME2
<DATATYPE> (SIZE));
SQL> ALTER TABLE <TABLE NAME> ADD
CONSTRAINT <CONST. NAME> REFERENCES
<TABLE NAME> (COLUMN NAME>);
QUERY:20
SQL>CREATE
TABLE DEPT
(DEPTNO
NUMBER(2)
PRIMARY KEY,
25
Check constraint
Column Level Check Constraint
Q.21. Write a query to create Check constraints with column level
Syntax for clumn level constraints using Check:
SQL:>CREATE <OBJ.TYPE> <OBJ.NAME> (COLUMN NAME.1 <DATATYPE>
(SIZE) CONSTRAINT <CONSTRAINTS NAME> <TYPE OF CONSTRAINTS>
(CONSTRAITNS CRITERIA) , COLUMN NAME2 <DATATYPE> (SIZE));
QUERY:21
SQL>CREATE TABLE EMP7(EMPNO
NUMBER(3), ENAME VARCHAR2(20),
DESIGN VARCHAR2(15),
SAL NUMBER(5)CONSTRAINT EMP7_SAL_CK CHECK(SAL>500
AND SAL<10001),
DEPTNO NUMBER(2));
Table Level Check Constraint:
Q.22. Write a query to create Check constraints with table level
Syntax for Table level constraints using Check:
SQL:>CREATE <OBJ.TYPE> <OBJ.NAME> (COLUMN NAME.1
<DATATYPE> (SIZE), (COLUMN NAME2 <DATATYPE> (SIZE),
CONSTRAINT <CONSTRAINTS NAME> <TYPE OF CONSTRAINTS>
(CONSTRAITNS CRITERIA)) ;
QUERY:22
SQL>CREATE TABLE EMP8(EMPNO NUMBER(3),
ENAME VARCHAR2(20),
DESIGN VARCHAR2(15),
SAL NUMBER(5),DEPTNO NUMBER(2),
CONSTRAINTS EMP8_SAL_CK CHECK(SAL>500 AND
SAL<10001));
26
Unique Constraint
Column Level Constraint
Q.24. Write a query to create unique constraints with
column level
Syntax for Column level constraints with Unique:
SQL :> CREATE <OBJ.TYPE> <OBJ.NAME>
(<COLUMN NAME.1> <DATATYPE> (SIZE)
CONSTRAINT <NAME OF CONSTRAINTS>
<CONSTRAINT TYPE>, (COLUMN NAME2
<DATATYPE> (SIZE)) ;
QUERY:24
SQL>CREATE TABLE
EMP10(EMPNO
NUMBER(3), ENAME
VARCHAR2(20),
DESGIN VARCHAR2(15)CONSTRAINT
EMP10_DESIGN_UK UNIQUE, SAL
NUMBER(5));
Table Level Constraint
Q.25. Write a query to create unique constraints with table
level
Syntax for Table level constraints with Unique:
SQL :> CREATE <OBJ.TYPE> <OBJ.NAME> (<COLUMN
NAME.1> <DATATYPE> (SIZE), (COLUMN NAME2
<DATATYPE> (SIZE), CONSTRAINT <NAME OF
CONSTRAINTS> <CONSTRAINT TYPE>(COLUMN
NAME);) ;
27
QUERY:25
SQL>CREATE TABLE
EMP11(EMPNO
NUMBER(3), ENAME
VARCHAR2(20),
DESIGN VARCHAR2(15),
SAL NUMBER(5),CONSTRAINT EMP11_DESIGN_UK
UNIGUE(DESIGN));
Table Level Constraint Alter Command
Q.26. Write a query to create unique constraints with table
level
Syntax for Table level constraints with Check Using
Alter
SQL :> CREATE <OBJ.TYPE> <OBJ.NAME>
(<COLUMN NAME.1> <DATATYPE> (SIZE),
(COLUMN NAME2 <DATATYPE> (SIZE)) ;
SQL> ALTER TABLE ADD <CONSTRAINTS>
<CONSTRAINTS NAME> <CONSTRAINTS
TYPE>(COLUMN NAME);
QUERY:26
SQL>
CREA
TE
TABLE
EMP1
2
(EMP
NO
NUMB
ER(3),
ENAM
E
VARC
HAR2(
20),
DESIG
N
VARC
HAR2(
15),
SAL
NUMB
ER(5))
;
SQL>ALTER TABLE EMP12 ADD
CONSTRAINT EMP12_DESIGN_UK
UNIQUE(DESING);
Not Null
Column Level Constraint
Q.27. Write a query to create Not Null constraints with
column level
Syntax for Column level constraints with Not Null:
SQL :> CREATE <OBJ.TYPE> <OBJ.NAME>
(<COLUMN NAME.1> <DATATYPE> (SIZE)
CONSTRAINT <NAME OF CONSTRAINTS>
<CONSTRAINT TYPE>, (COLUMN NAME2
<DATATYPE> (SIZE)) ;
QUERY: 27
SQL>
CREA
TE
TABLE
EMP1
3
(EMP
NO
NUMB
ER(4),
ENAME VARCHAR2(20) CONSTRAINT
EMP13_ENAME_NN NOT NULL, DESIGN
VARCHAR2(20),
SAL NUMBER(3));
28
Null
Column Level Constraint
Q.28. Write a query to create Null constraints with column
level
Syntax for Column level constraints with Null:
SQL :> CREATE <OBJ.TYPE> <OBJ.NAME>
(<COLUMN NAME.1> <DATATYPE> (SIZE)
CONSTRAINT <NAME OF CONSTRAINTS>
<CONSTRAINT TYPE>, (COLUMN NAME2
<DATATYPE> (SIZE)) ;
QUERY:28
SQL>
CREA
TE
TABLE
EMP1
3
(EMP
NO
NUMB
ER(4),
ENAME VARCHAR2(20) CONSTRAINT
EMP13_ENAME_NN NULL, DESIGN
VARCHAR2(20),
SAL NUMBER(3));
29
30
15
56
54.59
100
16
1
100.26
100.23
4
CHARACTER FUNCTIONS
Command Query Output
initcap(char);
lower (char);
31
CONVERSION FUNCTION
1. to_char()
Syntax: to_char(d,[format]);
This function converts date to a value of varchar type in a form specified by date format.
If format is negelected then it converts date to varchar2 in the default date format.
32
Example:
select
33
Ans:
SQL> select * from emp where ename not like 'A%';
EMPNO ENAME
JOB
---------- -------------------- ------------1
Mathi
AP
3
Gugan
ASP
4
Karthik
Prof
DEPTNO SAL
---------- ---------1
10000
1
15000
2
30000
34
Q3: Display the rows whose salary ranges from 15000 to 30000.
Ans:
SQL> select * from emp where sal between 15000 and 30000;
EMPNO
ENAME
JOB
DEPTNO
SAL
---------- ------------------- ------------------------------2
Arjun
ASP
2
15000
3
Gugan
ASP
1
15000
4
Karthik
Prof
2
30000
Q4: Calculate the total and average salary amount of the emp table.
Ans:
SQL> select sum(sal),avg(sal) from emp;
SUM(SAL) AVG(SAL)
---------- ---------80000 16000
Q5: Count the total records in the emp table.
Ans:
SQL>select * from emp;
EMPNO
------ 1
2
3
4
5
ENAME
------------------Mathi
Arjun
Gugan
Karthik
Akalya
JOB
------------AP
ASP
ASP
Prof
AP
DEPTNO
---------1
2
1
2
1
SAL
---------10000
15000
15000
30000
10000
35
MIN_SALARY
---------10000
36
Result:
Thus the nested Queries and join Queries was performed successfully and executed
37
Example: select ename, eno, address where salary >(select salary from
employee where ename =jones);
1.Subqueries that return several values
Example: select ename, eno, from employee where salary <any (select
salary from employee where deptno =10);
3.Correlated subquery
Example: select * from emp x where x.salary > (select avg(salary) from emp
where deptno
=x.deptno);
SQL COMMANDS
1. COMMAND NAME: SELECT
COMMAND DESCRIPTION: SELECT command is used to select records
from the table.
2. COMMAND NAME: WHERE
COMMAND DESCRIPTION: WHERE command is used to identify
particular elements.
3. COMMAND NAME: HAVING
COMMAND DESCRIPTION: HAVING command is used to identify
particular elements.
4. COMMAND NAME: MIN (SAL)
COMMAND DESCRIPTION: MIN (SAL) command is used to find minimum salary.
38
39
Queries:
Q1: Display all employee names and salary whose salary is greater than
minimum salary of the company and job title starts with M.
Solution:
1. Use select from clause.
2. Use like operator to match job and in select clause to get the result.
Ans:
SQL> select ename,sal from emp where sal>(select min(sal) from emp where
job like 'A%');
40
ENAME
JOB DEPTNO
-------------------- ---------- ---------Mathi
AP
1
Arjun
ASP
2
Gugan
ASP
2
Karthik
AP
1
SAL
---------10000
12000
20000
15000
SQL> select ename from emp where job=(select job from emp where ename='Arjun');
ENAME
-------------Arjun
Q3: Issue a query to display information about employees who earn more
than any employee in dept 1.
Ans:
SQL> select * from emp where sal>(select max(sal) from emp where empno=1);
EMPNO
---------2
3
4
ENAME
JOB
DEPTNO SAL
-------------------- ---------- ---------- ---------Arjun
ASP
2
12000
Gugan
ASP
2
20000
Karthik
AP
1
15000
41
42
NON-EQUIJOIN
Q5: Display the employee details, departments that the
departments are not same in both the emp and dept.
Solution:
1.Use select from clause.
2. Use non equi join in select clause to get the result.
Ans:
SQL> select * from emp,dept where emp.deptno!
=dept.deptno;
EMPNO ENAME JOB DEPTNO SAL DEPTNO DNAME
LOC
---------- -------------------- ---------- ---------- --------------------------------- ------------2 Arjun ASP 2 12000 1 ACCOUNTING NEW YORK
3 Gugan ASP 2 20000 1 ACCOUNTING NEW YORK
1 Mathi AP 1 10000 2 RESEARCH DALLAS
EMPNO ENAME JOB DEPTNO SAL DEPTNO DNAME
LOC
---------- -------------------- ---------- ---------- ---------- ----------------------- ------------4 Karthik AP 1 15000 2 RESEARCH DALLAS
1 Mathi AP 1 10000 30 SALES CHICAGO
2 Arjun ASP 2 12000 30 SALES CHICAGO
EMPNO ENAME JOB DEPTNO SAL DEPTNO DNAME
LOC
---------- -------------------- ---------- ---------- ---------- ----------------------- ------------3 Gugan ASP 2 20000 30 SALES CHICAGO
4 Karthik AP 1 15000 30 SALES CHICAGO
1 Mathi AP 1 10000 40 OPERATIONS BOSTON
EMPNO ENAME JOB DEPTNO SAL DEPTNO DNAME
LOC
---------- -------------------- ---------- ---------- ---------- ----------------------- ------------2 Arjun ASP 2 12000 40 OPERATIONS BOSTON
3 Gugan ASP 2 20000 40 OPERATIONS BOSTON
4 Karthik AP 1 15000 40 OPERATIONS BOSTON
12 rows selected.
43
LEFTOUT-JOIN
Tables used
SQL> select * from stud1;
Regno Name Mark2 Mark3 Result
---------- ----------- ---------- -----------------------------------------------101 john 89 80 pass
102 Raja 70 80 pass
103 Sharin 70 90 pass
104 sam 90 95 pass
SQL> select * from stud2;
NAME GRA
----------- ---------j
o
h
n
s
r
a
j
s
s
a
m
a
s
h
a
r
i
n
smith null
RIGHTOUTER-JOIN
Q7: Display the Student name, register no, and result
by implementing a right outer join.
Ans:
44
SELFJOIN
Q9: Write a query to display their employee names
Ans:
SQL> select distinct ename from emp x, dept y where x.deptno=y.deptno;
ENAME
-------------------Arjun
Gugan
Karthik
Mathi
45
46
47
48
49
Tables Used:
50
Ans:
SQL> Grant all on employees to departments;
Grant succeeded.
Q2: Develop a query to grant some privileges of employees table into departments table
Ans:
SQL> Grant select, update , insert on employees to departments with grant option;
Grant succeeded.
Q3: Develop a query to revoke all privileges of employees table from departments table
Ans:
SQL> Revoke all on employees from departments;
Revoke succeeded.
Q4: Develop a query to revoke some privileges of employees table from
departments table
Ans:
SQL> Revoke select, update , insert on employees from departments;
Revoke succeeded.
Q5: Write a query to implement the save point
Ans:
SQL> SAVEPOINT S1;
Savepoint created.
51
Result:
The DCL,TCL commands was performed successfully and executed.
52
ELSIF STATEMENTS:
Syntax:
IF condition1 THEN
53
NESTED IF:
Syntax:
IF condition THEN
statement1;
ELSE
IF condition THEN
statement2;
ELSE
statement3;
END IF;
END IF;
ELSE
statement3;
END IF;
:
ELSE
Statement n;
END CASE;
SEARCHED
CASE: Syntax:
CASE
WHEN searchcondition1 THEN statement1;
WHEN searchcondition2 THEN statement2;
::
54
ITERATIONS IN PL/SQL
SIMPLE LOOP
Syntax:
LOOP
statement1;
Loop
a := a+25;
exit when a=250;
end loop;
dbms_output.put_line(to_char(a));
end;
WHILE LOOP
Syntax
WHILE condition
LOOP statement1;
statement2;
END LOOP;
Example:
Declare
i number:=0;
j
num
ber:=
0;
begi
n
:
=
i+
2
;
e
n
d
lo
o
p
;
55
statement1;
statement2;
END LOOP;
Example:
Begin
For I in
1..2 Loop
Update emp set field = value where
condition; End loop;
End;
/
Q1: write a pl/sql program to swap two numbers
c) Procedure for doing the experiment:
Step
no.
numb
er(10)
;
begin
56
output:
SQL> @
swapping.sql 19 /
Enter value for a:
5 old 6: a:=&a;
new 6: a:=5;
Enter value for b:
3 old 7: b:=&b;
new 7: b:=3;
THE PREV VALUES OF A AND B
WERE 53
THE VALUES OF A AND B ARE
35
PL/SQL procedure successfully completed.
Q2: Write a pl/sql program to find the largest of three numbers
c) Procedure for doing the
experiment: Step
no.
Details of the step
1 Read three numbers through a, b & c
2 Find the biggest among three using nested if
statement 3 Display the biggest no as result
d)Program:
SQL>set server output
on; SQL>edit
biggest.sql declare
a number;
b number;
c
nu
m
be
r;
be
gin
a:
=&
a;
b:
=&
b;
c:=
&c
;
if a>b then
57
Q3: write a pl/sql program to find the total and average of 6 subjects and
display the grade
c) Procedure for doing the
experiment: Step
no.
Details of the step
1 Read six numbers and calculate total and average
2 Find whether the student is pass or fail using if statement
3 Find the grade using nested elseif statement
58
dbms_output.put_line('ENTER THE
MARKS'); java:=&java;
dbms:=&dbms;
co:=&co;
se:=&se;
es:=&es;
ppl:=&ppl;
total:=(java+dbms+co+se+es+ppl);
per:=(total/600)*100;
if java<50 or dbms<50 or co<50 or se<50 or es<50 or ppl<50
then dbms_output.put_line('FAIL');
if per>75 then
dbms_output.put_line('GRADE A');
elsif per>65 and per<75 then
dbms_output.put_line('GRADE B');
end;
e)output:
SQL> @
grade.sql 31 /
Enter value for java: 80
old 12: java:=&java;
new 12: java:=80; Enter
value for dbms: 70 old
13: dbms:=&dbms; new
13: dbms:=70;
59
a:=&a;
while
a>0 loop
d:=mod(a,10);
sum1:=sum1+d; Dr.N.N.C.E IT / IV Sem DBMS Lab LM 39
a:=trunc(a/10);
end loop;
60
d:=mod(a,10);
rev:=(rev*10)+d;
a:=trunc(a/10);
end loop;
dbms_output.put_line('no is'||
rev); end;
e)output:
SQL> @
reverse.sql 16 /
Enter value for a:
536 old 6: a:=&a;
new 6: a:=536;
no is 635
61
62
63
end loop;
end;
e)output:
3
4
5
6
SQL> @
AREAOFCIRCLE.SQL 13 /
PL/SQL procedure successfully
completed. SQL> SELECT * FROM
AREAS; RADIUS AREA
---------- ---------28.26
50.24
78.5
113.04
Q9: write a PL/SQL code block that will accept an account number from the user,
check if
the users balance is less than minimum balance, only then deduct rs.100/from the balance.
This process is fired on the acct table.
c) Procedure for doing the
experiment: Step
no.
Details of the step
1 Develop a query to Create the table acct and insert values into
them 2 Develop a PL/SQL program to read the account number.
3 Check the balance for the account no. check if the users balance is less
than minimum balance, only then deduct rs.100/- from the balance
4 Update the balance changes into the acct table. Dr.N.N.C.E IT / IV Sem
DBMS Lab - LM
42
d)Program:
SQL> create table acct(name varchar2(10),cur_bal number(10),acctno number(6,2));
64
65
PROGRAM:
SQL> create table student(regno number(4),name
varchar2)20),mark1 number(3), mark2 number(3), mark3
number(3), mark4 number(3), mark5 number(3));
Table created
SQL> insert into student values
(101,'priya', 78, 88,77,60,89); 1
row created.
SQL> insert into student values
(102,'surya', 99,77,69,81,99); 1
row created.
SQL> insert into student values
(103,'suryapriya', 100,90,97,89,91); 1
row created.
SQL> select * from student;
regno name mark1 mark2 mark3 mark4 mark5
-------------------------------------------------------------------101 priya 78 88 77 60 89
102 surya 99 77 69 81 99
103 suryapriya 100 90 97 89 91
S
Q
L
>
d
e
c
l
a
r
e
a
v
e
n
u
m
b
e
r
(
5
,
2
)
;
t
o
t
n
u
m
b
e
r
(
3
)
;
66
67
Implicit curscors:
SQL> DECLARE
2 ena EMP.ENAME%TYPE;
3 esa EMP.SAL%TYPE;
4 BEGIN
5 SELECT ENAME,SAL INTO ENA,ESA FROM EMP
6 WHERE EMPNO = &EMPNO;
7 DBMS_OUTPUT.PUT_LINE('NAME :' || ENA);
8 DBMS_OUTPUT.PUT_LINE('SALARY :' || ESA);
9
10 EXCEPTION
11 WHEN NO_DATA_FOUND THEN
12 DBMS_OUTPUT.PUT_LINE('Employee no does not exits');
13 END;
14 /
68
Output:
Enter value for empno: 7844
old 6: WHERE EMPNO = &EMPNO; new
6: WHERE EMPNO = 7844; PL/SQL
procedure successfully completed.
Explicit Cursors:
5
6
7
8
SQL> DECLARE
2 ena EMP.ENAME%TYPE;
3 esa EMP.SAL%TYPE;
4 CURSOR c1 IS SELECT ename,sal FROM EMP;
BEGIN
OPEN c1;
FETCH c1 INTO ena,esa;
DBMS_OUTPUT.PUT_LINE(ena || ' salry is $ ' || esa);
9
10 FETCH c1 INTO ena,esa;
11 DBMS_OUTPUT.PUT_LINE(ena || ' salry is $ ' || esa);
12
13
14
15
16
17
Output:
SMITH salry is $ 800
ALLEN salry is $ 1600
WARD salry is $ 1250
RESULT:
Thus the PL/SQL block to display the student name,marks,average is
verified and executed.
69
70