Saturday, June 15, 2013

Manual Database Creation

In order to manually create a database, mandb,  follow the steps-

Startup the current database.

Create pfile from spfile.
SQL> create pfile=’ /home/app/oracle/product/10.2.0/db_1/dbs/initmandb.ora’ from spfile;
Note: If there are more than one database you should provide the path and name of spfile.

Create a database creation script as, controlfile script.
SQL> alter database backup controlfile to trace as =’/home/app/oracle/product/10.2.0/      db_1/dbs/mandbctl.sql’;
SQL>exit;

Edit the pfile ( dbname, controfile path, dump folders path)
[oracle@localhost ~]$ cd $ORACLE_HOME
[oracle@localhost db_1]$ cd dbs
[oracle@localhost dbs]$ pwd
/home/app/oracle/product/10.2.0/db_1/dbs
[oracle@localhost dbs]$ vi initmandb.ora
[oracle@localhost dbs]$ cat initmandb.ora
mandb.__db_cache_size=444596224
mandb.__java_pool_size=4194304
mandb.__large_pool_size=4194304
mandb.__shared_pool_size=150994944
mandb.__streams_pool_size=0
*.audit_file_dest='/home/app/oracle/admin/mandb/adump'
*.background_dump_dest='/home/app/oracle/admin/mandb/bdump'
*.compatible='10.2.0.1.0'
*.control_files='/home/app/oracle/oradata/mandb/control01.ctl','/home/app/oracle/oradata/mandb/control02.ctl','/home/app/oracle/oradata/mandb/control03.ctl'
*.core_dump_dest='/home/app/oracle/admin/mandb/cdump'
*.db_block_size=8192
*.db_domain=''
*.db_file_multiblock_read_count=16
*.db_name='mandb'
*.db_recovery_file_dest='/home/app/oracle/flash_recovery_area'
*.db_recovery_file_dest_size=2147483648
*.dispatchers='(PROTOCOL=TCP) (SERVICE=mandbXDB)'
*.job_queue_processes=10
*.open_cursors=300
*.pga_aggregate_target=201326592
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.sga_target=605028352
*.undo_management='AUTO'
*.undo_tablespace='undotbs'
*.user_dump_dest='/home/app/oracle/admin/mandb/udump'

Edit the Controlfile Script
CREATE DATABASE mandb
    MAXLOGFILES 16
    MAXLOGMEMBERS 3
    MAXDATAFILES 100
    MAXINSTANCES 8
    MAXLOGHISTORY 292
LOGFILE
  GROUP 1 '/home/app/oracle/oradata/mandb/redo01.log'  SIZE 50M,
  GROUP 2 '/home/app/oracle/oradata/mandb/redo02.log'  SIZE 50M,
  GROUP 3 '/home/app/oracle/oradata/mandb/redo03.log'  SIZE 50M
DATAFILE
          '/home/app/oracle/oradata/mandb/system01.dbf' size 100m,
          '/home/app/oracle/oradata/mandb/usrsers01.dbf' size 10m
sysaux datafile
          '/home/app/oracle/oradata/mandb/sysaux01.dbf' size 100m
undo tablespace undotbs
datafile  '/home/app/oracle/oradata/mandb/undo01.dbf' size 50m
default temporary tablespace temptbs
tempfile  '/home/app/oracle/oradata/mandb/temp01.dbf' size 50m
CHARACTER SET WE8ISO8859P1
;

Manually / using commands create the folders:- mandb, adump, bdump, cdump, udump as
/home/app/oracle/oradata/mandb/
[oracle@localhost dbs]$mkdir  /home/app/oracle/oradata/mandb
[oracle@localhost dbs]$mkdir  /home/app/oracle/admin/mandb
[oracle@localhost dbs]$ mkdir  /home/app/oracle/admin/mandb/adump
[oracle@localhost dbs]$ mkdir  /home/app/oracle/admin/mandb/bdump
[oracle@localhost dbs]$ mkdir  /home/app/oracle/admin/mandb/cdump
[oracle@localhost dbs]$ mkdir  /home/app/oracle/admin/mandb/udump

Startup the database testdb in nomount using pfile as
[oracle@localhost dbs]$ sqlplus / as sysdba
SQL> startup pfile=’/ home/app/oracle/product/10.2.0/db_1/dbs/initmandb.ora’ nomount;

Run/Execute the Controlfile Script as
SQL>@/home/app/oracle/product/10.2.0/db_1/dbs/mandbctl.sql
Database Created

Run the following scripts for data dictionary, views, catalogs, and procedures
SQL>@/home/app/oracle/product/10.2.0/db_1/rdbms/admin/catalog.sql
SQL>@/home/app/oracle/product/10.2.0/db_1/rdbms/admin/catproc.sql.


Database Management Systems, DBMS

Data
Unanalyzed raw facts, figures, objects and events said called as Data.

Information
Process of the data are called Information.

Database
A database is a collection of related information. A database is a logically coherent collection of data with some inherent meaning.

DBMS
Database Management system is a collection of programs that enables user to create and maintain a database.
Thus a DBMS is a general purposed s/w system that facilitates the process of defining constructing and manipulating a database for various applications. (Defining a data base involves specifying the data types, structures and constraints for the data to be stored in the data database.
Constructing a data base is the process of storing data itself on some storage medium that is controlled by DBMS. Manipulating a database includes such functions as querying the data base to retrieve specific data, updating the database to reflect the changes in the mini-world.

Catalog
A catalog is a table that contain the information such as structure of each file, the type and storage format of each data item and various constraints on the data. The information stored in the catalog is called Metadata . Whenever a request is made to access a particular data, the DBMS s/w refers to the catalog to determine the structure of the file.

Data Warehousing and OLAP
Data warehousing and OLAP (online analytical processing ) systems are the techniques used in many companies to extract and analyze useful information from very large databases for decision making .

Real Time Database Technology
These are all the techniques used in controlling industrial and manufacturing processes.

Program-Data Independence
Unlike in the traditional file sys. the structure of the data files is stored in the DBMS catalog separately from the access programs . This property is called program-data independence i.e. We needn’t to change the code of the DBMS if the structure of the data is changed. Which is not supported by traditional file sys .

ORDBMS
Object oriented RDBMS is a relational DBMS in which everything is treated as objects. User can define operations on data as a part of the database definition.

Program-Operation Independence
An operation is specified in two parts .
a) Interface (operation name and data types of its arguments).
b) Implementation (the code part)
The implementation part can be changed without affecting the interface. This is called
program-operation independence.

View
A view may be a subset of the database or it may contain virtual data that is derived from the database files but is not explicitly stored .

OLTP
Online transaction processing is an application that involve multiple database accesses from different parts of the world . OLTP needs a multi-user DBMS s/w to ensure that concurrent transactions operate correctly.

DBA
A database administrator is a person or a group responsible for authorizing access to the database, for coordinating and monitoring its use, and for acquiring s/w and h/w resources as needed.

DB Designer
base and for choosing appropriate structure to represent and store this data .
Data base designers are responsible for identifying the data to be stored in the data

Different types of End users
a) Casual end-users
b) Naive or parametric end users
c) Sophisticated end users
d) Stand alone users.

Aadvantages of using a DBMS
a) Controlling redundancy.
b) Restricting unauthorized access.
c) Providing persistent storage for program objects and data structures.
d) Permitting inferencing and actions using rules.
e) Providing multi-user interfaces.
f)  Representing complex relationships among data.
g) Enforcing integrity constraints.
h)Providing backups and recovery.

Disadvantages of using a DBMS
a) High initial investments in h/w, s/w, and training.
b) Generality that a DBMS provides for defining and processing data.
c) Overhead for providing security, concurrency control, recovery, and integrity functions.

Data Model
It is a collection of concepts that can be used to describe the structure of a database. It provides necessary means to achieve this abstraction. By structure of a database we mean the data types, relations, and constraints that should hold on the data.

Categories of Data Models
a) High-level or conceptual data models.
b) Representational data models.
c) Low-level or physical data models.
High level data models provide the concepts that are close to the way many users perceive data.
Representational data models are provide concepts that provide the concepts that may be understood by end users but that are not too far removed from organization of data in the database.
Physical data models describe the details of how data is stored in the computers.

Schema
The description of a data base is called the database schema , which is specified during database design and is not expected to change frequently . A displayed schema is called schema diagram .We call each object in the schema as schema construct.

Database Management Systems, DBMS


Types of Schema
a) internal schema.
b) Conceptual schema.
c) External schemas or user views.


Data Independency
Data independency is defined as the capacity to change the conceptual schema without having to change the schema at the next higher level. We can define two types of data independence:
a) Logical data independence, LPD
b) Physical data independence, PDI
LDI is the capacity to change the conceptual schema without having to change external schemas or application programs.
PDI is the capacity to change the internal schema without having to change conceptual (or external) schemas.

DBMS Languages
a) DDL (Data definition language)
b) SDL (Storage definition language)
c) VDL (View definition language)
d) DML (Data manipulation language)

Types of DBMS
a) RDBMS (Relational)
b) ORDBMS (Object Relational)
c) DDBMS (Distributed)
d) FDBMS (Federated)
e) HDDBMS (Homogeneous)
f) HDBMS (Hierarchical)
g) NDBMS (Networked)

Entity
An entity is a thing in the real world with an independent existence.

Attributes
These are the particular properties that describe an entity.

Types of Attributes
a)  Composite Vs simple attributes.
b) Single valued Vs multi-valued attributes.
c) Stored Vs derived attribute.
d) Null valued attributes.
e)Complex attributes.
f) What is difference between entity set and entity type?

Domain Value or Value Set of an Attribute
It is the set of values that may be assigned to that attribute for each individual entities .

Degree of a Relationship
The no of entities participating in that relation .

Recursive Relationship
It is the relationship where both the participating entities belong to same entity type .

Relationship Constraints
a) Cardinality Ratio. b) Participation Constraints.

Cardinality Ratio
The cardinality ratio for a binary relationship specifies the number of relationship instances that an entity can participate in.

Participation Constraint
The participation constraint specifies whether the existence of an entity depends on its being related to another entity via the relationship type. This is of two types:
a) Total Participation.
b) Partial Participation.

Weak Entity Types
The entity types that do not have key attributes of their own are called weak entity types.
Rests are called strong entity types .The entity that gives identity to a weak entity is called owner entity. And the relationship is called identifying relationship. A weak entity type always has a total participation constraint with respect to its identifying relationship.

ER Diagram/ EER Diagram
This data model is based on real world that consists of basic objects called entities and of relationship among these objects. Entities are described in a database by a set of attributes.

Specialization
It is the process of defining a set of subclasses of an entity type where each subclass contain all the attributes and relationships of the parent entity and may have additional attributes and relationships which are specific to itself.

Generalization
It is the process of finding common attributes and relations of a number of entities and defining a common super class for them.

Generalization and Specialization
a) Disjoint ness constraints.
b) Completeness constraints.
c) Total Specialization constraints. Disjointness Constraint specifies that the subclasses of the specialization must be disjoint .i.e. an entity can be a member of at most one of the subclasses of the specialization. The reverse of it is overlapping. Completeness Constraint is a participation constraint which may be
i) Total
ii) Partial Total specialization Constraint tells that each entity in the super class must be a member of some subclass in the specialization. And partial specialization constraint allows an entity not to belong to any of the subclasses .Thus we do have the following 4 types of constraints on specialization:
i) Disjoint, total
ii)Disjoint, partial
iii)Overlapping, total
iv)Overlapping, partial

Ternary Relationship
A relationship with a degree 3 is called a ternary relationship.

Aggregation and Association
Aggregation is an abstraction concept for building composite objects from their component objects. The abstraction of association is used to associate objects from several independent classes.

RAID Technology
Redundant array of inexpensive (or independent) disks. The main goal of raid technology is to even out the widely different rates of performance improvement of disks against those in memory and microprocessor. Raid technology employs the technique of data striping to achieve higher transfer rates. 
Hashing Technique
This is a primary file organization technique that provides very fast access to records on certain search conditions. The search condition must be an equality condition on a single field, called hash field of the file.
a) Internal hashing
b) External hashing
c) Extendible hashing
d) Linear hashing
e) Partitioned hashing

Types of Relational Constraints
a) Domain Constraints
b) Key Constraints
c) Entity Integrity Constraints
d) Referential Integrity Constraints Domain Constraints specify that the value of each attribute must be an atomic value from the domain of the attributes. Key Constraints tell that no two tuples can have the same combination of values for all their attributes. Entity Integrity constraint states that no primary key value can be null. Referential Integrity constraints states that a tuple in one relation that refers to another relation must refer to an existing tuple in that relation it is specified between two relations and is used to maintain the consistency among tuples of the two relations.

Super Key, Key, Candidate Key, Primary Key 
A Super Key specifies a uniqueness constrain that no two distinct tuples in a state
can have the same value for the super key. Every relation has at least one default super key. A Key is a minimal super key or the subset of the super key which is obtained after
removing redundancy. A relation schema may have more than one key .In this case
each key is called a candidate key. One of the candidate key with minimum number
of attributes is chosen as primary key.

Foreign Key
A key of a relation schema is called as a foreign key if it is the primary key of
some other relation to which it is related to.

Transaction
A transaction is a logical unit of database processing that includes one or more database access operations. If a Transaction is not complete it rolled back or cancelled.

Properties of Transaction
a) Atomicity
b) Consistency preservation
c) Isolation
d) Durability (permanence)

Basic database Operations
a) Write_item(x) – Insert Command
b) Read_item(x) – Select Command

Disadvantages of not controlling concurrency
a) Lost update problem
b)Temporary update(Dirty read) problem
c) Incorrect summary problem

Serial, Non serial Sechedule
A schedule S is serial if, for every transaction T participating in the schedule, all the operations of T is executed consecutively in the schedule, otherwise, the schedule is called non-serial schedule.

Friday, June 14, 2013

Oracle Dabase Basics

SCN(system chain number):
SCN (System Change Number) is a primary mechanism to maintain data consistency in Oracle database. SCN is used primarily in the following areas, of course, this is not a complete list:
1.  Every redo record has an SCN version of the redo record in the redo header (and redo records can have non-unique SCN). Given redo records from two threads (as in the case of RAC), Recovery will order them in SCN order, essentially maintaining a strict sequential order. Every redo record has multiple change vectors too.
2.  Every data block also has block SCN (aka block version). In addition to that, a change vector in a redo record also has expected block SCN. This means that a change vector can be applied to one and only version of the block. Code checks if the target SCN in a change vector is matching with the block SCN before applying the redo record. If there is a mismatch, corruption errors are thrown.
3. Read consistency also uses SCN. Every query has query environment which includes an SCN at the start of the query. A session can see the transactional changes only if that transaction commit SCN is lower then the query environment SCN.
4. Commit. Every commit will generate SCN, aka commit SCN, that marks a transaction boundary. Group commits are possible too.

Achive and Noarchive:
Placing the database in archivelog mode prevents redo logs from  being overwritten until they have been archived. In  archive the data in the redo logs are wrote to the archive log files by the archiver(ARCn) before they are being overwritten.

Shared Connection and Dedicated Connection: 
Shared Connection: 
In Shared Connecion there is many-to-one ratio between server processes and user processes. Each server process uses system resources, including CPU cycles and memory and it shared among the user processes.     
Dedicated Connection:
In a Dedicated Connection there is one-to-one ratio between server processes and user processes. Each server process uses system resources, including CPU cycles and memory.

Server Processes and Background Processes:
Server Process:
Server Process connects to the Oracle instance and is started when a user establishes a session.
Background Processes:
Background Processes are started when an Oracle instance is started.

Block:
Database objects, such as tables and indexes, are stored as segments in tablespaces. Each segments contains one or more extents. An extent consists of contiguous data blocks, which means that each extent can exitst only in one data file. Data blocks are the smallest unit of I/O in the database. The size of the data block  can be set at the time of the creation of the database. The default size is 8KB.

Locally Managed and Dictionary Managed Tablespaces:
When Oracle allocates space to a segment (like a table or index), a group of contiguous free blocks, called an extent, is added to the segment. Metadata regarding extent allocation and unallocated extents are either stored in the data dictionary, or in the tablespace itself. Tablespaces that record extent allocation in the dictionary, are called dictionary managed tablespaces, and tablespaces that record extent allocation in the tablespace header, are called locally managed tablespaces.

Instance:
Each running Oracle database is associated with an Oracle instance. When a database is started on a database server, the Oracle software allocates a shared memory area called the System Global Area (SGA) and starts several Oracle background processes. This combination of the SGA and the Oracle processes is called an Oracle instance.

Parameter File and Password File:
Parameter File(p-file):
It is used to define how the istance is configured when it starts up.
Password File:
It allows the users to connect remotely to the database and perform administrative tasks.

Checkpoint:
Every three seconds (or more frequently), the CKPT  process stores data in the control file to document which modified data blocks DBWn has written from the SGA to disk. This is called a Checkpoint. The purpose of a checkpoint is to identify that place in the online redo log file where instance recovery is to begin(checkpoint position).

PMON and SMON:
PMON (Process Monitor):
Performs user and server processes cleanup when users process fails. Registers service with the listeners.
SMON ( System Monitor):
Performs crash recovery when the instance is started following a failure. It also clean Temp Segments and Collates Free Space.
Large Pool:
Large Pool is an optional area that provides large memory allocations for certain large processes, such as Oracle backup and recovery operations, and I/O server processes.

Phases of Statement Execution(Shortly):
1. Parse Phase - During the parse phase, Oracle opens the statement handle, checks whether the statement is OK (both syntactically and whether the involved objects exist and are accessible) and creates an execution plan for executing this statement. Parse call does not return an error if the statement is not syntactically correct. 
2. Execute Phase - During the execute phase, Oracle executes the statement, reports any possible errors, and if everything is as it should be, forms the result set. Unless the SQL statement being executed is a query, this is the last step of the execution.
3. Fetch Phase - During the fetch phase, Oracle brings the rows of the result to the program and makes them accessible by the PHP interpreter. Once more, the define and fetch phases are relevant for queries only. The Oracle OCI interface and the PHP OCI8 module contain calls to facilitate each of those phases.

Deadlocks:
A deadlock is a special example of a lock conflict. Deadlocks arise when two or more sessions wait for data locked by each other. Because each is waiting for the other, neither can complete their transaction to resolve the conflict.   
         Notes :  LOCKS:  Before the database allows a session to modify data, the  session must first lock the data that is being modified. A lock gives the session exclusive control over the data so that no other transaction can modify the locked data until the lock is released.

DDL and DML:
DDL (Data Definition Language): 
It is used to sets up, changes, and removes data structures from table. Collectively known as DDL (Data Definition Language). It consists of CREATE, ALTER, DROP, RENAME, TRUNCATE, COMMENT statements.
DML (Data Manipulation Language):
It is used to retrieves data from the database, enters new rows, changes existing rows, and removes unwanted rows from tables in the database. Collectively known as DML (Data Manipulation Language). It consists of SELECT, INSERT, UPDATE, DELETE, MERGE statements.
      
Library Cache:
The Library Cache is a piece of memory within the SGA that Oracle uses in order to store SQL statement. Whenever a Process issues an SQL Statement, the text of the Statement goes into the Library Cache where the statement is parsed an validated. If for example I do a insert into city (name, abbr) values ('Geneva', 'GE');. the Library Cache checks if there is a table named city having the columns name and abbr. As an additional task, the Library Cache also checks if the user's privileges are sufficient to execute the statement.
In a similar way, the Library Cache also caches PL/SQL Statements and Objects. The Library Cache was introduced because parsing (and validating and checking privileges) is expensive in terms of CPU cycles.

Data File and Control File:
Data Files:
Data files is a part of physical database Structure. It contains the user or application data of the database.
Controlfile Contents:
It contains data about the database itself ( that is, physical database structure information). These files are critical to the dataset. Without them, data files can’t be opened to access the data within the database. It is a binary file and it contains the name, startup, shutdown, and also many other vital information about the database.

Archiver(ARCn ):
Archiver copies redo log files to the archival storage when a log switch occurs.

User Process and Server Pocess:
User Process:
User Process is started at the time a database user requests a connection to the Oracle server.
Server Process: 
Server Process connects to the Oracle instance and is started when a user establishes a session.