Practical File of Advanced Database Management System Lab: CSP2301 Bachelor of Engineering
Practical File of Advanced Database Management System Lab: CSP2301 Bachelor of Engineering
Practical File of Advanced Database Management System Lab: CSP2301 Bachelor of Engineering
Of
Advanced Database
Management System Lab
CSP2301
Bachelor OF ENGINEERING
(Department of Computer Science & Engineering)
CHITKARA UNIVERSITY
PUNJAB, INDIA
INDEX
clusters. THEORY:
1. INDEXES
An index is used to speed up select quires and where clauses but slows down with
update or insert when data is put into it. Creation or dropping of index does not
affect data in the table.
Tablespace: These are logical partitions having physical space used to store data
files. Syntax: CREATE INDEX index_name ON table_name (col_name)
TABLESPACE tablespace_name;
Two Type of Index:
Implicit: Created Automatically
Explicit: Created by DBA
Index Creation Involves three steps:
Naming of index.
Specify the table at which column(s) to index.
Indicate if the index is ascending or descending.
TYPES OF INDEXES
Simple Indexes
Composite Indexes
Unique Indexes
Bitmap Indexes
Function Based Indexes
Reverse key Index
1. Simple Indexes: It is used to create index on single column.
Syntax: create index index_name ON table_name(column_name);
Program: CREATE INDEX RISHABH ON TEMP(NAME);
OUTPUT:
3. Unique indexes
Syntax: create unique index index_name ON table_name (column_1);
Program: create unique index unique_idx on temp(salary);
OUTPUT:
4. Bitmap indexes: These are created on column that have low cardinality and
are used with AND or OR operators.
Syntax: create bitmap index index_name on table_name (column_1);
Program: create bitmap index bitmap_idx on temp(egender);
OUTPUT:
5. Reverse indexes
Syntax: create index index_name on table_name (column_1) reverse;
Program: create index reverse_idx on temp(egender) reverse;
OUTPUT:
6. Index by function
Syntax: create index index_name on table_name ((function)column_1);
Program: create index function_idx on temp(upper(ename));
OUTPUT:
DROP INDEX
Syntax: drop index index_name
Program: drop index drop_idx;
OUTPUT:
REBUILD INDEX
Syntax: alter index index_name rebuild;
OUTPUT:
Drop view
Syntax: drop view view_name;
Output:
Programs:
Create a view from emp table consisting of employee no, name, commission
and salary increased with 20% of existing salary into the view?
Syntax: create view simpleview as select empno, ename, comm,(sal+sal*0.20) as
salary from emp
Output:
Create a view that contains deptname, min, sal, max salary and avg. salary
keeping the name as comp_view?
Syntax: create or replace view comp_view(ename,maxsal,minsal,avgsal) as
select d.dname,max(e.sal),min(e.sal),avg(e.sal) from emp e join dept d on
d.deptno=e.deptno group by d.dname
select * from comp_view
Output:
Create a view named as empvu20 that contains all the details of belonging to
deptno 20 but it should not support any dml operation while inserting data of
any employee other than any employee other than 20?
Syntax: create or replace view empvu20 as select * from emp where deptno=20
with check option constraint const_chk.
Output:
3. SEQUENCE
A sequence is a set of integers that are generated in order on demand.
They are used in database many applications acquire row to contain a unique
value and sequence provide easy way to generate. It generates Number in equal
and application use them no. where they require unique value such as primary key
value.
Features:
Available to all users.
Created using SQL statements.
Have Min. and Max. values.
They can be dropped but not reset.
Sequences are incremented by amount specified which created.
NEXTVALUE
Returns the next value of sequences and returns a unique value of the
sequences.
Output:
CURRVAL
Obtains the current value of sequences.
Output:
OUTPUT:
Program:
Create a sequence that start with 150 and grows up to 550 with cache
memory of 10 & a cycle?
Syntax: Create sequences seq2 start with 150 incremented by 1 maxvalue 550
cache 10 cycle.
4. CLUSTERS
Cluster is used for improving Oracle performance. They are used to store data from
different table in same physical data block. They are useful in case when record
from a table are frequently queried together. Every cluster stores data as well as
maintain index to sort the data.
Cluster Key: Column within cluster are called cluster key in foreign key of one
table that references primary key of another table.
TYPES OF CLUSTER
INDEX CLUSTER:
In an indexed cluster, Oracle Database stores together rows having the same cluster
key value. Each distinct cluster key value is stored only once in each data block,
regardless of the number of tables and rows in which it occurs. If you specify neither
INDEX nor HASHKEYS, then Oracle Database creates an indexed cluster by default.
Syntax: create cluster cluster_name(column_name type);
Example:
create cluster personall(deptno number(2))
create index c_idx on cluster personall
create table dept01 cluster personall(deptno) as select * from emp where
deptno=10;
OUTPUT:
AIM: Introduction to PL/SQL concepts, its features and shows how PL/SQL meets the
challenges of database programming, and how you can reuse techniques that you know
from other programming languages.
Features:
Declarations Section:
This section starts with the keyword DECLARE. It is an optional section and
defines all variables, cursors, subprograms, and other elements to be used in the
program.
Executable Commands
This section is enclosed between the keywords BEGIN and END and it is a mandatory
section. It consists of the executable PL/SQL statements of the program. It should have at
least one executable line of code, which may be just a NULL command to indicate that
nothing should be executed.
Exception Handling
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling>
END;
LOG File
1. Use of Exceptions.
2. Declaring Variables.
3. Input variables from user.
4. Declaring functions.
5. Loops can be used.
Advantages:
Error Handling: While execution of a PL/SQL program, it effectively handles all the errors
and gives user-friendly error messages to resolve the issues.
Block Structures: PL/SQL contains code blocks that can be nested into each other. Every
block forms a logical module that can be stored in database and can be reused later.
PLSQL Identifiers:
1. Declare variable
2. To Display Variables
Dbms_output.putline(a);
3. Comments
Single line comment: _Hello_
Multiple line comment: /* Hello
Hi */
4. Datatype
Number
Fixed-point or floating-point number with absolute value in range 1E-130
to (but not including) 1.0E126. A NUMBER variable can also represent.
Float
ANSI and IBM specific floating-point type with maximum precision of 126
binary digits (approximately 38 decimal digits)
Decimal
IBM specific fixed-point type with maximum precision of 38 decimal digits
Character
Alphanumeric values that represent single characters or strings of
characters.
Long
Variable-length character string with maximum size of 32,760 bytes
Date
The DATE datatype is used to store fixed-length datetimes, which include
the time of day in seconds since midnight. Valid dates range from January 1,
4712 BC to December 31, 9999 AD.
Time
Used to store time.
LOB
Large Object (LOB) data types refer to large data items such as text,
graphic images, video clips, and sound waveforms. LOB data types allow
efficient, random, piecewise access to this data.
Problems:
1. Area of circle
2. Calculator
AIM: How to structure the flow of control through a PL/SQL program. Connect the
statements by simple but powerful control structures that have a single entry and exit point.
Collectively, these structures can handle any situation. Their proper use should lead
naturally to a well-structured program.
Conditional Control
IF-THEN-ELSIF
The IF-THEN-ELSIF statement allows you to choose between several alternatives. An IF-
THEN statement can be followed by an optional ELSIF...ELSE statement. The ELSIF clause
lets you
add additional conditions.
When using IF-THEN-ELSIF statements there are a few points to keep in mind.
An IF-THEN statement can have zero or one ELSE's and it must come after any ELSIF's.
An IF-THEN statement can have zero to many ELSIF's and they must come before
the ELSE.
Once an ELSIF succeeds, none of the remaining ELSIF's or ELSE's will be tested.
Syntax:
IF (boolean_expression 1) THEN
S1; // Executes when the boolean expression 1 is true
ELSIF ( boolean_expression 2) THEN
S2; // Executes when the boolean expression 2 is true
ELSIF ( boolean_expression 3) THEN
S3; // Executes when the boolean expression 3 is true
ELSE
S4; // executes when the none of the above condition is true
END IF;
CASE expression:
Like the IF statement, the CASE statement selects one sequence of statements to
execute. However, to select the sequence, the CASE statement uses a selector
rather than multiple Boolean expressions. A selector is an expression, the value of
which is used to select one of several alternatives.
Syntax:
CASE selector
WHEN 'value1' THEN
S1; WHEN 'value2'
THEN S2; WHEN
'value3' THEN S3;
...
ELSE; -- default
case END CASE;
Example:
Iterative Control
1. While Loop:
A WHILE LOOP statement in PL/SQL programming language repeatedly executes a target
statement as long as a given condition is true.
Syntax:
WHILE condition LOOP
sequence_of_statements
END LOOP;
2. Basic Loop
Basic loop structure encloses sequence of statements in between the LOOP and END
LOOP statements. With each iteration, the sequence of statements is executed and
then control resumes at the top of the loop.
Syntax:
LOOP
Sequence of statements;
END LOOP;
3. For Loop
A FOR LOOP is a repetition control structure that allows you to efficiently write a
loop that needs to execute a specific number of times.
Syntax:
FOR counter IN initial_value .. final_value LOOP
sequence_of_statements;
END LOOP;
Sequential Control
1. GOTO
A GOTO statement in PL/SQL programming language provides an unconditional jump
from the GOTO to a labeled statement in the same subprogram.
Syntax:
GOTO label;
..
..
<< label >>
statement;
2. EXIT
The EXIT statement in PL/SQL programming language has the following two usages
−1. When the EXIT statement is encountered inside a loop, the loop is
immediately terminated and the program control resumes at the next statement
following the loop.
2. If you are using nested loops (i.e., one loop inside another loop), the EXIT
statement will stop the execution of the innermost loop and start executing the next
line of code after the block.
Syntax:
EXIT;
Program:
Aim: With PL/SQL mechanism called exception handling, design "bulletproof” program so
that it can continue operating in the presence of errors. (System defined, user-defined)
Introduction
An error occurs during the program execution is called Exception in PL/SQL.
PL/SQL allows to catch such conditions using exception block in the program and an
appropriate action is taken against the error condition. There are two type of exceptions:
User Syntax:
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling goes here >
WHEN exception1 THEN
exception1-handling-statements
WHEN exception2 THEN
exception2-handling-statements
WHEN exception3 THEN
exception3-handling-statements
........
WHEN others THEN
exception3-handling-statements
END;
System defined exception in PL/SQL are:
Programs:
Q. Make a program takes two input and shows the divide, but it should and shows the
divide, but the divisor is either 1 or 0.
Output:
Q. Generate a user defined invalid is exception for the customers_id that ranges less
than or equale to 0. The code must fetch name of the employee that matches with the id
of customers defined in the program also use predefined exception by oracle engine of
no data found.
Output:
Experiment No.-5
AIM: To use PL/SQL to Raise application error and design Pragma init exceptions.
Error_number: Any valid oracle number (It is always negative integer in the
range - 20000 ... -20999).
Any valid oracle number (It is always negative)
Exception_name: A user defined exception declared.
User Defined exception declared
Pragma: Statement which is compiler directed.
SQLERRM: The function SQLERRM returns the error message associated with its
error- number argument. If the argument is omitted, it returns the error message
associated with the current value of SQLCODE. SQLERRM with no argument is
useful only in an exception handler. Outside a handler, SQLERRM with no argument
always returns
the normal, successful completion message. For internal exceptions, SQLERRM
returns the message associated with the Oracle error that occurred. The message
begins with the Oracle error code.
SQL Code: When an error occurs SQL code returns the error no. he SQLCODE option
holds the value returned by the Oracle RDBMS after the most recently attempted
SQL operation.
Syntax:
DECLARE
deadlock_detected EXCEPTION;
PRAGMA EXCEPTION_INIT (deadlock_detected,
-60); BEGIN
NULL; -- Some operation that causes an ORA-00060
error EXCEPTION
WHEN deadlock_detected THEN
NULL; -- handle the error
END;
Programs:
Q. Find out whether user is allowed to vote by inputting age, if above 18 age, a person
can vote otherwise not?
Output: