Professional Documents
Culture Documents
Structure of PL/SQL-Blocks
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.
4.1.3 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 (see Section 1.1) 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 (thus the variable becomes a constant).
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;
Use the CREATE TRIGGER statement to create and enable a database trigger, which is
• A stored PL/SQL block associated with a table, a schema, or the database or
• An anonymous PL/SQL block or a call to a procedure implemented in PL/SQL or Java
Prerequisites
Before a trigger can be created, the user SYS must run a SQL script commonly called
DBMSSTDX.SQL. The exact name and location of this script depend on your operating system.
• To create a trigger in your own schema on a table in your own schema or on your own schema
(SCHEMA), you must have the CREATE TRIGGER privilege.
• To create a trigger in any schema on a table in any schema, or on another user's schema
(schema.SCHEMA), you must have the CREATE ANY TRIGGER privilege.
• In addition to the preceding privileges, to create a trigger on DATABASE, you must have the
ADMINISTER DATABASE TRIGGER system privilege.
If the trigger issues SQL statements or calls procedures or functions, then the owner of the trigger must
have the privileges necessary to perform these operations. These privileges must be granted directly to
the owner rather than acquired through roles.
Syntax
create_trigger::=
dml_event_clause::=
referencing_clause::=
Semantics
OR REPLACE
Specify OR REPLACE to re-create the trigger if it already exists. Use this clause to change the
definition of an existing trigger without first dropping it.
schema
Specify the schema to contain the trigger. If you omit schema, then Oracle creates the trigger in your
own schema.
trigger
Specify the name of the trigger to be created.
If a trigger produces compilation errors, then it is still created, but it fails on execution. This means it
effectively blocks all triggering DML statements until it is disabled, replaced by a version without
compilation errors, or dropped. You can see the associated compiler error messages with the SQL*Plus
command SHOW ERRORS.
3. Differentiate between shared pool and large pool of oracle memory component
Introduction
The Oracle shared pool provides critical services for sharing of complex objects among large
numbers of users. DBAs often spent much time learning about the shared pool memory
management and configure and tune shared pool usage.
The Automatic Shared Memory Management (ASMM) features introduced in 10GR1 solved
this problem by providing the DBA a simple, automatic self-tuning mechanism for configuring
shared memory components of SGA, including the buffer cache and shared pool.
NAME MB
-------------------------- ----------
ktcmvcb 6856.96056
KGH: NO ACCESS 1610.02487
sql area 571.81263
free memory 502.999672
CCursor 301.799118
PCursor 176.69886
library cache 112.56636
kglsim object batch 77.3775787
gcs resources 75.5597076
sql area:PLSQL 51.2854691
Multiple selects from V$SGASTAT will show that some components remain constant in size.
Large Pool
The large pool is to reduce the strain on the shared pool, the following will use the large pool if
configured:
• Shared server processes the UGA’s will be stored here
• Parallel execution servers if you have enabled parallel query, the parallel servers communicate
via the large pool
• I/O slave processes if you have enabled db writer slaves or tape i/o slaves.
• RMAN the rman channel processes use the large pool for caching buffers during backup and
restore operations
Large pool
show parameter large_pool_size;
configuration
Large Pool Size select * from v$sgastat where pool='large pool';
Java Pool
Java stored procedures are loaded from the data dictionary into the shared pool, the purpose of the Java
pool is to provide room for the runtime memory structures used by a Java application. It is controlled
by three instance parameters:
• Java_pool_size dynamically create the java pool
• Java_max_sessionspace_size maximum amount of space aloud for any one session (static)
• Java_soft_sessionspace_limit a message is written to the trace file when the limit is breached,
can help with monitoring. (static)
Java pool
show parameter java_pool_size;
configuration
Java Pool Size select * from v$sgastat where pool='java pool';
To maximize performance and accommodate many users, a multiprocess Oracle Database system uses
background processes. Background processes consolidate functions that would otherwise be handled by
multiple database programs running for each user process. Background processes asynchronously
perform I/O and monitor other Oracle Database processes to provide increased parallelism for better
performance and reliability.
Table 4-4 describes the basic Oracle Database background processes, many of which are discussed in
more detail elsewhere in this book. The use of additional database server features or options can cause
more background processes to be present. For example, when you use Advanced Queuing, the queue
monitor (QMNn) background process is present. When you specify the FILE_MAPPING initialization
parameter for mapping datafiles to physical devices on a storage subsystem, then the FMON process is
present.
Table 4-4 Oracle Database Background Processes
Process Name Description
Database writer (DBWn) The database writer writes modified blocks from the
database buffer cache to the datafiles. Oracle Database
allows a maximum of 20 database writer processes
(DBW0-DBW9 and DBWa-DBWj). The
DB_WRITER_PROCESSES initialization parameter
specifies the number of DBWn processes. The database
selects an appropriate default setting for this
initialization parameter or adjusts a user-specified
setting based on the number of CPUs and the number
of processor groups.
Log writer (LGWR) The log writer process writes redo log entries to disk.
Redo log entries are generated in the redo log buffer of
the system global area (SGA). LGWR writes the redo
log entries sequentially into a redo log file. If the
database has a multiplexed redo log, then LGWR
writes the redo log entries to a group of redo log files.
See Chapter 10, "Managing the Redo Log" for
information about the log writer process.
System monitor (SMON) The system monitor performs recovery when a failed
instance starts up again. In an Oracle Real Application
Clusters database, the SMON process of one instance
can perform instance recovery for other instances that
have failed. SMON also cleans up temporary segments
that are no longer in use and recovers dead transactions
skipped during system failure and instance recovery
because of file-read or offline errors. These transactions
are eventually recovered by SMON when the
tablespace or file is brought back online.
Process monitor (PMON) The process monitor performs process recovery when a
user process fails. PMON is responsible for cleaning up
Process Name Description
the cache and freeing resources that the process was
using. PMON also checks on the dispatcher processes
(described later in this table) and server processes and
restarts them if they have failed.
Archiver (ARCn) One or more archiver processes copy the redo log files
to archival storage when they are full or a log switch
occurs. Archiver processes are the subject of Chapter
11, "Managing Archived Redo Logs".
Site autonomy means that each server participating in a distributed database is administered
independently from all other databases. Although several databases can work together, each database is
a separate repository of data that is managed individually. Some of the benefits of site autonomy in an
Oracle Database distributed database include:
• Nodes of the system can mirror the logical organization of companies or groups that need to
maintain independence.
• Local administrators control corresponding local data. Therefore, each database administrator's
domain of responsibility is smaller and more manageable.
• Independent failures are less likely to disrupt other nodes of the distributed database. No single
database failure need halt all distributed operations or be a performance bottleneck.
• Administrators can recover from isolated system failures independently from other nodes in the
system.
• A data dictionary exists for each local database. A global catalog is not necessary to access
local data.
• Nodes can upgrade software independently.
Although Oracle Database permits you to manage each database in a distributed database system
independently, you should not ignore the global requirements of the system. For example, you may
need to:
• Create additional user accounts in each database to support the links that you create to facilitate
server-to-server connections.
All buildings, from the home you live in to the place you work, have
an architecture that keeps the building together. This architecture
(if it’s well designed), will keep the building from falling apart,
keep you nice and warm, and provide you with easy access to
facilities like water fountains and restrooms.
A well-crafted architecture will also help you work more efficiently.
The same is true for databases. Despite the name “Oracle”, there is
no magic!
The architecture that Oracle has built its database around is
designed to perform quickly, efficiently and without errors.
In this section we will introduce you to the Oracle architecture.
This architecture includes the following components:
* The System Global Area (SGA) and other memory areas that utilize
RAM
* Database related background processes
* Tablespaces and Datafiles
* Database related files
* The instance and the database
Next we will look at each of these components in more detail. Finally
we put the components together into a single “big picture” view, and
see how all these pieces fit together to complete the Oracle puzzle.
Keep in mind that the discussion in this chapter is limited to the
overall database architecture. In later chapters we will get into the
finer details of managing these structures.
With this in mind, let’s drill down a bit deeper into the
architecture of Oracle and learn more about memory structures, the
part of Oracle that utilizes your system’s RAM.
Diving into the RAM Pools
All computers have memory. Memory should not be confused with disk
storage. Memory is volatile, which means its content is lost after
the power is removed. Memory is also very fast. RAM memory is
expressed in nanoseconds (billionths of a second) and disk speed is
in milliseconds (thousandths of a second). In Oracle, RAM speed is
hundreds of times faster than disks. Don’t get burned by tiny
pools. See the Google search ”oracle cache disk speed” for details.
Disk storage is non-volatile. This means that the data stored on a
disk will remain after the power is turned off. Disks are always
slower than RAM, but disks are hundreds of times cheaper than RAM.
There is a trade-off between memory and disks: Memory is fast but
expensive (about $1,000 per gigabyte), whereas disks are slower but
very cheap. Thus, memory is used for short-term storage of
information that is frequently needed and disks are used for long-
term storage of information.
Oracle has a number of memory areas that it uses to store
information. In this section we will address the main Oracle memory
areas. They are called:
* The System Global Area (SGA) – RAM areas for the Oracle programs.
* The Program Global Area (PGA) – Private RAM areas for individual
client connections to Oracle
The Instance and the Database
We are almost at the end of our introduction to the Oracle database
architecture. We can’t complete this journey, however, until we
define two more terms, instance and database. The Oracle instance is
the combination of the Oracle SGA and the related background
processes (the programs, PMON, SMON, etc.). When the SGA RAM memory
is successfully allocated and the Oracle database processes are
running normally, the instance is said to be “up”.
However, and instance is sometimes different from a database. You can
have the instance running, but the database might not be mounted or
open. The Oracle Database includes the physical files we have
discussed: the datafiles, the control file, and the redo log files.
When the Oracle instance is running, it can attach itself to a
database, “mount” the control file, and finally “open” the datafiles
and redo log files. This is an important distinction because many
Oracle operations must be done with the instance started but the
database is not open.
Oracle Architecture Concepts
In the previous section we discussed the Oracle physical
architecture. Things like files, programs and hardware are all
considered physical pieces (or physical properties) of the Oracle
database. In this section we are concerned with the logical pieces of
the Oracle database.
Oracle segregates “physical components” (the .dbf files on disk) my
mapping them into “logical” containers called tablespaces. In turn,
we allocate our tables and indexes inside these tablespace, and
Oracle takes-care of the interface to the physical disk files.
• The database schema, which consists of items such as tables, clusters, indexes, views,
stored procedures, database triggers, sequences, and so on.
New Term: The database schema is a collection of logical-structure objects, known as schema objects,
that define how you see the database's data. These schema objects consist of structures such as tables,
clusters, indexes, views, stored procedures, database triggers, and sequences.
• Table--A table, which consists of a tablename and rows and columns of data, is the basic
logical storage unit in the Oracle database. Columns are defined by name and data type. A table
is stored within a tablespace; often, many tables share a tablespace.
• Cluster--A cluster is a set of tables physically stored together as one table that shares a
common column. If data in two or more tables is frequently retrieved together based on data in
the common column, using a clustered table can be quite efficient. Tables can be accessed
separately even though they are part of a clustered table. Because of the structure of the cluster,
related data requires much less I/O overhead if accessed simultaneously.
• Index--An index is a structure created to help retrieve data more quickly and efficiently
(just as the index in this book allows you to find a particular section more quickly). An index is
declared on a column or set of columns. Access to the table based on the value of the indexed
column(s) (as in a WHERE clause) will use the index to locate the table data.
NOTE: A new feature in Oracle8 is the index-only table. In an index-only table, the data
and index are stored together. This is discussed in detail on Day 13, "Using Indexes and
Sequences."
• View--A view is a window into one or more tables. A view does not store any data; it
presents table data. A view can be queried, updated, and deleted as a table without restriction.
Views are typically used to simplify the user's perception of data access by providing limited
information from one table, or a set of information from several tables transparently. Views can
also be used to prevent some data from being accessed by the user or to create a join from
multiple tables.
• Stored procedure--A stored procedure is a predefined SQL query that is stored in the
data dictionary. Stored procedures are designed to allow more efficient queries. Using stored
procedures, you can reduce the amount of information that must be passed to the RDBMS and
thus reduce network traffic and improve performance.
• Database trigger--A database trigger is a procedure that is run automatically when an
event occurs. This procedure, which is defined by the administrator or developer, triggers, or is
run whenever this event occurs. This procedure could be an insert, a deletion, or even a
selection of data from a table.
• Sequence--The Oracle sequence generator is used to automatically generate a unique
sequence of numbers in cache. By using the sequence generator you can avoid the steps
necessary to create this sequence on your own such as locking the record that has the last value
of the sequence, generating a new value, and then unlocking the record.
Extents
Extents are the building blocks of segments; in turn, they consist of data blocks. An extent is used to
minimize the amount of wasted (empty) storage. As more and more data is entered into tablespaces in
your database, the extents used to store that data can grow or shrink as necessary. In this manner, many
tablespaces can share the same storage space without preallocating the divisions between those
tablespaces.
At tablespace-creation time, you can specify the minimum number of extents to allocate as well as the
number of extents to add at a time when that allocation has been used. This arrangement gives you
efficient control over the space used in your database.
Data Blocks
Data blocks are the smallest pieces of an Oracle database; they are physically stored on disk. Although
the data block in most systems is 2KB (2,048 bytes), you can change this size for efficiency depending
on your application or operating system.
NOTE: Oracle blocks do not need to be, and may not be the same as, operating system data
blocks. In fact, in most cases they are not.
The Oracle instance consists of the Oracle processes and shared memory necessary to access
information in the database. The instance is made up of the user processes, the Oracle background
processes, and the shared memory used by these processes (see Figure 2.3).
New Term: Oracle uses shared memory for several purposes, including caching of data and indexes as
well as storing shared program code. This shared memory is broken into various pieces, or memory
structures. The basic memory structures associated with Oracle are the System Global Area (SGA) and
the Program Global Area (PGA).
Oracle9i, like previous versions, supports heterogeneous services to allow data in non-Oracle database
to be queried using SQL. This support has been in the form of transparent gateways, which are vendor
specific, or generic connectivity which uses ODBC or OLEDB to make the connections. The
functionality supported by generic connectivity is typically more limited than that possible when using
vendor specific gateways, but it is quick and simple to configure. The steps listed below can be used to
connect Oracle to any ODBC compliant database (MS Access, SQL Server etc.) on the local server:
• Make sure a valid username and password are present for the non-Oracle database
(MyUser/MyPassword).
• Create an ODBC DataSource for the non-Oracle database (Access1).
• Create a file called: "ORACLE_HOME\hs\admin\initACCESS1.ora" which contains:
ACCESS1.WORLD =
(DESCRIPTION=
(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))
(CONNECT_DATA=(SID=ACCESS1))
(HS=OK)
)
(SID_DESC=
(SID_NAME=ACCESS1)
(ORACLE_HOME=D:\Oracle\Ora\9011)
(PROGRAM=hsodbc)
)
• Reload the listener: lsnrctl reload
• Create a database link using:
SELECT *
FROM mytable@ACCESS1.WORLD;
With MS Access the first query is slow due to the Access application being loaded. As expected,
subsequent queries do not display a similar lag.
August 2009
Master of Science in Information Technology (MScIT-NEW) – Semester 3
MT0046 –: Oracle9i– 4Credits
(Book ID: B0894)
Assignment Set – 2 (40 Marks)
Ans. :
the main objective of query processing in a distributed environment is to form a high level query on
a distributed database, which is seen as a single database by the users, into an efficient execution
strategy expressed in a low level language on local databases.
An important point of query processing is query optimization. Because many execution strategies
are correct transformations of the same high level query, the one that optimizes (minimizes)
resource consumption should be retained.
16
Objectives of query processing
– transform a high-level query on a DDB into an efficient execution strategy in a low-level language
on local DB’s
query processor
Selecting alternatives
– A calculus query may have many equivalent algebra queries
17
– Select execution strategy that could minimize
Execution time, or total resource consumption
» Computationally intractable problem
Choose a solution close to the optimum
_ In DDB
» communication cost for data exchange occurs
– select best sites
» to process data, and possibly
» the way data should be transferred
Ans.
The RMAN repository is the collection of metadata about the target databases that RMAN uses for
backup, recovery, and maintenance. RMAN always stores this information in records in the control
file. The version of this information in the control file is the authoritative record of RMAN's backups
of your database. This is one reason why protecting your control file is a important part of your
backup strategy. RMAN can conduct all necessary backup and recovery operations using just the
control file to store the RMAN repository information, and maintains all records necessary to meet
your configured retention policy.
You can also create a recovery catalog, an external Oracle database in which to store this
information. The control file has finite space for records of backup activities, while a recovery
catalog can store a much longer history. The added complexity of operating a recovery catalog
database can be offset by the convenience of having the extended backup history available if you
have to do a recovery that goes further back in time than the history in the control file.
There are also a few features of RMAN that only function when you use a recovery catalog. For
example, RMAN stored scripts are stored in the recovery catalog, so commands related to them
require the use of a recovery catalog. Other RMAN commands are specifically related to managing
the recovery catalog and so are not available (and not needed) if RMAN is not connected to a
recovery catalog.
The recovery catalog's version of the RMAN repository is maintained solely by RMAN. The target
instance never accesses it directly. RMAN propagates information about the database structure,
archived redo logs, backup sets, and datafile copies into the recovery catalog from the target
database's control file after any operation that updates the repository, and also before certain
operations.
• Enable the control file autobackup feature, which causes RMAN to automatically back up
the control file, and also enables RMAN to restore the control file autobackup without
access to a repository
• Keep a record of your DBID, which you may need to recover your database in the event
that you lose your control file
If you lose the current control files, then you can restore a control file autobackup even if you do
not use a recovery catalog.
19
Registration of Databases in the Recovery Catalog
The enrolling of a database in a recovery catalog is called registration. You can register more
than one target database in the same recovery catalog. For example, you can register databases
prod1, prod2, and prod3 in a single catalog owned by catowner in the database catdb. Because
RMAN distinguishes databases by unique database identifier (DBID), each database registered in
a given catalog must have a unique DBID.
• Datafile and archived redo log backup sets and backup pieces
• Datafile copies
If the recovery catalog is destroyed and no backups of it are available, then you can partially
reconstruct the catalog from the current control file or control file backups. Nevertheless, you
should always aim to have a valid, recent backup of the catalog.
Media Management
Oracle9s Media Management Layer (MML) API lets third-party vendors build a media manager,
software that works with RMAN and the vendor's hardware to allow backups to sequential media
devices such as tape drives. The media manager handles loading, unloading and labeling of
20
sequential media such as tapes. You must install media manager software to use RMAN with
sequential media devices.
When backing up or restoring, the RMAN client connects to the target instance and directs the
instance to send requests to its media manager. No direct communication occurs between the
RMAN client and media manager.
For example, this sequence of commands would configure channels for the media manager and
back up the database to the media manager:
RMAN does not issue specific commands to load, label, or unload tapes. When backing up, RMAN
gives the media manager a stream of bytes and associates a unique name with that stream. When
RMAN needs to restore the backup, it asks the media manager to retrieve the byte stream. All
details of how and where that stream is stored are handled entirely by the media manager.
The media manager labels and keeps track of the tape and names of files on each tape, and
automatically loads and unloads tapes, or signals an operator to do so.
Some media managers support proxy copy functionality, in which they handle the entire data
movement between datafiles and the backup devices. Such products may use technologies such
as high-speed connections between storage and media subsystems to reduce load on the primary
database server. RMAN provides a list of files requiring backup or restore to the media manager,
which in turn makes all decisions regarding how and when to move the data.
21
3. Discuss the relative advantages of centralized and distributed databases
Ans. :
A distributed database is a database that is under the control of a central database management
system (DBMS) in which storage devices are not all attached to a common CPU. It may be stored
in multiple computers located in the same physical location, or may be dispersed over a network of
interconnected computers.
Collections of data (eg. in a database) can be distributed across multiple physical locations. A
distributed database can reside on network servers on the Internet, on corporate intranets or
extranets, or on other company networks. Replication and distribution of databases improve
database performance at end-user worksites. [1]
22
4. What benefits does strict two-phase locking provide? What disadvantages result?
Ans.
The management of distributed transaction means with interrelated problem like reliability,
concurrency control and the efficient utilization of the resources of the complete system. The two
type of protocols like 2 phase commit protocol for recovery and 2 phase locking for concurrency
control.
The basic idea of locking is that whenever a transaction accesses a data item, it locks it, and that a
transaction which wants to lock a data item which is already locked by another transaction must
wait until the other transaction has released the lock (unlock).
A sophisticated locking mechanism known as 2-Phase locking which included the all said
principles is normally used.
SYNTEX:
(Begin application)
Begin transaction
Acquire locks before reading or writing
Commit
Release locks
(End application)
24
5. What are characteristics of distributed query processor? Explain
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.
i. 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.
25
_replace a join by a series of semi-joins
iii. Optimization timing
– Static
» optimization before execution, i.e., at compile time
_ needs to estimate the size of the intermediate results
_ good for queries executed multiple times
» (ex) R*
– Dynamic
» optimization at query execution time
v. Decision sites
» who makes a decision?
– Centralized
26
» single site determines the “best” schedule
» simple, but need knowledge about entire DDB
– Distributed
» cooperation among sites to determine the schedule
» need only local information
» cost of cooperation
– Hybrid
» one site makes a major decision, and
_ other sites can make local decisions
» (ex) System R*
27
– optimized fragment query (e.g., join ordering) with communication operations
_ Local query optimization
– optimized local query
28