Sie sind auf Seite 1von 40

Data Base Management System

Database management system is a collection of programs


that enable to defining, constructing and
maintain/manipulating the Database (designed, built and
populated data)

Advantages of DBMS
-Control of data redundancy
- Data consistency
- Sharing of data
- Improved data integrity
- Increased productivity
- Improved security
- Improved maintenance through data independence
- Enforcement of standards
- Increased concurrency
- Improved backup and recovery services

Disadvantage in File Processing System


-Complexity, Size, Cost of DBMSs, Additional H/W cost
- Cost of conversion, Performance, Higher impact of a failure

Relational DBMS Codds principle


Relational database is a precise, mathematical definition proposed by Dr.
E. F. Codd which was published in the paper, "A Relational Model of Data
for Large Shared Data Banks" . He also tailored an applied predicate
calculus called relational calculus and the languages used in it were DSL
alpha and QUEL.
0. A relational DBMS must be able to manage databases entirely through
its relational capabilities.
1. The Information Rule.
2. Guaranteed Access.
3. Systematic Null Value Support.
4. Active, On-Line Relational Catalog.
5. Comprehensive Data Sublanguage.
6. View Updating Rule.
7. Handling of Set-Level operators while Insertion, Update, and
Deletion.
8. Physical Data Independence. Application programs and ad hoc
programs are logically unaffected when physical access methods or
storage structures are altered.
9. Logical Data Independence. Application programs and ad hoc
programs are logically unaffected, to the extent possible, when changes
are made to the table structures.
10. Integrity Independence. The database language must be capable of
defining integrity rules. They must be stored in the on-line catalog, and

Types of Databases
1) OLTP (Operational/Online transaction processing) - Stores
dynamic information which changes constantly and reflects
up-to-the minute information. For Ex-Inventory, Order
maintenance and hotel booking.
2) Analytical or Data Warehousing Used to store historical
and time dependent data to track trends, view statistical
data over a long period of time or make long-term
projections.
3) Operational Data Stores Data in the Operational Data
Store are constantly refreshed so that the resulting image
reflects the latest state of operations. It stores collective
integrated operational data.
Unlike the OLTP has Subject-Oriented and enterprise-wide
data.
Unlike Data warehousing DB stores are volatile, current and

DB terms
Relation: table, Attribute: Column, Tuple: Record or Row
Functional dependencies : The attribute X is functionally dependent on Y if
t1[X]= t2[ X] and t1[Y]=t2[Y]. If t1 and t2 are tuples of a record.
Multi-valued dependencies: If two tuples t1 and t2 exist in r such that t1[X] =
t2[X] then t3 and t4 should also exist in r with the following properties
t3[x] = t4[X] = t1[X] = t2[X]
t3[Y] = t1[Y] and t4[Y] = t2[Y]
t3[Z] = t2[Z] and t4[Z] = t1[Z]
where [Z = (R-(X U Y)) ]
Fully Functionally dependent: A functional dependency X Y is full functional
dependency if removal of any attribute A from X means that the dependency
does not hold any more.
Transitive rule: If {X Y, Y Z} then X Z
Super key : Uniquely identify a database record.
Candidate key: Special subset of super keys that do not have any extraneous
information in them
Primary key : Unique and not null
Foreign key : A column in the child table is a actually a primary key other
parent table.

Normalization
What is normalization
It is a process of analyzing and breaking down the given
relation schemas based on their Functional Dependencies
(FDs) and primary key to achieve the properties such as:
Minimizing redundancy
Minimizing insertion, deletion and update anomalies.
There are often good reasons NOT to normalize, too: If your
database is largely a reporting platform, you may want to
persist duplicated data to avoid unnecessary joins. OLAP (online analytical processing) is just a big excuse not to
normalize.

Normalization Forms
Form

Defined by

First normal form Two versions: E.F. Codd


(1NF)
(1970), C.J. Date (2003)
Second normal
form (2NF)

E.F. Codd (1971)

Third normal
form (3NF)

E.F. Codd (1971) and also


Carlo Zaniolo's equivalent but
differently-expressed
definition (1982)

Boyce-Codd
normal form
(BCNF)
Fourth normal
form (4NF)

Raymond F. Boyce and E.F.


Codd (1974)
Ronald Fagin (1977)

Brief definition
Table faithfully represents a relation and has no
repeating groups. It is a relation in which the intersection
of each row and column contains one and only one value
No non-prime attribute in the table is functionally
dependent on a proper subset of a candidate key. It is a
relation that is in 1NF and every non-primary-key
attribute is fully functionally dependent on primary key.
Every non-prime attribute is non-transitively dependent
on every candidate key in the table. It is a relation that is
in 1NF, 2NF in which no non-primary-key attribute is
transitively dependent on the primary key.
Every non-trivial functional dependency in the table is a
dependency on a super key. It is a relation in which
every determinant is a candidate key
Every non-trivial multivalued dependency in the table is
a dependency on a superkey.
Every non-trivial join dependency in the table is implied
by the superkeys of the table. Its a relation that contains
no join dependency

Fifth normal
form (5NF)

Ronald Fagin (1979)

Domain/key
normal form
(DKNF)

Ronald Fagin (1981)

Every constraint on the table is a logical consequence of


the table's domain constraints and key constraints.

Sixth normal
form (6NF)

C.J. Date, Hugh Darwen, and


Nikos Lorentzos (2002)

Table features no non-trivial join dependencies at all


(with reference to generalized join operator)

Oracle Security
Systems analysts, database designers, database administrators,
application developers and users.
Default User ids
SYS, SYSTEM (password assigned during installation)
SCOTT/TIGER (sample user with DBA role)
Admin User ids
SYSDBA (SYS schema)
SYSOPER (PUBLIC schema)
DBA role
Data Dictionary
User
(_tables,_constraints, _tab_columns, _cons_columns)
System (_tables, _columns, _Indexes)

Oracle Architecture

Oracle Database
Oracle Database = Oracle Instance + data files
Oracle Instance = Memory Architecture + Process Architecture
Memory Architecture
The memory used for the DB operations. Broadly divided into SGA
(System Global Area) and PGA (Program Global Area)
SGA Contains following data structure
Database buffer cache
Redo log buffer
Shared pool
Java pool
Large pool (optional)
Data dictionary cache
Other miscellaneous information
We can also categorized SGA into fixed SGA and variable SGA.

Variable SGA contains 4 main components


we can alter the size of each of these components manually using
ALTER SYSTEM.
The size of each components of variable SGA is determined by INIT.ORA
parameters.
Database Buffer Cache - db_block_buffers
Redo Log Buffer - log_buffer
Shared Pool - shared_pool_size
Large Pool - Large_pool_size
We cannot however alter the size of Fixed SGA.
Database Buffer Cache - This is used to hold the data into the
memory.
When ever a user access the data, it gets fetched into database buffer
cache and it will be managed according to LRU (Least recently used)
algorithm.
Advantages
Saves fetch time when recently read data is fetched for another time
in short period of time.
The read is faster.
If a user is modifying the data, it can be modified in the buffer cache
which is a faster operation then modifying the data directly on the disk.
Redo Log Buffer This memory block hold the data which is going to
be written to redo log file. But instead of writing the data directly to the

Shared Pool - This contains 2 memory section, 1) Library Cache 2)


Dictionary Cache.
Library cache hold the parsed SQL statement and execution plans and
parsed PLSQL codes. Dictionary cache hold the information about user
privileges, tables and column definitions, passwords etc.
Large Pool - If defined then used for heavy operations such as bulk copy
during backup or during restore operation.
The total size of SGA is determined by a parameter SGA_MAX_SIZE.
Below is the simple calculation of memory sizes
SQL> show sga
Total System Global Area 577574308 bytes
Fixed Size 452004 bytes
Variable Size 402653184 bytes
Database Buffers 163840000 bytes
Redo Buffers 10629120 bytes
This will show fixed and variable size SGA. Fixed size SGA, as I said is not
in our control. However we can verify the size of variable SGA and other
memory values shown above.
Database Buffers 163840000 bytes
SQL> show parameters db_block_buffer
NAME TYPE VALUE

db_block_buffers integer 20000

This value is in terms of blocks. we can find the size of a block using
DB_BLOCK_SIZE parameter
SQL> show parameters db_block_size
NAME TYPE VALUE

db_block_size integer 8192
Database Buffers = db_block_buffers X db_block_size=20000 X 8192 =
163840000 bytes
Variable size = Shared Pool Size + Large Pool Size + Java Pool
size
SQL> SELECT pool, sum(bytes) from v$sgastat group by pool;
POOL SUM(BYTES)
java pool 50331648
shared pool 352321536
11069860
Variable size = 352321536 + 50331648 = 402653184 bytes
Program Global Area
PGA contains information about bind variables, sort areas, and other

The reason being that even though the parse information for SQL or
PLSQL may be available in library cache of shared pool, the value
upon which the user want to execute the select or update statement
cannot be shared. These values are stored in PGA. This is also called
Private Global Area.
Database buffer cache is again divided into 3 different types of
cache.
Default Cache
Keep Cache
Recycle Cache
default cache
Define the cache size using DB_CACHE_SIZE (or DB_BLOCK_BUFFER and
specify the block size).
The cache has a limited size, so not all the data on disk can fit in the cache.
When the cache is full, subsequent cache misses cause Oracle to write dirty
data already in the cache to disk to make room for the new data.
You can configure the database buffer cache with separate buffer pools that
either keep data in the buffer cache or make the buffers available for new
data immediately after using the data blocks.
The KEEP buffer pool retains the schema objects data blocks in memory. This
is defined using the INIT.ORA parameter DB_KEEP_CACHE_SIZE

Shared Pool Reserved Size


Over a period of time parsed SQL and executiono plan gets
fragmented and iverwritten which will also lead to larger packages
being aged out with new entries going into shared pool.
Hence access to such larger packages will take time to parse and
create execution plan. This might cause performance issues.
To avoid such situation, you can define a parameter
SHARED_POOL_RESERVED_SIZE. This will reserve some additional
space other then shared_pool_size.
If an object (either parsed SQL statement or execution plan) is
stored in reserved shared pool area then it will not age out.
For large allocations, the order in which Oracle attempts to allocate
space in the shared pool is the following:
1) From the unreserved part of the shared pool.
2) If there is not enough space in the unreserved part of the shared
pool, and if the allocation is large, then Oracle checks whether the
reserved pool has enough space.
3)If there is not enough space in the unreserved and reserved parts
of the shared pool, then Oracle attempts to free enough memory for
the allocation. It then retries the unreserved and reserved parts of
the shared pool.

Oracle Process Architecture

Process Architecture
Oracle has several process running in the background for proper functioning of
database.
Following are the main categories of process.

Server Process

Background Process
Server Process - to handle the requests of user processes connected to the instance.
Server processes (or the server portion of combined user/server processes)
created on behalf of each users application can perform one or more of the
following:

Parse and execute SQL statements issued through the application

Read necessary data blocks from datafiles on disk into the shared database buffers
of the SGA, if the blocks are not already present in the SGA

Return results in such a way that the application can process the information
Background Process - An Oracle instance can have many background processes; not
all are always present. The background processes in an Oracle instance include the
following:

Database Writer (DBW0 or DBWn)

Log Writer (LGWR)

Checkpoint (CKPT)

System Monitor (SMON)

Process Monitor (PMON)

Archiver (ARCn)

Recoverer (RECO)

Lock Manager Server (LMS) - Real Application Clusters only

Queue Monitor (QMNn)

Dispatcher (Dnnn)

Contd

Database writer (DBWn) - The database writer process (DBWn) writes the contents of
buffers to datafiles. The DBWn processes are responsible for writing modified (dirty)
buffers in the database buffer cache to disk. Although one database writer process
(DBW0) is adequate for most systems, you can configure additional processes (DBW1
through DBW9) to improve write performance if your system modifies data heavily. These
additional DBWn processes are not useful on uniprocessor systems.
Log Writer (LGWR) - The log writer process (LGWR) is responsible for redo log buffer
managementwriting the redo log buffer to a redo log file on disk. LGWR writes all redo
entries that have been copied into the buffer since the last time it wrote.
Checkpoint (CKPT) - When a checkpoint occurs, Oracle must update the headers of all
datafiles to record the details of the checkpoint. This is done by the CKPT process. The
CKPT process does not write blocks to disk; DBWn always performs that work.
System Monitor (SMON) - The system monitor process (SMON) performs crash
recovery, if necessary, at instance startup. SMON is also responsible for cleaning up
temporary segments that are no longer in use and for coalescing contiguous free extents
within dictionary-managed tablespaces. If any dead transactions were skipped during
crash and instance recovery because of file-read or offline errors, SMON recovers them
when the tablespace or file is brought back online. SMON wakes up regularly to check
whether it is needed.
Process Monitor (PMON) The process monitor (PMON) performs process recovery when a user process fails.
PMON is responsible for cleaning up the database buffer cache and freeing resources that
the user process was using. For example, it resets the status of the active transaction
table, releases locks, and removes the process ID from the list of active processes.
PMON periodically checks the status of dispatcher and server processes, and restarts any
that have died (but not any that Oracle has terminated intentionally). PMON also registers
information about the instance and dispatcher processes with the network listener.

Contd
Archiver Process (ARCn) The archiver process (ARCn) copies online redo log files to a designated storage
device after a log switch has occurred. ARCn processes are present only when the
database is in ARCHIVELOG mode, and automatic archiving is enabled.
An Oracle instance can have up to 10 ARCn processes (ARC0 to ARC9). The LGWR
process starts a new ARCn process whenever the current number of ARCn processes
is insufficient to handle the workload. The ALERT file keeps a record of when LGWR
starts a new ARCn process.
Recoverer (RECO) - The recoverer process (RECO) is a background process used
with the distributed database configuration that automatically resolves failures
involving distributed transactions. The RECO process of a node automatically
connects to other databases involved in an in-doubt distributed transaction. When
the RECO process reestablishes a connection between involved database servers, it
automatically resolves all in-doubt transactions, removing from each databases
pending transaction table any rows that correspond to the resolved in-doubt
transactions.
The RECO process is present only if the instance permits distributed transactions
and if the DISTRIBUTED_TRANSACTIONS parameter is greater than zero. If this
initialization parameter is zero, RECO is not created during instance startup.
Lock Manager Server (LMS) - In Oracle9i Real Application Clusters, a Lock Manager
Server process (LMS) provides inter-instance resource management.
Queue Monitor (QMNn) - The queue monitor process is an optional background
process for Oracle Advanced Queuing, which monitors the message queues. You can
configure up to 10 queue monitor processes. These processes, like the Jnnn
processes, are different from other Oracle background processes in that process
failure does not cause the instance to fail.

Oracle Files
Datafile

Redo Log Files


Control Files
Parameter File
Archive File
Log File (alert*.log, sqlnet.log, listener.log...)
Trace File

Storage Architecture

Physical storage structures

Data files
Segments
Extents
Blocks
Logical storage structures
Tablespaces
Tables / Clusters / Indexes
Rows
Columns

Physical Storage
Architecture
Relationship among Segments, Extents, and Blocks
Segment
96K

Extent

Extent

24K

72K

2K
2K
2K
2K
2K
2K
2K
2K
2K
2K

2K
2K
2K
2K
2K
2K
2K
2K
2K
2K

2K
2K
2K
2K
2K
2K
2K
2K
2K
2K

2K
2K
2K
2K
2K
2K
2K
2K
2K
2K
2K
2K

Database Blocks

Segment
a set of one or more extents that contains all the data for a specific type of
logical storage structure within a tablespace
Data Segment
- A collection of extents that holds all of the data for a table or a cluster
Index Segment
- A collection of extents that holds all of the index data for search
optimization on large tables and clusters
Rollback Segment
- A collection of extents that holds rollback data for rollback, readconsistency, or recovery
Temporary segment
- A collection of extents that holds data belonging to temporary tables
created during a sort operation
Bootstrap segment
- An extent that contains dictionary definitions for dictionary tables to be
loaded when the database is opened.

Extent
A set of contiguous database blocks within a
datafile.
Extent are allocated when.
- The segment is created (INITIAL EXTENT)
- The segments grows (NEXT EXTENT)
- The table is altered to allocate extents.

Extent are de-allocated when the


- The segment is dropped and truncated.
- The segment is larger than optimal and contains free extents
(for rollback segments only)

Each segment is created with at least on


extend( initial extent )
( Rollback segment : 2)

Block
General Block Information
(Block add, Segment type)
85 ~ 100 bytes
Table info in Cluster

Header

Table Dictionary

Row Dictionary
Free Space

Row info in Block


(2 byte per row)
using when New Row
Insert or Update
(pctfree, pctused)
Table or Index Data

Row Data

Logical Storage Architecture


Relationship between tablespaces and
datafiles
Database
System Tablespace

DATA1.ORA

DATA2.ORA

USER Tablespace

DATA3.ORA

Contd
Objects stored in tablespaces
Tablespace (one or more datafiles)

Table
INDEX
INDEX

Database Files

Table

INDEX

INDEX

INDEX

INDEX

INDEX

INDEX

INDEX

(Physical structures associated


with only one tablespace)

Objects

INDEX
Table

(stored in tablespace may


span serveral datafiles)

Oracle Data Storage and


Row Id

Oracle Client/Server
Architecture
NETWORK
Server b

Client
Application

Server/Server

Client/Server

Server A
Benefit of Client/Server Component
- Database S/W work on Server
- Only Server upgrade to increase size
- Minimize network resource
- Minimize Client H/W spec
- concurrency, consistency, transparency
- concurrency, consistency, transparency

SQL*Net
What is SQL*Net?
- Oracles Client/Server middleware product
- transparent connection from client tool to DB ( from on DB to another )
- works across multiple network protocol and operation system

What is TNS?
- Transparent Network Substrate
- Oracles Network applications to access the underlying network protocols
transparently
- TNS-based application, Oracle Protocol Adapters, Network software like TCP/IP

Configuration File
-

TNSNAME.ORA ( Client )
TNSNAV.ORA ( Client )
SQLNET.ORA ( Client, Server )
LISTENER.ORA ( Server )

SQL Statement Processing

SELECT ename FROM emp;


1. Parse

3. Fetch

SGA
Shared Pool Database Buffer

SGA
Shared Pool Database Buffer

Datafiles
User
Process

Datafiles

Server
Process

User
Process

SGA
Shared Pool Database Buffer

Smith
Griego
Begay
Chen

2. Execute

Datafiles
User
Process

Server
Process

Server
Process

SQL
SQL is the basic language used to manipulate and
retrieve data from database
Strutured English Querylanguage (pronounced
"sequel")
SQL is used by all DBMS like DB2, Oracle, Microsoft's
SQL Server and Access, Sybase, Informix, MySQL,
mSQL etc.
The early versions of SQL like SQL86, SQL89, and
SQL92 (SQL2) etc were combined in to SQL99 with
implementing more features.
Later on SQL2003 and SQL2008 were also standardize

SQL History
Dat Events
e
1970

Dr E.F. Tedd Codd defines relational database model

1974

IBMs System/R project begins. First article describing the


SEQUEL language.

1978

System/R customer tests

1979

Oracle introduces first commercial RDBMS

1981

Relational Technology introduces Ingres. IBM announces


SQL/DS.

1982

ANSI forms SQL standards committee.

1983

IBM announces DB2.

1986

ANSI SQL1 standard ratified. Sybase introduces RDBMS for


transaction processing

1987

ISO SQL1 standard ratified.

1988

Ashton-Tate and Microsoft announce SQL Server for OS/2

1989

First TPC benchmark (TPC-A) published

1990

SQL History-cont.,
Date Events
1991

SQL Access Group database access specification


published.

1992

Microsoft publishes ODBC specification. ANSI SQL2


standard ratified. TPC-C (OLTP) benchmark published.

1993

First shipment of specialized SQL data warehousing


systems, ODBC products.

1994

TPC-D (decision support) benchmark published.


Commercial shipment of parallel database server
technology.

1996

Publication of standard API for OLAP database access


and OLAP benchmark.

1997

IBM DB2 UDB unifies DB2 architecture across IBM &


other vendor platforms. Major DBMS vendor announces
Java integration strategies.

1998

Microsoft SQL server 7 provides enterprise-level


database support for Windows NT. Oracle 8i provides
database/Internet integration and moves away from
client/server model.

SQL statement category


Data Manipulation
Language DML
Used to access, create,
modify, or delete data in
the existing structures of
the database.

Data Definition Language


DDL
Used to define, alter, or
drop database objects and
their privileges

SELECT
INSERT
UPDATE
DELETE),
conditional update or insert
operation
MERGE
EXPLAIN PLAN
LOCK TABLE
CREATE
ALTER
DROP
RENAME
TRUNCATE
GRANT,
REVOKE
AUDIT/ NOAUDIT
COMMENT

Transaction
Control

COMMIT
ROLLBACK
SAVEPOINT
SET TRANSACTION

Session Control

SET ROLE
ALTER SESSION

System Control

ALTER SYSTEM

Static SQL

Static SQL is fixed SQL code, written in


advance rather than generated at runtime
used in applications. Fixed SQL code also
may contain variables or parameters that
pass values to or from an application.

Dynamic SQL

Dynamic SQL is SQL code generated by an


application itself at runtime. It cannot be
determined at the time the application is
written. It depends on choices that the user
makes. This form of SQL often can be
generated by tools such as graphical query
language in response to user activity.

Dual
SQL> desc dual
Name
Null? Type
----------------------------------------- -------- ---------------------------DUMMY
VARCHAR2(1)
SQL> select * from dual;
D
X
SQL> select 'SQL Training' as Heading from dual;
HEADING
-----------SQL Training

SQL*PLUS
It is the default oracle client to connect to any server.
In windows the connection is done as:

In DOS/unix, the connection is done as:


sqlplus username/password@connectstring
afiedt.buf is the default SQL PLUS buffer editor.
List, Append, Change, Input, del and clear buffer are the instructions
used.

SQL PLUS commands


Command Purpose
@ Runs the SQL*Plus statements in the specified script or command
file.
@@ Runs the specified command file. This command is similar to the
@ command, useful for running nested command
files because it looks for the specified command file in the same path
as the command file from which it was called.
/ Executes the SQL command from the SQL buffer.
ACCEPT Reads a line of input and stores it in a variable.
APPEND Adds text to the end of the current line in the SQL buffer.
BREAK Specifies where and how to make format changes to a report.
BTITLE Similar to TTITLE, but provides a title at the bottom of each
page.
CHANGE Changes the first occurrence of the specified text on the
current line of the SQL buffer.
CLEAR Erases the current format settings for columns, breaks,
computes, etc.
COLUMN Defines display attributes for a column.
COMPUTE Defines and prints summary lines.
CONNECT Connects a given username to Oracle.
DEFINE Defines a user variable and assigns it a CHAR value.

SQL PLUS commands


Command Purpose
DESCRIBE Lists the column definitions for a table, view, or synonym,
or the specifications for a function or procedure.
DISCONNECT Commits pending changes to the database and logs the
current user out of Oracle, but does not exit SQL*Plus.
EDIT Edits the SQL buffer or a file.
EXIT Disconnects from Oracle and terminates SQL*Plus.
GET Loads text from a command file to the SQL buffer.
HELP Gets help on a SQL*Plus command.
INPUT Adds one or more lines of text after the current line in the SQL
buffer.
LIST Lists one or more lines of the SQL buffer.
PASSWORD Allows you to change a password without echoing the
password.
PAUSE Displays the specified text, then waits for the user to press
Return after displaying a page of query results.
PROMPT Displays the specified message or a blank line on the
screen.

SQL PLUS commands


Command Purpose
QUIT Similar to EXIT.
REMARK Specifies single-line comments.
REPFOOTER Provides a report footer, appearing once for each report.
REPHEADER Provides a report header, appearing once for each
report.
RUN Executes the SQL command currently in the SQL buffer.
SAVE Saves the contents of the SQL buffer in an operating system
file.
SET Sets a system variable to alter the SQL*Plus environment
settings.
SHOW Shows the value of a SQL*Plus system variable or the current
SQL*Plus environment.
SPOOL Stores query results in an operating system file.
START Runs the SQL*Plus statements in the specified command file.
STORE Saves attributes of the current SQL*Plus environment in a
command file.
TTITLE Places and formats a title at the top of each report page.
UNDEFINE Deletes one or more user variables that are defined.

Next Day
SQL commands
String and Date Functions
Advanced SQL commands

Das könnte Ihnen auch gefallen