Data Base Management System Lab Manual
Data Base Management System Lab Manual
CONTENTS
1. Creating tables for various relations.
3. SQL Reporting.
4. Creating views.
5. Introduction to pl/sql
Implementing operations on relations.
6. Writing Cursors.
7. Exception handling.
8. Writing function.
9. Writing procedure
1
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
We imagine that each field was an object created for us by oracle. Then
number of fields created in same horizontal plane would be another object
created for us by oracle. Multiple fields placed in the same horizontal plane
is an object called a Record by oracle. Several ‘Records’, of equal length,
placed one below the other to enable users to continue to store data is
called a ’Table’(consists of rows and columns used for storing data).
The table therefore becomes the third object after fields and rows.
2
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
Introduction to SQL
A table is primary database object of SQL ,which is used to store the data in
the form of rows and columns. SQL developed by IBM is used as standard
language to access data from database. In order to communicate with
database SQL has been divided into three sub languages and each sub
language consists of different commands, they are
TCL COMMANDS:
COMMIT
ROLLBACK
SAVEPOINT
3
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
EXERCISE -1
DATA TYPES
A data type associates a fixed set of properties with the values that can
be used in a column of a table or in an argument of a procedure or function.
These properties cause oracle to treat values of one data type different from
values of another data type.
CHARACTER DATATYPES
Char(size)
Fixed-length character data of length size bytes(maximum 2000 bytes
per row ,default size is 1 byte per row).
Varchar2(size)
variable length character data.
NUMBER DATATYPE
Number(p,s)
4
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
DATE DATATYPE
Date
Fixed length date and time date.
The standard format is dd-mon-yy.
To enter dates other than standard format ,use the appropriate
functions.
LONG DATATYPE
Long
Like the oracle internal data types that are already present the user
can create new data type.
5
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
Column name
cccccccol data type size
empname varchar2 20
emp no. varchar2 6
Address varchar 30
Pin code number 6
Salary number 10,2
6
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
CONSTRAINTS
Integrity constraints:
An integrity constraint is the rule that restricts the values for one or
more columns in a table.
NOTE: This syntax can define any type of integrity constraint except a NOT
NULL constraint.
7
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
Syntax:
create table <table_name>
(column_name datatype Not Null);
Check constraint
Syntax:
create table <table_name>
(column_name datatype check <condition>);
Unique_constrain
syntax:
Primary key
syntax:
Note: The DISABLE option oracle to define the constraint but not enforce
it.
8
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
Syntax:
4. Foreign key
On Delete Cascade
Syntax:
9
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
I DEPARTMENT:
DCODE number
DNAME varchar2
II EMPLOYEE:
ECODE number
ENAME varchar2
GCODE varchar2
DOB date
DOJ date
DCODE number
SEX char
BASIC number
REPORTTO number (Same as ecode)
III PROGRAME:
PCODE varchar2
ECODE number
IV PROGSEC:
PCODE varchar2
PNAME varchar2
DURATION number
DESCRIPTION varchar2
V GRADE:
GCODE varchar2
GNAME varchar2
MAXBASIC number
MINBASIC number
RAISE number
10
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
Exercise -2
SELECT colum1,colum2[,column3,....]
FROM table1[,table2,....]
OPTIONAL CLAUSES:
this clause is used to give some condition which can be imposed on selected
columns.
GROUP BY column1,column2
11
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
HAVING condition.
PSEUDO COLUMNS:
NUMBER FUNCTIONS :
ABS,POWER,ROUND,SQRT.
STRING FUNCTIONS:
DATE FUNCTIONS :
12
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
CONVERSION FUNCTIONS:
TO_CHAR, TO_DATE, TO_NUMBER
Exercise-2(A)
1. Insert values into your table for specific columns.
2. Insert values into your table for all columns.
3. Insert values into one table using other table.
Exercise-2(B)
Exercise-2(C)
1. Update a single column value in a table.
2. Update more than one column value in a table.
Exercise-2(D)
13
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
JOINS
Introduction to Joins different or same tables(is done using concept
called JOINS):
1. NORMAL JOIN
CARTISIAN PRODUCT
SELECT *
FROM EMP,DEPT
SELECT *
FROM EMP, DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO
SELECT *
FROM EMP, DEPT
WHERE EMP.DEPTNO<>DEPT.DEPTNO
3. SELF JOIN
SELECT EMPNO,MGR
FROM EMP A, EMP B
WHERE A.EMPNO=B.MGR
14
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
4. OUTER JOIN
SELECT EMPNO,ENAME
FROM EMP, DEPT
WHERE EMP.DEPTNO(+)=DEPT.DEPTNO
SELECT EMPNO,ENAME
FROM EMP, DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO(+)
lpad(string,total no of characters,format)
3. Get list of all employees in a particular age group. Specify the age
group like 22-30 yrs (hint:months_between).
4. Get the list of all employees whose first letters of the names have been
specified.
15
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
Exerice:3
SQL REPORTING:
EXAMPLE:
Q: To get the list of all employees in their of order of employee names
Output:
Employees
Name
...
...
CMRCET
16
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
Generate SQL Report for the following queries using above sql report
commands
Employee Department
Name Name
17
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
Exercise -4
Example:
create view dsk as select empno eno from emp;
UPDATABLE VIEWS:
18
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
Exercise -5
Introduction to PL/SQL:
DATA TYPES
1. Number
Float
Integer
2. character type
char
varchar2
3. Boolean
4. Date
Declare
<All declarations> (%TYPE,%ROWTYPE)
Begin
<Executable statements>
Exception
<Exception handling>
End;
19
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
Control Structures:
1. If <condition> then
<statements>
End if;
2. If <condition> then
<statements>
elsif <condition> then
<statements>
else
<statements>
End if;
Loops:
1. GOTO:
<<lablename>>
<statements>
go to lablename;
2. Simple loop:
loop
<statements>
exit <condition>
end loop
3. For loop:
for <counter> in <startvalue> .. <end value>
loop
<statements>
end loop
4. While loop:
While <condition>
loop
<statements>
end loop;
20
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
3. To count the words, vowels, consonants and length of the given string
(Hint: substr).
21
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
Exercise –6
Exceptions
1. dup_val_on_index:
2. Invalid_number
3. Login_denined
4. No_data_found
5. Not_logged_on
6. Program_error
7. Zero_divide
Ect..
22
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
USER_DEFINED EXCEPTION
<exception_name> exception;
Raise<exception_name>;
23
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
Exercise –7
CURSORS
IMPLICIT CURSOR:
Is the cursor which is always generated
Implicitly by the oracle. We can use the Implicit cursor as SQL ,which
is its name.
EXPLICIT CURSORS
These cursors are always defined by the user in PL/SQL block with
the following syntax.
DECLARE
CURSOR <Cursorname> IS <Select statement>;
BEGIN
< Select statements>;
END;
PARAMETRIC CURSORS:
24
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
CURSOR ATTRIBUTES:
%FOUND
%NOTFOUND
%ISOPEN
%ROWCOUNT
Using the above attributes we can Know the different states of cursor.
CURRENT OF <cursorname>
25
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
EXERCISE
26
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
Exercise-8
SUBPROGRAMS
FUNCTION
Example:
Create or replace function add(a number, b number)return number is
C number;
Begin
C:=a+b;
Return C;
End;
Exercise:
27
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
Exercise-9
PROCEDURE
MODES OF PARAMETER:
literal,expression,variable
Example:
Exercise:
28
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
Exercise -10
PACKAGE:
PACKAGE SPECIFICATION:
create or replace package <packagename> as
variable that you want to include in this package;
cursors that are common for the entire package;
function prototype declaration of a function with return type;
procedure prototype declaration of a procedure;
end <packagename>
PACKAGE BODY:
create or replace package body <packagename> as body of a function ;
(by discarding create or replace as in ordinary function);
body of a procedure ;
(by discarding create or replace as in ordinary procedure);
end <packagename>
29
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
Exercise:
30
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
Exercise-11
TRIGGER
Before
After
For each row
For each statement(default)
Syntax:
create or replace trigger <triggername>
{before/after/instead of}
{insert/delete/update[of column1[,column2..]]} on
<table name/view name > [for each statement/
row] [when <condition>]
DECLARE
<declarations>
BEGIN
<executable statements>
END;
31
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
Note:
1) A Database trigger can also have declarative and exception
Handling parts.
2) A sub query cannot be included in when clause.
Enable/Disable:
EXERCISE
32
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
Exercise-12
FORMS
Developer 2000 Forms is used to develop Client/Server applications
by which can be used to enter, access, change or delete data from oracle
database in A form based environment.
Form
Menu
Collection of Objects such as menu items, submenus, submenu
items And PL/SQL Code blocks.
PL/SQL Libraries
Collection of PL/SQL functions and procedures stored in a single Library
file. This library file is then attached to a form/menu.
Item
These are objects contained in blocks, that present data values to the user
depending upon the item type(text, checkbox, list ,radio group, button...).
Block
A block is group of related items. They can be span over many canvases.
33
DBMS Department of Computer Science
Data Base Management Systems Laboratory Manual
Canvasview
Window
window is similar to an empty picture frame, which can hold the
canavasview and allows to resize, move the canvaseview.
Trigger
A forms object it associates a Pl/SQL block with an event.
REPORTS
Exercise
5. Generate a master details report with the department name and total
no.of employees and total salary of that department.
34