Anda di halaman 1dari 19

c 

  


  SQL that is abbreviated from Structured Query Languages is a data oriented language
for selecting and operating sets of data. SQL is commonly used by relational database
technologies such as Oracle, Microsoft Access, and Sybase etc. The term, PL/SQL is a procedural
extension of SQL. It is the Oracle native programming language that provides an edge to design
database-centric application. Both SQL and PL/SQL languages are designed with a common
purpose, to access data within Oracle databases, but PL/SQL is designed to address the
limitation of SQL because it cannot be used to manipulate procedural programming with
conditional, iterative and sequential statements. To work with PL/SQL, the users normally
interact with an application that uses SQL to access a relational database on the back-end.

SQL is widely popular as data oriented language for selecting and executing the sets of data. It
is differentiated from the PL/SQL in relation to its capability to create applications that may be
the source of data for your screens, web pages and reports while PL/SQL, known as a
procedural language, might be used to create format and display those screens, web page and
reports that are designed with the help of SQL. It also acts as an application language like Java
or PHP can.

The commands and instructions used in SQL aren͛t actually a part of the SQL standard but are
supported by the tools of SQL and are usually executed to assist format output like BREAK,
BTITLE, COLUMN, PRINT or they are meant to create or store data/scripts. It also supports the
commands that directly interact with the database to perform the SHUTDOWN, CONNECT or
COPY actions. PL/SQL is also known as fourth-generation programming language because of its
ability to support the data encapsulation, overloading, collection types, exceptions, and
information hiding functions. PL/SQL also offers rapid prototyping and use variables and their
declarations, conditional controls like IF and CASE used in other programming languages like C,
C++, Java, etc. It also uses looping structure such as LOOP, FOR LOOP and WHILE LOOP.

Another advantage of using PL/SQL is its fast execution speed because it passes a block of
statements to be executed to the server, whereas in SQL, only one statement is transferred to
the server at a time which is a time consuming process.

Summary:
SQL is a structured query language used for data manipulation, whereas PL/SQL is a procedural
language to design applications.
PL/SQL is an application language usually used to build, format and display the user screens,
web pages and reports, while SQL provides data for these applications.
The user can embed SQL in a PL/SQL program or statement. But the vice versa is not possible.
SQL is slower because it executes one statement at a time, while PL/SQL executes as a block of
code.
The core use of SQL is to code queries, data manipulation and development statements, but
PL/SQL is broadly used to code program blocks, triggers, functions, etc.
È      
Ö [       
      
!

"# " # $# 


"%c &È 
 
Ê 

    

   
 
 
 Ê !" #!$  
Ê%Ê&   '!(

)


A !# "!'  ( 


! h) !'     
! ! h) '  
! o'  
! (*  '  

 : The types of following Data Fragmentation Mechanisms are as follows:

! h) ' 

Horizontal Fragmentation consists of partitioning the tuples of a global relation into subsets;
this is clearly useful in distributed databases, where each subset can contain data that have
common geographical properties. It can be defined by expressing each fragment as a selection
operation on the global relation.

* let a global relation be

m m
 
  

Then the horizontal fragmentation can be defined in the following way:

m 1 =    + ,-

m  =    + ,-


Now let us verify whether this fragmentation fulfills the conditions stated earlier.

r    If ͞Manipal͟ and ͞Udupi͟ are the only possible values of the  
attribute, then it satisfies this condition.

r #    can be verified easily, because it is always possible to reconstruct
the m global relation through the following operation.

m m 1 +.m È

The /    is clearly verified.

# : The predicate, which is used in the selection operation and defines a fragment, is
called asQualification. For instance, in the above example the qualifications

q1 :  = ͞Manipal͟

qÈ :  = ͞Udupi͟

We can generalize from the above example that in order to satisfy the completeness condition,
the set of qualifications of all fragments must be complete, at least with respect to the set of
allowed values. The reconstruction condition is always satisfied through the union operation,
and the disjoint ness condition requires that qualifications be mutually exclusive.

! ! h) ' 

This is a type of fragmentation, which is derived from the horizontal fragmentation of another
relation.

*: Consider a global relation

m m

 
 

where m is a supplier number. If it is required that a fragment has to contain the tuples for
suppliers, which are in a given city, and then we have to go for derived fragmentation. A semi-
join operation with the fragments SUPLIER1 and SUPLIERÈ is needed in order to determine the
tuples of m 
which correspond to the suppliers in a given city. The derived fragmentation
of m can be therefore defined as follows:

m 1 = m  SNUM=SNUMm 1

m  = m  SNUM=SNUMm È


The # of the global relation m can be performed through the union
operation as was shown for m 

r  of the above fragmentation requires that there be no supplier numbers in
the SUPPLY relation, which are not contained also in the SUPPLIER relation. This is a typical, and
reasonable, integrity constraint for this database and usually is called as the  
 "constraint.

r /    is satisfied if a tuple of the SUPPLY relation does not correspond to
two tuples of the SUPPLIER relation that belong to two different fragments. In this case this
condition is easily verified, because the supplier numbers are unique keys of the SUPPLIER
relation.

! o' 

The o  !of a global relation is the subdivision of its attributes into groups;
fragments are obtained by /  the global relation over each group. This can be useful in
distributed databases where each group of attributes can contain data that have common
geographical properties. The fragmentation is correct; if each attribute is mapped into at least
one attribute of the fragments; moreover, it must be possible to reconstruct the original
relation by joining the fragments together.

* Consider a global relation

EMP (EMPNUM, NAME, SAL, TAX, MGRNUM, DEPTNUM)

A vertical fragmentation of this relation can be defines as

EMP1= EMPNUM,NAME, MGRNUM, DEPTNUM EMP

EMPÈ= EMPNUM, SAL,TAX EMP

The reconstruction of relation EMP can be obtained as

EMP=EMP1 .EMPNUM=EMPNUM EMPÈ

This is because; EMPNUM is a key of EMP.

Let us draw some important points to be noted from this example.

· The purpose of including the " of the global relation into each fragment is to ensure the
reconstruction property.
· An alterative way to provide the reconstruction property is to generate #   that
are used as system-controlled keys. This can be convenient in order to avoid the replication of
large keys; moreover, users cannot modify tuple identifiers.

Let us finally consider the problem of fragment disjoint ness. First, we have seen that at least
the key should be replicated in all fragments in order to allow reconstruction. In fact, if we
include the same attribute in two different vertical fragments, we know exactly that the column
that corresponds to this attribute.

For example, consider the following vertical fragmentation of relation  :

 1 =   
 
"
   

 È =   
 
m 
#  

The attribute  is replicated in both fragments. We can remove this attribute when we
reconstruct relation  through an additional projection operation.

 =  1 .      
m 
 # È

! (* ' 

The fragments that are obtained by the above fragmentation operations are relations
themselves, so that it is possible to apply the fragmentation operations recursively, provided
that the correctness conditions are satisfied each time. The reconstruction can be obtained by
applying the reconstruction rules in reverse order.

*Consider the same global relation

  
 
m 
 #
 "
  

The following is a mixed fragmentation, which is obtained by applying the vertical


fragmentation of the previous example, followed by a horizontal fragmentation on   $
' 0 0r   

The reconstruction of relation  is defined by the following expression:

 = +.( 1, 


 %) . =  

  
m 
 # &

A fragmentation tree can conveniently represent mixed fragmentation (as shown in the above
figure). In a fragmentation tree, the root corresponds to a global relation, the leaves
corresponds to the leaves correspond to the fragments, and the intermediate nodes
correspond to the intermediate results of the fragment-defining expressions.

r1( 2!!3:

The following codes shows the global and fragmentation schemata of EXAMPLE_DDB. Most of
the global relations of EXAMPLE_DDB and their fragmentation have been already introduced. A
 relation, horizontally fragmented into three fragments on the value of the   
attribute, is added.

[

  
 
m 
 #
 "
  

  
 
 
 " 

m m

 
 
'  

 1 =   £c   
 
 "
   

 È = c   £È   
 
 "
   

 3 =   >È   
 
 "
   

 4 =   
 
m 
 # 

 1=   'c  

 È= c   '  

 3=   (  

m 1 =  )m*m 

m È =  ) m 

m 1 = m  m m m 1

m È = m  m m m È

å h "#"" 4

  ris a logical grouping of database objects, usually to facilitate security,


performance, or the availability of database objects such as tables and indexes. A tablespace is
composed of one or more datafiles on disk.

A tablespace is the highest level of logical objects in the database. A database consists of one or
more tablespaces. A tablespace will frequently group together similar objects, such as tables,
for a specific business area or a specific function. A particular tablespace can be reorganized,
backed up, and so forth with minimal impact to other users whose data may be in other
tablespaces.

All Oracle databases must have at least one tablespace: the SYSTEM tablespace. Having more
than one tablespace is highly recommended when creating a database. In the figure 6.1 logical
structures, you can see the SYSTEM tablespace and two others.

A!3is the smallest unit of allocation in an Oracle database. One or more database
blocks compose a database extent. At the other end of the spectrum of logical objects is the
 (also known as an Oracle block), the smallest unit of storage in an Oracle
database. Every database block in a tablespace has the same number of bytes. As of Oracle9i,
different tablespaces within a database can have database blocks with different sizes. Typically,
one or more rows of a table will reside in a database block, although very long rows may span
several database blocks.

Extents group together logically contiguous database blocks in a tablespace. All database blocks
within a single extent will store the same kind of information. A database block can have a size
of ÈKB, 4KB, 8KB, 16KB, or 3ÈKB. Once any tablespace, including the SYSTEM tablespace, is
created with a given block size, it cannot be changed. If you want the tablespace to have a
larger or smaller block size, you need to create a new tablespace with the new block size, move
the objects from the old tablespace to the new tablespace, and then drop the old tablespace.



   5È
c * ##    

#* 

  ,   


PL/SQL provides iterative control and execution of PL/SQL statements in the block. This is the
ability to repeat or skip sections of a code block. Following are the four types of iterative
statements provided by the PL/SQL

· The Loop statement

· The WHILE Loop statement

· The GOTO statement

· FOR Loop

!   

A loop repeats a sequence of statements. The format is as follows.

LOOP

Statements

END LOOP;

The one or more PL/SQL statements can be written between the key words LOOP and END
LOOP. Once a LOOP begins to run, it will go on forever. Hence loops are always accompanied by
a conditional statement that keeps control on the number of times it is executed. We can also
build user defined exists from a loop, where required.

*: LOOP

Cntr : = cntr + 1;

IF cntr > 100

EXIT;

END IF;
END LOOP;

EXIT statement brings the control out of loop if the condition is satisfied.

!  

The WHILE loop enables you to evaluate a condition before a sequence of statements would be
executed. If condition is TRUE then sequence of statements are executed. This is different from
the FOR loop where you must execute the loop atleast once. The syntax for the WHILE loop is
as follows:

m
: WHILE < Condition is TRUE > LOOP

< Statements >

END LOOP;

* : DECLARE

Count NUMBER(È) : = 0;

BEGIN

WHILE count < = 10 LOOP

Count : = count + 1;

Message(͛while loop executes͛);

END LOOP;

END;

    :

EXIT and EXIT WHEN statements enable you to escape out of the control of a loop. The format
of the EXIT statement is as follows:

m
: EXIT;

EXIT WHEN statements have following syntax

m
: EXIT WHEN <condition is true >;
EXIT WHEN statement enables you to specify the condition required to exit the execution of the
loop. In this case no if statement is required.

*6c: IF count > = 10 EXIT;

*6È: EXIT WHEN count > = 10;

! r[r  

The GOTO statement allows you to change the flow of control within a PL/SQL block. The syntax
is as follows

m
: GOTO <label name>;

The label is surrounded by double brackets (&&%%) and label must not have a semi colon after
the label name. The label name does not contain a semi colon because it is not a PL/SQL
statement. But rather than identifier of a block of PL/SQL code. You must have at least one
statement after the label otherwise an error will result. The GOTO destination must be in the
same block, at the same level as or higher than the GOTO statement itself.

*: IF result = ͚fail͛ THEN

GOTO failed_stud

END IF;

<<failed_stud>>

Message (͛student is failed͛);

The entry point of the destination block is defined within << >> as shown above, i.e. labels are
written within the symbol &&%%. Notice that <<failed_stud>> is a label and it is not ended with
semicolon ( 7 ).

! '- 

The FOR loop will allow us to execute a block of code repeatedly until some condition occurs.
The syntax of FOR loop is as follows.

m
:

FOR loop_index IN [ REVERSE] low_value High_value LOOP

Statements to execute
END LOOP;

The loop_index is defined by oracle as a local variable of type integer. REVERSE allows you to
execute the loop in reverse order. The low_value High_value is the range to execute the loop.
These can be constants or variables. The line must be terminated with loop with no semicolon
at the end of this line. You can list the statements to be executed until the loop is executed is
evaluated to false.

*: FOR v_count IN 1 5 LOOP

Message (͛for loop executes͛);

END LOOP;

In the above example the message ͚for loop executes͛ is displayed five times.

We can terminate the FOR loop permanently using EXIT statement based on some BOOLEAN
condition. Nesting of FOR loop can also be allowed in PL/SQL. The outer loop executed once,
and then the inner loop is executed as many times as the range indicates, and then the control
is returned to the outer loop until its range expires.

*: FOR out_count IN 1..È LOOP

FOR in_count IN 1..È LOOP

Message (͛nested for loop͛);

END LOOP;

END LOOP;

In the above example the message ͚nested for loop͛ is displayed four times.

Let us discuss some examples from the understanding how to write a PL/SQL block structure.
Here we assume that a table called "EMP" is created and the datas are already inserted into it.

r (

Create table EMP

( emp_no NUMBER (3),

name VARCHARÈ (15),

salary NUMBER (6,È),


dept VARCHARÈ (15),

div VARCHARÈ (È) );

1( 6c:

DECLARE

num NUMBER (3);

sal emp salary %TYPE;

emp_name emp name %TYPE;

count NUMBER (È) : = 1;

starting_emp CONSTANT NUMBER(3) : = 134;

BEGIN

SELECT name, salary INTO emp_name, sal

FROM EMP

WHERE emp_no = starting_emp;

WHILE sal < 4000.00 LOOP

Count : = count + 1;

SELECT emp_no, name, salary INTO

Num, emp_name, sal FROM EMP

WHERE emp_no > È150;

END LOOP;

Commit;

END;

In the above example there are five statements in the declaration part. The  is a integer
type,   and   takes the similar data type of the   
and  columns of EMP
table respectively. Ê  is a variable of type integer and takes initial value 1. m    is a
constant and it is of integer type with immediately assigned value 134.

Between BEGIN and END key words, there are some SQL executable statements used for
manipulating the table data. The SELECT statement extracts data stored in and   

columns of EMP table corresponding to the employee having employee number 134. It stores
those values In the variables   and  respectively.

If   less than 4000 then the statements within the loop will be executed. Within the loop,
there are two SQL statements, the first one increments the count value by 1 and the second
statement is a SELECT statement. The commit statement commits the changes made to that
table. The END statement terminates the PL/SQL block.

1( 6È

This example assumes the existence of table accounts created by using the following SQL
statements.

Create table Accounts

(accnt_id NUMBER(3),

name VARCHARÈ(È5),

bal NUMBER(6,È)

);


È  
#      # 
 
create or replace FUNCTION fac (n POSITIVE) RETURN INTEGER IS -- returns n!
BEGIN
IF n = 1 THEN -- terminating condition
RETURN 1;
ELSE
RETURN n * fac(n - 1); -- recursive call
END IF;
END fac;
/

SQL> select fac(5) from dual;

1È0
SQL> select fac(6) from dual;

7È0

A !    "   


 

r  ʹ The contents of this memory area are shared by multiple users and hence
the name shared pool. Two specific caches form the shared pool. The sizes of the two caches
are not set individually and are automatically determined by the Oracle Server. However it is
possible to set the overall size of the Shared pool by using the SHARED_POOL_SIZE initialization
parameter.

r ʹ This is an optional pool. The size of this memory area is determined by the
LARGE_POOL_SIZE initialization parameter. It is used for handling large I/O requests of server
processes. Its main functions are to provide memory for session memory (UGA) for the shared
server environment, parallel execution message buffers.

  
å # 8/   
Ö   ##
Ö !3
Ö "  ##
 

  ##

The Oracle database is divided into increasingly smaller logical units to manage, store, and
retrieve data efficiently and quickly. The figure 4.1 shows the relationships between the logical
structures of the database like

· tablespaces,

· segments,

· extents, and

· blocks
'#å c ## 

Structures in an Oracle database that a database user would see, such as a table, as opposed to
the underlying physical structures at the data file level. The logical storage management of the
database͛s data is independent of the physical storage of the database͛s physical files on disk.
This makes it possible for changes to the physical structures to be transparent to the database
user at the logical level.

ris a logical grouping of database objects, usually to facilitate security, performance,


or the availability of database objects such as tables and indexes. A tablespace is composed of
one or more datafiles on disk.

A tablespace is the highest level of logical objects in the database. A database consists of one or
more tablespaces. A tablespace will frequently group together similar objects, such as tables,
for a specific business area or a specific function. A particular tablespace can be reorganized,
backed up, and so forth with minimal impact to other users whose data may be in other
tablespaces.

All Oracle databases must have at least one tablespace: the SYSTEM tablespace. Having more
than one tablespace is highly recommended when creating a database. In the figure 6.1 logical
structures, you can see the SYSTEM tablespace and two others.

 is a set of extents allocated for a single type of object, such as a table.A tablespace is
further broken down into  . A database segment is a type of object that a user typically
sees, such as a table. Tablespace1 in the logical structure figure 4.1 consists of three segments,
which could be tables, indexes, and so forth. It͛s important to note that this is the logical
representation of these objects; the physical representation of these objects in the operating
system files will most likely not resemble the logical representation.

* is a contiguous group of blocks allocated for use as part of a table, index, and so forth.
The next lowest logical grouping in a database is the extent. A segment groups one or more
extents allocated for a specific type of object in the database. SegmentÈ in the logical structure
illustration consists of two extents. Note that an extent cannot be shared between two
segments. Also, a segment, and subsequently an extent, cannot cross a tablespace boundary.

!3

A!3is the smallest unit of allocation in an Oracle database. One or more database
blocks compose a database extent. At the other end of the spectrum of logical objects is the
 (also known as an Oracle block), the smallest unit of storage in an Oracle
database. Every database block in a tablespace has the same number of bytes. As of Oracle9i,
different tablespaces within a database can have database blocks with different sizes. Typically,
one or more rows of a table will reside in a database block, although very long rows may span
several database blocks.

Extents group together logically contiguous database blocks in a tablespace. All database blocks
within a single extent will store the same kind of information. A database block can have a size
of ÈKB, 4KB, 8KB, 16KB, or 3ÈKB. Once any tablespace, including the SYSTEM tablespace, is
created with a given block size, it cannot be changed. If you want the tablespace to have a
larger or smaller block size, you need to create a new tablespace with the new block size, move
the objects from the old tablespace to the new tablespace, and then drop the old tablespace.

is a named group of objects associated with a particular user account, such as tables,
indexes, functions, and so forth.A  is another logical structure that can classify or group
database objects. A schema has a one-to-one correspondence with a user account in the Oracle
database, although some schemas may be designed to hold only objects that may be
referenced by other database users. For instance, in the logical structure illustration, Segments
1 and 3 may be owned by the HR schema, while Segment È may be owned by the SCOTT
schema.

A schema is not directly related to a tablespace or any other logical storage structure; the
objects that belong to a schema may be in many different tablespaces. Conversely, a tablespace
may hold objects for many different schemas. A schema is a good way to group objects in the
database for purposes of security and access control.

"  ##

" ##is the structure of an Oracle database, such as datafiles on disk that are
not directly manipulated by users of the database. Physical structures exist at the operating
system level.
From the perspective of building queries and running reports, regular users don͛t need to know
much about the underlying physical structure of the database on disk. However, DBAs do need
to understand these database components.

The "## of the Oracle database consists of datafiles, redo log files, and control
files. On a day-to-day basis, the DBA will deal most often with the datafiles, since this is where
all of the user and system objects, such as tables and indexes, are stored. The figure 4.È shows
the physical structure and its relationship to the Oracle memory structures and logical storage
structures.

'#å È"## 

!'that contains all of the database data that the users of the database save and
retrieve using SELECT and other DML statements. The  in a database contain all of the
database data that the users of the database save and retrieve. A single data file is an operating
system file on the server͛s disk. Each data file belongs to only one tablespace; a tablespace can
have many datafiles associated with it.

A tablespace comprises one or more datafiles. There are four physical data files in the database
in the physical structure illustration: one is used for the SYSTEM tablespace, two data files are
assigned to Tablespace1, and the fourth data file is assigned to TablespaceÈ.
- Files that contain a record of all changes made to both the data in tables and
indexes, as well as changes to the database structures themselves. The   facilitate
the Oracle mechanism to recover from an instance failure or a media failure. When any changes
are made to the database, such as updates to data or creating or dropping database objects,
the changes are recorded to the redo log files first.

A database has at least two redo log files, and it is recommended that multiple copies of the
redo log files be stored on different disks. (Oracle automatically keeps the multiple copies in
synch.) If the instance fails, any changed database blocks that were not yet written to the
datafiles are retrieved from the redo log files and written to the datafiles when the instance is
started again. These files are used to recover changed data that was in memory at the time of a
crash.

 'is a file that records the physical structure of a database, the database name, and
the names and locations of datafiles and redo log files.

The   maintains information about the physical structure of the entire database. It
stores the name of the database, the names and locations of the tablespaces in the database,
the locations of the redo log files, information about the last backup of each tablespace in the
database, and much more. Because of the importance of this file, it is recommended that a
copy of the control file reside on at least three different physical disks. As with the redo log
files, Oracle keeps all copies of the control file in synch automatically.

The control file and redo log file contents do not map directly to any database objects, but their
contents and status are available to the DBA by accessing virtual tables called data dictionary
views, which are owned by the SYS schema.

Anda mungkin juga menyukai