Mohamed Azar Shaj

Oracle DBA, azarmohds@gmail.com, Riyadh Kingdom Of Saudi Arabia

Oracle Certification ** Oracle-Base ** Export and Import ** Charater set migration

About Me

My photo
Database Administrator and like to read about hackers and them activities...and completed Oracle 10g Certified associate. Active member of Oracle Forum

Interview

3:12 PM / Posted by Azar Mohamed Shaj / comments (0)

How Do you move table from sys tablespace to user tablespace :-

sql>alter table tablename move tablespace tablespacename;

Finding out Table and Tablespace Location :-

sql>select owner,table_name,tablespace_name from dba_tables where table_name='tablename';


How many memory layers are in the shared pool?

Shared poll consists of two layers.

  • Library Cache ---- parsed sql staments and cursor information its a symmentric check.
  • Data Dictionary Cache --- user account,privilege,segments,datafile and extend information stored in the data dictionary cache.

User and Privilege and Roles

3:35 PM / Posted by Azar Mohamed Shaj / comments (0)

Users Privilege :-

sql>create user azar idenitified by azar;

or

sql>create user azar idenitified by azar default tablespace temporary tablespace quota unlimited on password expire;

sql>grant create session,create table to azar;

or

sql>Grant connect,resource to azar;

sql>show user;
azar

sql>alter user azar account lock;

sql>alter user azar account unlock;

Password Change

sql>alter user azar identified by ;


sql>drop user azar cascade;


System Privilege

sql>Grant create any table to azar;

Revoke Privilege

sql>revoke create any table from azar;

View :- dba_sys_privs

sql>select * from dba_sys_privs where grantee= 'username';


Object Privilege :

sql>Grant select,update,insert,delete on to azar;

sql>Revoke select,update,insert,delete on from azar;

View :- dba_tab_privs


sql>grant dba to azar;

sql>revoke dba from azar;

sql>select * from dba_role_privs where grantee ='dba';

sql>select role from session_roles;


Managing Tablespace

3:39 PM / Posted by Azar Mohamed Shaj / comments (0)

Tablespace :-

Logical

database ---- > tablespace --- > segment ---- > extent ---- > Oracle block

* An Oracle Database can be logically grouped into smaller logical areas of space Known as Tablespace

* Tablespace Consists of one or more datafiles.

* It consists of zero or more segments


Creating Tablespace

sql>create tablespace azar datafile '\oracle\oradata\azar01.dbf' size 20m;

Check your tablespace view dba_tablespaces

sql>select * from dba_tablespaces;

sql>alter tablespace azar read only;

Your tablespace in read only mode you can drop your tablespace contents.

sql>select tablespace_name,status from dba_tablespaces;

sql>alter tablespace azar read write;

sql>select tablespace_name,status from dba_tablespaces;

sql>alter tablespace azar offline;

sql>alter tablespace azar online;

Increase your size tablespace in three Ways :-

sql>alter tablespace azar datafile '\oracle\oradata\azar01.dbf' size 20m autoextend on;

sql>alter tablespace azar add datafile '\oracle\oradata\azar02.dbf' size 10m;

sql>alter database datafile '\oracle\oradata\azar01.dbf' resize 30m;

View:-

dba_data_files

dba_free_space

Moving tablespace from one location to another location

  • Offline your tablespace
  • Cut your datafile and paste datafile in target place
  • sql>alter tablespace azar rename datafile '\oracle\oradata\azar01.dbf' to 'any path' ;
  • Online your tablespace

Create temporary tablespace :-

Using for only sort operation

sql> Create temporary tablespace azardba tempfile '\oracle\oradata\temp02.dbf' size 2m;

If you want change your temporary tablespace to default tablespace

sql>alter database default temporay tablespace azardba;

View:-

dba_temp_files

v$tempfile

v$temp_space_header

Redo Log FIle

6:24 PM / Posted by Azar Mohamed Shaj / comments (0)


Redo log File :-

* Redo log files record all changes made to data and provide a recovery mechanism from a system or media failure.

* Redo log files are organized into groups

* An Oracle database requires at least two groups

* Each redo log within a group is called a member.
Online Redo log groups :-
* A set of identical copies of online redo log file is called an Online redo log group.
* Log writer writes same information to all online redolog files in a group.
* Oracle Server Needs minimum two online redo log group.
Online Redo log Member :-
* Each online redol og file in a group called Online redo log member.
* Each member in a group has a identical log sequence number and same size.
* The log sequence number is assigned each time the oracle server starts writing to a log group to identify each redo log file uniquely.
* The current log sequence number is stored in the control file and header of all data files.
Redo logs Work :-
* When you redo log file is full, Logwriter will move to the next Log group is called Log switch
* Checkpoint occurs
* Information written to Control files
Logwriter Writes Under the Condition
  • When a transaction commits
  • Redolog buffer becomes one-third full
  • More than a megabytes of changed record in the redo log buffer
  • Before database writer writer to the datafile.
Forcing Log switch and Checkpoints :-
* Log switches and checkpoints are automatically done at certain operation point of the database.
but a DBA force to log switch or checkpoint occur.
sql> alter system switch logfile;
sql> alter system checkpoint;
Archive mode
sql> archive log list;
database log mode noarchive
automatic archival disable
Change your database from noarchive mode to archive mode
Mount stage
sql> shutdown immediate
sql> startup mount
sql> archive log list;
sql> alter database archivelog;
database altered
sql> archive log list;
database log mode archive mode
automatice archival disabled
sql> archive log start;
If you want set instance level
sql> show parameter log_archive_start
value
false
sql> alter system set log_archive_start=true scope=spfile
system altered
sql> shutdown immediate
sql> startup
sql> archive log list;
Change your archive destination
sql> alter system set log_archive_dest = '\oracle\admin\{sid}' scope=spfile
shutdown and startup
sql> archive log list;
Adding Group and Member :-
sql>alter database add logfile group 3
'\oracle\oradata\{sid}\redo03a.log' size 5m;
sql> alter database drop logfile group 3;
adding member
sql>alter database add logfile member '\oracle\oradata\{sid}\redo03b.dbf to group 3;
sql>alter database drop logfile member '\oracle\oradata\{sid}\redo03b.log' ;
View :-
V$log
v$logfile
Group and member status :-
active - archived ,instance required
Inactive -- archived but instance not required
curretn -- currently log writer writes data in a the group
********* drop your group only inactive state,you cannot drop current state.
If you want to drop current state you sholud log switch logfile.
sql>select archiver from v$instance;
started

Control File

11:23 AM / Posted by Azar Mohamed Shaj / comments (0)

Control File :-

* Small Binary file necessry for the database to start and operate successfully.

* Each Control file associated with only one database.

* Before database opened, the control file is read to determine if the database is in a valid state to use.

* Control file is updated continously by the oracle server.

* It must be available for writing whenever the database is open.

* No databse administrator or end user can not edit this file.

* If all copies of a control file lost, the database must be recovered before database opened.

* Control file should be multiplexed upto eight times.

Control Files Contents :-

  • Database name and identifier
  • Time stamp of database creation
  • Tablespaces names
  • Names and locations of datafiles and redolog files.
  • Current redo log file sequence number.
  • Checkpoint information
  • Begin and end of undo segments
  • Redo log archive information
  • Backuop information.

sql> show parameter control_files;

Multiplex control files using pfile

(alreay have two control files and then)

sql> Alter system set control_files= '\oracle\oradata\{sid}\control01.ctl',

'\oracle\oradata\{sid}\control02.ctl','\oracle\oradata\{sid}\control03.ctl' scope = spfile ;

system altered.

sql> show parameter control_files;

***** Now not effet right now and so you shutdown the database and startup manually and copy the controlfile os level and paste it and rename it.

(control01.ctl (copy) and paste it same location (copy of control01.ctl ) and then rename it (control03.ctl))

sql>shutdown immediate

copy and paste and rename it

sql> startup

Sql> show parameter control_files;

Backup the control file :-

sql> Alter database backup controlfile to 'filename';

sql>alter database backup controlfile to trace;

View :-

  • v$controlfile
  • v$parameter
  • v$controlfile_record_section
  • show parameter control_files
  • v$backup
  • v$datafile
  • v$tempfile
  • v$tablespace
  • v$archive
  • v$log
  • v$logfile
  • v$loghist
  • v$archived_log
  • v$database

Data Dictionary

4:42 PM / Posted by Azar Mohamed Shaj / comments (0)

Data Dictionary :-

* During Database creation, The oracle server crerates additional object structures within datafiles.

  • Data Dictionary Tables
  • Dynamic Performance Tables

Data Dictionary :-

* Set of read only tables and views that record,verify and provide information associated with database.

*Describe databases and its objects

Two types of objects

  • Base Tables

--- Store description of database

--- Created with create database

  • Data Dictionary Views

--- Summarize base table information

--- Created using catalog.sql script

Base Tables :-

* Base tables are the first objects created in any oracle database.

* They are automatically created when the Oracle server runs the sql.bsq script at the time database is created.

* Users rarely access them directly. because it stored in a cryptic format.

* Example -- IND$ table which contains information about the index in the database.

Create Database

3:29 PM / Posted by Azar Mohamed Shaj / comments (0)

How to Create Database :-

When you install oracle ,database automatically created. If you want create manually doing following steps...( Windows )

  • SID ---> Any name that is your database name

Step 1 :

  • Copy the default parameter file and prepare it ( \Oracle\ora90\database)

Step 2 :

  • You should change sid name as like your database name (put in pfile folder in \oracle\admin\{sid}\pfile)
  • Put it five folder in \oracle\admin\{sid}\(put it and rename like create,pfile,udump,bdump,cdump)
  • Save the parameter file

Step 3 :

* create password file

type on cmd platform

Run ---> cmd Enter

cmd>orapwd file= \oracle\ora90\database\pwd{sid}.ora pwd=sys (as your choice)

Step 4:

* In windows create services for instance

cmd>oradim -new -sid {sid}

* Set Oracle Sid

cmd>set oracle_sid={sid}

Step 5:

cmd>sqlplus

Enter username : sys/sys as sysdba

Connected to an idle instance

sql>Startup nomount

sql>create database {sid}

Logfile

group 1 ('\oracle\oradata\{sid}\redo01.log') size 100m,

group 2 ('\oracle\oradata\{sid}\redo02.log') size 100m

datafile '\oracle\oradata\{sid}\system01.dbf' size 20m autoextend on

undo tablespace undotbs

datafile '\oracle\oradata\{sid}\undotbs01.dbf' size 20m

default temporary tablespace tempts1

tempfile '\oracle\oradata\{sid}\tempts01.dbf'

size 20m

/

Database created

sql>@c:\oracle\ora90\rdbms\admin\catalog.sql

sql>@c:\oracle\ora90\rdbms\admin\catproc.sql

****

http://www.adp-gmbh.ch/ora/admin/creatingdbmanually.html

Shutdown Database

3:00 PM / Posted by Azar Mohamed Shaj / comments (0)

Shutdown Mode :-

  • Shutdown immediate
  • Shutdown Abort
  • Shutdown Transactional
  • Shutdown Normal

Sql> Shutdown [immediate Normal Abort Transactional ]

  • Database buffer cache written to the data files.
  • Uncommitted Changes Roll Back
  • Resources released

Shutdown Normal :-

  • No New Connections can be made.
  • The oracle server waits for all users to disconnect before completing shutdown
  • Database and redo buffers are written to disk
  • Bg processes are terminated. and the sga is removed from the meory.
  • Oracle closes and dismounts the database before shutting down the instance
  • The next startup does not require an instance recovery.

Shutdown Transactional :-

* Transactional shutdown prevents client from losing work.

  • No client can start a new transaction on this particular instance.
  • A client is disconnected when the client ends the transaction
  • When all transactions have finished ,a shutdown immediately occurs
  • The next startup does not require an instance recovery.

Shutdown immediate:-

  • Current sql statments being processed by oracle are not completed.
  • Oracle server does not wait for users currently connected to the database to disconnect.
  • Oracle rolls back active transactions.
  • Oracle closes and dismounts the database before shutting the instance.
  • The next startup does not require intance recovery.

Shutdown abort :-

  • sql statments by the oracle server immediately terminated.
  • oralce does not wait for users currently connected to the database to disconnect.
  • database and redo buffers are not written to disk.
  • Uncommited transactions are not rolled back.
  • the instance is terminated without closing the files.
  • database not closed or dismounted.
  • Next startup needs instance recovery. which occurs automatically.

Startup

5:38 PM / Posted by Azar Mohamed Shaj / comments (0)

Starting up a database (Nomount) :-

* When starting the database, you select the states in which it starts.


  • Usually start an instance without mounting a database only during database creation or the recreation of control-files.

sql>Startup nomount

*Starting an instance include following tasks,

  • Reading the initializtion files
  • Allocating the SGA
  • Starting the background processes
  • Opening the alertSID.log file and the trace files.
  • The database must be named with the db_name parameter.

Starting up a database (Mount) :-

* The database must be mounted but not open during the following tasks,

  • Renaming datafiles
  • Enabling and disabling redo log archive options
  • Performing full database recovery
  • Locating and opening the control file specified initializtion parameters file.
  • Reading the control files to obtain the name and status of the redo log files and datafiles.

Starting up a database (Open) :-

* An instance started and database mounted and then datbase opened. During database open ,the user can be attempt to open database and performing.

*During the database open following tasks,

  • Datafiles and ONline redolog files are Available

Note :- When you open the database, a datafiles and online redo log file not avilable ,the oracle returns error. Mean time database needs recovery options.

*** If necessary,System Monitor background process (SMON) initiates instance recovery.

Startup Command :-

To startup an instance, use the following commands

  • Startup [force] [restrict] [pfile=filename] [open [recover] [database] Mount Nomount ]

Note :- This is not the Complex syntax.

  • Force ---> aborting the running instance before performing a startup
  • Restrict ---> Enables only users with restricted session privilege to access the database
  • Recover ---> can be media recovery when the database starts

**** Attempting to open Oracle utilities without start the service, Oracle returns a error Ora-12547 --TNS

The Alter Database Command :-

* Chang the states of the database from Nomount ot Mount stage,

sql> alter database db01 mount;

* Open the database read only mode

sql> alter database db01 open read only;

Alter database db01 open [read only read write]

where

  • Read Write ---> Opens the database read write mode so users can generate redo logs.
  • Read Only ----> Restrict users to read only transactions,preventing them from generating Redo log information.

Restrict Command :-

sql>startup restrict;

* Use the Alter system command in restriction mode,

sql> alter system enable restricted session;

**** When you perform struture maintanice or database export or import. The database can be restricted session so that it is available only to the restricted session privilige users.

Alter system [enable disable ] restricted session;

  • Enable ---> who have restircted privilige users
  • Disable ----> users who do not have the priviliges also log on.

Terminate Session :-

* After placing an instance in resticted mode, you may want to kill all current user sessions before performing administrative tasks.

  • Alter system kill session 'integer1,integer 2'

Where

  • Integer 1 ----> Value of the column in the v$session view
  • Integer 2 ----> value of the serail# in the v$session view

***** http://www.oracle-base.com/articles/misc/KillingOracleSessions.php

Efects of Terminating a Session :-

* Pmon perform following tasks,

  • Roll back the users current transaction
  • Release all currently held table or rows
  • Free all resources currently reserved by the user

Opening database in Read Only Mode :-

  • Executes query
  • Execute disk sort using locally managed tablespace
  • Take datafiles offline and Online ,.Not tablespace
  • Perform recovery offlines of datafile and tablespace.

Managing an Oracle Instance

4:36 PM / Posted by Azar Mohamed Shaj / comments (0)

Initialization Parameter Files :-

sql>Connect / as sysdba

sql>startup

* To start an instance, the Oracle server must read on the Initializtion parameter Files.

* Changes to entries in the file take effect based on the type of initialization parameter file used.

  • Static Parameter file , PFILE ----> commonly refered as initSID.ora
  • Persistent Parameter file , SPFILE ----> Commonly refered as spfileSID.ora

Parameter File Contents,

  • A list of instance parameters.
  • The name of the database instance assoiciated with
  • Allocations for memory strutures of the System global area
  • what to do with filled Online redo log files
  • The names and locations of control files
  • Information on undo segments.

PFILE :-

  • The pfile is a text file .it maintained by standard operating system editor.
  • Paramerter file is read only during instance startup.
  • If the file is modified,the instance must be shutdown and restarted in order to make the new parameter effective.

SPFILE :-

  • Binary file
  • Maintained by oracle server
  • Alter system command using to changes made

sql> alter system set uno_tablespace='undo2';

sql> alter system set parameter = value [scope =memory spfile both]

  • Memory ---> Changes the parameter values only in the currently running file.
  • Spfile ----> changes the parameter values only in the spfile.
  • Both -----> Currently running the instnace and spfile.

Creating a spfile,

sql>create spfile from pfile;

Background Process

3:12 PM / Posted by Azar Mohamed Shaj / comments (0)

Background Processes :-

* The relationship between physical (database) and memory sturture maintain and enforced by the Oracle's Background processes.

* Mandatory backgroun processes,

  • DBWR -- Database Writer
  • LGWR -- Log writer
  • PMON -- Process Monitor
  • SMON -- System Monitor
  • CKPT -- Checkpoint

Database Writer :-

* Database Writer writes when,

  • Checkpoint Occur
  • Dirty buffer thershold reached
  • No free Buffers
  • Timeout
  • RAC ping request
  • Tablespace offline
  • Tablespace read only
  • Table DROP or Truncate
  • Tablespace BEGIN BACKUP

*db writer writes dirty buffer from the database buffer cache to the datafiles.

What is Dirty Buffer?

* A buffer that is currently not in use, and contains data that has been committed but not written back to the disk by the buffer writer process is called a dirty buffer.

* If a user process finds a dirty buffer in the least recently used (LRU) list, it moves the dirty buffer to the write list and continues searching for more dirty buffers.

Log Writer :-

  • At commit
  • When one-third full
  • When there is 1MB of redo
  • Every 3 seconds
  • Befor DBWN writes

System Monitor :-

* Instance recovery

  • Rolls forward changes in the redo logs.
  • Opens the database for user access
  • Roll back uncommitted transactions.

* Coalesces free space ever 3 sec

* Deallocates temporary segments.

Process Monitor :-

* Cleans up after failed processes by

  • Rolling back the transaction
  • Releasing locks
  • Releasing Other Resources
  • Restarts dead dispatchers

What is Checkpoint?

* A checkpoint occurs when the db writer process writes all modified buffers in the sga buffer cache to the database datafiles.

* Data file headers are also updated with the latest checkpoint SCN, even if the file had no changed blocks.

* Checkpoints occur AFTER (not during) every redo log switch.

* set parameter log_checkpoints_to_alert=true to observe checkpoint start and end times in the database alert log.

* Checkpoints can be forced with the Alter system checkpoint command.

What is SCN?

* System Change Number ---> A number,internal to Oracle that is incremented Over time as change vectors are generated,applied and written to the redo log.

* System commit Number ---> A number,internal to oracle that is incremented with each database commit.

Memory Struture

10:57 AM / Posted by Azar Mohamed Shaj / comments (0)

Memory Struture:-
* SGA (system global area) --- Allocated When instance started.
* PGA (program global Area) --- Allocated when server process Started.

Sytem Global Area ( Shared Global Area):-

  • Shared Pool ( Library Cache and Data Dictionary Cache)
  • Database buffer Cache
  • Redo log buffer cache

two optionally memory struture

  • Java Pool
  • Large Pool

* Sga is used to store database information that is shared by database process.It Contains data and control information for the database.

* Its allocated for the virtual memory of the computer where oracle resides.

sql> show sga; (this is view of sga).

Dynamic Sga :-

* It allows sga configuration changes without Shutdown the instance . It Can be shared pool ,database buffer cache ,redo log buffer cache.

buffer ( temporarly storage one,example browsing any page when click on any link its seeking that page very slowly ,when you enterd backside button,its coming very fastly).

* A granule is a unit of contiguous(in contact or sharing an edge) virtual memory allocation.

* one granule size 4MB means if estimated sga size is <128mb.

*One granule size is 16MB Otherwise. >128MB.

*buffer cache,large pool,shared pool are allowed to shrink and grow based on granules boundries.

*Allocation of granules via ALTER SYSTEM

*Target size in granules tracked and displayed by the V$BUFFER_POOL view.

Shared Pool :-

* Its used to store the most recently executed sal statments and data definitions.

  • Library cache
  • Data dictionary cache

sql> alter system set shared_pool_size=64MB;

* It Contains both fixed and variable size.

* Variable Struture ---- It grow and shrink depends on user programme requirments.

** Total sga size cannot exceed sga_max_size.

Library Cache :-

* the query or sql statment against the database.

* It stores information about most recently executed used shared sql and pl/sql statments.

* It managed by least recently used (LRU) algorithm.

* size determined by shared pool size.

* If the size of the shared pool too small,statments are reloaded into the library cache ,which affects performance.

* As the cache fills, Less recently used execution paths and parse trees removed from the library cache to make room for the new entries. They eventually aged out.

Data Dictionary Cache :-

* Its a collection of most recently used definitions in the database.

* It includes information about

  • Database files
  • Indexes
  • tables
  • columns
  • users
  • priviliges and other database objects.

* During the parse phase ,the server process looks at the data dictionary information to resolve object names and validate access.

* Caching the data dictionary information into memory improves response time on queries.

* Size determined by shared pool sizing.

Database Buffer Cache :-

* It stores copies of data block that retrived from the datafiles.

* It enables great performance gains when you obtain and update data.

* It is managed by LRU algorithm

* db_block_size determined by the primary block size.

  • query process --->server process looks ----> database buffer cache for any need blocks.
  • If not found, ----> server process reads the block -----> datafiles.and places copies in db cache.

* Consistent of independent of sub-caches

  • db_cache_size --> default buffer cache size only and cannot set to be zero.
  • db_keep_cache_size ---> To retain blocks in memory that are likely to be reused.
  • db_recycle_cache_size ---> eliminate block from the memory that have little reused.

* Db cache dynamically resized

  • sql>alter system set db_cache_size = 96M;

* Size of each buffer cache is equal to the size of an Oracle Block.

* Non-standard block size any power of two value between 2kb and 32kb.

Redolog Buffer Cache :-

* It records all changes made to the database data blocks.

* Its primary purpose recovery.

* Changes recorded within are called redo entries.

* Redo entries contain information to the reconstruct or redo changes.

* size is defined by Log_buffer.

Oracle Fundamentals I

4:38 PM / Posted by Azar Mohamed Shaj / comments (0)


Oracle Architectural Components

Oracle Server:-\

* It contains Several files,process and memory strutures in an oracle server .Not all of them are used when executing a sql statment.

*The database can be recovered in event of software or hardware error.
*It consists of Oracle Instance and Database.
  • Client-application server-server
  • client-server
  • host-based
Client-application-server:-\ (three tiers) user access the database from local computer through an application server.
Client-Server:-\ (two tiers) User access the database from their personal computer(client) over a network.Database sits in a separate computer(server).
Host-Based :-\ Users directly connected to the same computer on which the database resides.
Oracle Instance:-\
* It combination of Background process and memory struture.
*Instance must be started to access the data in database.
*when instance start----> SGA(system global Area ) allocated & bg process are started.
*It perform functions on behalf of invoking processes.
*Always open one and only database.

Oracle Database :-\
*It consists of Operating system files and also known as datafiles.
*It provide actual storage information and data kept consistent and can be recovered in event of
software or hardware error.
* It consists of three files,
Datafiles--Actual data in a database.
Redo logs --- A record changes of made to the database to enable recovery of the data in case of failures.
Control files ---- containg information necessary to maintain and verify database integrity.
Other Key Files :-
Password file
Parameter file (characterstics of an Oracle instance.it contains size of the sga and so) and Archived redo log file (Offline copies of the redo log files).
Establishing a connection & Creating Session:-
Database user(user process)<------connection established----->server process<---->(session create) oracle server.
* Before users can submit Sql statments to an oracle database,they must connect to an instance.
* Such as sql*plus (tool) or Oracle Forms(Application) is excuted as s user process.
* When user logs on to the Oracle server, A process is created on the computer running the Oracle Server. This process is called a Server process.
Sessions:-
* The session starts when the user is validated by the oracle Server and it ends when the user logs out or when there is an abnormal condition.
Example: When you log on to any mail id,it created session and then if you are not using particular time ,the session will be ended and then you again login.
* One to one correspondance between a user and server process is called a dedicated server.
* Possible for multiple user processes to share server processes.