Study of Relational Database Management Systems Through OraclePL SQL
Study of Relational Database Management Systems Through OraclePL SQL
DBMS
Study of Relational Database Management
Systems through Oracle/PL SQL
Unit 5
Study of Relational Database Management Systems through Oracle/PL SQL
5.1 QL/MySQL:
5.1.1 Architecture,
5.1.2 physical files,
5.1.3 memory structures,
5.1.4 background process.
5.2 Concept of table spaces,
5.2.1 segments,
5.2.2 extents and block.
5.3 Dedicated server,
5.3.1 multi threaded server.
5.4 Distributed database,
5.4.1 database links, and snapshot.
5.5 Data dictionary,
5.5.1 dynamic performance view.
Unit 5
Study of Relational Database Management Systems through Oracle/PL SQL
5.6 Security,
5.6.1 role management,
5.6.2 privilege management,
5.6.3 profiles,
5.6.4 invoker defined security model.
5.7 SQL queries,
5.7.1 Data extraction from single, multiple tables equi- join, non equi-join, self -join, outer join.
5.8 Usage of like, any, all, exists, in Special operators.
5.9 Hierarchical quires,
5.9.1 inline queries,
5.9.2 flashback queries.
5.10 Introduction of ANSI SQL,
5.10.1 anonymous block,
5.10.2 nested anonymous block,
5.10.3 branching and looping constructs in ANSI SQL.
Unit 5
Study of Relational Database Management Systems through Oracle/PL SQL
5.11 Cursor management:
5.11.1 nested and parameterized cursors,
5.11.2 Oracle exception handling mechanism.
5.12 Stored procedures,
5.12.1 in, out, in out type parameters,
5.12.2 usage of parameters in procedures.
5.13 User defined functions their limitations.
5.14 Triggers,
5.14.1 mutating errors,
5.14.2 instead of triggers.
What is Exception Handling in PL/SQL(Oracle)?
• An exception occurs when the PL/SQL engine encounters an instruction
which it cannot execute due to an error that occurs at run-time. These
errors will not be captured at the time of compilation and hence these
needed to handle only at the run-time.
• For example, if PL/SQL engine receives an instruction to divide any number by ‘0’,
then the PL/SQL engine will throw it as an exception.
• The exception is only raised at the run-time by the PL/SQL engine.
• Exceptions will stop the program from executing further, so to avoid such
condition, they need to be captured and handled separately. This process
is called as Exception-Handling, in which the programmer handles the
exception that can occur at the run time.
What is Exception Handling in PL/SQL(Oracle)?
Exception-Handling Syntax
• Exceptions are handled at the block, level, i.e., once if any exception occurs in any
block then the control will come out of execution part of that block.
The exception will then be handled at the exception handling part of that block. After handling
the exception, it is not possible to resend control back to the execution section of that block.
• The below syntax explains how to catch and handle the exception.
What is Exception Handling in PL/SQL(Oracle)?
Syntax Explanation:
• In the above syntax, the exception-handling block contains series of WHEN condition to handle the exception.
• Each WHEN condition is followed by the exception name which is expected to be raised at the run time.
• When any exception is raised at runtime, then the PL/SQL engine will look in the exception handling part for that
particular exception. It will start from the first ‘WHEN’ clause and, sequentially it will search.
• If it found the exception handling for the exception which has been raised, then it will execute that particular handling
code part.
What is Exception Handling in PL/SQL(Oracle)?
Syntax Explanation(Contd…) :
• If none of the ‘WHEN’ clause is present for the exception which has been raised, then PL/SQL engine will execute the ‘WHEN
OTHERS’ part (if present). This is common for all the exception.
• After executing the exception, part control will go out of the current block.
• Only one exception part can be executed for a block at run-time. After executing it, the controller will skip the remaining
exception handling part and will go out of the current block.
Note: WHEN OTHERS should always be at the last position of the sequence. The exception handling part present after WHEN
OTHERS will never get executed as the control will exit from the block after executing the WHEN OTHERS.
Types of Exception
There are two types of Exceptions in PL/SQL.
1.Predefined Exceptions
2.User-defined Exception
Predefined Exceptions
• Oracle has predefined some common exception. These exceptions have a unique
exception name and error number.
• These exceptions are already defined in the ‘STANDARD’ package in Oracle. In code,
we can directly use these predefined exception name to handle them.
Few predefined exceptions are:
CASE_NOT_FOUND ORA-06592 None of the ‘WHEN’ clause in CASE statement satisfied and no ‘ELSE’ clause is specified
COLLECTION_IS_NULL ORA-06531 Using collection methods (except EXISTS) or accessing collection attributes on a uninitialized
collections
DUP_VAL_ON_INDEX ORA-00001 Storing a duplicate value in a database column that is a constrained by unique index
INVALID_NUMBER ORA-01722 Conversion of character to a number failed due to invalid number character
NO_DATA_FOUND ORA-01403 When ‘SELECT’ statement that contains INTO clause fetches no rows.
ROW_MISMATCH ORA-06504 When cursor variable data type is incompatible with the actual cursor return type
TOO_MANY_ROWS ORA-01422 When a ‘SELECT’ statement with INTO clause returns more than one row
VALUE_ERROR ORA-06502 Arithmetic or size constraint error (eg: assigning a value to a variable that is larger than the
variable size)
ZERO_DIVIDE ORA-01476 Dividing a number by ‘0’
Types of Exception
User-defined Exception
• In Oracle, other than the above-predefined exceptions, the programmer can create their own exception and handle them. They
can be created at a subprogram level in the declaration part.
• These exceptions are visible only in that subprogram. The exception that is defined in the package specification is public
exception, and it is visible wherever the package is accessible.
Syntax Explanation:
• In the above syntax, the keyword RAISE is used in the exception handling block.
• Whenever program encounters exception “exception_name”, the exception is handled and will be completed normally
• But the keyword ‘RAISE’ in the exception handling part will propagate this particular exception to the parent program.
PL/SQL Raise Exception
Note: While raising the exception to the parent block the exception that is getting raised should also be visible at parent block, else
oracle will throw an error.
• We can use keyword ‘RAISE’ followed by the exception name to raise that particular user-defined/predefined exception. This
can be used in both execution part and in exception handling part to raise the exception.
Syntax Explanation:
•In the above syntax, the keyword RAISE is used in the execution part followed by exception “exception_name”.
•This will raise this particular exception at the time of execution, and this needs to be handled or raised further.
PL/SQL Exception Example
Example : In this example, we are going to see
• How to declare the exception
• How to raise the declared exception and
• How to propagate it to the main block
PL/SQL Exception Example
Code Explanation:
• Code line 2: Declaring the variable ‘sample_exception’ as EXCEPTION type.
• Code line 3: Declaring procedure nested_block.
• Code line 6: Printing the statement “Inside nested block”.
• Code line 7: Printing the statement “Raising sample_exception from nested block.”
• Code line 8: Raising the exception using ‘RAISE sample_exception’.
• Code line 10: Exception handler for exception sample_exception in the nested
block.
• Code line 11: Printing the statement ‘Exception captured in nested block. Raising
to main block’.
• Code line 12: Raising the exception to main block (propagating to the main block).
• Code line 15: Printing the statement “Inside the main block”.
• Code line 16: Printing the statement “Calling nested block”.
• Code line 17: Calling nested_block procedure.
• Code line 18: Exception
• Code line 19: Exception handler for sample_exception in the main block.
• Code line 20: Printing the statement “Exception captured in the main block.”
PL/SQL Exception Example
A privilege is permission to execute one particular type of SQL statement or access a second persons’ object. Database
privilege controls the use of computing resources. Database privilege does not apply to the Database administrator of the
database.
System privileges —
A system privilege is the right to perform an activity on a specific type of object.
for example, the privilege to delete rows of any table in a database is system privilege. There are a total of
60 different system privileges. System privileges allow users to CREATE, ALTER, or DROP the database
objects.
Object privilege —
An object privilege is a privilege to perform a specific action on a particular table, function, or package.
For example, the right to delete rows from a table is an object privilege.
For example, let us consider a row of table SISTec that contains the name of the employee who is no longer a part of the
organization, then deleting that row is considered as an object privilege.
Object privilege allows the user to INSERT, DELETE, UPDATE, or SELECT the data in the database object.
Privileges and role management process.
Roles :
• A role is a mechanism that can be used to allow authorization. A person or a group of people can be
allowed a role or group of roles.
• By many roles, the head can manage access privileges very easily.
• The roles are provided by the database management system for easy and managed or controlled
privilege management.
Privileges and role management process.
Properties of Roles –
The following are the properties of the roles which allow easy privilege management
inside a database:
• Application-specific security —
• The user can also protect the use of a role by using a password. Applications can be created to
allow a role when entering the correct and best password. Users are not allowed the role if they
do not know about the password.
Privileges and role management process.
Example:
1. Create table applog:
=> CREATE TABLE applog (id int, sourceID VARCHAR(32), data TIMESTAMP, event VARCHAR(256));
2. Create the logreader role and grant it read-only privileges on table applog:
CREATE ROLE:
=> CREATE ROLE logreader;
GRANT PRIVILEGE:
=> GRANT SELECT ON applog TO logreader;
3. Create the logwriter role and grant it write privileges on table applog:
CREATE ROLE:
=> CREATE ROLE logwriter;
GRANT PRIVILEGE:
=> GRANT INSERT, UPDATE ON applog to logwriter;
Distributed Database System
• A distributed database is basically a database that is not limited to one
system, it is spread over different sites, i.e, on multiple computers or over a
network of computers.
• A distributed database system is located on various sites that don’t share
physical components.
• This may be required when a particular database needs to be accessed by
various users globally. It needs to be managed such that for the users it
looks like one single database.
Types of Distributed Database System
1. Homogeneous Database:
In a homogeneous database, all different sites store database identically.
The operating system, database management system, and the data structures used – all
are the same at all sites. Hence, they’re easy to manage.
2. Heterogeneous Database:
In a heterogeneous distributed database, different sites can use different schema
and software that can lead to problems in query processing and transactions. Also, a
particular site might be completely unaware of the other sites.
Different computers may use a different operating system, different database
application. They may even use different data models for the database.
Distributed Database System
• Advantages of Distributed Database System :
1)There is fast data processing as several sites participate in request processing.
2) Reliability and availability of this system is high.
3) It possess reduced operating cost.
4) It is easier to expand the system by adding more sites.
5) It has improved sharing ability and local autonomy.
Syntax:
Example Query:
DECLARE s1 CURSOR FOR SELECT * FROM studDetails
Fetch Data from the Cursor
There is a total of 6 methods to access data from the cursor. They are as
follows:
1. FIRST is used to fetch only the first row from the cursor table.
2. LAST is used to fetch only the last row from the cursor table.
3. NEXT is used to fetch data in a forward direction from the cursor table.
4. PRIOR is used to fetch data in a backward direction from the cursor table.
5. ABSOLUTE n is used to fetch the exact nth row from the cursor table.
6. RELATIVE n is used to fetch the data in an incremental way as well as a
decremental way.
Syntax:
FETCH NEXT/FIRST/LAST/PRIOR/ABSOLUTE n/RELATIVE n FROM cursor_name
Fetch Data from the Cursor
Example :
Output:
• The program below updates a table by increasing the salary of each employee
by 1500.
• After the update, the SQL%ROWCOUNT attribute is used to find out how
many rows were affected by the operation.
Fragmentation in Distributed DBMS
• Fragmentation is a process of dividing the whole or full database into
various subtables or sub relations so that data can be stored in different
systems.
• The small pieces or sub relations or sub-tables are called fragments.
These fragments are called logical data units and are stored at various
sites.
• It must be made sure that the fragments are such that they can be used to
reconstruct the original relation (i.e, there isn’t any loss of data).
Fragmentation in Distributed DBMS
• In the fragmentation process, let’s say, If a table T is fragmented and is
divided into a number of fragments say T1, T2, T3….TN. The fragments
contain sufficient information to allow the restoration of the original table T.
• This restoration can be done by the use of UNION or JOIN operation on
various fragments. This process is called data fragmentation.
• All of these fragments are independent which means these fragments can not
be derived from others.
• The users needn’t be logically concerned about fragmentation which means
they should not concerned that the data is fragmented and this is
called fragmentation Independence or we can say fragmentation
transparency.
Fragmentation in Distributed DBMS
• Advantages :
• As the data is stored close to the usage site, the efficiency of the database system
will increase
• Local query optimization methods are sufficient for some queries as the data is
available locally
• In order to maintain the security and privacy of the database system,
fragmentation is advantageous
• Disadvantages :
• Access speeds may be very high if data from different fragments are needed
• If we are using recursive fragmentation, then it will be very expensive
Fragmentation in Distributed DBMS
Methods for data fragmenting of a table
1. Horizontal fragmentation:
• Horizontal fragmentation refers to the process of dividing a table
horizontally by assigning each row (or a group of rows) of relation to
one or more fragments.
• These fragments can then be assigned to different sites in the distributed
system. Some of the rows or tuples of the table are placed in one system
and the rest are placed in other systems.
• The rows that belong to the horizontal fragments are specified by a
condition on one or more attributes of the relation. In relational algebra
horizontal fragmentation on table T, can be represented as follows:
Fragmentation in Distributed DBMS
Methods for data fragmenting of a table
1. Horizontal fragmentation:
T = T1 ∪ T2 ∪ …. ∪ TN
Fragmentation in Distributed DBMS
Methods for data fragmenting of a table
2. Vertical fragmentation:
• Vertical fragmentation refers to the process of decomposing a table vertically
by attributes or columns. In this fragmentation, some of the attributes are
stored in one system and the rest are stored in other systems.
• This is because each site may not need all columns of a table. In order to take
care of restoration, each fragment must contain the primary key field(s) in a
table.
• The fragmentation should be in such a manner that we can rebuild a table from
the fragment by taking the natural JOIN operation and to make it possible we
need to include a special attribute called Tuple-id to the schema.
• For this purpose, a user can use any super key. And by this, the tuples or rows
can be linked together.
Fragmentation in Distributed DBMS
Methods for data fragmenting of a table
This is T2 and to get back to the original T, we join these two fragments
T1 and T2 as πEMPLOYEE (T1 ⋈ T2)
Fragmentation in Distributed DBMS
Methods for data fragmenting of a table
3. Mixed Fragmentation:
The combination of vertical fragmentation of a table followed by further horizontal
fragmentation of some fragments is called mixed or hybrid fragmentation. For defining
this type of fragmentation we use the SELECT and the PROJECT operations of relational
algebra. In some situations, the horizontal and the vertical fragmentation isn’t enough to
distribute data for some applications and in that conditions, we need a fragmentation called
a mixed fragmentation.