DBMS Lab Manual
DBMS Lab Manual
DBMS Lab Manual
Prepared By
Sri V Prasad
Associate Professor
Dept. Of Computer Science & Engg.
S.No
Content
Lab Objective
Introduction to DBMS
Introduction to SQL
6
Introduction to Class Marks Management Systems
(a) DDL & DML Statements(Table Creations for CMM)
(b) Importance of Dual Command
(c) Queries and Sub Queries Generation Using
7
(i)
(ii)
(iii)
(iv)
(v)
Relational functions
Aggregate Functions
Conversion Functions
String Functions
Date Functions
10
Functions Implementation
11
Procedure Implementation
12
Page No.
INDEX
13
13
14
Reference Books
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.
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.
Some sample applications, which may be programmed, are given below:
1. Class marks management.
2. Accounting package for a shop.
3. Database manager for a Magazine agency or a newspaper agency.
4. Ticket booking for performances.
5. Preparing greeting cards & birthday cards.
6. Personal accounts - Insurance, loans, mortgage payments, etc.
7. Doctor's diary& billing system.
8. Personal bank account.
9. Hostel accounting.
10. Video Tape library.
11. History of cricket scores.
12. Cable TV transmission program manager.
13. Personal library.
2.
3.
4.
5.
6.
Application development
Data analysis
Concurrency and robustness
Efficiency
Security
Applications of DBMS
1. Data indepence
2. Efficient data access
3. Data integrity
4. Data security
5. Data admistration
6. Concurrent access
7. Crash recovery
8. Reduced application development
Examples of DBMS are:
1. Banking
2. Finance
3. Human resources
4. Tele communication
5. Universities
6. Airlines
7. Online ticket booking
8. In e-seva .
Oracle Applications Release 11i (aka Oracle eBusiness Suite, Oracle Financials or Oracle 11i):
a suite of business applications;
Oracle
JDeveloper 10g:
development environment;
a Java integrated
http://www.oracle.com/technology/productias/
index.html.
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 .
Class marks management system is an entity-relationship model based database
management project. We use Oracle 10g to implement this project. It has been designed to maintain
and manage the database of the marks and other information of the students in a class. In this
database we maintain the information of the students, the faculty who teach these students and the
subjects they handle along with the department information.
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.
the
attributes-subid(subjects
id)
fname(facultys
name),
-Marks entity: It has the attributes-sid, subid and marks (marks of each student in each
subject).
-Faculty entity:It has the attributes-fid(facultys id), fname(faculty;s name), fsubject(subject
taught by that faculty).s
-Department entity: It has the attributes-did (departments id), dname(departments name),
dhod(departments head).
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.
-studies: This relationship is between the student and department entities.
-monitors: This relationship is an aggregation relationship between the relationship set that
consists of faculty and department and the entity student.
We use various key constraints in these entities, such as:
-primary key: This key uniquely identifies a tuple. The primary keys used in the defined entities
are:->Sid in student entity
->subid in subject entity
SID
ROLL
NAME
DEPTNO
DEPTNAME
EMPNO
AGE
EMPNAME
SID
ROLL
DEPTNO
MARKS
SID
ROLL
NAME
AGE
ADDRESS
RENAME :
rename student to student1;
Name */
TRUNCATE :
Truncate student;
Removed*/
or
SQL> @ path\filename
ROLL
NAME
yash
raj
Save xyz.txt
Srinu
Lakhan
2 RUN
2 RUN
Get xyz.txt
DML Statements :
insert into student values(1001,1,'raj',78);
insert into student values(1002,2,'Yash',67);
insert into student values(1003,3,'Srinu',84);
insert into student values(1004,4,'Lakhan',97);
Inserting Values at the run time :
SID
ROLL
NAME
MARKS
1002
yash
20
1001
raj
19
1003
Srinu
22
1004
Lakhan
23
SID
ROLL
NAME
AGE
1002
Yash
19
1001
Raj
20
1003
Srinu
22
1004
Lakhan
23
DUAL
Its a Single column /single table to evaluate constant expression in a select statement. (Contains
Dummy Value)
select * from dual;
Output : X
Output : 5
ROLL
NAME
MARKS
ROLL
1
2
3
4
NAME
Raj
Yash
Srinu
Lakhan
MARKS
93
78
89
68
ANY:
select * from marks where marks = ANY (89,
78);
Output -
ROLL
2
3
NAME
Yash
Srinu
MARKS
78
89
ROLL
1
4
NAME
Raj
Lakhan
MARKS
93
68
ROLL
2
NAME
Yash
MARKS
78
ALL:
select * from marks where roll <> ALL (2, 3);
Output -
UNION :
select * from marks where marks <80 union
select * from marks where marks > 70;
Output -
INTERSECT:
select * from marks where marks <80 intersect
select * from marks where marks > 50;
Output is: roll 5,6
ORDER BY & DESC:
select * from marks ORDER BY marks DESC;
Output-
ROLL
NAME
MARKS
Raj
93
Srinu
89
Yash
78
Lakhan
68
SUB-QUERIES:
Output Select * from (select * from marks ORDER BY marks DESC)
where rownum < 3;
ROLL
NAME
1
Raj
3
Srinu
IN:
select * FROM marks where roll IN (3,4);
Output ROLL
3
4
NOT IN:
select * FROM marks where roll NOT IN
(2,3,4);
ROLL
1
MARKS
89
68
NAME
Raj
MARKS
93
NAME
Raj
MARKS
93
OutputROLL
1
EXISTS:
select * FROM marks where EXISTS
(select marks FROM marks where marks = 68);
NAME
Srinu
Lakhan
Output-
SELECTION :
select marks FROM marks where marks = 93;
MARKS
93
89
OutputROLL
4
NAME
Lakhan
MARKS
68
NOT EXISTS:
select * FROM marks where NOT EXISTS (select marks FROM marks where marks = 93);
Output : No Data will be available
Aggregate functions
(COUNT, SUM, AVG, MAX and MIN), GROUP BY,
HAVING .
COUNT:
select COUNT (marks) FROM marks;
Output - 4
select COUNT (marks) FROM marks where marks
>70;
Output -3
SUM:
select SUM (marks) FROM marks;
Output -328
select SUM (marks) FROM marks where marks>70;
Output -260
AVG:
select AVG (marks) FROM marks;
Output is : 82.0
select AVG (marks) FROM marks where marks>90;
Output is: 93
MAX:
select MAX(marks) from marks;
Output is : 93
MIN:
select MIN(marks) from marks;
Output is: 68
select MAX (marks), MIN (marks) FROM marks;
Output is : 93 & 68
GROUP BY:
The GROUP BY clause can be used to summarize
rows into a group or groups of rows based on a
grouping function placed into the select clause.
HAVING:
The HAVING clause can then be used to filter out
unwanted groups much like the where clause.
The expression for the select statement must
include at least one grouping function such as MAX()
or COUNT().
You use the GROUP BY clause to group rows into
blocks with a common column value
GROUP BY:
Select sdept, max(sbudget) from student3 group by
sdept;
Output is : Dpet name with Highest Budget each
HAVING:
select sdept,count(sname) from student3 group by
sdept having count(sname)>2;
Output : ECE with 3
Conversion Functions
String Functions
Date Functions
To char . To Date Functions
Least , Great ,Truncate & Round Functions
to char, to date
SELECT TO_DATE ('2-1-10', 'mm-dd-yy') FROM
DUAL;
ROLL
1
3
2
4
NAME
Raj
Srinu
Yash
Lakhan
MARKS
93
89
78
68
Distinct Command :
All Varieties of Categories can be seen
select distinct name from marks ;
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.
Program 1: General Pl/SQL Program
DECLARE
Cmarks NUMBER;
Creg NUMBER;
BEGIN
SELECT marks,reg into cmarks,creg from student WHERE reg=®
DBMS_OUTPUT.LINE_LINE (----------------------------);
DBMS_OUTPUT.LINE_LINE(the given number||creg||got||cmarks|| );
EXCEPTION
WHEN no_data_found THEN
DBMS_OUTPUT.PUT_LINE(no such reg number);
END;
OutputEnter value for reg:5
Old 6: where reg>®
New 6 : where reg=5;
The given number 5 got 90
Program 2: CURSOR REPRESENTATION
DECLARE
Cmark mrk%rowtype;
Cursor xyz is
SELECT * from mark where marks>&marks;
BEGIN
Open xyz;
LOOP
Exit WHEN xyz%notfound;
Fetch xyz into cmark;
DBMS_OUTPUT.PUT_LINE(CMARK.REG||||CMARK.NAME||||CMARK.MARK);
END LOOP;
END;
Output- Enter value for marks:85
Old 4: where mark>&mark;
New 4: where mark>85;
5
90 gyan
7
11
17
18
96
96
88
94
anita
ann
suleiman
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;
OutputEnter 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.
END CASE;
DBMS_OUTPUT.PUT_LINE ('------------ ');
DBMS_OUTPUT.PUT_LINE (' grade is: '||grade);
END;
OutputEnter 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.
FUNCTIONS
A function is similar to a procedure except that a function must return a value.
PROCEDURES
Program for Representing a procedure without parameters:
Create or replace procedure PR1 as
Begin
DBMS_OUTPUT.PUT_LINE(Hai);
END;
Output- SQL>@PR1
3/
Procedure created
SQL>Execute PR1
Hai
PL/SQL Procedure successfully completed.
TRIGGERS
Triggers are similar to procedures or functions in that they are named PL/SQL blocks with declarative,
executable, and exception handling sections.
Triggers must be stored as stand-alone objects in the database.
A trigger is executed implicitly whenever the triggering event happens, and a trigger doesnt 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
CREATE OR REPLACE TRIGGER trigmrk
AFTER UPDATE OF marks ON mrk
FOR EACH ROW
WHEN (OLD.marks != NEW.marks)
BEGIN
DBMS_OUTPUT.PUT_LINE('marks '
||:OLD.marks
||' have been change to '
||:NEW.marks);
END;
SET SERVEROUTPUT ON
UPDATE mrk
SET marks = 0
WHERE reg = ®
OUTPUT:
Old Marks 20 are changed to New Marks 50.
A Small Alert is Given when the Updation in the Table is given
To
If installation process completes successfully then a window as shown below will appear.
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
Step 6: Then Start Executing ur program in the browser by http://localhost:8080/it/reg.jsp
Program 1: Program For Entering Data into the Data Base :
/*.Update.jsp.*/
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"
<HTML>
<script type="text/javascript" src="JScript/script.js"></script>
<body id="reg">
<FORM name="form">
<h1><center> DBMS Lab -- Importance of Data Base as Back Ground </center> </h1><br><br><br>
<table align="center" border="2">
<tr>
<td>Name Of the Candidate :</td>
<TD><INPUT TYPE="text" name="name" class="reqd name"></td> </tr> <tr>
<td>Password Chosen :</td>
<td> <INPUT TYPE="PASSWORD" name="passwd" class="reqd passwd"> </td></tr>
<tr><td>Email ID:</td>
<td> <INPUT TYPE="TEXT" name="EMAIL" class="reqd email"> </td> </tr></tr>
<td>Phone Number :</td>
<td><INPUT TYPE="TEXT" name="PNO" class="reqd pno"> </td></tr>
<tr>
<td align="right"><input type="submit" id="submit"></td>
<td align="center"><input type="reset"></td>
</tr>
</table>
</FORM>
<%
String name = request.getParameter("name");
String pass = request.getParameter("passwd");
String email = request.getParameter("EMAIL");
String pno = request.getParameter("PNO");
/* Create string of connection url within specified format with machine name, port number and database name. Here
machine name id localhost and database name is logindata. */
String connectionURL = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
// declare a connection by using Connection interface
Connection connection = null;
// declare object of Statement interface that uses for
PreparedStatement pstatement = null;
Program 2: Program for Retrieving the Data From the Data Base :
/*.display.jsp.*/
<%@ page import="java.sql.*" %>
<% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); %>
<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");
Statement statement = connection.createStatement() ;
ResultSet resultset =
statement.executeQuery("select * from reg11") ;
%>
<br><br>
<TABLE BORDER="1" align="center">
<TR>
<TH>Name </TH>
<TH>Address</TH>
<TH>Country</TH>
<TH>Hobbies</TH>
</TR>
<% while(resultset.next()){ %>
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.