0% found this document useful (0 votes)
156 views

Study of Relational Database Management Systems Through OraclePL SQL

The document discusses Relational Database Management Systems through Oracle/PL SQL. It covers topics like SQL/MySQL architecture, tablespaces, distributed databases, security, SQL queries, hierarchical queries, stored procedures, triggers and exception handling in PL/SQL. Exception handling allows capturing errors at runtime and handling them to avoid program termination. The document provides syntax for declaring exceptions and exception handling blocks to catch specific exceptions or generic exceptions.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
156 views

Study of Relational Database Management Systems Through OraclePL SQL

The document discusses Relational Database Management Systems through Oracle/PL SQL. It covers topics like SQL/MySQL architecture, tablespaces, distributed databases, security, SQL queries, hierarchical queries, stored procedures, triggers and exception handling in PL/SQL. Exception handling allows capturing errors at runtime and handling them to avoid program termination. The document provides syntax for declaring exceptions and exception handling blocks to catch specific exceptions or generic exceptions.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

UNIT V

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:

Exception Error Code Exception Reason


ACCESS_INTO_NULL ORA-06530 Assign a value to the attributes of uninitialized objects

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

CURSOR_ALREADY_OPEN ORA-06511 Trying to open a cursor which is already opened

DUP_VAL_ON_INDEX ORA-00001 Storing a duplicate value in a database column that is a constrained by unique index

INVALID_CURSOR ORA-01001 Illegal cursor operations like closing an unopened cursor

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.

• In the above syntax, the variable ‘exception_name’ is defined as ‘EXCEPTION’ type.


• This can be used as in a similar way as a predefined exception.
PL/SQL Raise Exception
• All the predefined exceptions are raised implicitly whenever the error occurs. But the user-defined exceptions needs to be raised
explicitly. This can be achieved using the keyword ‘RAISE’. This can be used in any of the ways mentioned below.
• If ‘RAISE’ is used separately in the program, then it will propagate the already raised exception to the parent block. Only in exception
block can be used as shown below.

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

Important points to note in Exception


• In function, an exception should always either return value or raise the
exception further. else Oracle will throw ‘Function returned without a
value’ error at run-time.
• Transaction control statements can be given at exception handling block.
• If an exception is not handled then by default all the active transaction in
that session will be rolled back.
Privilege and role management process.
• Confidentiality, integrity, and availability are the pillars of database security.
• Authorization is the allowance to the user or process to access the set of
objects.
• The type of access granted can be any like, read-only, read, and write.
• Privilege means different Data Manipulation Language(DML) operations
which can be performed by the user on data like INSERT, UPDATE,
SELECT and DELETE, etc.

There are two methods by which access control is performed is done by


using the following.
1. Privileges
2. Roles
Privilege and role management process.
Privileges :

1. The authority or permission to access a named object as advised


manner,
for example: permission to access a table.
2. Privileges can allow permitting a particular user to connect to the
database.
3. In, other words privileges are the allowance to the database by the
database object.
Privileges and role management process.
Database privileges —

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:

• Reduced privilege administration —


• The user can grant the privilege for a group of users who are related instead of granting the same
set of privileges to the users explicitly.

• Dynamic privilege management —


• If the privilege of the group changes then, only the right of role needs to be changed.

• 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.

• Disadvantages of Distributed Database System :

1) The system becomes complex to manage and control.


2) The security issues must be carefully managed.
3) The system require deadlock handling during the transaction processing
otherwise the entire system may be in inconsistent state.
4) There is need of some standardization for processing of distributed database
system.
cursor in SQL
• Cursor is a Temporary Memory or Temporary Work Station.
• It is Allocated by Database Server at the Time of
Performing DML(Data Manipulation Language) operations on
the Table by the User.
• Cursors are used to store Database Tables.
• There are 2 types of Cursors:
1. Implicit Cursors: Implicit Cursors are also known as Default Cursors
of SQL SERVER. These Cursors are allocated by SQL SERVER when
the user performs DML operations.
2. Explicit Cursors: Explicit Cursors are Created by Users whenever the
user requires them. Explicit Cursors are used for Fetching data from
Table in Row-By-Row Manner.
Create Explicit Cursor

1. Declare Cursor Object

Syntax:

DECLARE cursor_name CURSOR FOR SELECT * FROM table_name

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 :

FETCH FIRST FROM s1


FETCH LAST FROM s1
FETCH NEXT FROM s1
FETCH PRIOR FROM s1
FETCH ABSOLUTE 7 FROM s1
FETCH RELATIVE -2 FROM s1
Create an Implicit Cursor
• An implicit cursor is a cursor that is automatically created by PL/SQL when you
execute a SQL statement.
• We don’t need to declare or open an implicit cursor explicitly. Instead, PL/SQL
manages the cursor for you behind the scenes.
• To create an implicit cursor in PL/SQL, we simply need to execute a SQL
statement. For example, to retrieve all rows from the EMP table, we can use the
following code:
Create an Implicit Cursor
In PL/SQL, when we
perform INSERT, UPDATE or DELETE operations, an implicit
cursor is automatically created. This cursor holds the data to be
inserted or identifies the rows to be updated or deleted. we can
refer to this cursor as the SQL cursor in our code.
This SQL cursor has several useful attributes.
1.%FOUND is true if the most recent SQL operation affected at least
one row.
2.%NOTFOUND is true if it didn’t affect any rows.
3.%ROWCOUNT is returns the number of rows affected.
4.%ISOPEN checks if the cursor is open.
Implicit Cursor
%BULK_ROWCOUNT and %BULK_EXCEPTIONS

• %BULK_ROWCOUNT and %BULK_EXCEPTIONS are


specific to the FORALL statement, which is used to
perform multiple DML operations at once.

• %BULK_ROWCOUNT returns the number of rows


affected by each DML operation, while
%BULK_EXCEPTION returns any exception that
occurred during the operations.
Example: Implicit Cursor
Consider This Code and Observer the Output

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:

Note that a union operation can be performed on the fragments to


construct table T. Such a fragment containing all the rows of table T is
called a complete horizontal fragment.
Fragmentation in Distributed DBMS
Methods for data fragmenting of a table
Example: Horizontal fragmentation
consider an EMPLOYEE table (T) :
This EMPLOYEE table can be divided into different fragments like:
EMP 1 = σDep = 1 EMPLOYEE
EMP 2 = σDep = 2 EMPLOYEE

These two fragments are: T1 and T2.


Where T1 fragment of Dep = 1:
Fragmentation in Distributed DBMS
Methods for data fragmenting of a table
Example: Horizontal fragmentation
consider an EMPLOYEE table (T) :
This EMPLOYEE table can be divided into different fragments like:
EMP 1 = σDep = 1 EMPLOYEE
EMP 2 = σDep = 2 EMPLOYEE

Similarly, the T2 fragment on the basis of Dep = 2 will be :


Fragmentation in Distributed DBMS
Methods for data fragmenting of a table
Example: Horizontal fragmentation
consider an EMPLOYEE table (T) :

Now, here it is possible to get back T as

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

• The projection is as follows:


Fragmentation in Distributed DBMS
Methods for data fragmenting of a table
For example, for the EMPLOYEE table we have T1 as :
For the second. sub table of relation
after vertical fragmentation is given as
follows :

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.

Mixed fragmentation can be done in two different ways:


1.The first method is to first create a set or group of horizontal fragments and then create
vertical fragments from one or more of the horizontal fragments.
2.The second method is to first create a set or group of vertical fragments and then create
horizontal fragments from one or more of the vertical fragments.
Fragmentation in Distributed DBMS
Methods for data fragmenting of a table
Example: Mixed Fragmentation:

• For example, for our EMPLOYEE table, below is the implementation


of mixed fragmentation is
πEname, Design (σEno < 104(EMPLOYEE))
• The result of this fragmentation is:
Oracle Application Express(Oracle Apex)
• Oracle Application Express (Oracle Apex) formerly called
HTML DB.
• It is a rapid web application development tool for Oracle
databases & available by default in all oracle database
installations.
• It is developing a fully interactive, web-based system for data
that resides within the Oracle database.
• Using only a web browser and limited programming experience,
you can develop professional applications that are both fast and
secure.
Oracle Application Express(Oracle Apex)
• APEX is written using PL/SQL and works within a web
browser completely.
• Oracle Apex is an IDE and runtime environment. It is used to
write web applications. With APEX IDE, one can create forms,
reports, and charts.
• One can also compile your JavaScript code if you need it.
Outputs from APEX programs can be on-screen or PDF, in an
Excel spreadsheet, Flash, or integrated with a web service.
Oracle Application Express(Oracle Apex)
Key Functionalities:
• Build good-looking web applications both fast and secure.
• It continues and remains on the Oracle database. The APEX framework and metadata are
stored in Oracle tables.
• IT’S COMPLETELY FREE. No license is needed.
• The quick learning curve for developers to build applications.
• Deploying an application is as simple as ‘Export and Import’ if the indicators with a
strong code of changing values between environments are avoided.
• Extensive range of customization application look and feel.
• Scalable with a high volume of users. ASKTOM, the previous ORACLE METALINK
(before being moved to the Flash version), and Many Applications were developed using
APEX.
• It can be configured to use Oracle SSO and EBS find user repository.
Stored Procedures
• A stored procedure is a prepared SQL code that you can
save, so the code can be reused over and over again.
• So if you have an SQL query that you write over and
over again, save it as a stored procedure, and then just
call it to execute it.
• You can also pass parameters to a stored procedure, so
that the stored procedure can act based on the
parameter value(s) that is passed.
Stored Procedures
Stored Procedure Example

• The following SQL statement


creates a stored procedure named
"SelectAllCustomers" that selects all
records from the "Customers" table:
Thank You

You might also like