Dbms Lab Manual
Dbms Lab Manual
Prepared By
Sri V Prasad
Associate Professor
Dept. Of Computer Science & Engg.
1 Lab Objective
2 Introduction to DBMS
4 Introduction to SQL
Project -1
10 Functions Implementation
11 Procedure Implementation
INDEX
13 E-R Diagrams for CMM
Project -2
14 Reference Books
DBMS Lab Manual for III B.Tech II Sem CSE & IT Raghu Institute Of Technology
Lab Objective
Upon completing the course, students will be fully prepared to design, implement and manage DBMS to serve a wide range
of goals in a range of educational settings.
Build tables and construct relationships among them utilizing normalized data.
Design and implement a base DBMS to integrate into a specific classroom instructional situation.
Know advanced DBMS topics and techniques regarding design, data and tables, queries, forms and reports.
Analyze an educational situation to identify data components and relationship among them.
Use a student achievement DBMS to guide instructional decisions for classrooms, schools, and districts.
Select among DBMS products in terms of tradeoffs between flexibility, performance, easy of use and cost.
DBMS Lab Manual for III B.Tech II Sem CSE & IT Raghu Institute Of Technology
Study features of a commercial RDBMS package such as ORACLE/DB2, MS Access, MYSQL & Structured
Query Language (SQL) used with the RDBMS.( Select two of RDMSs)
Laboratory exercises should include defining schemas for applications, creation of a database,
writing SQL queries, to retrieve information from the database, use of host languages,
interface with the embedded SQL, use of forms & report writing packages available with the chosen
RDBMS product.
9. Hostel accounting.
Information on space allocated and used for Oracle Applications Release 11i (aka Oracle e-
schema objects. Business Suite, Oracle Financials or Oracle 11i):
a suite of business applications;
The Program Global Area or PGA memory-area of an Oracle Developer Suite 10g (9.0.4);
Oracle instance contains data and control-
Oracle JDeveloper 10g: a Java integrated
information for Oracle's server-processes. The
development environment;
dynamic performance views (also known as "fixed
views") within an Oracle database present The latest certification information of oracle
information from virtual tables (X$ tables) built on application server log(10.1.4)
the basis of database memory. Database users can
access the V$ views to obtain information on Licensing information regarding compliance for
database structures and performance. oracle application server is available at
Version numbering:
The basic idea of generating this project is for showing the essentiality of ER Diagrams in the
Data base management systems. Organizing of the data according to the relevant information is
clearly specified in our project .
We create the entities- student, faculty, department, subject and marks. We establish
relationships between the entities such as assigns, learns, studies, handles and an aggregation
relationship-monitors. There are integrity constraints for every entity which makes the database more
flexible and data retrieval easy.
We normalize the data stored in the database so as to eliminate data duplication which can
further lead to the destruction of data integrity. The efficiency of the output has been improved by
imbibing various features into the program like nested-if, case and case expressions, cursors,
conversion functions, procedures and functions. We also use triggers to make our system more
responsive.
DBMS Lab Manual for III B.Tech II Sem CSE & IT Raghu Institute Of Technology
-Student entity: It has the attributes-sid (student id), sname (student’s name),saddr (student’s
address).
-Marks entity: It has the attributes-sid, subid and marks (marks of each student in each
subject).
The relationships that we established between the above defined entities are:
-learns: This relationship is between the entities student and subject. It contains the
descriptive attribute ‘since’ and derived attributes-sid, subfac.
-assigns: This relationship is between the entities faculty and marks. It has the derived
attributes-fid, sid, subid, smarks.
-handles: This relationship is between the entities department and faculty. It has the
descriptive attribute-since.
-monitors: This relationship is an aggregation relationship between the relationship set that
consists of faculty and department and the entity student.
-primary key: This key uniquely identifies a tuple. The primary keys used in the defined entities
are:-
Here the entity “marks” doesn’t have a primary key, it depends on another entity for a unique id,
and hence it’s a weak entity.
-foreign key: To keep the data consistent, we use an integrity constraint that involves relations,
linking them both. This is nothing but the foreign key. Some of the foreign keys used here are:
SQL queries: They allow the user to describe desired data, leaving the database management system
(DBMS) responsible for planning, optimizing, and performing the physical operations necessary to
produce that result as it chooses. To enhance the efficiency of these queries we use:
-nested queries, in which a relation referred to in the query is itself defined within the query.
-aggregate operators, which allows us to write SQL queries that are not expressible in relational
algebra.
-triggers are which describe actions to be taken when certain situations arise. These are invoked
when such situations arise.
-procedures and functions, are a set of statements that can be executed. A procedure doesn’t
return a value, but a function does.
In this project, we also implemented views (a relation whose instance is not explicitly
stored but are computed as need) to enable logical independence.
PL/SQL programs have been written using many enhanced features of SQL, to improve the
efficiency of programming, like:
-nested-if.
Conversion functions for string manipulation and date format conversions have also been
implemented.
DBMS Lab Manual for III B.Tech II Sem CSE & IT Raghu Institute Of Technology
DDL Statements:
Creation Of Tables with & Without Constraints :
CREATE TABLE student( roll NUMBER PRIMARY KEY, SID ROLL MARKS
name VARCHAR2(10) NOT NULL,
marks NUMBER NOT NULL CHECK
( marks BETWEEN 50 AND 99));
alter table student add address varchar(10); SID ROLL NAME AGE ADDRESS
DBMS Lab Manual for III B.Tech II Sem CSE & IT Raghu Institute Of Technology
DROP:
RENAME :
TRUNCATE :
EDIT xyz.txt
SQL> @ filename or SQL> @ path\filename
ROLL NAME
2 RUN 2 RUN
2 yash
Enter the Data in a file as:
1 raj
Select roll,name from student;
Get xyz.txt
DML Statements :
SID ROLL NAME MARKS
insert into student values(1001,1,'raj',78);
1002 2 yash 20
insert into student values(1002,2,'Yash',67);
1001 1 raj 19
insert into student values(1003,3,'Srinu',84);
1003 3 Srinu 22
insert into student values(1004,4,'Lakhan',97);
1004 4 Lakhan 23
Inserting Values at the run time :
DBMS Lab Manual for III B.Tech II Sem CSE & IT Raghu Institute Of Technology
1001 1 Raj 20
1003 3 Srinu 22
1004 4 Lakhan 23
UPDATE student SET name = 'raju' WHERE roll = 2; /* Changing name of Yash to Raju */
DELETE FROM mrk WHERE reg = 2; /* Delete a tupple whose reg equals 2*/
DUAL
It’s a Single column /single table to evaluate constant expression in a select statement. (Contains
Dummy Value)
To current Date:
DBMS Lab Manual for III B.Tech II Sem CSE & IT Raghu Institute Of Technology
Queries (along with sub Queries) using ANY, ALL, IN, EXISTS, NOTEXISTS, UNION, INTERSECT,
Constraints.
ANY:
ROLL NAME MARKS
select * from marks where marks = ANY (89,
2 Yash 78
78);
Output - 3 Srinu 89
ALL:
select * from marks where roll <> ALL (2, 3); ROLL NAME MARKS
Output - 1 Raj 93
4 Lakhan 68
UNION :
ROLL NAME MARKS
select * from marks where marks <80 union
select * from marks where marks > 70; 2 Yash 78
Output -
INTERSECT:
select * from marks where marks <80 intersect ROLL NAME MARKS
select * from marks where marks > 50; 1 Raj 93
Output is: roll 5,6
3 Srinu 89
ORDER BY & DESC: 2 Yash 78
select * from marks ORDER BY marks DESC;
Output- 4 Lakhan 68
DBMS Lab Manual for III B.Tech II Sem CSE & IT Raghu Institute Of Technology
SUB-QUERIES: Output -
Select * from (select * from marks ORDER BY marks DESC)
where rownum < 3; ROLL NAME MARKS
1 Raj 93
3 Srinu 89
IN: Output -
ROLL NAME MARKS
select * FROM marks where roll IN (3,4); 3 Srinu 89
4 Lakhan 68
SELECTION : Output-
ROLL NAME MARKS
select marks FROM marks where marks = 93; 1 Raj 93
EXISTS: Output-
ROLL NAME MARKS
select * FROM marks where EXISTS
4 Lakhan 68
(select marks FROM marks where marks = 68);
NOT EXISTS:
select * FROM marks where NOT EXISTS (select marks FROM marks where marks = 93);
select MAX (marks), MIN (marks) FROM marks; Select LTRIM ('** Prasad ** ', '*') FROM dual;
Output is : 93 & 68
select RTRIM (' ** Prasad is Great **', '*') FROM
GROUP BY: dual;
The GROUP BY clause can be used to summarize
rows into a group or groups of rows based on a select TRIM (both '*' from '** Prasad **') FROM
grouping function placed into the select clause. dual;
SELECT TO_CHAR (sysdate, 'mm-dd-yy') FROM SELECT ROUND (10.567, 2), TRUNC (10.567, 2)
DUAL; FROM DUAL;
SELECT GREATEST(10, 20, 30), LEAST (10, 20, 30)
least, greatest, trunc, round FROM DUAL;
The result of a query is always a table, or more precisely, a derived table. Compared with “real” tables in the
database, the result of a query is volatile, but nevertheless, the result is a table. The only thing that is missing for
the query result is a name. Essentially, a view is just a query result with a given name. Retrieving information
from a view is done in the same manner as retrieving from a table: you simply include the view in the FROM
clause of a query. With some views you can also perform DML operations on the base tables. Views don’t store
rows. Rows are always stored in tables.
Creating a View:
create view vmarks as select * from marks; ROLL NAME MARKS
Output: View Created 1 Raj 93
3 Srinu 89
Working on a View: 2 Yash 78
Check output for Vmarks 4 Lakhan 68
Select * from vmarks;
Describe Command:
Desc marks;
DBMS Lab Manual for III B.Tech II Sem CSE & IT Raghu Institute Of Technology
PL / SQL Programming
PL/SQL stands for Procedural Language Extension to SQL.
PL/SQL contains both SQL statements and standard programming constructs like variable declarations,
assignments, FOR, WHILE, IF, CASE and so on.
Output-
Enter value for reg:5
Old 6: where reg>®
New 6 : where reg=5;
The given number 5 got 90
5 90 gyan
DBMS Lab Manual for III B.Tech II Sem CSE & IT Raghu Institute Of Technology
7 96 anita
11 96 ann
17 88 suleiman
18 94 salman
INTO vmarks
FROM mrk
WHERE reg = ®
if vmarks >= 90 THEN grade := 'A' ;
elsif
vmarks >= 80 THEN grade := 'B' ;
elsif
vmarks >= 70 THEN grade := 'C' ;
elsif
vmarks >= 60 THEN grade := 'D' ;
ELSE grade := 'F';
END if;
DBMS_OUTPUT.PUT_LINE ('------------ ');
DBMS_OUTPUT.PUT_LINE (' grade is: '||grade);
END;
Output-
Enter value for reg:18
Old 8: select marks into vmarks FROM mrk WHERE reg=®
new 8: select marks into vmarks FROM mrk WHERE reg=18;
Grade is :A
PL/SQL Procedure successfully completed.
DECLARE
vmarks NUMBER ;
grade char(1);
BEGIN
SELECT marks
INTO vmarks
FROM mrk
WHERE reg = ®
CASE
WHEN vmarks >= 90 THEN grade := 'A' ;
WHEN vmarks >= 80 THEN grade := 'B' ;
WHEN vmarks >= 70 THEN grade := 'C' ;
WHEN vmarks >= 60 THEN grade := 'D' ;
ELSE grade := 'F';
DBMS Lab Manual for III B.Tech II Sem CSE & IT Raghu Institute Of Technology
END CASE;
DBMS_OUTPUT.PUT_LINE ('------------ ');
DBMS_OUTPUT.PUT_LINE (' grade is: '||grade);
END;
Output-
Enter value for reg:5
Old 8: select marks into vmarks FROM mrk WHERE reg=®
new 8: select marks into vmarks FROM mrk WHERE reg=5;
Grade is :A
PL/SQL Procedure successfully completed.
DECLARE
vmarks NUMBER ;
grade char(1);
BEGIN
SELECT marks
INTO vmarks
FROM mrk
WHERE reg = ®
grade :=
CASE
WHEN vmarks >= 90 THEN 'A'
WHEN vmarks >= 80 THEN 'B'
WHEN vmarks >= 70 THEN 'C'
WHEN vmarks >= 60 THEN 'D'
ELSE 'F'
END ;
DBMS_OUTPUT.PUT_LINE ('------------ ');
DBMS_OUTPUT.PUT_LINE (' grade is: '||grade);
END;
Output-
Enter value for reg: 17
Old 8: select marks into vmarks FROM mrk WHERE reg=®
new 8: select marks into vmarks FROM mrk WHERE reg=17;
Grade is: B
DBMS Lab Manual for III B.Tech II Sem CSE & IT Raghu Institute Of Technology
CASE
WHEN expression1 = expression2 THEN NULL
ELSE expression1
END
Program 8: NULLIF
DECLARE
n NUMBER := &sn;
vremainder NUMBER;
BEGIN
-- calculate the remainder and if it is zero return a NULL
vremainder := NULLIF(MOD(n,2),0);
DBMS_OUTPUT.PUT_LINE ('v remainder: '||vremainder);
END;
Output-
Enter value for sn:43
Old2: n number=&sn;
New2: n number=43;
Vremainder=1
DBMS Lab Manual for III B.Tech II Sem CSE & IT Raghu Institute Of Technology
FUNCTIONS
A function is similar to a procedure except that a function must return a value.
Output- SQL>@area
6/
Function Created
SQL>select area(10) from dual;
Area(10)
314
DBMS Lab Manual for III B.Tech II Sem CSE & IT Raghu Institute Of Technology
PROCEDURES
TRIGGERS
Triggers are similar to procedures or functions in that they are named PL/SQL blocks with declarative,
executable, and exception handling sections.
A trigger is executed implicitly whenever the triggering event happens, and a trigger doesn’t accept arguments.
The act of executing a trigger is known as firing the trigger. The triggering event can be a
DML (INSERT, UPDATE, or DELETE) operation
On a database table or Certain kinds of views or a system event, such as database startup or shutdown
SET SERVEROUTPUT ON
UPDATE mrk
SET marks = 0
WHERE reg = ®
OUTPUT:
Old Marks 20 are changed to New Marks 50.
Step 1: Installation of JDK: Don't forget to install JDK on your system (if not installed) because any tomcat requires the
Java 1.5 (Java 5) and Java 1.6 (Java 6) and then set the class path (environment variable) of JDK.
To
DBMS Lab Manual for III B.Tech II Sem CSE & IT Raghu Institute Of Technology
If installation process completes successfully then a window as shown below will appear.
DBMS Lab Manual for III B.Tech II Sem CSE & IT Raghu Institute Of Technology
Step 4: Add a Jar File Called ojdbc14.jar into the C:\Program Files\Java\Tomcat 6.0\lib
Then Once again restart your tomcat webserver and check whether the Server Responding properly or not.
Step 5: Place All yout JSP files in the folder C:\Program Files\Java\Tomcat 6.0\webapps by creating a New
Folder
Into the New Folder copy the Content i.e., META-INF Files and images files and index.html and index.jsp files into your
New Folder Which is Created
/*…….Update.jsp…….*/
if (updateQuery != 0) { %>
<script type="text/javascript">
alert("Data Entered Successfully");
</script>
<%
}
}
catch (Exception ex) { %>
<script type="text/javascript">
alert("invalid");
</script>
<%
}
finally {
// close all the connections.
pstatement.close();
connection.close();
}
}
}
%>
</body>
</html>
DBMS Lab Manual for III B.Tech II Sem CSE & IT Raghu Institute Of Technology
Output Screens :
DBMS Lab Manual for III B.Tech II Sem CSE & IT Raghu Institute Of Technology
DBMS Lab Manual for III B.Tech II Sem CSE & IT Raghu Institute Of Technology
Program 2: Program for Retrieving the Data From the Data Base :
/*……….display.jsp……….*/
<HTML>
<HEAD>
<TITLE>Retrieving the Data From the Data Base </TITLE>
</HEAD>
<BODY>
<H1><center> Retrieving the Data Fro the Data Base <center > </H1>
<%
Connection connection = DriverManager.getConnection
("jdbc:oracle:thin:@127.0.0.1:1521:XE", "system", "oracle");
<TH>Name </TH>
<TH>Address</TH>
<TH>Country</TH>
<TH>Hobbies</TH>
</TR>
<% while(resultset.next()){ %>
DBMS Lab Manual for III B.Tech II Sem CSE & IT Raghu Institute Of Technology
<TR>
<TD> <%= resultset.getString(1) %></td>
<TD> <%= resultset.getString(2) %></TD>
<TD> <%= resultset.getString(3) %></TD>
<TD> <%= resultset.getString(4) %></TD>
</TR>
<% } %>
</TABLE>
</BODY>
</HTML>
Output Screens :
DBMS Lab Manual for III B.Tech II Sem CSE & IT Raghu Institute Of Technology
References:
ORACLE PL/SQL by example. Benjamin Rosenzweig, Elena Silvestrova, Pearson Education 3 rd Edition
ORACLE DATA BASE 1OG PL/SQL Programming SCOTT URMAN, Tata Mc-Graw Hill.
SQL & PL/SQL for Oracle 10g, Black Book, Dr.P.S. Deshpande.