Sunteți pe pagina 1din 7

PROBLEM STATEMENT: - Study of Stored procedures, triggers, assertions, cursors.

STORED PROCEDURES:-

WHAT IS A STORED PROCEDURE?

A stored procedure is a named group of SQL statements that have been previously
created and stored in the server database. Stored procedures accept input parameters so
that a single procedure can be used over the network by several clients using different
input data. Stored procedures reduce network traffic and improve performance.
Additionally, stored procedures can be used to help ensure the integrity of the database.
They are made up of 3 parts:-

a) Declarative part
b) Executable part
c) Exception Handling part

WHERE DO THEY RESIDE?


They are stored in the Oracle database. They can be invoked or called by any PL/SQL
block that appears within an application.

ADVANTAGES OF USING PROCEDURES:-


1) Help to enforce data security
2) No compilation required to execute code
3) Productivity can be increased
4) Only requires to be tested once.

SYNTAX:
CREATE OR REPLACE PROCEDURE [Schema.] <Procedure Name>
(<Argument> {IN, OUT, IN OUT} <Data Type>,…){IS, AS}
<Variable> declarations;
<Constant> declarations;
BEGIN
<PL/SQL subprogram body>;
EXCEPTION
<Exception PL/SQL block>;
END;

TRIGGERS:-

They are database objects created on the client and stored on the server in the Oracle
engines system table. The basic command used to create a new trigger is the CREATE
TRIGGER statement. This statement is followed by the details of when the trigger should
fire. Triggers may be associated with INSERT, UPDATE and DELETE events and may
be used either INSTEAD OF or AFTER the specified operation. Once you've specified
the trigger criteria, you supply the SQL statement that contains the trigger's payload.

SYNTAX:
1) TRIGGER CREATION:-

CREATE OR REPLACE TRIGGER [Schema.] <Trigger Name>


{BEFORE, AFTER}
{DELETE, INSERT, UPDATE [OF COLUMN …}}
ON [Schema.] <Table Name>
[Referencing {OLD as old, New as new}]
[FOR EACH ROW [WHEN Condition]]
DECLARE
<Variable> declarations;
<Constant> declarations;
BEGIN
<PL/SQL subprogram body>;
EXCEPTION
<Exception PL/SQL block>;
END;

2) TRIGGER DELETION:-

DROP TRIGGER <TRIGGER NAME>

USE OF TRIGGERS:-
1) To prevent invalid transactions
2) Enforce complex security authorizations
3) Provide high customization
4) To keep an audit trail of a table.

CURSORS:

Whenever a SQL statement is issued the Database server opens an area of memory in
which the command is parsed and executed. This area is called a cursor.They are of two
types :-

A) IMPLICIT CURSOR:-

When the executable part of a PL/SQL block issues a SQL command, PL/SQL creates an
implicit cursor which has the identifier SQL. PL/SQL manages this cursor for you.

PL/SQL provides some attributes which allow you to evaluate what happened when the
implicit cursor was last used. You can use these attributes in PL/SQL statements like
functions but you cannot use then within SQL statements.

The SQL cursor attributes are:-

%ROWCOUNT The number of rows processed by a SQL statement.


%FOUND TRUE if at least one row was processed.
%NOTFOUND TRUE if no rows were processed.
%ISOPEN TRUE if cursor is open or FALSE if cursor has not been opened or
has been closed. Only used with explicit cursors.

An example follows :-

DECLARE
ROW_DEL_NO NUMBER;
BEGIN
DELETE * FROM JD11.SECTION;
ROW_DEL_NO := SQL%ROWCOUNT;
END;

B) EXPLICIT CURSOR:

SELECT statements that occur within PL/SQL blocks are known as embedded, they must
return one row and may only return one row. To get around this you can define a
SELECT statement as a cursor (an area of memory), run the query and then manipulate
the returned rows within the cursor. Cursors are controlled via four command statements.
They are :-

Defines the name and structure of the cursor together with the SELECT
DECLARE statement that will populate the cursor with data. The query is validated but
not executed.
OPEN Executes the query that populates the cursor with rows.
Loads the row addressed by the cursor pointer into variables and moves the
FETCH
cursor pointer on to the next row ready for the next fetch.
Releases the data within the cursor and closes it. The cursor can be reopened
CLOSE
to refresh its data.

Cursors are defined within a DECLARE section of PL/SQL block. An example follows :-

DECLARE
CURSOR MYCUR IS SELECT ISBN, COST FROM JD11.BOOK;

The cursor is defined by the CURSOR keyword followed by the cursor identifier
(MYCUR in this case) and then the SELECT statement used to populate it, the SELECT
statement can be any legal query.

Cursors are opened with the OPEN statement, this populates the cursor with data.

DECLARE
CURSOR MYCUR IS SELECT ISBN, COST FROM JD11.BOOK;
BEGIN
OPEN MYCUR;
END;

To access the rows of data within the cursor we use the FETCH statement.

DECLARE
CURSOR MYCUR IS SELECT ISBN, COST FROM JD11.BOOK;
THISISBN NUMBER(10);
THISCOST NUMBER(10,2);
BEGIN
OPEN MYCUR;
FETCH MYCUR INTO THISISBN, THISCOST;
END;

The FETCH statement reads the column values for the current cursor row and puts them
into the specified variables. The cursor pointer is updated to point at the next row. If the
cursor has no more rows the variables will be set to null on the first FETCH attempt,
subsequent FETCH attempts will raise an exception.

The CLOSE statement releases the cursor and any rows within it, you can open the cursor
again to refresh the data in it.

ASSERTIONS:-

1. An assertion is a predicate expressing a condition we wish the database to always


satisfy.
2. Domain constraints, functional dependency and referential integrity are special
forms of assertion.
3. Where a constraint cannot be expressed in these forms, we use an assertion,

e.g. Ensuring the sum of loan amounts for each branch is less than the sum of all
account balances at the branch.

4. Assertion mentioned above can be written as follows.

Ensuring the sum of loan amounts for each branch is less than the sum of all
account balances at the branch.

create assertion sum-constraint check

(not exists (select * from branch

where (select sum)amount) from loan

where (loan.bname = branch.bname >=

(select sum)amount) from account where (account.bname =


branch.bname)))

INDEXES
An index is an ordered list of the contents of a column(or group of columns) of a
table. Indices are created in an existing table to locate rows more quickly and
efficiently. It is possible to create an index on one or more columns of a table, and
each index is given a name. The users cannot see the indexes, they are just used to
speed up queries.

Updating a table containing indexes takes more time than updating a table
without, this is because the indexes also need an update. So, it is a good idea to
create indexes only on columns that are often used for a search.

A Unique Index

Creates a unique index on a table. A unique index means that two rows cannot
have the same index value.

CREATE UNIQUE INDEX index_name ON table_name (column_name)

The "column_name" specifies the column you want indexed.

A Simple Index

Creates a simple index on a table. When the UNIQUE keyword is omitted,


duplicate values are allowed.

CREATE INDEX index_name ON table_name (column_name)

The "column_name" specifies the column you want indexed.

This example creates a simple index, named "PersonIndex", on the LastName


field of the Person table:

CREATE INDEX PersonIndex ON Person (LastName)

If you want to index the values in a column in descending order, you can add the
reserved word DESC after the column name:

CREATE INDEX PersonIndex ON Person (LastName DESC)

If you want to index more than one column you can list the column names within
the parentheses, separated by commas:

CREATE INDEX PersonIndex ON Person (LastName, FirstName)

Drop Index

You can delete an existing index in a table with the DROP INDEX statement.

DROP INDEX index_name ON table_name


SEQUENCES:
Oracle provides an object called a sequence that can generate numeric values. The value
generated can have a maximum of 38 digits. A sequence can be defined to:
1) Generate numbers in ascending or descending order
2) Provide intervals between numbers
3) Caching of sequence numbers in memory to speed up their availability

Creating Sequences:-
CREATE SEQUENCE <Sequence Name>
[INCREMENT BY <Integer value>
START WITH <Integer value>
MAXVALUE <Integer Value> / NOMAXVALUE
MINVALUE <Integer Value> / NOMINVALUE
CYCLE / NOCYCLE
CACHE <Integer Value>/ NOCACHE
ORDER / NOORDER]

Where,
sequence-name
Names the sequence. The name, including the implicit or explicit qualifier, must
not identify a sequence or data area that already exists at the current server.

START WITH numeric-constant


Specifies the first value that is generated for the sequence. The value can be any
positive or negative value that could be assigned to a column of the data type
associated with the sequence, without non-zero digits to the right of the decimal
point. If a value is not explicitly specified when the sequence is defined, the
default is the MINVALUE for an ascending sequence and the MAXVALUE for a
descending sequence.

This value is not necessarily the value that a sequence would cycle to after
reaching the maximum or minimum value of the sequence. The START WITH
clause can be used to start a sequence outside the range that is used for cycles.
The range used for cycles is defined by MINVALUE and MAXVALUE.

MINVALUE Specifies the minimum value at which a descending sequence either cycles
or stops generating values, or an ascending sequence cycles to after reaching the
maximum value.
MAXVALUE Specifies the maximum value at which an ascending sequence either cycles
or stops generating values, or a descending sequence cycles to after reaching the
minimum value.
CYCLE or NO CYCLE
Specifies whether this sequence should continue to generate values after reaching
either the maximum or minimum value of the sequence.
CYCLE
Specifies that values continue to be generated for this column after the maximum
or minimum value has been reached. If this option is used, after an ascending
sequence reaches the maximum value of the sequence, it generates its minimum
value. After a descending sequence reaches its minimum value of the sequence, it
generates its maximum value. The maximum and minimum values for the column
determine the range that is used for cycling.

When CYCLE is in effect, duplicate values can be generated for a sequence by


the database manager.

NO CYCLE
Specifies that values will not be generated for the sequence once the maximum or
minimum value for the sequence has been reached. This is the default.

CACHE or NO CACHE
Specifies whether to keep some preallocated values in memory. Preallocating and
storing values in the cache improves the performance of the NEXT VALUE
sequence expression.
CACHE integer-constant
Specifies the maximum number of values of the sequence that the database
manager preallocates and keeps in memory. The minimum value that can be
specified is 2, and the maximum is the largest value that can be represented as an
integer. The default is 20.

When a job ends, all cached sequence values that are yet to be assigned are lost,
and thus, will never be used. Therefore, the value specified for CACHE also
represents the maximum number of values for the sequence that could be lost
during a job end.

NO CACHE
Specifies that values for the sequence are not preallocated. If NO CACHE is
specified, the performance of the NEXT VALUE sequence expression will be
worse than if CACHE is specified.
ORDER or NO ORDER
Specifies whether the sequence values must be generated in order of request.
ORDER
Specifies that the values are generated in order of request. If ORDER is specified,
the performance of the NEXT VALUE sequence expression will be worse than if
NO ORDER is specified.
NO ORDER
Specifies that the values do not need to be generated in order of request. This is
the default.

EXAMPLE:

Create a sequence called ORG_SEQ that starts at 1, increments by 1, does not cycle, and
caches 24 values at a time:

CREATE SEQUENCE ORG_SEQ


START WITH 1
INCREMENT BY 1
NO MAXVALUE
NO CYCLE
CACHE 24

S-ar putea să vă placă și