Sie sind auf Seite 1von 13

Manipal

BCA REVAMPED PROGRAMME


IV SEMESTER
ASSIGNMENT
Name

VELMURUGAN C
______________________________________________________________

Registration No.

531210112
______________________________________________________________

Learning Center

KUWAIT EDUCATIONAL CENTER


______________________________________________________________

Learning Center Code

2527
______________________________________________________________

Course/Program

BCA
______________________________________________________________

Semester

IV Semester
______________________________________________________________

Subject Code

BC0050
______________________________________________________________

Subject Title

ORACLE AND DISTRIBUTED DATABASES


______________________________________________________________

Date of Submission

26.02.2014
______________________________________________________________

Marks Awarded

:
______________________________________________________________

Average marks of both assignments

_________________________________________________

_______________________________________________

Signature of Center Coordinator

Signature of Evaluator

Directorate of Distance Education


Sikkim Manipal University
II Floor, Syndicate House, Manipal 576 104

SMU

_________________________________________

Sikkim Manipal University

Directorate of Distance Education

Question 1: Give the structure of PL/SQL Blocks and explain


Ans.:
PL/SQL is a block-structured language. Each block builds a (named)
program unit, and blocks can be nested. Blocks that build a procedure, a
function, or a package must be named. A PL/SQL block has an optional
declare section, a part containing PL/SQL statements, and an optional
exception-handling part. Thus the structure of a PL/SQL looks as follows
(brackets [ ] enclose optional parts):

[<Block header>]
[declare
<Constants>
<Variables>
<Cursors>
<User defined exceptions>]
begin
<PL/SQL statements>
[exception
<Exception handling>]
end;

The block header specifies whether the PL/SQL block is a procedure, a


function, or a package. If no header is specified, the block is said to be an
anonymous PL/SQL block. Each PL/SQL block again builds a PL/SQL
statement. Thus blocks can be nested like blocks in conventional
programming languages. The scope of declared variables (i.e., the part of
the program in which one can refer to the variable) is analogous to the
scope of variables in programming languages such as C or Pascal.
Declarations:
Constants, variables, cursors, and exceptions used in a PL/SQL block must
be declared in the declare section of that block. Variables and constants
can be declared as follows:
<variable name> [constant] <data type> [not null] [:= <expression>];
Valid data types are SQL data types and the data type boolean. Boolean
data may only be true, false, or null. The not null clause requires that the

declared variable must always have a value different from null.


<expression> is used to initialize a variable. If no expression is specified,
the value null is assigned to the variable. The clause constant states that
once a value has been assigned to the variable, the value cannot be
changed. Example:
declare hire_date date; /* implicit initialization with null */
job_title varchar2(80) := Salesman;
emp_found boolean; /* implicit initialization with null */
salary_incr constant number(3,2) := 1.5; /* constant */
...
begin . . . end;

Instead of specifying a data type, one can also refer to the data type of a
table column. For example, EMP.Empno%TYPE refers to the data type of
the column Empno in the relation EMP. Instead of a single variable, a
record can be declared that can store a complete tuple from a given table
(or query result). For example, the data type DEPT%ROWTYPE specifies a
record suitable to store all attribute values of a complete row from the
table DEPT. Such records are typically used in combination with a cursor. A
field in a record can be accessed using <record name>.<column name>,
for example, DEPT.Deptno. A cursor declaration specifies a set of tuples
(as a query result) such that the tuples can be processed in a tupleoriented way (i.e., one tuple at a time) using the fetch statement.

Question 2: Write a note on Database triggers


Ans.:
PL/SQL can be used to write data base triggers. Triggers are used to define
code that is executed / fired when certain actions or event occur. At the
data base level, triggers can be defined for events such as inserting a
record into a table, deleting a record, and updating a record.
The trigger definition consists of following basic parts.

The events that fires the trigger


The database table on which event must occur
An optional condition controlling when the trigger is executed
A PL/SQL block containing the code to be executed when the trigger
is fired.

A trigger is a data base object, like a table or an index. When you define a
trigger, it becomes a part of the database and it is always is executed
when the event for which it is defined occurs.

Syntax for creating a data base trigger is shown below.


CREATE [ or REPLACE ] TRIGGER trigger-name
{ BEFORE | AFTER } verb-list ON table-name
[ FOR EACH ROW [ WHEN condition ] ]
DECLARE
Declarations
BEGIN
PL/SQL code
END;

In the above syntax


REPLACE

Is used to recreate if trigger already exists.

trigger-name Is the name of the trigger to be created.


verb-list

The SQL verbs that fire the Create, i.e. it may be INSERT,
UPDATE or DELETE.

table-name

The table on which the trigger is defined.

Condition

An optional condition placed on the execution of the


triggers.

declarations. Consists of any variable, record or cursor declarations


needed by this PL/SQL blocks.
PL/SQL code PL/SQL code that gets executed when the trigger fires.

Question 3: Explain the following a) Parameter file b) Archive files


Ans.:
PARAMETER FILE:
The parameter file is a file which contains a number of parameters that
can be used to configure the Oracle9i instance. There are two kinds of
parameter files available. The first one is called the Server parameter file
(spfile), which allows you to specify the values to parameters that can be
enforced without bouncing the database as well as after re-starting the
database. These are called persistent parameters. The second kind of file
is called Parameter file (pfile) which has been around for a while now. It
holds parameters and their values however any change to a parameter's
values requires you to bounce or restart the database.

ARCHIVE FILE:
These are files that can be created by a process known as archiving. When
a database operates in archive log mode, the contents of the redo log file
are transferred to an offline file known as the archive file. This file is very
useful in the event of a failure and is used for recovery purposes. Usually
complete recovery up to the point of failure is possible if all archive files
are available. Archive files can be created on disk and transferred to tape
to create space for new archive files.

Question 4: What are the uses of Distributed Databases?


Ans.:
There are several reasons why distributed databases are developed. The
following is a list of the main motivations.

Organizational and economic reasons


Usage and interconnection of existing databases
Incremental growth of an organization
Reduced communication overhead
Performance aspects
Increased reliability and availability

Organizational and economic reasons: Many organizations are


decentralized, and a distributed database approach fits more naturally the
structure of the organization.
The organizational and economic
motivations are probably the most important reason for developing
distributed databases.
Interconnection of existing databases: Distributed databases are the
natural solution when several databases already exist in an organization
and the necessity of performing global applications arises.
Incremental growth: If an organization grows by adding new, relatively
autonomous organizational units (new branches, new warehouses, etc.),
then the distributed database approach supports a smooth incremental
growth with a minimum degree of impact on the already existing units.
Reduced communication overhead: In a geographically distributed
database like the database of Example 1.1, the fact that many

applications are local clearly reduces the communication overhead with


respect to a centralized database.
Performance considerations: The existence of several autonomous
processors results in the increase of performance through a high degree of
parallelism. This consideration can be applied to any multiprocessor
system, and not only to distributed databases.
Reliability and availability: The distributed database approach,
especially with redundant data, can be used also in order to obtain higher
reliability and availability.

Question 5: Explain any three characteristics of Query processor


Ans.:
It is very difficult to give the characteristics, which differentiates
centralized and distributed query processors. Still some of them have
been listed here. Out of them, the first four are common to both and the
next four are particular to distributed query processors.
Languages: The input language to the query processor can be based on
relational calculus or relational algebra. In distributed context, the output
language is generally some form of relational algebra augmented with
communication primitives.
Types of Optimization: Conceptually, query optimization is to choose a
best point of solution space that leads to the minimum cost. A popular
approach called exhaustive search is used. This is a method where
heuristic techniques are used. In both centralized and distributed systems
a common heuristic is to minimize the size of intermediate relations.
Performing unary operations first and ordering the binary operations by
the increasing size of their intermediate relations can do this.
Optimization Timing: A query may be optimized at different times
relative to the actual time of query execution. Optimization can be done
statically before executing the query or dynamically as the query is
executed. The main advantage of the later method is that the actual sizes
of the intermediate relations are available to the query processor, thereby
minimizing the probability of a bad choice.

Question 6: Explain the properties of Transaction?


Ans.:
The management of distributed transaction means dealing with
interrelated problem like reliability, concurrency control and the efficient
utilization of the resources of the complete system. In this unit we have
considered the well known protocols like 2-Phase commit protocol for
recovery and 2-Phase locking for concurrency control.
The Transaction is an application or part of application that is
characterized by the following properties.
Atomicity: Either all or none of the transactions operations are
performed. It requires that if a transaction is interrupted by a failure its
partial results are not at all taken into consideration and the whole
operation has to be repeated. The two types of problems that do not allow
the transaction to complete are:
Transaction aborts: This may be requested by the transaction itself
as some of its inputs are wrong or it has been estimated that the
results produced may become useless. It also may be forced by the
system for its own reason. The activity of ensuring atomicity in the
presence of Transaction aborts is called Transaction recovery.
System Crashes: It is because of some catastrophic effects that
crash the system without any prior knowledge. The activity of
ensuring atomicity in the presence of system crashes is called crash
recovery.
The completion of transaction is called Commit. The primitives that can be
used for carrying out the transaction are:
Begin _Transaction

Commit

Begin _Transaction

Abort

Begin _Transaction

System

Forces Abort
Durability: Once a transaction is committed, the system must guarantee
that the results of operations will never be lost, independent of
subsequent failures. The activity of providing Durability of the transaction
is called Database recovery.

Serializability: If many transactions execute concurrently, the result


must be same as if they were executed serially in the same order. The
activity of providing Serializability of the transaction is called Concurrency
control.
Isolation: This property states that an incomplete transaction cannot
disclose its result to other transactions until it is committed. This property
has to be strictly followed to avoid a problem called Cascading Aborts
(Domino Effect). According to this all the transactions that have observed
the partial results have to be aborted.

Question 7: Create a trigger to get confirmation from user on


deleting record on the employee table.
Ans.:
PL/SQL can be used to write data base triggers. Triggers are used to define
code that is executed / fired when certain actions or event occur. At the
data base level, triggers can be defined for events such as inserting a
record into a table, deleting a record, and updating a record.
The trigger definition consists of following basic parts.

The events that fires the trigger


The database table on which event must occur
An optional condition controlling when the trigger is executed
A PL/SQL block containing the code to be executed when the trigger
is fired.

A trigger is a data base object, like a table or an index. When you define a
trigger, it becomes a part of the database and it is always is executed
when the event for which it is defined occurs.
We could then use the CREATE TRIGGER statement to create an BEFORE DELETE trigger as
follows:

CREATE OR REPLACE TRIGGER orders_before_delete


BEFORE DELETE
ON orders
FOR EACH ROW
DECLARE
v_username varchar2(10);

BEGIN
-- Find username of person performing the DELETE on the table
SELECT user INTO v_username
FROM dual;
-- Insert record into audit table
INSERT INTO orders_audit
( order_id,
quantity,
cost_per_item,
total_cost,
delete_date,
deleted_by )
VALUES
( :old.order_id,
:old.quantity,
:old.cost_per_item,
:old.total_cost,
sysdate,
v_username );
END;

In the above syntax


trigger-name Is the name of the trigger to be created.
verb-list The SQL verbs that fire the Create, i.e. it may be INSERT, UPDATE
or DELETE.

Question 8: Write a cursor to open an employee database and


fetch the employee record whose age is greater than 45.
Ans.:
PL/SQL cursors provide a way for your program to select multiple rows of
data from the database and then to process each row individually. Cursors
are PL/SQL constructs that enable you to process, one row at a time, the
results of a multi row query.
There are 2 types of cursors.

Implicit cursors
Explicit cursors

Implicit cursors are declared by ORACLE for each UPDATE, DELETE and
INSERT SQL commands. Explicit cursors are declared and used by the user
to process multiple row, returned by SELECT statement. The set of rows
returned by a query is called the Active Set. Its size depends on the
number of rows that meet the search criteria of the SQL query. The data
that is stored in the cursor is called the Active Data Set.
ORACLE cursor is a mechanism used to easily process multiple rows of
data. Cursors contain a pointer that keeps track of current row being
accessed, which enables your program to process the rows at a time.

SOLUTION:
DECLARE
CURSOR a_emp
IS
SELECT *
FROM employees
WHERE emp_age > 45;
a_emp employees.a_emp%TYPE;
BEGIN
OPEN a_emp
LOOP
FETCH a_emp;
END LOOP;
CLOSE a_emp;
END;

Das könnte Ihnen auch gefallen