Oracle db ebook

378
1 Oracle Database Administration C Co on n c ce e p p t t s s & & I I m mp p l l e e m me e n n t t a a t t i i o on n Made Simple BY A AS S I I M M A AB B B B A AS S I I .:|:. Takveen, Inc. South River, NJ

Transcript of Oracle db ebook

Page 1: Oracle db ebook

1

OOrraaccllee DDaattaabbaassee AAddmmiinniissttrraattiioonn CCoonncceeppttss && IImmpplleemmeennttaattiioonn

MMaaddee SSiimmppllee

BBYY

AASSIIMM AABBBBAASSII

..::||::.. TTaakkvveeeenn,, IInncc.. SSoouutthh RRiivveerr,, NNJJ

Page 2: Oracle db ebook

2

Copyright © 2005 Takveen, Inc. P.O. Box 1, South River, NJ 08882. All rights reserved. No part of this publication may be stored in a retrieval system, transmitted, or reproduced in any way, including but not limited to photocopy, photography, magnetic, or other record, without the prior agreement and written permission of the publisher and author. ISBN: 0-9770739-0-4 Takveen and the Takveen logo are either registered trademarks or trademarks of Takveen, Inc. in the United States and/or other countries. The author and publisher have made their best effort to prepare this book, and the content is based upon final release software whenever possible. Portion of the manuscript may be based upon pre-release versions supplied by the software manufacturer(s). The author and the publisher make no representation or warranties of any kind with regard to the completeness or accuracy of the contents herein and accept no liability of ANY KIND including but no limited to performance, merchantability, fitness for any particular purpose, or any losses or damages of ANY KIND caused or alleged to be caused directly or indirectly from this book. Manufactured in the United States of America.

Page 3: Oracle db ebook

3

TToo AArrsshhiiaa aanndd MMaatteeeenn ……

Page 4: Oracle db ebook

4

Page 5: Oracle db ebook

5

About the Author Mr. Abbasi is President and CEO of Takveen, Inc. NJ, which is firm providing information technology related ‘Optimal Solutions’ to industry. He is also attached with three top-notch computer schools of NJ teaching Oracle Database Administration in Unix/Windows Server environments. Before coming to NJ, he used to teach Oracle in heart of NC, Raleigh while working as Chief Technologist with a business solutions development firm. Mr. Abbasi has been attached intensely with the IT field for past 13 years and has written dozens of softwares and tons of code from programming microcontrollers using Assembly language to writing 3-tier multi-language web applications. He did B.Sc. Electrical Engineering from University of Engineering & Technology, Lahore, Pakistan (www.uet.edu.pk). He is Oracle Certified Professional Database Administrator (OCP), Microsoft Certified Professional (VB) and lots of other computer certifications. Besides certifications, he has lots of publications and has filed 1 patent with United States Patent & Trade Mark Office. When asked Mr. Abbasi, he said that it all erupted with a saying of my Advanced Engineering Mathematics professor, “Think yourself ….”

Page 6: Oracle db ebook

6

Page 7: Oracle db ebook

7

Acknowledgements

First, I am really thankful to God and I always remember Him as “Supreme Programmer”. The only One who’s every release is a final release and no upgrades, version releases or service packs after words. I salute your sea of computational and engineering skills my Lord and your graciousness for letting me have a droplet from it. I really want to thank my father and mother for their wonderful support. Although my father is no more living but his exceptional sacrifices will never be forgotten … I like to thank my wife Arshia and son Mateen for their encouragement to let me work on this book even during the time which I should had spent with them. I also want to thank my brother Atif Abbasi for providing support, suggestions & technical help whenever I needed. My special thanks for Maryann Master, Margorie Michele, Daisy Maxwell, Carol Taylor, Baber & Assiya Wasti and William Branch. Finally, I am thankful to all the wonderful instructors, professors, scholars, students and friends I had for their support and encouragement.

Page 8: Oracle db ebook

8

Page 9: Oracle db ebook

9

Table of Contents Chapter 1: Visualizing Complete Oracle Environment .. 11 Chapter 2: SQL: Structured Query Language ................ 25 Chapter 3: Oracle Database Concepts ......................... 107 Chapter 4: Oracle Instance Concepts ........................... 127 Chapter 5: Oracle In Action ......................................... 147 Chapter 6: Steps Of Implementation (Oracle Sys.) ..... 161 Chapter 7: Oracle Database Design ............................. 179 Chapter 8: Oracle Network Administration ................. 231 Chapter 9: Oracle Database Management ................... 267 Chapter 10: Oracle Backup & Recovery ..................... 291 Chapter 11: Oracle Performance Tuning ..................... 329

Page 10: Oracle db ebook

10

Page 11: Oracle db ebook

11

CChhaapptteerr 11

Visualizing The Complete Oracle Environment

Page 12: Oracle db ebook

12

Page 13: Oracle db ebook

13

Oracle is such a complex system that without analogies it’s really very hard to comprehend and grasp the concept of Oracle architecture and its working. If you are new to Oracle or an experienced DBA, understanding of concepts is the key to success not only in Oracle Certifications but in real world. Also once you understand the concepts then there is nothing left for cramming. Oracle System is basically divided into two things namely Oracle Instance and Oracle Database. We have taken the analogy of a Genie (Ghost/Djin): just like the one you might have seen in the movie/story of “Aladdin & the Magic Lamp”. When Aladdin rubbed the lamp; Genie became available and fulfilled all his commands. Similarly when you double click “Oracle” icon in your

Key Concepts

1) Oracle Server is comprised of two things

o Instance o Database

2) Anything that is going

into the database or coming out of the database has to be through Oracle Instance.

3) To communicate with

Oracle Instance we need to learn SQL.

Page 14: Oracle db ebook

14

system, Oracle Instance gets alive and waits for users information/data related commands to fulfill, as it is a magnificent “Data Management Genie”. Oracle Instance is something with which users interact in terms of storing and retrieving information. There are no physical files like in case of COBOL or MS Access for user database interaction. Anything that is going into the database or coming out of the database has to be through Oracle Instance and to communicate with Oracle Instance we need to learn Genie’s language: SQL. It stands for Structured Query Language and pronounced as See-Qual. Just imagine as if Aladdin is from France and knows only “French” language but when he came to US he found this magic lamp. When he rubbed the lamp Genie came out. Now even though Genie wants to help Aladdin and fulfill his commands but he cannot do anything due to language barrier. Aladdin has only two options either to teach Genie “French” or teach himself “English”. As Aladdin is depending on Genie to gets his commands fulfilled so ethically it’s his job to go to school or buy a book entitled “English” or in Oracle Instance case “SQL”. So after learning SQL, Aladdin will be fully capable to command the Instance. The Oracle Database is basically Genie’s area to write information and to make the changes permanent on the hard disk. Just like we can remember something in our brain but when it comes to remembering lots of stuff we have to take the help of

Page 15: Oracle db ebook

15

notebook to make things permanent otherwise we will forget.

Page 16: Oracle db ebook

16

Oracle Database physically composed of three different types of files: Control file, Redo log files & Data files. We will be discussing each of these files in great detail in the chapters ahead but just for a brief introduction, Oracle Instance writes the information about the structure (tables’, views’ etc. structure but not data) of the database in Control file. All the changes (data related like Update, Inserts etc) gets written to Redo log files. Data files are basically the files that contain users data. If user wants to write some information into the database, Oracle instance will actually make that change permanent by writing it to the data files. The Oracle Database is encrypted or in other words we cannot read the data files through any text editor like MS Word. Only Oracle Server knows how to Decrypt the information residing in the

Key Concepts

1) Oracle Database is encrypted and we cannot read it through any Text Editor like MS Word.

2) SQL commands are used

to store and retrieve information.

Page 17: Oracle db ebook

17

database. That’s why anything going into the database or coming out from the database has to be through Oracle Instance. Each connected user sends requests in the form of SQL commands to Oracle Instance to retrieve information from Oracle Database or to store information into the database.

Page 18: Oracle db ebook

18

The communication between Oracle and user takes place in a fashion that first user has to establish a connection with Oracle server or more precisely oracle instance by providing his/her credentials (Username and password). Once verified Oracle accepts the connection otherwise the connection request will get rejected. In Oracle world each connected user with oracle server is considered as session. If there are three connected users with oracle server, we can say that there are three sessions with oracle server. If one person logs off then the number of sessions drops to two. The connected users use SQL commands like SELECT, UPDATE, INSERT etc. (which we will learn in detail in chapter ahead) to communicate with Oracle server. SQL commands are very simple and very

Key Concepts

1) Each connected user with Oracle Instance is considered as “Session”

2) Never forget the concept

of following three terms

o Instance o Database o Session

3) Following analogies

help you in building concept

Instance = Genie Database = Notebook Session = Aladdin (or connected Users

Page 19: Oracle db ebook

19

easy to master, as it is comprised of all full-length English words with no or very little abbreviations. For example lets have a look on following SQL command SELECT name, phone FROM address_book; Once Oracle Instance will receive this command, it will retrieve all the “name” and “phone” information from the address_book table and send it to the user who had asked for it. If there are 1000 records or rows in that table; all of them gets sent to user computer. If the user wants to see all those people having name beginning with letter “A”. To make this to happen we have to use the “WHERE” clause of SELECT Statement. SELECT name, phone FROM address_book WHERE name LIKE “A*”; Thus you have seen how simple the SQL language is. Let me put it this way anyone who can read and understand English, can learn SQL with very small efforts. Understanding of Instance, Database and Session are considered as the foundation. You should not forget the concept and meaning of these three terms as we are going to build the Oracle Architecture concept over it and we will be using these terms in the upcoming discussion. So let me define these three terms one by one.

Page 20: Oracle db ebook

20

Session: Each connected User with Oracle Instance is considered as one Session. Instance: All users interact with Instance to retrieve or store information. Database: Is like the notebook where instance writes the information and is encrypted.

Page 21: Oracle db ebook

21

Users from all over can connect to Oracle Server provided they have access. Based on the privileges they have, users can store, retrieve information, and create new tables etc. from any part of the world whether they are on wired network or wireless network. The slide above shows the complete Oracle Environment. Now we can line up stuff that we will be learning. First we will learn about SQL or in other words how to communicate with Oracle Instance. Second, Oracle Database Administration (DBA) or in other words how to create/manage Oracle Instance as well as Oracle Database. Third, Oracle Network Administration or in other words how to develop/manage Oracle Network so that users from all parts of the world can communicate with Oracle

Key Concepts

1) Users can connect to Oracle server independent of their location.

2) Oracle Server: When we

talk about Oracle Instance & Oracle Database collectively we use the word Oracle Server.

3) Steps to Master Oracle

o SQL o DBA o Network Admin o Bkup & Recov. o Performance

Tuning

Page 22: Oracle db ebook

22

Instance as if they are in the same room or on the same machine running Oracle. Fourth, Backup & Recovery or in other words how to backup Oracle Database and recover information in case of all different types of failure and lastly, Oracle Performance Tuning or in other words once we have Oracle system up and running, how to monitor & enhance its performance in order to get maximum efficiency.

Page 23: Oracle db ebook

23

We Did It!

In this chapter, we learnt about complete Oracle environment architecture and also visualized it using Genie and Genie’s Notebook analogy. Oracle Instance ~ Genie Oracle Database ~ Genie’s Notebook Connected users with Oracle Instance (Genie) are called sessions. Oracle Data files are encrypted and only Oracle Instance knows how to decrypt it. People from all over the world can connect to Oracle Instance and retrieve/store information from Oracle Instance. To communicate with Oracle Instance, we need to learn Genie’s language i.e. SQL

Page 24: Oracle db ebook

24

Page 25: Oracle db ebook

25

CChhaapptteerr 22

SQL: Structured Query Language

Page 26: Oracle db ebook

26

Page 27: Oracle db ebook

27

Like I said in the previous chapter that SQL is the language that we will be using to communicate with the Instance. It’s just like I am right now using “English” as a way of communication because you guys understand it. Similarly to communicate with Oracle Instance we use SQL (Seeqal). Before we proceed with the language I want to clarify three terms that is used wide and sometimes few people gets confused. These are SQL, PL/SQL and SQL*Plus. SQL as I already told you are the language to communicate with Oracle Instance whereas PL/SQL is the superset of SQL i.e. we use PL/SQL instead of SQL when our target/goal requires multiple SQL commands to be executed based on certain logic, event or condition then we take the benefit of this language.

Key Concepts

1) Understanding the difference between three terms: SQL, PL/SQL, SQL*Plus

2) Horizontal goes the

Records/Rows and vertical goes the Fields/Columns.

3) We will write SQL

commands in SQL*Plus software.

Page 28: Oracle db ebook

28

SQL*Plus is basically the environment where we write SQL or PL/SQL commands. In other words it’s a front end tool we use to communicate with Oracle Instance. There are lots of SQL front-end tools available but it’s the one that gets installed once you install Oracle whether its 8i or 9i or 10g. In Oracle 9i and 10g they have introduced iSQL*Plus which is a web-interface for executing SQL and Pl/SQL code. If you are good in programming you can develop one on your own using Visual Basic or Visual C++ or even JAVA. Next thing I would to discuss over here is few important concepts about RDBMS or Relational Database Management System. There is a long old history about “RDBMS standard” but it all started with Dr. E.F. Codd in 1969. Making a long story short there are lot other database beside Oracle that claim to be RDBMS e.g. MS SQL Server, MS Access, MySQL etc. If I want to create my own database product named “Takveen” and I have big solid team of programmers; we all first have to read and understand that RDBMS standard and then we can start writing the code otherwise we cannot claim in the market that it’s a RDBMS database. The way I categorize the RDBMS database products available in the market keeping in mind their strength to handle data & handle concurrent user requests is as follows. Small Companies :: MS Access Mid Size Companies :: MS SQL Server

Page 29: Oracle db ebook

29

Large Organization/Enterprise :: Oracle Once you see a database product saying its RDBMS it means it uses SQL as a language and Data inside it is written always in table form i.e. rows/records & columns/fields. Moreover there may or may not be relationship in-between those tables. Each of the RDBMS Database manufacture has their own version of SQL. But they all are more than 90% alike. If it takes you to learn Oracle SQL in a week after that you can learn SQL for MS SQL server in less than an hour. There is one SQL standard defined by ANSI (American National Standard Institute) and is called ANSI SQL. Nearly all RDBMS database manufacture incorporates this ANSI SQL and then builds a layer of some new stuff on top of it to provide added functionality in the language. The SQL that we will be learning in this book will be more precisely Oracle SQL. Concept of arranging the data in the form of table is very simple. We have been doing it on the papers for years and the concept is still the same. There are two terms that we will be using a lot. One is called “Record(s)” or “Row(s)” and other one is called “Field(s)” or “Column(s)”. Like shown in the figure below, there are 5 records in the table and 3 fields. Each field in Oracle table has been assigned a “Data type”. While creating a table if you have assigned Last Name field character data type, you cannot insert date in it or you cannot insert numeric value in it. We

Page 30: Oracle db ebook

30

will learn this stuff in great detail when we learn about CREATE TABLE statement. There is one more thing that we can do with fields, we can assign constraints to them e.g. If you assign UNIQUE constraint to any column then you would not be able to insert any value in it which is already their in the same column. Constraints help in maintaining the data integrity of the system.

Figure 2.1 Table Showing Records/Rows And

Fields/Columns SQL is nothing but a set of command statements and are categorized into following five groups viz., DQL: Data Query Language, DML: Data Manipulation Language, DDL: Data Definition Language, TCL: Transaction Control Language, DCL: Data Control Language. The commands following in each of these categories are shown in the following table. DQL SELECT DML DELETE, INSERT, UPDATE DDL CREATE, DROP,

TRUNCATE, ALTER

Page 31: Oracle db ebook

31

TCL COMMIT, ROLLBACK, SAVEPOINT

DCL GRANT, REVOKE Table 2.1 0

Once we finish up with SQL you will say to yourself, “Oh! That was all”. Lets that the following example.

SELECT name, phone, city FROM address_book;

Anyone knowing English language can easily imagine what the output of this statement would look like. The output will be three fields with all the records from address_book table. So you completed your first SQL statement.

Page 32: Oracle db ebook

32

The SELECT statement as the name says is used to extract data from Oracle Database. The syntax for the simplest SELECT statement is as follows.

SELECT column_name1, column_name2, … FROM table_name1;

The examples we are going to present over here are only for you to understand and go through the whole chapter. The actual time of implementation comes after we teach you how to install Oracle software and how to login using SQL*Plus then if you like you can come back to this chapter and start implanting those statements. So following are some examples

Key Concepts

1) SELECT statement used for retrieval of information from Oracle Database through Oracle Instance.

2) Oracle Instance receives

these commands from connected users.

Page 33: Oracle db ebook

33

Example:

SELECT * FROM emp;

This command will display all the fields of the table emp and all of the records. Example:

SELECT ename, sal FROM emp WHERE sal > 2000;

The result of this statement will be only two columns of emp table and only those records where salary is greater than 2000. Example:

SELECT ename, salary FROM emp WHERE sal > 2000 ORDER BY ename;

The output of this statement will be exactly the same as the one above except that the output will be sorted based on ename column.

Page 34: Oracle db ebook

34

Just like we used the greater than (>) operator in the previous examples, there are other SQL operator too. They are mostly used in the WHERE clause to filter certain records or to reduce the number of records in the output. “Reduce the number of records”, what this means? Why we need to reduce the number of records in the output. Imagine a SSA (Social Security Administration) office. The database they are having must be having millions of records in it. You can well realize the drawback if someone would execute the following statement.

SELECT * FROM ssa;

One other aspect is, why to look for something you don’t need. If you are looking one person’s information why to execute the query that will give you all

Key Concepts

1) Operators help in filtering the number of records

2) Use parenthesis rather

than utilizing “order of precedence” among operators.

Page 35: Oracle db ebook

35

the records rather than executing the following query.

SELECT * FROM ssa WHERE ssn = ‘123-455-6677’;

The way we have used the “greater than” operator on the same lines we can use the “less than” (<), “equal to” (=), “less than equal to” (<=) & “greater than equal to” (>=). ANY/SOME and ALL can be best understood by first looking at their respective real world example. Example: SELECT * FROM emp WHERE sal = ANY (SELECT sal FROM emp WHERE deptno = 30); Compares a value to each value in a list or returned by a query. Must be preceded by =, !=, >, <, <=, >=. Evaluates to FALSE if the query returns no rows. Example: SELECT * FROM emp WHERE sal >= ALL ( 1400, 3000); Compares a value to every value in a list or returned by a query. Must be preceded by =, !=, >, <, <=, >=. Evaluates to TRUE if the query returns no rows. The next category of SQL operator is called Arithmetic operators and used if

Page 36: Oracle db ebook

36

you want to perform some arithmetic operation. The thing to remember over her is, although there is some thing “order of precedence” but its always recommended to use parenthesis whenever your statement involves two or more arithmetic operators. Parenthesis over rides the “order of precedence”. Now the question is, what is order of precedence? It means if you are not using any parenthesis and you calculation involves two or more arithmetic operators so which operations should Oracle perform first. Multiplication and Division will be preferred over Subtraction and Addition. Moving from left to right, if Multiplication comes first than it will be handled first otherwise division, it applies to subtraction and addition too. Example:

SELECT ename, sal, sal + sal*5/100 “Next Year Sal” FROM emp;

Logical Operators and the operators in the Other category can be best understood by looking at their respective real world example. Example:

SELECT sal FROM emp WHERE deptno = 30 AND sal > 2000;

Page 37: Oracle db ebook

37

The output of the query will be only one column i.e. sal and only those records will be displayed where department number is 30 and the salary is greater than 2000. So when you use AND operator it means both conditions needs to satisfy for the record to appear in the output but in case of OR, either first condition needs to be true or the second one e.g.

SELECT sal FROM emp WHERE deptno = 30 OR sal > 2000;

Example: SELECT * FROM emp WHERE job IN ('CLERK','ANALYST'); The output of the query will be all the coulums of emp table but only those records where job column contains either “CLERK” or “ANALYST”. You can also use IN operator with as follows. SELECT * FROM emp WHERE sal IN (SELECT sal FROM emp WHERE deptno = 30); By having NOT before IN can complete invert the results like in the following example. Such types of queries fall under the category called “Sub-Queries” which we will discuss in the article ahead in this chapter. There is a special technique to interpret them.

Page 38: Oracle db ebook

38

SELECT * FROM emp WHERE sal NOT IN (SELECT sal FROM emp WHERE deptno = 30);

Example:

SELECT * FROM emp WHERE sal BETWEEN 2000 AND 3000;

Only those records will be displayed where the salary is between 2000 and 3000 including both 2000 and 3000. Example: SELECT ename, deptno FROM dept WHERE EXISTS (SELECT * FROM emp WHERE dept.deptno = emp.deptno); TRUE if a sub-query returns at least one row. In other words the output will be two columns from dept table, all the records only if the query after EXISTS results in at least one record. Example:

SELECT sal FROM emp WHERE ename LIKE 'SM%';

The output will be only those salaries from emp (employee) table where ename

Page 39: Oracle db ebook

39

(employee name) begins with “SM”. Another variation of above query is as follows.

ename LIKE 'SMITH_' The output will be only those records where ename begins with “SMITH” and there should not be more than one character after it. Example:

SELECT ename, deptno FROM emp WHERE comm IS NULL;

The output will be ename and deptno but only those records where comm field has NULL value. NULL is a special value and just keep in mind that its not Zero. It can be visualized as empty field occupying zero byte.

Page 40: Oracle db ebook

40

SQL language like all the other computer languages comes with many pre-built functions. These functions can be categorized into two major groups. One is called “Single Row” functions and the other one is called “Group” functions. Single Row function as the name says gets implemented on single records whereas Group functions get implemented on the multiple records. These functions will get clarified in your mind once we start discussing one by one. Single Row Functions Single Row functions are further subdivided into five categories viz., Character, Data, Numeric, Conversion & other functions. First we will start with Character Functions or more precisely “Single Row Character Functions.”

Key Concepts

1) Single Row functions will result in same number of records as were there in the output of the query without them e.g. TOCHAR, INITCAP etc.

2) Group functions will

usually result in lesser number of records as were there in the output of the query without them e.g., AVG, SUM, COUNT etc.

Page 41: Oracle db ebook

41

Character Functions: Following are the functions that fall under this category.

CHR LTRIM INSTR ASCII RTRIM INSTRB CONCAT TRIM LENGTH INITCAP REPLACE LENGTHB LOWER SOUNDEX UPPER SUBSTR LPAD SUBSTRB RPAD

Example:

SELECT CHR(67)||CHR(65)||CHR(84) "Pet" FROM DUAL;

Output:

Pet --- CAT

Example:

SELECT ASCII('Q') FROM DUAL;

Output:

ASCII('Q') ---------- 81

Page 42: Oracle db ebook

42

Example:

SELECT CONCAT(ename, ' is a good boy') "Result" FROM emp WHERE empno = 7900;

Output:

Result ----------------- JAMES is a good boy

Example:

SELECT INITCAP('the king') "Capitals" FROM DUAL;

Output:

Capitals --------- The King

Example:

SELECT LOWER('THE KING') "Lowercase" FROM DUAL;

Output:

Lowercase ------------- the king

Page 43: Oracle db ebook

43

Similarly we can use the UPPER function. Example:

SELECT LPAD('Page 1',15,'*+') "LPAD example" FROM DUAL;

Output:

LPAD example --------------- *+*+*+*+*Page 1

Similarly we can use RPAD. Example:

SELECT LTRIM('121SPIDERMAN','12') "Result" FROM DUAL;

Output:

Result -------- SPIDERMAN

Similarly we can use RTRIM. Example:

SELECT TRIM (0 FROM 001234567000) Result" FROM DUAL;

Page 44: Oracle db ebook

44

Output:

Result -------- 1234567

SELECT TRIM (LEADING 0 FROM 001234567000) Result" FROM DUAL;

Output:

Result -------- 1234567000

Similarly we can replace LEADING with TRAILING to omit trailing zeros in 001234567000 and the output will then be 001234567. Example:

SELECT REPLACE('KING KONG','KI','HO') Result" FROM DUAL;

Output:

Changes -------------- HONG KONG

Page 45: Oracle db ebook

45

Example:

SELECT ename FROM emp WHERE SOUNDEX(ename) = SOUNDEX('SMYTHE');

Output:

ENAME ---------- SMITH

This function allows you to compare words that are spelled differently, but sound alike in English. You must have noticed that if you do a search in google (www.google.com) using wrong spelling e.g. as shown in the following figure I made a wrong spelled word “Neus” search on google and it came up with, “Did you mean News?”. That is basically the beauty of this function.

Example:

SELECT SUBSTR('SPIDERMAN',7,3) "Result" FROM DUAL;

Page 46: Oracle db ebook

46

Output:

Result --------- MAN

Similarly we can use SUBSTRB; for a single-byte database character set, SUBSTRB is equivalent to SUBSTR. Floating-point numbers passed as arguments to SUBSTRB are automatically converted to integers. Assume a double-byte database character set:

SELECT SUBSTRB(' SPIDERMAN',7,4.3) "Result" FROM DUAL;

Output:

Result -------- DE

Example:

SELECT INSTR('CORPORATE FLOOR','OR', 3, 2) "Instring" FROM DUAL; Output:

Result ---------- 14

Similarly we can use INSTRB; for a single-byte database character set, INSTRB is equivalent to INSTR. Lets suppose a double-byte database character set.

Page 47: Oracle db ebook

47

SELECT INSTRB('CORPORATE FLOOR','OR',5,2) "Result" FROM DUAL; Output:

Result -------- 27

Example:

SELECT LENGTH('SPIDERMAN') "Result" FROM DUAL;

Output:

Result -------- 9

Similarly we can use LENGTHB; for a single-byte database character set, LENGTHB is equivalent to LENGTH. Lets suppose a double-byte database character set.

SELECT LENGTHB ('SPIDERMAN') "Result" FROM DUAL;

Page 48: Oracle db ebook

48

Output:

Result -------- 14

Date Functions: Following functions fall under this category.

ADD_MONTHS MONTHS_BETWEEN LAST_DAY ROUND SYSDATE TRUNC

Example:

SELECT ADD_MONTHS(hiredate,1) FROM emp WHERE ename = 'SMITH';

Example:

SELECT SYSDATE, LAST_DAY(SYSDATE) "Last", LAST_DAY(SYSDATE) - SYSDATE "Days Left" FROM DUAL;

Output:

SYSDATE Last Days Left --------- --------- ---------- 23-OCT-97 31-OCT-97 8

Page 49: Oracle db ebook

49

Example:

SELECT MONTHS_BETWEEN(SYSDATE, hiredate) “Months of Service" FROM DUAL;

Example:

SELECT ROUND (TO_DATE ('27-OCT-92'),'YEAR') "New Year" FROM DUAL; Output:

New Year --------- 01-JAN-93

Example:

SELECT TRUNC(TO_DATE('27-OCT-92','DD-MON-YY'), 'YEAR') "New Year" FROM DUAL;

Output:

New Year --------- 01-JAN-92

Page 50: Oracle db ebook

50

Numeric Functions: The following functions fall under this category.

ABS ROUND SIGN TRUNC CEIL SQRT FLOOR MOD

Example:

SELECT ABS(-25) "Result" FROM DUAL;

Output:

Result ---------- 25

Example:

SELECT SIGN(-15) "Result" FROM DUAL;

Output:

Result ---------- -1

Example:

SELECT CEIL(25.7) "Result" FROM DUAL;

Page 51: Oracle db ebook

51

Output:

Result ---------- 26

Example:

SELECT FLOOR(25.7) "Result" FROM DUAL;

Output:

Result ---------- 25

Example:

SELECT ROUND(25.29,1) "Round" FROM DUAL;

Output:

Round ---------- 25.3

SELECT ROUND(25.29,-1) "Round" FROM DUAL;

Output:

Round ---------- 30

Page 52: Oracle db ebook

52

Example:

SELECT TRUNC(25.29,1) "Truncate" FROM DUAL;

Output:

Truncate ---------- 25.2

SELECT TRUNC(25.29,-1) "Truncate" FROM DUAL;

Output:

Truncate ---------- 20

-1 will truncate (make zero) first digit left of the decimal point of 25.29 Example:

SELECT MOD(11,4) "Modulus" FROM DUAL;

Output:

Modulus ---------- 3

Page 53: Oracle db ebook

53

Example:

SELECT SQRT(25) "Square root" FROM DUAL;

Output:

Square root ----------- 5

Conversion Functions: The following functions fall under this category

TO_CHAR TO_DATE TO_NUMBER

Example:

SELECT TO_CHAR(HIREDATE, 'Month DD, YYYY') "Result" FROM emp WHERE ename = 'BLAKE';

Output:

Result ------------------ May 01, 1981

Page 54: Oracle db ebook

54

Example:

SELECT TO_CHAR(-10000,'L99G999D99MI') "Result" FROM DUAL;

Output:

Result -------------- $10,000.00-

Example:

SELECT TO_DATE('January 15, 1989, 11:00 A.M.', 'Month dd, YYYY, HH:MI A.M.', 'NLS_DATE_LANGUAGE = American') FROM DUAL;

Example:

SELECT TO_NUMBER('$10,000.00-', 'L99G999D99MI') "Result" FROM DUAL;

Output:

Result --------- -1000

Page 55: Oracle db ebook

55

Other Single Row Functions: The following functions fall under this category.

NVL VSIZE

Example:

SELECT ename, NVL(TO_CHAR(COMM), 'NOT APPLICABLE') "COMMISSION" FROM emp WHERE deptno = 30;

Output:

ENAME COMMISSION ---------- ------------------------- ALLEN 300 WARD 500 MARTIN 1400 BLAKE NOT APPLICABLE TURNER 0 JAMES NOT APPLICABLE

Page 56: Oracle db ebook

56

Example:

SELECT ename, VSIZE (ename) "BYTES" FROM emp WHERE deptno = 10; Output:

ENAME BYTES ---------- ---------- CLARK 5 KING 4 MILLER 6

Group Functions A group function as the same says gets implemented on more than one record within a column. They can be better understood by looking at their real world examples. There are five very important group functions

AVG COUNT MAX MIN SUM

Example:

SELECT AVG(sal) "Average" FROM emp;

Output:

Average ---------- 2077.21429

Page 57: Oracle db ebook

57

Example:

SELECT COUNT(*) "Total" FROM emp;

Output:

Total ---------- 18

Example:

SELECT MAX(sal) "Maximum" FROM emp;

Output:

Maximum ---------- 5000

On the same line we can find out minimum value using the MIN group function. Example:

SELECT SUM(sal) "Total" FROM emp;

Output:

Total ---------- 29081

Page 58: Oracle db ebook

58

Query inside another query is called a Sub-Query. Sub-Queries are mostly used in the WHERE clause but you can use them in the FROM or even in the SELECT clause. The important thing is to understand the technique, how to interpret such queries or in other words how Oracle instance will interpret such queries. The technique is, start from the bottom query first or the one within parenthesis, imagine what’s its output will be, replace that query with its imagined output and move to the next higher one. Keep on doing this unless you end up with the main query.

Key Concepts

1) Technique for Sub-Query Interpretation: Start from the bottom query first, imagine what its output is would be, replace that query with its imagined output and move to the next higher one.

Page 59: Oracle db ebook

59

This concept will get further clarified once we start looking at the following real world examples. Example SELECT * FROM emp WHERE sal IN (SELECT sal FROM emp WHERE deptno = 30); Based of the technique that we have defined, we will first imagine the output of the bottom most query. It will give us all the salary values from department number 30. Now replace this query with those imagined values. Even if multiple values appear as a result of bottom query we can still use them with IN operator. But the following query will result in an error if the bottom SELECT statement results in multiple values. SELECT * FROM emp WHERE sal = (SELECT sal FROM emp WHERE deptno = 30); So be careful when you use operators with sub-queries. The syntax of the function, clause or Operator should justify under all circumstances then you are allowed to use sub-queries wherever you like.

Page 60: Oracle db ebook

60

Joins help in extracting data from two or more tables based on some condition specified in the WHERE clause. The very important point to note over here is that Joins are only at the query level. It means the output that you see as a result of statement containing any form of “Join”, is only for that instance and its in the volatile memory (RAM). There are following different kinds of Joins and each having its own application. Out of these, two are having extremely high importance viz., Equi-Join and Outer-Join.

Self Joins Cartesian Products Equijoins Outer Joins

Key Concepts

1) Sole purpose of Join statement is to extract data from more than one table based on some condition specified in the WHERE clause.

2) Primary/Foreign key

relationship needs not to be present among tables for making SELECT statement containing “joins” to run.

Page 61: Oracle db ebook

61

SELECT statements containing Joins should not be confused with “Primary/Foreign” key relationship between the tables commonly known as Linking Tables which we will discuss later in this chapter. Just keep in mind whether you have established a primary/foreign key relationship between the tables or not, you can still run the SELECT statements containing joins against those tables. Self Join A self join is a join of a table to itself. The same table appear twice after the FROM clause. We have used e1 and e2 as two different aliases for the same table. It can be visualized by looking at the following example. Example:

SELECT e1.ename||' works for '||e2.ename "Result" FROM emp e1, emp e2 WHERE e1.mgr = e2.empno;

Output:

Result --------------------- BLAKE works for KING CLARK works for KING JONES works for KING FORD works for JONES SMITH works for FORD ALLEN works for BLAKE WARD works for BLAKE MARTIN works for BLAKE SCOTT works for JONES

Page 62: Oracle db ebook

62

TURNER works for BLAKE ADAMS works for SCOTT JAMES works for BLAKE MILLER works for CLARK

Cartesian Product If in the joins statement you forget to put the WHERE condition or intentionally don’t write the WHERE clause at all, the result will be Cartesian product. Such type of output will be very rarely useful. If one table had five records and other one had six records; in the output you will get thirty records (5x6=30). That’s why we use the word product with such type of join.

Page 63: Oracle db ebook

63

In the word Equijoin, Equi comes because we use Equal sign (=) in the join condition. The query containing Equijoin will give you total number of records equal to or less than the number of records of the one table among all the tables we have used in the query having least number of records in it. Lets take a example of two table Equijoin query like the one shown in the slide, only those records will be displayed from either of two tables where there is an exact match between the columns used in the WHERE clause. Example: SELECT ename, job, dept.deptno, dname FROM emp, dept WHERE emp.deptno = dept.deptno;

Key Concepts

1) Only those records will be displayed from either of the two tables where there is an exact match between the columns used in the WHERE clause.

2) Primary & Foreign Key

has nothing to do with Joins statements.

3) All different forms Joins

are at the query level. The output exists in the RAM and is volatile.

Page 64: Oracle db ebook

64

Or if you want to further reduce the number of records in the output you can extend the filter in the WHERE clause just like the example mentioned below. In the WHERE clause there are two conditions on both sides of AND operator, only those records will be displayed that satisfy both of these conditions. Example: SELECT ename, job, dept.deptno, dname FROM emp, dept WHERE emp.deptno = dept.deptno AND job = 'CLERK'; Output: ENAME JOB DEPTNO DNAME ---------- --------- ---------- -------------- MILLER CLERK 10 ACCOUNTING SMITH CLERK 20 RESEARCH ADAMS CLERK 20 RESEARCH JAMES CLERK 30 SALES

4) We can run Join queries on tables having Primary & Foreign Key relationship established or not having this relationship.

Page 65: Oracle db ebook

65

If you ask me what is the most important thing in SQL and my response will always be “Outer Join”. This is because it has very high practical value. It has been used a lot in the real world wherever SQL is utilized. I want you to cram/memorize its definition word by word. I will first give you its definition and explanation will follow afterwards. Outer Join will give you all the records from the first table and only those records from the second table where there is a join. It means that the total number of in the result will be equal to the number of records of the one table among the two having no positive sign (+) attached with its column in the WHERE clause. In other words, the way you create an Outer

Key Concepts

1) Outer join will give you all the records from the first table and only those records from the second table where there is a join.

2) Positive sign (+) will be

placed with the column of the table having least number of records, in the WHERE clause.

Page 66: Oracle db ebook

66

join is very simple. First create a Equijoin query and then place the positive sign (+) with the column of the table having least number of records, in the WHERE clause. I would like to have your focus on the NULL (empty) values in the slide; the one shown in the output table. These order dates are NULL because there were no supplier ids 10002 & 10003 in the orders table.

Page 67: Oracle db ebook

67

UPDATE statement is used to edit the information already existing in the table. If you want to insert a new record in the table then we user the INSERT statement and if you want to delete any record or more than one record we use the DELETE statement. Example:

UPDATE emp SET comm = NULL WHERE job = 'TRAINEE';

Example:

INSERT INTO dept VALUES (50, 'PRODUCTION', 'SAN FRANCISCO');

Key Concepts

1) If you forget to mention the WHERE clause in the UPDATE statement, all the values in that column will get update.

2) While inserting new

values in the table the order of columns should be kept in mind otherwise you may get a type mismatch error. To avoid this try using the INSERT syntax where you have to specify the column names.

Page 68: Oracle db ebook

68

Another version,

INSERT INTO emp (empno, ename, job, sal, comm, deptno) VALUES (7890, 'JINKS', 'CLERK', 1.2E3, NULL, 40);

Example: DELETE FROM emp WHERE JOB = 'SALESMAN' AND COMM < 100;

Page 69: Oracle db ebook

69

Before explaining how to create table I would like to give you slight explanation about the word “Schema”, what it is? And what it means in Oracle Jargon. Schema is basically has a Latin word and it means, a diagrammatic representation; an outline or model. Schema can be visualized by looking at the organization chart of a company e.g., the one shown below.

Key Concepts

1) Each user we create in Oracle is considered as a Schema.

2) Each object created by

user lies under his/her schema.

3) DBA is the owner of all

the schemas and hence all the objects within each of these schemas.

Page 70: Oracle db ebook

70

Figure: Organizational Chart. Showing Chief’s

Schema.

Each user we create in Oracle database is a “Schema”. All the objects (tables, Views, Sequences etc.) created by that user fall under his schema. Oracle DBA (Database Administrator) is the owner of all the schemas and the objects under each. He is just like the “Chief” in the above figure. To create table first you have to decide which schema you want this table to be in. Then log into Oracle system (usually SQL*Plus) using that schema’s (or User’s) credentials i.e. username/password. Upon successful login, execute the CREATE TABLE command to create required table under that schema. If you look in the slide, especially on the syntax of CREATE TABLE statement, there is something mentioned “data type” besides table name which will be name of the table that you want to create and the

Page 71: Oracle db ebook

71

columns names which will be the names of the columns that you want to be in the table. While creating table you also have to tell Oracle what type of data goes in which column or in other words data type of each column e.g., if you tell oracle that I want to store date type of information in first column and text type of information in second column then while inserting data into this table using the INSERT statement you cannot insert number or currency like information in first column. If you do so, you will get “type miss-match” like error. Now lets see what different types of “Data Types” are available in Oracle so we can utilize them in the data type assignment of each column within a table in the CREATE TABLE statement.

CHAR (size) A fixed-sized field of characters. Max 2000 bytes/characters.

VARCHAR2 (size) A variable-sized field of characters. Max 4000 bytes/characters.

LONG A variable-sized field of characters. Max 2GB.

NUMBER (precision, scale)

A variable-sized number. A NUMBER datatype with only one parameter is NUMBER (precision), where the parameter specifies the precision of the number. The precision can range from 1 to 38 and the scale can range from -84 to 127. A NUMBER datatype with no parameters is set to its maximum size.

DATE A fixed-sized 7 bit field that is used to store dates. One thing to note

Page 72: Oracle db ebook

72

is that the time is stored as part of the date.

RAW (size) A variable-sized field of raw binary data. Max 2000 bytes.

LONG RAW A variable-sized field of raw binary data. Max 2 GB.

BLOB

The Binary Large Object is a field that holds unstructured binary data. Max 4 GB.

CLOB

The Character Large Object is a field that holds single byte character data. Max 4 GB.

BFILE

An external binary file. The maximum size for this file is 4 GB. The size is also limited by the operating system.

Example:

CREATE TABLE album( id NUMBER(6), title VARCHAR2(100), artist VARCHAR2(100), activity DATE );

Once you write this statement in SQL*Plus or iSQL after logging in, a table with the name “album” will get created having four columns viz., id, title, artist and activity. In the last column we will insert the date when the record got submitted. You can create as many tables you want and they all will be under your schema.

Page 73: Oracle db ebook

73

No one else beside DBA can have access to them unless you or DBA grant access explicitly. Example: CREATE TABLE emp_dept

AS SELECT * FROM scott.emp WHERE deptno = 10;

This is another way of creating table. Not only the structure but the data will get copied too in the emp_dept table.

Page 74: Oracle db ebook

74

Once you have created a table you can alter its structure using the ALTER TABLE statement. You can add column, modify the attributes of columns e.g. data type, attach a constraint, rename a column or even drop it. All these tasks can be performed with an ALTER TABLE statement as shown in the slide. Do keep in the back of your mind that these modifications are 100% possible when you are not having any data inside the table but for cases where you already have data you cannot perform certain task e.g if the NUMBER column already contains data you cannot decrease its size etc.

Key Concepts

1) Certain modifications using ALTER TABLE statement are not possible if you are already having the data inside that table.

2) One way of getting rid

of unwanted column or unwanted data is to utilize the power of CREATE TABLE AS statement.

Page 75: Oracle db ebook

75

Example:

ALTER TABLE customers RENAME COLUMN limit TO amount;

One thing I would like to mention over here, if you want to get rid of unwanted data is to utilize the beauty of CREATE TABLE …. AS SELECT statement.

Page 76: Oracle db ebook

76

While creating tables besides mentioning about data types of columns we can assign constraints on each column wherever we feel necessary. Constraints help in achieving data integrity. In other words, before inserting or updating any information in database Oracle instance makes sure that none of the constraints get violated. For example if you have implemented UNIQUE constraint on one column and if you try to insert a record with a duplicate value that was already there in the column having UNIQUE constraint, you will a “constraint violation” error. Let’s study all of these constraints and their applications one by one. NOT NULL is a type of constraint once implemented on any column; you would not be able to insert NULL values in it.

Key Concepts 1) Each value in the Primary

Key column is a unique identifier for the whole record/row or in other words, no duplicate or NULL values are allowed in Primary key constraint column.

2) Oracle creates a implicit

index for a column having Primary key constraint on it.

3) Index helps in speeding up

the search process just like the index at the end of a book.

Page 77: Oracle db ebook

77

Similarly with UNIQUE once defined on any column we would not be able to insert any record in the table with a value that was already there in UNIQUE constraint column. Example: CREATE TABLE dept (deptno NUMBER(2), dname VARCHAR2(9) CONSTRAINT unq_dname UNIQUE, loc VARCHAR2(10) ); In this example we have assigned a unique constraint on dname column. We have also used the CONSTRAINT clause to assign this constraint a name. We can also achieve the same result using the following statement. CREATE TABLE dept (deptno NUMBER(2), dname VARCHAR2(9) UNIQUE, loc VARCHAR2(10) ); The difference between this statement and the one mentioned above, we haven’t used the CONSTRAINT clause. So what is the benefit of using this clause? The benefit is, we can disable/enable this constraint using the ALTER TABLE statement.

4) Referential Integrity constraint also known as PRIMARY/FOREIGN KEY relationship between two or more tables results in two thing

• Cannot delete any

record from parent table with related PRIMARY KEY column value in child table’s FOREIGN KEY column unless we use ON DELETE CASCADE clause.

• Cannot update/insert anything in child’s table FOREIGN KEY column with no corresponding value in parent table PRIMARY KEY column

Page 78: Oracle db ebook

78

Example: ALTER TABLE dept DROP CONSTRAINT unq_dname; This statement will drop the constraint only from the table altogether. But if you would like to disable the constraint momentarily its better to use the DISABLE clause instead of DROP it. Example: ALTER TABLE dept DISABLE CONSTRAINT unq_dname; Once disabled this constraint will not be checked each and every time whenever there is a new insertion or update of record. Now the next question will popup in your mind, why there is a need to disable a constraint? One of the application of disabling a constraint comes at times when we do data migration from one oracle system to another or in other words bulk data loading into tables using SQL*Loader, we will this later in this book. If we are already sure that this data is already checked for uniqueness as its coming from table already having UNIQUE constraint on the column under discussion so there is no need for Oracle to perform this step while uploading bulk records and in this way we can save time of bulk data transfer. Once done we can enable the constraint using the following statement and this will bring the table back to its original state. A constraint that is currently disabled can be enabled in one of the two

Page 79: Oracle db ebook

79

ways: enable NOVALIDATE or enable VALIDATE. The former does not check existing data for constraint violation. ALTER TABLE dept ENABLE VALIDATE CONSTRAINT unq_dname; Or, ALTER TABLE dept ENABLE NOVALIDATE CONSTRAINT unq_dname; On the other hand, CHECK constraint is called custom constraint. You have given the liberty to create or define the constraint according your needs. Example: CREATE TABLE suppliers

( supplier_id numeric(4), supplier_name varchar2(50), CONSTRAINT check_supplier_id CHECK (supplier_id BETWEEN 100 and 9999) );

Similarly in this case too Oracle Instance will make sure before inserting or updating any information into this suppliers table that the supplier_id lies between 100 and 9999. If it doesn’t then the user will get a “constraint violation” error and the request will not get processed. Lets now talk about Primary Key constraint. Once you implement this

Page 80: Oracle db ebook

80

constraint on any column, you would not able to insert duplicate and NULL value i.e. each value in that will be a unique identifier for that whole record/row. Moreover Oracle creates a implicit index on that column. Index created by Oracle for any column of a table is having the same analogy of index we are having at the end of each book. Indexes created by Oracle serve the same purpose as the indexes at the end of books i.e. helps in speeding up the search process. Example: CREATE TABLE dept_test (deptno NUMBER(3) CONSTRAINT pk_dept_test PRIMARY KEY, dname VARCHAR2(15), loc VARCHAR2(10) ); Once you have created this table dept_test start inserting values in it. Remember since you have created a PRIMARY KEY constraint on deptno, column you would not be able to insert NULL value or any thing that was already there in deptno but can insert NULL or duplicate values in rest of the columns. Also Oracle will create a implicit index on this primary key column because Oracle presumes that you would be using this column a lot in your search criteria (WHERE caluse) and there are bringht chances that you may link this table with some other table using FOREIGN KEY constraint. Once you have installed Oracle with all the default options, Oracle Universal Installer will create a general purpose Oracle database by default. That database will be having dept and emp

Page 81: Oracle db ebook

81

table already along with some data. That’s the reason in examples above I use the dept_test name for table otherwise I would have used the word dept only. Next we will create another table emp_test and link it with the dept_test table using FOREIGN KEY constraint. Once such a table gets created, the table having PRIMARY KEY is called the “Parent Table” and the one having the FOREIGN KEY is called the “Child Table”. A patent table can have as many child tables as you like or in other words a primary key constraint column can be linked to one or more tables using FOREIGN KEY constraint. No value can be inserted in the FOREIGN KEY constraint column having no exactly similar value in the referenced PRIMANARY KEY constraint column of parent table. Example: CREATE TABLE emp_test (empno NUMBER(4) CONSTRAINT pk_dept_test PRIMARY KEY, ename VARCHAR2(10), job VARCHAR2(10), deptno NUMBER(3), CONSTRAINT fk_emp_test FOREIGN KEY (deptno) REFERENCES dept_test(deptno) ); Once this table gets created you can say that you have developed a “link” between the two tables and this link will be permanent. The two beauties of PRIMARY/FOREIGN KEY relationship are as follows.

Page 82: Oracle db ebook

82

• Keeping in focus PRIMARY

KEY constraint column value(s) of parent table, if someone tries to delete a record from the parent table and there are corresponding value(s) in one or more child table(s) FOREIGN KEY constraint column, this task with result in Referential Integrity constraint violation error. But we can override this clause if we add ON DELETE CASCADE clause as shown in the example below.

CREATE TABLE emp_test (empno NUMBER(4) CONSTRAINT pk_dept_test PRIMARY KEY, ename VARCHAR2(10), job VARCHAR2(10), deptno NUMBER(3), CONSTRAINT fk_emp_test FOREIGN KEY (deptno) REFERENCES dept_test(deptno) ON DELETE CASCADE );

Because of the cascade delete, when a record in the dept_test table is deleted, all records in the emp_test table will also be deleted that have the same deptno value.

• If someone tries to Update/Insert a value in the FOREIGN KEY constraint column of child table and there is no such value in the parent table PRIMARY KEY column, this task will result again in Referential Integrity constraint violation error.

Page 83: Oracle db ebook

83

Developing a PRIMARY KEY and FOREIGN KEY relationship between two or more tables has one another name, its called Referential Integrity (RI) constraint. So instead of saying that you have developed and implemted PRIMARY/FOREIGN KEY relationship between dept_test and emp_test tables, you can say that you have implemented RI constraint between dept_test and emp_test.

Page 84: Oracle db ebook

84

Whenever you come across “Data Integrity” just say in your mind that the author or speaker is referring to all different sorts of constraints. Being a DBA, it will be one of your jobs to take all possible steps for having data integrity in the database because its is enterprise wide system users from all over the world may be interacting with the system. For any given instance some request would be coming to Oracle instance to insert new records others would be to update certain records and just think for a moment if would not be having all these different kinds of constraints, then there would be lots of redundancy of information, lots of errors etc. Its like filling a form online and you are allowed to enter date of birth in the zip code field.

Key Concepts 1. Having a clear concept

about the following two terms is really essential because these two terms are widely used in all RDBMS.

2. Many author uses these

terms a lot keeping in their mind that the read knows it’s meaning already.

Page 85: Oracle db ebook

85

In short all these constrains available in Oracle helps attaining maximum data integrity. Another term that is used widely in all sorts of RDBMS (Relational Database Management Systems) not only just Oracle is “Data Consistency”. It means that every one connected to Oracle Instance will see the consistent view of information. Oracle has very beautiful built-in locking mechanism. Above all Oracle takes care of this by default magnificently but if you want you can configure and override the default configuration. Transactional changes will only made visible to everyone once committed by executing the COMMIT command.

Page 86: Oracle db ebook

86

Like I said before, all the joins techniques we have learnt before viz., Outer Join, Equijoin etc. are at the query level can be implement on tables having Primary/Foreign Key relationship or not. Implementing Primary/Foreign key relationship between two or more tables is also known as implementing Referential Integrity (RI) constraint between tose two or more tables. The reason of bringing this topic up is, most beginners often get confused when they read about joins and then primary key and foreign key.

Key Concepts

1) Joins statements can be executed on tables having Referential Integrity constraint or not.

2) The word “Join” should

not be confused with as I if you are joining the two tables permanently.

Page 87: Oracle db ebook

87

They think that joins statements are only executed on tables linked through primary and foreign key relationship but it’s not true. The word “joins” sometimes gives the feeling as if we are about to join two or more tables permanently but no, its just you are querying data from two or more tables using of the techniques that fall under the title called “Joins”.

Page 88: Oracle db ebook

88

Besides tables that we create within our schema, there are other logical structures called database objects. One of those objects is called a View. Like the name says, it’s a view, a window through which we can see only the data we need or in other words we can create a view and grant users to view the information. While creating the view we will include only those information that we want others to have access. Views are comprised of one or more tables or views and they themselves contain no data and the underlying tables are called based tables. Creating views is exactly similar creating tables, the only difference is we use CREATE VIEW statement instead of CREATE TABLE statement.

Key Concepts 1) Once you drop a VIEW, base

tables and views will not get deleted.

2) For each index you create

Oracle creates an implicit table based on that particular column in a manner that will help it in locating records easier.

3) Sequences are like auto-

number objects. You can always get the next number and insert it in any number datatye column, usually id kind of field e.g. emp_id.

Page 89: Oracle db ebook

89

Example:

CREATE VIEW emp_view AS SELECT * FROM employees WHERE department_id = 20;

Dropping a view will not drop the underlying base tables or views. Also you can insert and update the base tables through the view provided the constraints implemented on the based tables won’t get violated. We can use any of the form of Join-based SELECT statement to have the view based on more than one table. We can also create a read only view using the WITH READ ONLY clause and grant access to users so that then they would not be able to modify the base tables. Example:

CREATE VIEW emp_view AS SELECT * FROM employees WHERE department_id = 20 WITH READ ONLY;

Example:

DROP VIEW emp_view;

Dropping view neither drops base tables or views nor the data residing in each of those base tables. Next we would to talk about are the Indexes. We have seen previously that an index gets created whenever you create a PRIMARY KEY constraint on any

Page 90: Oracle db ebook

90

column of a table, implicit. We call it an implicit index because you haven’t executed any index creation statement but it gets created as a result of something else we wanted to do or in other words you can say the term it’s a “by product” of PRIMARY KEY constraint. There are certain pros and corns associated with index. The sole purpose or you can say the benefit of index is, it speeds up the search process. Just like the index available to you at the end of book. Imagine a book having no index and comprised of 1000 pages and 30 likes per page. I hope you got the essence, the beauty, and the power of having an index. Now the next question comes in mind. What’s the criterion of selecting a column for index purpose? or in other words which columns should we create index on? And what’s wrong with having indexes created on all the columns of all the tables? Do think for a while at least before reading below the reasons. The criterion of selecting a column for index purpose is, the column we would be using a lot in there WHERE clause. The emphasis is on “a lot”. How to figure out which columns will be used a lot? Some columns are apparent e.g. in SSA (Social Security Administration) office, its obvious that most of the time they would be pulling up the information using ssn or most of times requests coming to SSA to look up for something would be through ssn. It doesn’t mean that you cannot make a search based on other columns.

Page 91: Oracle db ebook

91

Each time you create a index on any column of a table Oracle creates a implicit table that can help it in locating the records much faster. That table size depends on the size of column on which you have created the index but both these quantities are in direct relationship with each other i.e. more the number of values in column, on which you have created the index, more would be size of implicit table that Oracle will create. So creating indexes on all the columns of all the table inside the database will to do much rather it will tremendously increase the size of the database and its growing rate. Moreover, you would not be using them all in your search criteria. Example:

CREATE INDEX emp_test_idx ON emp_tests (empno);

Where emp_test_idx is the name that we have given to this index and its created on empno column of emp_test table. Example:

CREATE INDEX m1_idx ON m1 (id1, id2);

Where m1_idx is the name of the index, which is created on two columns id1, and id2 of m1 table. Such type of index is called composite index as they gets created on two or more than two columns.

Page 92: Oracle db ebook

92

Example:

ALTER INDEX m1_idx RENAME TO m2_idx;

This statement will rename the index to m2_idx. There are lot other stuff that we can do with ALTER INDEX command but we will it later in this book when we talk about different forms of indexes that are available. Example:

DROP INDEX m2_idx; This statement will drop the index on that particular column all together. Sequence is also a logical structure in the database. Just like tables and views, they are also called as objects. Basically Sequence generates sequence number so you would have to remember what id number should we give to newly hired employee. Just ask the sequence for next value and it provide you one. How the sequence generates numbers, it all depends on the way you create them. Example:

CREATE SEQUENCE emp_seq START WITH 1000 MAXVALUE 99999 INCREMENT BY 1 NOCYCLE;

This statement will create sequence emp_seq which will start with 1000 and increment by 1 i.e. each time you ask for

Page 93: Oracle db ebook

93

new value from this sequence it will give you a number 1 higher than the previous one. Maximum value would be 99999 and if you want you can use NOMAXVALUE. CYCLE clause we use if you want to force the sequence to start all over again once it hits the maximum value otherwise use NOCYCLE. Example:

SELECT emp_seq.NEXTVAL FROM dual;

We use NEXTVAL method to retrieve the next value from the sequence so that we don’t have to worry about what id to allot to a newly hired employee.

INSERT INTO emp_info (id, name, email)

VALUES (emp_seq.NEXTVAL, 'Gregory', ‘[email protected]' );

Example:

SELECT emp_seq.CURRVAL FROM dual;

You can also find out the current value of the sequence using CURRVAL method. Example:

ALTER SEQUENCE emp_seq MAXVALUE 1500;

We can also alter the structure of the already exisiting sequence in the database

Page 94: Oracle db ebook

94

using the ALTER SEQUENCE statement. Example:

DROP SEQUENCE emp_seq; We can also drop a sequence from the database using the DROP SEQUENCE statement. Lastly, we create synonym using CREATE SYNONYM statement. Synonym is basically an alias or another name given to already existing database object e.g. table, view, sequence, stored procedure, stored function, package etc. The last three names you just read are also object of oracle database but the fall under the category of PL/SQL language. It can be further clarified by looking at the following real world example. Example:

CREATE SYNONYM offices FOR hr.locations;

So instead of typing the full name for the object i.e. schema.object_name, you have to just type the synonym name. If you want to grant access to other users, the users need not to know which schema this object is in. Moreover, if you drop this synonym all users will not be having access to hr.locations table any more. Remember dropping the synonym will not drop the underlying table or object.

Page 95: Oracle db ebook

95

Example:

CREATE PUBLIC SYNONYM employees

FOR hr.employees@sales;

Public synonym are accessible to all the users provided they have access to underlying object. The above statement will create a employee synonym for a employees table in hr schema in a remote sales database. Example:

DROP SYNONYM offices; We use DROP SYNONYM statement to drop the synonym completely from the database. The underlying object will not get removed. I thing you must be looking that I haven’t discussed something like ALTER SYNONYM. So just keep in your mind there is no such statement. If you want to alter synonym just recreate it by first dropping it and then using CREATE SYNONYM statement.

Page 96: Oracle db ebook

96

To install Oracle software on the machine you have to be the member of OS (Operating System) SYSOPER or SYSDBA group. Once you install Oracle software and have default database created by Oracle Universal Installer. There will be two users created at the end of creation of database. One is “sys” with the password “change_on_install” and other one “system” with the password “manger”. It’s highly recommended that you change these passwords since these are standard accounts and everywhere is the same. Its also recommended that you should create at least one more dba level account. We will learn in more detail regarding the purpose of having a sys and system accounts later in this book.

Key Concepts 1. Users can be authenticated

by the database or the OS. 2. Privileges can be assigned to

users and roles.

Page 97: Oracle db ebook

97

To create a new user (schema) you should login to Oracle Instance using one of the DBA’s account and then user the CREATE USER statement to create new user. Example: CREATE USER sophia IDENTIFIED BY evergreen DEFAULT TABLESPACE user_demo; This statement will create a new user with the login as sophia and password as evergreen and whatever objects (tables, views etc.) that user sophia will create will reside in the user_demo tablespace. Such types of accounts are called “Database-Authenticated User Accounts” where users get verified by the database. There is another form of authentication which is called “Externally Authenticated User Accounts” or also known as OPS$ accounts. Such users get verified by OS once they log in. When Oracle 6 was introduced such category of accounts were to be prefixed with OPS$ and that’s why we call them OPS$ user accounts. If os_authent_prefix init.ora paramenter is left as default i.e. OPS$, then the Externally Authenticated User Account can be created as follows. CREATE USER ops$sophia

IDENTIFIED EXTERNALLY; On the other hand if its set to blank (os_authent_prefix = “ ”) then no prefix is required as shown in the following example. CREATE USER sophia

Page 98: Oracle db ebook

98

IDENTIFIED EXTERNALLY; Example:

ALTER USER sophia IDENTIFIED BY black123;

Or,

ALTER USER sophia DEFAULT TABLESPACE hr_data;

To change the password of sophia or anything else related to user, use the ALTER USER statement. Once you have created the user and assigned him/her default tablespace then the next thing is to grant that user is ready to go. He can now create database objects (tables, views etc) under his schema. You can also assign him access to objects under your schema using GRANT statement and you can also take that privilege back using the REVOKE statement. Next very important concept I would like to give you is the “role”. Role in database jargon means the group of privileges bundled together and given a name or alias. So what is the benefit of creating roles? For example, let say Human Resources (HR) department of a company has 65 people and out of those 65 people, 35 were having SELECT, INSERT, UPDATE, DELETE privileges on Payroll department tables and we are assuming a scenario where you have assigned each of these 35 people privileges not role. After

Page 99: Oracle db ebook

99

a month change of business rule comes up and its described that those 35 HR would only be having SELECT privilege on Payroll tables. Without using role you have to run REVOKE statement 35 times and if you would go by “role” technique one statement can implement that change no matter whether there are 35 people or 3500 people. Privileges can be granted to users as well as roles. Example: CREATE ROLE hr_emps

IDENTIFIED BY evergreen; By default a role is created with the password and is enabled. One that is created with the password is disabled by default and can be enabled using SET ROLE statement. SET ROLE hr_emps

IDENTIFIED BY evergreen; SET ROLE statement can be used to enable or disable a gole. Once we have created the role, next thing is to assign this role, privileges.

GRANT SELECT, UPDATE ON emp_test TO hr_emps;

There are two kinds of privileges, one is called “Object Privileges” and the other one is called “System Privileges”. The complete list of each of these object privileges is given below along with their

Page 100: Oracle db ebook

100

application.

ALTER Table, Sequence DELETE Table, View INDEX Table INSERT Table, View READ Dictionary REFERENCE Table SELECT Table, Sequence, View UPDATE Table, View EXECUTE PL/SQL Objects e.g.

Functions, Package etc. The way to interpret this table is, if you keep the above example in your mind, you can grant ALTER, DELETE object privilege on any table but you cannot grant READ on any table. This implies to EXECUTE, one cannot grant EXECUTE privilege on any table, view etc. its only meant for PL/SQL objects. On the other hand, System privileges allow the ability to create, alter, drop and manage database objects or features. There is a long list of system privileges and for ease of study we have categorized them in the following categories.

CREATE CLUSTER CREATE ANY CLUSTER ALTER ANY CLUSTER

Cluster

DROP ANY CLUSTER ALTER DATABASE ALTER SYSTEM Database AUDIT SYSTEM CREATE TABLE CREATE ANY TABLE ALTER ANY TABLE DROP ANY TABLE SELECT ANY TABLE UPDATE ANY TABLE

Table

DELETE ANY TABLE

Page 101: Oracle db ebook

101

INSERT ANY TABLE LOCK ANY TABLE CREATE SEQUENCE CREATE ANY SEQUENCE ALTER ANY SEQUENCE DROP ANY SEQUENCE

Sequence

SELECT ANY SEQUENCE CREATE SYNONYM CREATE ANY SYNONYM CREATE PUBLIC SYNONYM DROP ANY SYNONYM

Synonym

DROP PUBLIC SYNONYM CREATE SESSION Session ALTER SESSION CREATE TABLESPACE ALTER TABLESPACE DROP TABLESPACE MANAGE TABLESPACE

Tablespace

UNLIMITED TABLESPACE CREATE USER ALTER USER BECOME USER User

DROP USER CREATE VIEW CREATE ANY VIEW View DROP ANY VIEW CREATE ANY INDEX ALTER ANY INDEX Index DROP ANY INDEX CREATE ROLLBACK SEGMENT ALTER ROLLBACK SEGMENT Rollback Segment

DROP ROLLBACK SEGMENT

Page 102: Oracle db ebook

102

Besides these are system privileges for PL/SQL functionalities like CREATE PROCEDURE, CREATE ANY PROCEDURE, EXECUTE ANY PROCEDURE etc. Two very special and important system privileges are SYSDBA and SYSOPER. That’s why I want to discuss them separately. Following table describes the power lies beneath these two system privileges.

Create new database Startup, Shut down database Alter database with the OPEN, MOUNT, BACKUP, CHANGE CHARACTER SET, ARCHIVELOG and RECOVER options

SYSDBA

Create SP file Startup, Shut down database Alter database with the OPEN, MOUNT, BACKUP, ARCHIVELOG and RECOVER options

SYSOPER

Create SP file Example:

CREATE ROLE hr_emps; GRANT SELECT, UPDATE ON emp_test TO hr_emps; GRANT hr_emps TO margorie, daisy, jessie, semper;

Page 103: Oracle db ebook

103

In this example we have first created the role hr_emps and next we assign certain privileges to it and finally we have assigned it to all the employees of hr (Human Resources department). After a month, new business rule comes up which says that all the hr employees should only be having SELECT privilege on emp_test table. To implement this rule all you have to do is to revoke the privilege from the role e.g.

REVOKE UPDATE ON emp_test FROM hr_emps;

Lastly, there is one more thing just like role, its called profile. Profiles allow you to assign resource consumption limit or setting password policies on user accounts. All user accounts have profile assigned to them. If you don’t assign it explicitly while creating or altering the user account explicitly, a default one gets assigned automatically. You can assign profile to a user account at the time of creation of account or you can use ALTER USER command to assign it later. Let’s have a look on the following example.

Page 104: Oracle db ebook

104

Example:

CREATE USER marion IDENTIFIED mateena PROFILE paramod;

Or,

ALTER USER marion PROFILE paramod;

Here mateena is a password for username marion and paramod is the name of a profile we created earlier, it could be any name. Now its see how we can create profile. Example:

CREATE PROFILE paramod LIMIT CONNECT_TIME UNLIMITED;

Or,

ALTER PROFILE sidney LIMIT CONNECT_TIME 240;

When the CONNECT_TIME value is exceeded Oracle rolls back the current transaction and ends the session. Example:

CREATE PROFILE hr_dept LIMIT FAILED_LOGIN_ATTEMPTS UNLIMITED;

Page 105: Oracle db ebook

105

Or,

CREATE PROFILE hr_dept LIMIT FAILED_LOGIN_ATTEMPTS 5;

I think this is self-explanatory. Besides these, the other resource consumption and password settings are, LOGICAL_READS_PER_CALL, SESSIONS_PER_USER, PASSWORD_LIFE_TIME, PASSWORD_GRACE_TIME, PASSWORD_LOCK_TIME, PASSWORD_REUSE_TIME, PASSWORD_REUSE_MAX etc.

Page 106: Oracle db ebook

106

We Did It!

In this chapter we basically learnt the “grammar” of communication language (SQL); language that we will use to communicate with Genie (Oracle Instance). We learnt all different types of statements like SELECT, INSERT, UPDATE, CREATE etc. and concepts of “joins” and “referential integrity” constraints. We also talked about privileges, roles and profiles. In short we learnt all ins and outs about SQL: Structured Query Language.

Page 107: Oracle db ebook

107

CChhaapptteerr 33

Oracle Database Concepts • Physical Architecture • Logical Architecture • Relationship between Physical and Logical

Architecture of Oracle database • Redo Log Files utilization

Page 108: Oracle db ebook

108

Page 109: Oracle db ebook

109

Like I said before, the complete Oracle system consists of two things viz., Oracle Instance and Oracle Database. To better understand and visualize the system you can thing of Oracle Instance as Genie and the Oracle Database as Genie’s Notebook. This is a data management genie and it has some magical powers. It utilizes its notebook to remember data or information and fulfill users’ requests coming from all over the world. This chapter basically deals with the Oracle database and its architecture. When you were learning SQL and executing quires in SQL*Plus environment, you must be thinking where this stuff is actually coming form. In which physical file emp table is residing and in which file you created emp_test table. After reading this chapter all your

Key Concepts

1) Oracle Server is comprised of two things

o Instance o Database

2) Visualize Oracle

Instance as Genie and Database as Genie’s Notebook, which he uses to remember information for longer period of time.

3) Oracle database has a

logical architecture and a physical architecture.

Page 110: Oracle db ebook

110

questions like this and many more will get resolved. Oracle database has a logical architecture as well as physical architecture. When I first time read this thing I got confused. While reading this chapter, many such things will come up, don’t stop just keep reading till the end of the chapter. You will see that everything will get clear. When we talk about database objects, we are actually talking about logical architecture and when are talking about physical files like control file, data files etc., we are actually talking about physical architecture of Oracle database.

Page 111: Oracle db ebook

111

We can look at Oracle Database through two angles viz., logically and physically. We will talk about logical architecture of Oracle Database in the next article. Physical architecture of Oracle Database is comprised of five different types of files. Those five files along with their brief description are given below.

Control File Data Files Main Oracle Database

Files. Redo Log Files Oracle Instance reads

it at startup. Parameter File (init.ora)

Archiving of Redo Log Files

Archived Redo Log Files

Control File: Every database has one control file and it basically contains the information regarding the structure of database. Control file doesn’t contain any records.

Key Concepts

1) Oracle Instance updates the control file whenever the physical structure of database is changed by DBA.

2) Data Files holds the

actual data and if you keeping added records to your database tables, the sizes of these data files increase along with the size of redo Log files.

3) Redo Log files get

utilized in a circular fashion.

Page 112: Oracle db ebook

112

If you keep inserting records in your tables using the INSERT statement, the size of your control file will not change. Oracle Instance reads control file at the time of startup to find out database name, location of data files and redo log files. You can tell Oracle Instance to “Multiplex” control file or in other words having a exact mirror copy of control file sitting at some other disk. In case media failure occurs you would always be having another copy available on the other disk. Never place the mirror copy on the same disk. Data Files: Data files are the files where the actual database’s data resides. If you keep inserting the records using the INSERT statement the size of these files increases. Oracle database can have one or more data files. The size of each data file is limited to OS (operating system) maximum file size. But the size of the Oracle Database can be whatever you like. There is no limit on the maximum size of the database. If you are running out of space you can always add more data files to the database by bringing in hard drives into your system. Redo Log Files & Archived Redo Log Files: Redo Log files are the files that maintains the changes happening in data. To visualize this keep Microsoft Word in mind, it also keeps the changes you make in data so that you can revert back as required. Every Oracle Database has set of two or more redo log files. Each set is

4) It is recommended that Oracle should be run under “Archived Log Mode” so that Redo Log files get copied before getting overwritten.

5) Redo Log files along

with Archived Redo Log files plays a major role in the database recovery process.

Page 113: Oracle db ebook

113

called redo log file group. Redo log files gets utilized in a circular fashion i.e. when Oracle Instance finishes writing the last redo log file then it starts overwriting the first one unless you archived that redo log file or in other words unless you order Oracle Instance (Giene) to make sure redo log files gets archived before they get over written. We will study in great detail in the last slide of this chapter. The way we can protect overwriting of Redo Log files is to run the Oracle Instance in Archive Log Mode. If Oracle is running in this mode than redo log files will get archived to the destination you specify in the parameter file before getting overwritten. Parameter File (PFILE): Parameter file contains the name of the database, location of control file, memory structure of Oracle Instance and certain other necessary Oracle system configuration parameters. PFILE is read by Oracle Instance while starting up the database or even before that i.e. when the Instance creates itself. We will talk about instance in the next chapter but let me give you a brief image what instance is. The background processes and memory architecture constitute an instance. You may find it difficult to understand it at this spot. Do not stop. Keep reading and everything will be fine. Since Instance is the combination of “Memory Architecture” and “Background Processes” so when you double click oracle icon or runs

Page 114: Oracle db ebook

114

oracle system, the first thing that gets erected is Oracle Instance. How the memory structure would be for that instance, that’s why we define in parameter file.

Page 115: Oracle db ebook

115

Physically the data inside the database resides in the data files located on hard drives and if you want you can copy them to different location but remember you cannot read those files by yourself. The data in data files are encrypted and can only be understandable to Oracle Instance, so if you want to take anything out from those data files you first have to make the Oracle Instance alive and then you have to present your credential (username/password) to the Instance and based on your access level you data related wishes will get fulfilled. Logically the data inside the database resides inside the tablespaces. Each tablespace is comprised of segments. Oracle Instance allocates a segment each time you create a object (table, view etc) inside the database. Each segment is

Key Concepts

1) Logically database is composed of tablespaces. Each table space can be visualized as a department level database e.g for each department of a company you would be having one tablespace.

2) Each tablespace is

composed of segments and Oracle allots a segment each time you create a database object e.g. tables, views etc using CREATE statement

3) Each Segment is

composed of Extents and extents are nothing but contiguous DB Blocks.

Page 116: Oracle db ebook

116

composed of Extents and What are Extents? Each extent is a group of contiguous DB Blocks. Now what are DB Blocks? A DB Block is the smallest building block of the database and is usually the even multiple of OS Block e.g. DB Block can be (2 * OS Block) but not (1.37 * OS Block). The size of the DB Block can be specified using DB_BLOCK_SIZE init.ora parameter. You can assign values like 2KB (2 Kilo Byes) and if the OS permits you can have 4KB, 8KB etc.

4) DB Block is usually the even multiple of OS Block and its value is specified in parameter file (init.ora)

Page 117: Oracle db ebook

117

This is a side by side relationship development between logical and physical architecture of Oracle database. The database is the same but you can look at it through two angles. You can talk about its files (data files, control file etc.) or you can talk about its tablespaces, segments (objects) etc. Tablespaces are like small departmental level databases with a bigger umbrella of company’s database. Data inside these tablespaces within the database resides in the data a file. You insert data into the segments (tables) using INSERT statement so the segment grows. When the segment grows, tablespace within which its residing grows and the size of data file(s) associated with the tablespace grows. If there is only one data file associated with the tablespace then the maximum size of the tablespace cannot

Key Concepts

1) Data inside the tablespace reside in the data files which are physical files like all other files located on your computer.

2) Size of the tablespace

and hence the size of database is not limited to the OS marked limit on the file size.

Page 118: Oracle db ebook

118

be more than the Operating System maximum files size. But you can always add another data file to the tablespace using the ALTER TABLESPACE statement. In this way the maximum size of tablespace is not limited to the OS marked limit on file size. When we talk about the relationship between logical and physical architecture of database, we mean that we are talking about how tablespaces are related to the data files.

Page 119: Oracle db ebook

119

When we talk about the relationship between physical and logical architecture of oracle database, we are actually talking about the relationship between data files and tablespaces or vice versa. Data file is the physical quantity. Its physical because you can see the actual data files sitting in your computer just like the other files sitting in the computer whereas the tablespaces (as well as segments, extents and DB Blocks) you cannot see it or point at it or in other words cannot copy using OS copy command like you can with data files or other files in your computer. After having this concept, I would like to define the relationship between logical and physical architecture of Oracle Database in the following lines. It’s very important and I want you to memorize it word by word. First understand it then memorize it.

Key Concepts

1) Each tablespace can have one or more data files associated with it but each data file can never be associated with more than one table space.

2) Segments can be sitting

under two or more data files under the same tablespace.

Page 120: Oracle db ebook

120

Each tablespace can have one or more data files associated with it but each data file can never belong to more than one tablespace. Segments (database objects) can lie in more than one data file within the table space. This can be better visualized by looking at the next slide. Whenever you create a database object, Oracle Instance allocates a segment in the tablespace assigned to your schema at the time of creation of user account using CREATE USER statement.

Page 121: Oracle db ebook

121

With this slide you can easily visualize the physical and logical architecture of Oracle Database. For some people this figure would be self-explanatory and how truly someone has said, “A Picture is better than thousand words”. As shown in the slide, this Oracle Database is comprised of two tablespaces namely Tablespace 1 and Tablespace 2. Tablespace 1 is comprised of two data files whereas Tablespace 2 is comprised of one data files. So we can say that this oracle database has three data files. Tablespace 1 has total of six segments or database objects (tables, views, synonym etc.) and some segments are totally residing in one data file whereas some are residing partially in one data file and partially in other data file e.g. keep in focus Tablespace 1, the data inside segment 2 (SG 2) and segment 5 (SG 5)

Key Concepts

1) Data inside the segment can reside in two or more data files with the same tablespace.

2) Tablespace is nothing

but just an alias given to the group of data files or data file.

Page 122: Oracle db ebook

122

is distributed among two data files. Tablespace 2 is comprised of only one data file so all the objects created by the user will reside in this one data file. The major purpose of adding more data files to the tablespace is to remove the limit of OS defined maximum limit of file size e.g. those are familiar with MS Access know very well that the size of MS Access “database” can never be more than OS defined maximum file size.

Page 123: Oracle db ebook

123

Just a slight revision, we said the database is comprised of five different types of files and we have learnt the purpose of each. Among those five different types of files one is Redo log file. We have learnt earlier in this chapter that a database should have two or more redo log groups. Each redo log group should have at least two or more redo log files called members. Within one Redo log group, all redo log files are mirror copies of each other and its recommend that they all should reside on different physical hard drives or tape drives to increase the fault tolerance factor. Having mirror copies of the original Redo Log file within a group is called Multiplexing of Redo Log files. Its DBA’s job to select how many Redo Log files mirroring is required and where to place each mirrored copy.

Key Concepts

1) A database should have at least two Redo Log groups.

2) Each Redo Log group

should have two or more redo log files called “members”

3) Redo Log files get

utilized by Oracle Instance in a circular fashion.

4) Redo Log files and

Archived Redo Log files play a great role in Oracle Database recovery in times of any kind of failure.

Page 124: Oracle db ebook

124

Whatever data changes are happening in the database, all gets written to the Redo Log files. In other words you can say it’s the “Undo” copy of the original database. Keeping in focus the above slide, Oracle Instance (or more precisely, the Log Writer process) will start writing these changes in data from Redo Log group 1, once it gets filled up, Oracle instance will move to group 2 and finally group 3. Once group 3 finishes up, Oracle instance will start all over gain from group 1. That’s the reason we say that Redo Log files get utilized in a circular fashion. To avoid overwriting of Redo Log files, you should run the Oracle Instance in Archive Log Mode. Whenever Oracle is running in this mode, redo log files gets archived well before they get overwritten. This Redo Log mechanism and Archived Redo Log files play a great role in Oracle database recovery in times of disaster of any kind. We will learn about Oracle Backup & Recovery later in this book in great detail.

Page 125: Oracle db ebook

125

We Did It!

In this chapter we have learnt about “physical” as well as “logical” architecture about Oracle Database. Physically a database is comprised of Control file, Data files, Redo Log files, Parameter file and Archived Redo Log files. But the actual users’ data resides in the data files. Whereas logically, a database is comprised of tablespaces, segments, extents and DB Blocks or database blocks. Moreover, we also talked in detail regarding the utilization of Redo Log files.

Page 126: Oracle db ebook

126

Page 127: Oracle db ebook

127

CChhaapptteerr 44

Oracle Instance Concepts

Page 128: Oracle db ebook

128

Page 129: Oracle db ebook

129

Oracle system or more commonly we use the word Oracle server or Oracle Database, whatever name you use, just keep one thing in mind that Oracle has two pieces. Once is called Oracle Instance and the other one is Oracle Database. User sends commands to the Oracle Instance (Genie) and Oracle Instance after interacting with the database will send the result back to the original user. In this chapter we will only talk about Oracle Instance and you can visualize it as a genie. You cannot retrieve the data directly from the database. You have to send the request to Oracle Instance and it can fulfill your request.

Key Concepts

1) The background processes and memory architecture constitute the Oracle Instance.

Page 130: Oracle db ebook

130

The thing I want you to memorize if you haven’t memorize it yet, the definition of Oracle Instance i.e. the background processes and memory architecture constitute the Oracle Instance.

Page 131: Oracle db ebook

131

Oracle Instance can be defined as, the background processes and memory architecture constitute the instance. There are very few things in this whole book that I will request you to memorize and this definition is one of them. Now the question is what is a “background process”? And what is “memory architecture”? To better understand these two thing I want to give you the analogy of the way human brain works. Human brains works pretty much the same way Oracle Instance works. Human brain also has a memory architecture and background processes. I am giving you the link to the article “Reducing The Factor Of Making Wrong Decisions”.

Key Concepts

1) The background processes and memory architecture constitute the Instance.

2) Human brain works

pretty much the same way Oracle Instance works.

Page 132: Oracle db ebook

132

The last thing I have discussed in this article is, how to optimize the working of human brain and is available at (http://www.selfgrowth.com/ articles/Abbasi1.html). So the brief study of Human Brain working can really help us in visualizing Oracle Instance and how it works. How background processes interacts with the memory architecture to get things done faster and efficiently. The sources of input to the Brain are our five senses similarly the sources of input

Figure Background Processes & Memory Architecture Inside Brain to the Oracle Instance are connected users and their requests. Human brain interacts with the external world utilizing those five senses. Background processes receive inputs from these five senses and based on the request, interacts with the memory and sends the output or the “decision”. For example, you must have noticed, if you come across a very old friend that you

Page 133: Oracle db ebook

133

last met 15 or 25 years ago. You saw him and he saw you in a shopping mall. Both of you will stop and say in astonishment, “I am feeling difficult in recalling but I must have seen you some where”. That’s the point where one of the background processes in brain is

busy searching the memory/dictionary/library (or whatever you name it) to pull up the record/information about that person. And after few seconds you say, “David, my old Kindergarten friend”. He says, “Wait a minute …”. “Julie, good to see you after a long time”. Did you notice one thing, David said wait a minute. So the search that was being performed by one of the background processes in David’s brain came up with the result little late than Julie’s brain. With some people, results come out mush faster but for others very slow. That’s the reason; the article explains how to optimize the working of human brain. Physically all human brains are similar i.e. two hemispheres but it’s the way you build the logical architecture on top of physically architecture which makes one “Einstein” and other me. Whenever we use the word memory we usually are referring to RAM (Random Access Memory) inside the computer. Why we need another kind of memory:

Page 134: Oracle db ebook

134

RAM inside the computer if we already have hard drives? Its because the speed with which Microprocessor (CPU) communicates with the RAM is much faster (Electric Speed, speed with which electric current travels from one point to another or in other words speed of light) compared to the speed with which CPU interacts with the hard drive (Hard Drives involves a mechanical movement of motors inside the hard drive). If you ask me regarding my life 15 years back, day by day e.g. what I did everyday in January, similarly in February and so on up till today. I would never be able to tell you that. But what I can tell, are the major events that happened in my life in the past fifteen years e.g. I got my BS in Electrical Engineering July 2000 or my father bought a brand new PC for me in 1993 etc. If you start utilizing the Note Books (Papers) and start writing the story about your life each and everyday then you can tell everything regarding your past day by day. Similarly Oracle Instance has to utilize the data files to make the data/information permanent.

Page 135: Oracle db ebook

135

Oracle System Human System Oracle Instance + Oracle Database

Human Brain + Note Books (Papers etc.)

Oracle Instance is composed of background processes & memory architecture

Human brain is composed of background processes & memory architecture.

Human brain is far more complex than what I explained. The sole purpose is to develop the analogy, which helps in understanding and leaves fewer burdens on brain to swallow the principles.

Page 136: Oracle db ebook

136

By saying, memory architecture of Oracle Instance we mean how RAM is utilized by Oracle Instance or more precisely part of RAM is dedicated for Oracle purpose, since its not only Oracle that is utilizing the RAM in your computer. Every program that you run under any OS (Windows, Linux etc.) occupies certain amount of RAM. For example, if your computer has 256MB of RAM and lets suppose MS Word occupies 50 MB. If you keep on opening MS Word instances your system will turn slower and slower and eventually will halt. Every software that you run, utilizes RAM for speedy processing and when it comes to making changes permanent (when you press the save button etc.) that software writes the stuff in the hard disk.

Key Concepts

1) Every software that you run under any OS occupies RAM (or Memory), so does Oracle.

2) SGA is the most

important piece and it stands for System Global Area.

3) Non-shared memory is

not shared among all the users whereas shared is shared among all.

4) Database Buffer Cache

has three sub-caches.

o KEEP o RECYCLE o DEFAULT

Page 137: Oracle db ebook

137

Similarly Oracle utilizes RAM too for the same purpose what other programs does i.e. speedy processing. How that RAM is categorized or what’s its architecture, that’s exactly what, is shown in the slide. The RAM or simply memory, occupied by Oracle is divided into two major categories. One is called Shared Memory and the other one is called Non Shared Memory. Oracle allocates SGA (System Global Area) whenever Instance starts and deallocates it when the Instance ends. In OracleRAC (Oracle Real Application Cluster, used to be called as Oracle Parallel Server) multiple Instances can access the same database and we can achieve performance using load balancing etc, each instance has its own SGA. The study of OracleRAC (multiple instances and one database) is beyond the scope of this book but you cannot jump on to that topic without mastering Oracle system with one instance and one database. If you have full mastery over the topics discussed in this book then learning and implementing OracleRAC will be piece a cake. Shared Memory is shared among all connected users with the Oracle Instance i.e. if one user has submitted the SQL statement from Canada and another user in Japan has submitted the same SQL statement, if its parsed version is available in SGA, Oracle uses that instead of reparsing.

5) Three different states of buffers within Database Buffer Cache.

o Dirty Buffers o Free Buffers o Pinned Buffers

6) Non-Shared part of

Oracle memory architecture is also called PGA or Program Global Area.

7) The utilization of PGA

depends how you configure the Oracle Server.

8) Oracle Server can be

configured to run as Dedicated Server mode or Shared Server mode.

9) Unlike SGA where RAM

is shared by all the background process, PGA is utilized by server process for sorting, session information and as stack space.

Page 138: Oracle db ebook

138

SGA has three mandatory areas and two optional areas as shown in the slide. Optional area means you can have it if the requirement arises. In mandatory areas we have, Shared Pool, Database Buffer Cache and Redo Log Buffer Cache whereas in optional areas we have Large Pool and Java Pool. Shared Pool is utilizes for SQL requests coming from the connected users, PL/SQL objects execution, data dictionary views, locks and so on. But the main purpose is SQL, PL/SQL, locking & data dictionary that’s how you can remember. There is a special area inside Shared Pool that deals with SQL and PL/SQL, is called the Library Cache. If one user has executed the SQL statement that statement will get parsed and complied utilizing this area. If someone else has submitted the same SQL, it will be used from this area pre parsed and complied. So the time it takes to execute the second SQL statement will be much less compared to the first one. The “Data Dictionary Cache” holds the most recently used database dictionary information. Data Dictionary is basically the information about the data inside the database or in other words metadata in the form of tables and view about the database. Oracle uses it at the time of parsing of SQL statement. The size of shared pool can be adjusted dynamically using SHARED_POOL_SIZE of init.ora file. Database Buffer Cache holds the DB Blocks recently read from the data files.

Page 139: Oracle db ebook

139

Database Buffer Cache has further three sub-caches. One is called KEEP, second one is RECYCLE and the third one is DEFAULT. In KEEP, DB blocks will be retained in the RAM and will not get aged out. Oracle Instance will remove stuff palced in RECYCLE from the RAM as soon as they are not needed. DB Blocks without any assigned attribute (KEEP or RECYCLE) are placed in the DEFAULT buffer pool. Two init.ora parameters I really like to mention over here that are related to KEEP and RECYCLE are DB_RECYCLE_CACHE_SIZE and DB_KEEP_CACHE_SIZE. We will see the init.ora file and specially its application in chapters ahead. Within whole Database Buffer Cache, there lie three different types of buffers or in other words three different states of buffers. These three states are, Dirty Buffers, Free Buffers and Pinned Buffers. Buffers that needs to be written to the data files are marked dirty and hence got the name Dirty Buffers. Once written to the data files they are available to be over-written or those, which never had any data, are called Free Buffers. Buffers that are currently being accessed or those that are explicitly marked for future use, are called Pinned Buffers e.g. KEEP buffer pool. Redo Log Buffer Cache holds the information about the changes that are being made to the database. Any change that you or connected users make using any DML (INSERT, UPDATE, DELETE) or DDL (CREATE, ALTER,

Page 140: Oracle db ebook

140

DROP) statement will get recorded in this part of the RAM or in other words SGA. The parameter that determines the size of Redo Log Buffer is LOB_BUFFER. Large Pool is the optional area of SGA and can be requested as needed. Its size can be adjusted using LARGE_POOL_SIZE parameter inside init.ora file and can be dynamically altered using ALTER SYSTEM statement. Large Pool is optionally required for specific database operation like backup or restore to avoid contention with rest of the good stuff of SGA. Java Pool is another optional area and can be requested to provide memory for java objects within Oracle database. Ever since Sun Microsystems’s collaboration with Oracle, we can now create and store pre-complied Java programs as database objects within the Oracle database and are treated the way other objects are treated inside the database e.g. Java object created by you or in other words under you schema will not be accessible to anyone else unless you grant access. The Non-Shared part of memory is called PGA (Program Global Area) and contains the data and control information for a server process. The information in PGA depends on the server configuration. Oracle can be configured to run as dedicated server process or shared server process. In dedicated server process we have one server process per session

Page 141: Oracle db ebook

141

whereas we in a shared server process we have multiple sessions served by one server process. We will learn in detail regarding it later in this book. Stack space in the PGA is utilized by server process to hold variables, arrays and other useful information pertaining to connected session. Unlike SGA, which is shared by several background processes, PGA is an area for the server process utilization and utilized for sort operations, containing information pertaining to session and stack space for variables and arrays.

Page 142: Oracle db ebook

142

Oracle Server can be configured to run under Shared Server mode or as Dedicated Server mode. The slide shows Oracle Instance working under Shared Server configuration. The beauty of shared server configuration is that a single server process handles multiple sessions and hence the idle time of server process reduces. We will talk in detail regarding this slide in the next chapter as it literally shows Oracle in action under shared server mode. In this slide you can see pictorially the sentence we used to define oracle instance i.e. The background processes and memory architecture constitute the instance. LGWR (Log Writer Process), PMON (Process Monitor), SMON (System Monitor), ARCn (Archiver

Key Concepts

1) RECO, LGWR, SMON, PMON, ARCn etc. are all background processes.

2) SGA is the shared part

of memory architecture of Oracle.

3) Oracle can be

configured to run as dedicated server mode or shared server mode.

Page 143: Oracle db ebook

143

Process) etc. are all Oracle background processes and SGA in the center is the memory architecture of Oracle. So all these background process utilize the memory architecture (shared area) to fulfill requests of all the connected users or in other words Oracle Instance fulfill the requests of all the connected users.

Page 144: Oracle db ebook

144

This slide shows the same thing as the previous one but here you might have noticed that we have less number of blocks, especially there is no Dispatcher process and no Request and Response Queue. This is because; here we are showing Oracle working in Dedicated Server mode. We will talk in detail regarding both of these modes (Dedicated and Shared) and how Oracle system works, in the next chapter. If you have a clear understanding of concepts so far, in the next chapters many mysteries about Oracle that you had in your mind will unfold, as you will see Oracle in Action. In dedicated server mode, each connected user we would be having a separate server process. Although this configuration would be having high idle

Key Concepts

1) There would not be any Request and Response Queues in a dedicated server configuration.

2) There would not be any

dispatcher process in dedicated server mode.

3) For each connected user

the there would one dedicated server process.

4) The idle time of server

will be more but this configuration has the highest performance factor.

Page 145: Oracle db ebook

145

server time as most of the time Server process would be doing nothing after fulfilling the single connected user request and waiting for next SQL statement to arrive. This configuration has high performance factor but it should only be utilized when you have plenty of system resources available.

Page 146: Oracle db ebook

146

We Did It!

In this chapter, we learnt about the concepts relating to Oracle Instance. The Oracle Instance is comprised of two things: Background processes and Memory architecture. We have used the analogy of human brain as it is a very good example to further clarify the concepts relating to Oracle Instance. Background processes utilize the Memory architecture to do processing of the user submitted request. Memory architecture (SGA & PGA) is basically portion of your computer system RAM utilized by Oracle Instance (or more precisely Background Processes).

Page 147: Oracle db ebook

147

CChhaapptteerr 55

Oracle In Action • Working of Oracle under Shared Server

Mode • Working of Oracle under Dedicated Server

Mode

Page 148: Oracle db ebook

148

Page 149: Oracle db ebook

149

You can run Oracle system in two modes: Shared Server Mode or Dedicated Server Mode. The slide shown above depicts how Oracle system (Instance as well as database) works in the shared server mode. The Shared Server mode can be best visualized if you imagine a restaurant as shown in the following picture. There are two waitresses serving more people. So two waitresses are being shared by lets suppose 10 people.

Key Concepts

1) Oracle two modes of operations.

o Shared Server o Dedicated

Server

2) Utilizing Shared Server mode you can have least Idle Time but performance would be as that of Dedicated Server mode.

3) If system resources are

not a problem keeping in mind the connected users, then always go with dedicated server mode.

Page 150: Oracle db ebook

150

Figure Restaurant Running Under Shared

Waitress Mode In this case the “idle time” for each waitress will be less compared to the situation where we will be having one waitress per person. Similarly in a shared server mode we would be having less Server Processes serving greater number of connected users. One more thing that I like to mention here, in a restaurant environment, if its really busy, then you may have to wait unless someone leaves and you get the place to sit. So there will be a “waiting list” maintained by someone, as soon as you enter the restaurant that person will note down your name in the list and whenever there is a vacant place to sit and provided its your turn on the list, you will be directed to that vacant place. Similarly in Shared Server mode there is a dispatcher process, which receives the users requests and places it in the “Request Queue”. Server process “when it’s free”’ picks up the request from the request queue, processes it and put the response back in response queue. Dispatcher process “when it’s free” picks up the response from the response queue and sends it to the specific user who

4) Software application (e.g. MS Word) has GUI whereas a Software Process is independent of GUI and requires no user interaction (e.g. LGWR)

5) Depends on the way you

program a Process, usually a process runs when certain event happens and re-runs if the event happens again.

6) DBWR writes the dirty

buffers from Database Buffer Cache to the data files.

7) LGWR writes the redo

log buffers from Redo Log Cache to the redo files.

8) Log Switch happens

whenever LGWR process finishes writing one Redo Log file and moves to the next one.

9) LGWR process will start

new ARCn process whenever ARCn gets overloaded.

10) PMON frees up the

resources from a failed user process.

11) SMON is responsible for

Instance recovery, if necessary.

Page 151: Oracle db ebook

151

submitted that request. The point to be noted over here is that request and response queues are not processes. They are part of SGA or in other words part of memory utilized by processes. Whereas Server process and Dispatcher process are processes and are not the part of SGA. Whenever the word SGA comes, imagine as if we are talking about a big sheet of paper for rough work or rough calculations and whenever the word “process” comes, imagine as if we are talking about a “live” person with a assigned job or in other words person who has being assigned a specific job. The difference between “process” (e.g. Oracle Background Process like Dispatcher, Server Process, DBWR etc.) and “software application” (e.g. like MS Word, Excel etc.) is that you can or users can interact with the “software application” whereas a process is also a software but it has a pre-assigned task set by the “process developer” or computer programmer. Software process never allows users to interact and more precisely certain environmental events result in the cause of its execution. It re-executes if that event happens gain or terminates itself after executing once just like in the movie “Mission Impossible”. The mission is described to the team through a “system” which after completion of mission description destroys itself. It all depends on the programmer who designed or wrote the software code for the process. Similarly, Oracle background processes have been assigned a specific job. Each process gets

Page 152: Oracle db ebook

152

executed under certain circumstances. If those circumstances happen again, the process gets executed again. DBWn DBWn or Database Writer process writes the contents of dirty buffers of Database Buffer Cache to the data files. When you start the database, Oracle starts one Database Writer process (DBW0). For situations where we have multiple users accessing the Oracle system, we can add nine more (DBW1 to DBW9) Database Writer processes. DBWn writes the contents of Dirty buffers of the Database Buffer Cache to the Data files under following circumstances or events.

o When the checkpoint occurs. Checkpoint occurs periodically or when “log switch” occurs. A log switch occurs when Oracle finishes writing one redo log file and moves to the next one.

o When we place tablespace offline. o When we drop or truncate table. o When we change the tablespace to

read only. o Whenever the threshold mark of

free buffers reaches. It’s the mark when the quantity of free buffers in database buffer cache drops to a certain level.

Page 153: Oracle db ebook

153

LGWR Like I said before, redo log files gets written by Log Writer process (LGWR) in a circular fashion i.e. when a Log Writer process finishes writing the last redo log file, it will start writing or rather overwriting the first one. Whenever the Log Writer process switches from one file to another, “Log Switch” happens. When Log Switch happens, checkpoint happens, when checkpoint happens, DBWn writes the dirty buffers to the data files. The Log Writer process writes the “Redo Log Buffers” from the Redo Log Cache to the Redo Log files under following circumstances or events.

o Whenever we issue COMMIT statement to complete the transaction.

o Every three seconds LGWR writes to all the Redo Log members or mirror images simultaneously even if one of the member is damaged, LGWR will continue to write on the good ones. ARCn If we don’t want the Redo Log files to be overwritten by LGWR process then we have to run the database in ARCHIVELOG mode and we have to enable automatic archiving by setting LOG_ARCHIVE_START = True. Under this mode Archiver process (ARCn) become alive. ARCn process makes sure that the Redo Log files gets

Page 154: Oracle db ebook

154

archived to the archived location before it gets overwritten by LGWR process. In Oracle 9i, we can have as many as ten Archiver processes (ARC0 to ARC9). The LGWR process will start new ARCn process whenever current number of ARCn processes is insufficient to handle the workload. CKPT (Checkpoint) CKPT process updates the header of data files and control file. It’s a event that happens automatically and kicks the DBWR process to write the dirty buffers to data files. It also happens when the Log Switch happens. SMON, PMON SMON or System Monitor performs instance recovery, if necessary on system startup whereas PMON or Process Monitor frees up all the resources held by a user process whenever it fails. RECO RECO or Recoverer Process is a background process and used with distributed database configuration. It automatically resolves failures involved in distributed transactions. LCKn LCKn or Lock processes (LCK0 to LCK9) are processes and available in Oracle Real Application Cluster environment for inter-instance locking. Oracle RAC ( or we use to it Oracle

Page 155: Oracle db ebook

155

Parallel Server) gives you the ability to have multiple instances interacting with the same database. QMNn QMNn or Queue Monitor processes (QMN0 to QMN9) is an optional background process for Oracle Advanced Queuing and monitor the message queues. The init,ora parameter AQ_TM_PROCESSES specifies the number of queue monitor processes. Oracle Advanced Queuing gives the ability to distributed applications to communicate asynchronously using messages. Dnnn or Dispatcher Process(es) Dnnn or Dispatcher process is only available if you run Oracle system in Shared Server mode. It prime job is to take the request from the connected user and place in the in the Request Queue. Server process when its free picks up the request form the request queue and put the response back in the “Response Queue” after processing the request. Dispatcher process when it’s free picks that response from the response queue and sends it back to the user who submitted that request. You can have multiple Dnnn processes for single Database Instance. Snnn or Shared Server Process(es) Snnn process works exactly the same manner like the Dedicated Server process except that the Shared Server processes

Page 156: Oracle db ebook

156

utilizing the beauty of Dispatcher process and Request and Response Queue can server more than one user. The number of Shared Server processes can be controlled using two init.ora parameters, SHARED_SERVERS and MAX_SHARED_SERVERS.

Page 157: Oracle db ebook

157

After studying Shared Server Mode of Oracle System, Dedicated Sever Mode is just piece a cake. In Dedicated Server mode there is neither Request/Response Queues nor any Dispatcher process. Whenever a user connects to Oracle Instance, one new Server process gets created to serve him/her. To better visualize a Dedicated Server mode, think of a very luxurious restaurant where they have one waiter dedicated for each new customer entering the restaurant and will stay with them all the time (waiting for their request) unless they leave the restaurant or the husband say to him, “Can you let me have some private discussion with my wife”.

Key Concepts

1) Oracle running under Dedicated Server mode works the same as Shared Server mode except that a new server process gets created to serve the new logged in user.

2) Idle Time of Server

process(es) will be more compared to Shared Server environment.

3) For unlimited resources,

always go with Dedicated Server Mode otherwise stick with Shared Server Mode.

Page 158: Oracle db ebook

158

Figure 1 Restaurant Running Under Dedicated

Waiter Mode This is called the restaurant running under Dedicated Waiter (Server) environment. If you are having unlimited resources available then to achieve ultimate performance always go with Dedicated Waiter (Server) mode otherwise stick with the Shared Server Mode. In real world the word “Resources” means money whereas in computer world this term stands usually for “RAM/Memory”. Rest of the processes works the same way like in case of Shared Server mode. In Dedicated server mode we have more server “idle time” or in other words server process doing nothing waiting for user request. Let me give you an idea how to find out which will work best for you, Shared Server or Dedicated Server. Each Dedicated Server takes approximately of 20MB of memory. If your system running Oracle have 1 GB of RAM available and your Oracle system needs to support 10 users and on the average of 7 concurrent users. Under this scenario always go with

Page 159: Oracle db ebook

159

Dedicated Server Mode otherwise stick with Shared Server Mode.

Page 160: Oracle db ebook

160

We Did It!

In this chapter we basically learnt and saw how Oracle works. Oracle can be configured to run in two different modes: Dedicated Server mode or Shared Server mode. In Dedicated Server mode, for each connected user there will be dedicated Server process. Although the Server process “idle time” is high in this mode but to attain maximum performance this mode is always suggested. This mode requires high capacity of RAM as each Server process consumes approximately of 20MB of RAM. Shared Server mode is suggested when Oracle has to support a large community of users or more precisely concurrently connected users where we cannot afford to run Oracle in Dedicated Server mode. In Shared Server mode, there will be more than one users served by a single Server process. The Server process “idle time” will be less in this scenario.

Page 161: Oracle db ebook

161

CChhaapptteerr 66

Complete Oracle System (Instance & Database) Design: Steps Of Implementation

Page 162: Oracle db ebook

162

Page 163: Oracle db ebook

163

Oracle is a very large subject and there is so much information that one can easily get lost and goes off track. That’s what happened with me when I first started my journey reading books on Oracle. Over here we have described the complete line step by step on how to erect the Oracle System from scratch. In this chapter we will explain up till step 07. Next steps are covered in the chapters ahead.

Key Concepts

1) Remember these points as it’s the steps of erecting complete Oracle System from scratch.

Page 164: Oracle db ebook

164

The sole purpose of step 1 to step 3 is basically to figure out three parameters that we need before we start the process of creation of Oracle Database: initial size of database, frequency with which data will grow and maximum number of connected users to Oracle Database. We cannot find out the exact value of these three parameters but we can have estimated values after doing company’s analysis or in other words once we develop tables structure required for each department in that company on paper. The very first step is to analyze the company’s organizational chart. Organization chart will give you an idea regarding how many departments are there and who to contact within each department. Remember once you get

Key Concepts

1) The parameters estimated values are required before we start the process of actually creating Oracle Database in computer.

2) Developing Primary and

Foreign Key relationship between the tables helps in reducing redundancy of information.

3) It is recommended to

have at least one tablespace designed for each department.

Page 165: Oracle db ebook

165

hired you would be in the IS&T (Information System & Technology) department or in some companies it’s simply called as IS (Information System) department. Once you have analyzed the organizational chart of the company then start with one department. Go and meet with the department’s head and explain the purpose and mission of IS&T department that IS&T is developing a database for the company and we need to do analysis of your department’s processes or in other words how things work in your department. How you manage data (most probably on paper)? Once done you can start creating tables’ structure and assigning them data types and once you have all the tables required by that department then you can start implementing primary and foreign key relationships between those tables on paper (I mean not actually implementing using CREATE TABLE statement). Developing Primary/Foreign Key relationship helps in reducing redundancy of information or Data. Once we reach a level where we can say that no further relationship is required and redundancy of information has came to its minimum then we can estimate the size of each table. Let’s see how? I want to take your mind to the CREATE TABLE statement, when you described data types for each column. Lets spose we have a table named “address_book” where we are maintaining just the name and addresses of people. It has a “name” column and “address” column as shown below.

4) The sum of “initial” estimated sizes of all the tables within that department will give us the initial estimated size for a tablespace.

5) The sum of all the initial

estimated sizes of all the tablespaces will give us the initial estimated size of database.

6) The sum of frequencies

of growth of all the tablespace will give us the frequency of growth of database.

7) If we know how to find

out the estimated size of a table and its frequency of growth then we can also find out the estimated size of tablespace and frequency of its growth.

8) If we know how to find

out the “initial” estimated size of a tablespace and its frequency of growth then we can also find out the estimated size of database and frequency of its growth.

9) Maximum number of

concurrent connected users parameter helps whether we go with Shared Server mode or Dedicated Server mode.

Page 166: Oracle db ebook

166

Table: addres_book name VARCHAR2(20) address VARCHAR2(200) Keeping in mind that it takes one byte to store one character, usually. We can easily estimate the size of one record in this address_book table in terms of bytes. So the formula of finding out the estimated size of table is given below. Estimated Size Of Table (Bytes) = Size Of A Record (Bytes) X Total Number Of Records So in our scenario the size of record would be (200 + 20) 220 bytes and if initially we have to place 100 people information in it then the “initial” estimated size of table would be (220 X 100) 22,000 bytes or 22 KB. Following on the same lines we can find out the “initial” estimated size of all the tables for that particular department. It’s recommended to have at least one tablespace created for each department. So the sum of the initial estimated sizes of all the tables within that department will give us the initial estimated size of a tablespace. Moreover, we can also estimate the frequency of data growth in each table e.g. while in analysis phase we can ask a worker in that department how many people you add or sign-up for service per month and if he/she say 10 then keeping in mind address_book table. The formula to find out estimated frequency of growth of table would be as follows.

Page 167: Oracle db ebook

167

Table’s Estimated Frequency Of Growth (Bytes) Per Month = (Number Of New Records Insertion per Month) X (Size Of Record) In our scenario it will come out to be (10 X 220) 2200 bytes per month or in other words 2.2 KB (Kilo Bytes). The sum of all the estimated frequencies of growth of all the tables within a department will give us the estimated frequency of growth for a tablespace that we will create for that department. Following on the same lines the sum of all the frequencies of growth of all the tablespaces with the database will give us the frequency of growth of database. Same applies to figuring out the initial size of the database i.e. sum of initial estimated sizes (bytes) of all the tablespaces gives us the size of the database. Whereas the size of the tablespace is the sum of initial estimated sizes of all the tables within that tablespace or in other words department. We can very easily roughly estimate the maximum number of connected user at any given instance if we know the number of employees that the company has and the number of clients and/or customers, accessing the Oracle Database.

Page 168: Oracle db ebook

168

The purpose of finding out the rough estimation of maximum number of concurrent users accessing the Oracle Database is to come up with the decision whether “Shared Server” mode is more preferable for us or “Dedicated Server” mode.

Page 169: Oracle db ebook

169

OFA stands for Optimal Flexible Architecture and it’s not at all that scary as the same sounds. It’s just the stand directory architecture that Oracle recommends us to follow for the placement of Oracle physical files e.g. once you create Oracle database using DBCA (Database Configuration Assistant), it automatically follows OFA and will place your database related files in the pre-defined stand file locations. According to OFA, “bdump” folder contains the background processes traces or dump files, “udump” is for users session related trace information, “pfile” folder contains the init.ora file so on and so forth. Similarly data files will be under the folder oradata\SID. In our case as shown in the slide SID is test.

Key Concepts

1) Oracle Flexible Architecture (OFA) defines the standard directory architecture for Oracle physical files.

2) Oracle Managed Files

defines the naming conventions for Oracle physical files.

3) If use DBCA to create

the Oracle database then you don’t have to worry about OFA and OMF.

4) DBCA follows OFA and

OMF automatically.

Page 170: Oracle db ebook

170

OMF stands for Oracle Managed Files and it defines the standard for names that we give to the Oracle physical files or in other words supplies us with the naming convention for Oracle physical files. According to OMF, all data files should be having .DBF extension, control file should be having .CTL and redo log files should be having .LOG extension. Again you don’t have to worry about OMF or OFA if you using DBCA as it follows these stands automatically.

Page 171: Oracle db ebook

171

Oracle Server is available in three different editions and each differs based on the features & functionalities e.g. Oracle Enterprise Edition has all the functionalities and features plus there is no four processors limit that we have in case of Standard Edition whereas Oracle Personal Edition is for single user development and is excellent for web based applications. Enterprise Edition is having the highest price then comes Standard and Personal Edition (Standard1) is least expensive product. After making a decision regarding which edition you need, the next thing is to find out how you want to have the licensing of the software. You can buy the licenses based on the number of users using the Oracle Server or based on the number of processors you

Key Concepts

1) Oracle Server software has following three editions.

o Enterprise o Standard o Standard1

(Personal)

Page 172: Oracle db ebook

172

will be having in the server machine. The computer systems (or machines) that we normally buy from the stores are more specifically called Desktop Machines. Desktop machine has one processor, one power-supply, one network card etc. and the fault tolerance is not that much compared to a server machine where we are having multiple processors, two or more power-supplies, two or more network cards, multiple hard disks (SCSI) etc., everything in a single box. The reason of having two or more power supply is that if one fails the second one jump in automatically. If one network card burns out, the other one jumps in automatically. Multiple processors in a single server machine increases the machines power to serve more users or in other word all the processors within one server machine work together based on the technique call Parallel Operation. I bought a used PIII Xeon Quad Server machine from ebay at a cost of $380. This machine is having 4 PIII Xeon processors, three power supplies, three hard disk (SCSI) drives which I have configured for mirroring. If one hard disk fails, the server will still be up and running. Currently, Oracle9i RAC costs $10,000 per processor above and beyond the per-processor cost of the database itself. The Oracle9i Enterprise Edition database costs $40,000 per processor, while the Standard Edition is $15,000 per processor. (Source: www.crn.com) RAC in Oracle9i RAC stands for Real Application Cluster and its old name was

Page 173: Oracle db ebook

173

Oracle Parallel Server. Oracle RAC is basically your next step after having command over Oracle System we are learning in this book. In normal Oracle system there is one Oracle Instance and one Oracle Database but in Oracle Real Application Cluster we have multiple Oracle Instances running and interacting with a single Oracle database to achieve high performance and to server larger community of users. Learning and understanding of Oracle RAC will be piece a cake if you have full understanding of one Instance Oracle systems. For up to date information about pricing of different editions of Oracle software, visit www.oracle.com

Page 174: Oracle db ebook

174

Good news for those who are learning Oracle software is that you can download complete Oracle software for free from oracle website (www.oracle.com) for learning purpose. You will require a broadband (Cable/DSL) internet connection as the download will be large files and will take a week if you try to do it using a dial-up internet connection. Once you download the Oracle software then you would need to unzip those download files most probably before running the setup.exe file which will start the Oracle Universal Installer (OUI). Follow the instructions on the web page where the hyperlinks to downloadable Oracle software files are mentioned. But most probably those files will be in zip or

Key Concepts

1) Oracle software is free for learning purpose and can be downloaded form Oracle website.

2) Oracle Universal

Installer installs the Oracle system on the machine.

3) OUI is java based and

hence the process of installation is totally independent of underlying OS.

Page 175: Oracle db ebook

175

jar format and can be unzipped using WinZip software. The evaluation version of WinZip software can be downloaded for free from www.winzip.com website. Once you run the setup.exe file OUI will start and then you will see the following window with default values.

There will be three text boxes in this window. All three text boxes will be having default values in it. The first one will ask you the location of Oracle software files that needs to be installed. The second is the Name text box where you write the Oracle Home name and the third one is the destination location where you want Oracle software to be installed. Click the Next button once done. In the next window select the first option of Oracle 9i Database. Second option is for installing a client and would lack the ability to create the database. The third optional is basically an add on feature which will install Oracle Management Server and other related management and integration too.

Page 176: Oracle db ebook

176

Once you have selected Oracle9i Database option then click ‘Next’ button.

In the next window OUI will ask you regarding which edition of Oracle you would like to have. Select Enterprise Editon and then click the ‘Next’ button.

The next window that pops up select the option of ‘Software Only’. By default you will see ‘General Purpose’ selected. OUI has the ability not only to install the Oracle software but to create the database too during the installation process. In other words you can see it has the ability

Page 177: Oracle db ebook

177

of DBCA (Database Configuration Assistant) that we will learn in the next chapter.

Once you select the ‘Software Only’ option and click ‘Next’ button the installation process will start and upon successful completion it will say, “Oracle Software has been installed successfully”. If you encounter any problem during the installation phase try to understand the error message. Most of the time these error messages are very explanatory otherwise re-run the OUI and select the option “Remove” installed products which will uninstalled all the previously installed products. Once done then do the fresh installation. Once you have installed the software successfully then next step would be to create the database which we would be studding in the next chapter.

Page 178: Oracle db ebook

178

We Did It!

In this chapter we laid out the steps of implementation for erecting a complete Oracle environment from nothing to 100%. From nothing to 100% means that you go to a company having no database at all to a complete Oracle Server environment where employees from all parts of the world can interact with the Oracle Server. Eleven steps are presented to achieve this goal. Up till seven steps are discussed in this chapter. Rest are discussed in chapters ahead.

Page 179: Oracle db ebook

179

CChhaapptteerr 77

Oracle Database Design (Instance & Database)

• Designing Using DBCA • Designing Using SQL statements

Page 180: Oracle db ebook

180

Page 181: Oracle db ebook

181

In the previous chapter we learnt about from step 1 to step 7. This chapter talks about step 8 only i.e. creating Oracle Database. When we are talking about creation of Oracle Database, it’s understood that we are referring not only to Oracle Database but Oracle Instance too. There are two options for us for this purpose. We can create the Oracle database using a software wizard called DBCA (Database Configuration Assistant). Wizard is a special kind of software that takes you through step by step procedure to the completion of certain task.

Key Concepts

1) Two ways to achieve same goals i.e. development of Oracle Database.

2) Those two ways are

DBCA & Manual.

Page 182: Oracle db ebook

182

All you have to do is to keep clicking ‘Next’ button or change the default values then click ‘Next’ button. Therefore we have no doubt in saying that DBCA is a software wizard. The other option is Manual i.e. you have to create the database using SQL DDL statement and everything you have to do by yourself. Before the development of DBCA, DBA used to develop databases using this Manual technique. Now with DBCA life turned out to be very simple. In this chapter we will discuss both of these methods and then leave the rest on you to decide which one to choose. Let’s start our discussion with the first method i.e. developing database using DBCA.

Page 183: Oracle db ebook

183

Before even we start DBCA we have to set some of the OS environment variables. Those variables are mentioned below along with the brief description. ORACLE_BASE describes the top most directory where Oracle software will be installed. In Windows environment we can set it using the SET command. Lets suppose the DOS prompt is C:\>. C:\> SET ORACLE_BASE C:\Oracle Whereas in Unix we write the command as shown below. % setenv ORACLE_BASE /Oracle ORACLE_HOME specifies the directory that lies beneath ORACLE_BASE and its here where Oracle products reside.

Key Concepts

1) Before starting DBCA we need to set certain OS environment variables.

2) Those environment

variables are ORACLE_BASE, ORACLE_HOME, ORALCE_SID and, PATH.

Page 184: Oracle db ebook

184

According to OFA (Optimal Flexible Architecture), this environment variable should to set to ORACLE_BASE/product/release#. Lets suppose the DOS prompt is C:\>. C:\> SET ORACLE_HOME C:\Oracle\database\9_2 Whereas in Unix we write the command as shown below. % setenv ORACLE_HOME /Oracle/product/9_2 ORACLE_SID is the instance name or rather default instance name on the machine having Oracle installed. This should be the unique identifier of the Oracle Instance on the machine. A single machine can have more than one instance, so each should be having separate SID. Lets suppose the DOS prompt is C:\>. C:\> SET ORACLE_SID test Whereas in Unix we write the command as shown below. % setenv ORACLE_SID test PATH is the environment variable and its usually set to ORACLE_HOME/bin folder. The bin folder contains all the tools that shipped with Oracle. With this done, you can run programs in the bin directory from any DOS prompt. No need to go, each and every time to the folder containing that specific executable file. Once this being set, you can run any Oracle supplied took from any prompt of DOS or Unix.

Page 185: Oracle db ebook

185

After setting the above mentioned parameters the next step would be to run DBCA.

Page 186: Oracle db ebook

186

DBCA can be invoked from the Windows operating system start menu as shown below

Figure Windows Server 2003, location of DBCA

Or by entering the following Unix command. dbca DBCA is a standalone application and its started automatically by OUI (Oracle Universal Installer) as a last step in the installation process if you make the

Key Concepts

1) DBCA can be run through the start menu in Windows or by entering the dbca command in Unix OS.

Page 187: Oracle db ebook

187

selection of General Purpose database. Once DBCA gets started you will see this window which describes the power of DBCA in brief. Click ‘Next’. DBCA works exactly the same in Unix and Windows Operating Systems.

Page 188: Oracle db ebook

188

Using DBCA you can create a new database, configure already existing database, delete an already existing database and manage templates. In this window you will see the second and third option as gray since there was no previous database on that system. Once you have passed through all the steps of DBCA and you are at the final window, you would be provided the option either to let DBCA create the database or save the configurations you made in the form of template as well. Saving in the form of template can help in creating a clone database. You can also make changes to the saved template using DBCA.

Key Concepts

1) DBCA helps in doing four things we can

o Create a New Database

o Configure Database

o Delete Database

o Manage Template

Page 189: Oracle db ebook

189

Template basically contains the structure of the database & instance. You can save it on the disk (floppy/USB Flash Drives etc.) and take it to some other PC having Oracle software on it. Run DBCA and create the exact same database you that template. Make sure that ‘Create a Database’ option is selected and then click ‘Next’ button.

Page 190: Oracle db ebook

190

DBCA comes with four pre-built templates. These templates names are provided below.

Data Warehouse General Purpose New Database Transaction Processing

Each template has default settings for database configuration parameters but you have been given full access to change any according to your needs. Make sure that ‘General Purpose’ option is selected and then click ‘Next’ button.

Key Concepts

1) DBCA comes with four pre-built templates.

Page 191: Oracle db ebook

191

Two parameters you have to input in this window. The Global Database Name and the SID. Oracle Database is uniquely identified by a global database name and SID is the name of the Oracle Instance. In our case we have entered test.takveen as the Global Database Name and test will come up automatically as SID. Once done then click the ‘Next’ button.

Key Concepts

1) Global Database name is usually described in name.domain format and is the unique identification of Oracle database.

2) SID is the name of the

instance on the machine. You can never have two or more instance having the same name on the same machine.

Page 192: Oracle db ebook

192

You have been provided with two options whether you want to run the Oracle Server in Dedicated Server Mode or Shared Server Mode. In Dedicated Server Mode as explained before there would be one server process per connected user whereas in Shared Server Mode multiple users would be served by a single server process. Select the Dedicated Server Mode if you want high performance and if you have plenty of system resources available otherwise go with Shared Server Mode. In Dedicated Server Mode the idle time for each server process will be more compared to the idle time of each server in the Shared Server Mode. These two

Key Concepts

1) We can either run Oracle in Dedicated Server Mode or Shared Server Mode.

Page 193: Oracle db ebook

193

topics are explained in greater detail with real world analogies in the previous chapter. Make sure that ‘General Purpose’ option is selected and then click ‘Next’ button.

Page 194: Oracle db ebook

194

Here you can either use the default values provided for memory architecture of Oracle or change them according to your needs. Once done click the ‘Archive’ tab to make sure that the stuff in there is according to your requirement.

Key Concepts

1) Memory architecture of Oracle can be configured through this window of DBCA.

Page 195: Oracle db ebook

195

You can run Oracle Server in ARCHIVELOG Mode or NOARCHIVELOG Mode. In the archive log mode, the Redo Log files will get archived before they get over written by LGWR (log writer process). The Redo Log files get utilized in a circular fashion. By default its no archiving mode. If you select the archiving mode then all you have to specify is the destination or the path and location of folder where you want to place the archived Redo Log files. Once done click the ‘DB Sizing’ tab to make sure that the stuff in there is according to your requirement.

Key Concepts

1) Oracle Server can be run in ARCHIVELOG mode or NOARCHIVELOG mode.

2) In archive log mode

Redo Log files get archived to the destination you specify.

3) In no archive mode once

Redo Log files get filled up, Oracle will start overwriting them.

4) Redo Log Files size

remains the same.

Page 196: Oracle db ebook

196

Three parameters can be adjusted using this tab viz., Sort Area Size, Database Character Set and National Character Set. All the parameters would be having a default value but you would be having the full control to change it. Whenever Oracle performs the sorting operation, it needs the extra-space or memory (RAM) to do it. Higher this value faster the operation would be. This value should be adjusted keeping in mind actual RAM size in your system.

Key Concepts

1) Higher the Sort Area size, faster will be the sort operation.

2) Sort area specifies the

part of memory (RAM) you want to reserve for Oracle sorting operations.

Page 197: Oracle db ebook

197

For English language leave Database Character Set and National Character Set as default. Once done click the ‘File Locations’ tab to make sure that the stuff in there is according to your requirement.

Page 198: Oracle db ebook

198

DBCA follows OFA (Optimal Flexible Architecture) and OMF (Oracle Managed Files) stands. All the default locations and file names specified in this screen are according to these standards. So you don’t have to worry about it. To erect Oracle system we need a initialization parameter file or in other words ini.ora (a.k.a pfile) file. The parameter file contains all the settings for the instance, location of control file, background processes trace file etc. If you do not select ‘Create persistent initialization parameter file’ or in other words spfile, every time you make a change to any of the parameters defined in the pfile, you have to shutdown the system as normal and then start over again to make that change to be effective. On the other hand, if you select ‘Create

Key Concepts

1) spfile has made possible the ALTER SYSTEM statement to get utilized in changing any initialization parameter with no need to restart the Oracle Server.

Page 199: Oracle db ebook

199

persistent initialization parameter file’ DBCA will create spfile using the pfile and the benefit would be that you can change any of the initialization parameters using ALTER SYSTEM statement without any need to restart the Oracle server.

Page 200: Oracle db ebook

200

Click on the ‘File Location Variables’ button to see and to sure the values of these four variables viz., ORACLE_BASE, ORALCE_HOME, DB_NAME and SID. Once done click the OK button which will close this window and take you back to the previous window.

Key Concepts

1) Four very important initialization parameters viz., ORACLE_BASE, ORACLE_HOME, DB_NAME and ORACLE_SID

Page 201: Oracle db ebook

201

To see the complete list of initialization parameters click on the ‘All Initialization Parameters’ button. The popped up window will be having complete list of all the initialization parameters, those you have selected and those you haven’t select. It would be a completely editable window i.e. you can select and deselect any of the parameters and edit the values. Once done click ‘Close’ button and it will take you back to the previous window. Click the ‘Next’ button.

Key Concepts

1) We can select and deselect any of the initialization parameters and edit the values.

Page 202: Oracle db ebook

202

If you click the Control file item on the left pane, on the right pane you will see all the multiplexed control files names along with their location. Each database can have one control file but you can have mirror copies at different locations. Having mirror copies is also known as multiplexing of a file. Oracle writes to all these multiplexed control files together. Once done click the ‘Options’ tab.

Key Concepts

1) It’s better to have mirror copies of control file reside on different physical disks.

Page 203: Oracle db ebook

203

The options tab under contolfile item defines the maximum limit on data files, redo log groups and log members in each redo log group that this database could ever possiblely has. The default values are sufficient enough so it’s better to stick with default. Once done then click ‘Database’ item on the right pane.

Key Concepts

1) We can always add data files, redo log groups and redo log members after creation of the database using the ALTER DATABASE Statement.

2) Normally we cannot

surpass the maximum limit in adding these files.

Page 204: Oracle db ebook

204

Once you click the ‘Data files’ in the right pane, the left pane will show all the data files your database is comprised off along with their location in the system. Since you have select ‘General Purpose’ database template, Oracle create certain tablespaces for you by default. The data files shown above belong to those tablespaces. Remember that once you have created a complete database either through DBCA or Manual technique, you can always add new tablespaces or delete previously designed. Click the redo log group ‘1’ and information in the left pane will change.

Key Concepts

1) Each Tablespace can have one or more data files but each data file can never be associated with two or more Tablespace.

Page 205: Oracle db ebook

205

Once you click ‘1’ under Redo Log Groups item in the right pane, left pane will show member name and its location. You can add more members and specify their location where you want to place them. All members with in a redo log group are mirror copies of each other. Click ‘2’ to see information about redo log group 2 and similarly for 3. After viewing the information regarding redo log group 3, click ‘Next’.

Key Concepts

1) All members within each redo log group are mirror copies of each other.

Page 206: Oracle db ebook

206

You can create the Database based on the configuration settings you provided or you can just save the configuration settings as a template or you can do both. Once done click ‘Finish’ button and it take you next window.

Key Concepts

1) Once the template will be saved, it will add on to the four pre-built templates in DBCA.

2) Having a template helps

in the creation of clone database.

Page 207: Oracle db ebook

207

Over here it shows you the default passwords for two most powerful accounts in Oracle SYS and SYSTEM. The password for SYS is change_on_install and the password for SYSTEM is manager. These passwords are same no matter where you buy or download Oracle product. So everyone in this field knows these credentials. It is therefore highly recommended to change these passwords as soon as possible. Over here if you click ‘Exit’ then the passwords would be same as default if you like to change password for these default Oracle accounts click ‘Password Management’ button.

Key Concepts

1) SYS and SYSTEM accounts get created as default with default passwords.

2) It’s highly recommended

to change passwords of these accounts as soon as possible.

Page 208: Oracle db ebook

208

Over here you can not only change the passwords for SYS and SYSTEM but you can also Lock/Unlock accounts. After changing the password, click ‘Ok’ button which will close this window and you will be back on the previous screen. Click ‘Exit’ and the database creation process will start. Once you see the message, database has been created successfully. Open SQL*Plus and login using scott account which is mentioned below. Login: scott Password: tiger If everything as been done well then you should be able to login and execute SELECT statements mentioned in the

Key Concepts

1) SYS is the owner of Data Dictionary Views and SYSTEM is a DBA account.

2) Oracle recommends to

have at least two or more DBA account not just one.

3) DBCA does network

configuration for your database automatically so that not only users but DBAs can also access it for remote database management.

Page 209: Oracle db ebook

209

SQL chapter. Once you have database up and running you can always change its structure by first logging in as DBA account like SYSTEM and using ALTER DATABASE statement. We can also create new users, new tablespaces etc. DBCA once creates the database, does the network configuration for the database too automatically. We will learn about Oracle Networking in great detail in chapters ahead. After seeing the message that your database has been developed successfully. Open up OEM (Oracle Enterprise Manager) as shown in the figure below.

Figure Windows Server 2003, accessing OEM

We will learn about OEM (Oracle Enterprise Manager) in great detail in the chapter ahead. Once you click ‘Enterprise Manager Console’, following window will popup asking you whether you want to run OEM standalone or you want to login to the Oracle Management Server.

4) OEM is the tool used by DBAs for local/remote database administration.

Page 210: Oracle db ebook

210

Oracle Management Server is an add on product and we haven’t installed it so will be running OEM as ‘standalone’. Click Ok.

The look and features or in other words GUI (Graphical User Interface) of OEM is exactly the same whether you run it under Unix, Windows or Linux operating systems. The next popped up window will be the main control panel of OEM. From here you can control all the databases you are running all over the world as a DBA. Right now it’s showing you only one database i.e. TEST.

TEST is the database that we have developed using DBCA. If you click on TEST item in the tree, the following

Page 211: Oracle db ebook

211

window will appear asking you for credentials i.e. login and password.

Figure Connecting as SYSDBA to TEST database

There is no need to specify the login and password just select SYSDBA from ‘Connect as’ drop down option box and click OK button.

Figure 0Main Control Panel of OEM

Now you would be able to expand the tree under TEST database. If you click ‘Tablespaces’ under ‘Storage’ item, you would be able to see the list of all the tablespaces and corresponding data files that DBCA has developed for you. Let me give you a closer look to this window in the following picture.

Page 212: Oracle db ebook

212

Figure List of tablespaces within TEST database We will learn in depth about OEM and other good stuff under Database Management chapter later in this book. Next we look into the manual way of creating database and it will give us more insight to the process of development of database.

Page 213: Oracle db ebook

213

Although development of database using DBCA is piece a cake and you should be preferring it over the Manual technique in the real world system development as helps in reducing the factor of making mistakes which may come up while you are doing everything own your own. Manual technique actually gives you more insight to DBCA as what it does behind the scenes for you.

Key Concepts

1) It’s recommended to prefer DBCA over manual method.

2) Manual Technique gives

the insight as to what DBCA does behind the scenes.

Page 214: Oracle db ebook

214

Just like with DBCA technique, you have to first set up few environment variables the same way you did before invoking DBCA. But here we will be setting two additional parameters LD_LIBRARY_PATH which will point to Oracle library files associated with Oracle Instance and ORA_NLS33 its an optional environmental variable unless you want to specify other than US7ASCII.

Key Concepts

1) OS Environmental variables need to be set before using this technique.

2) Setting of OS

environmental variables for Unix and Windows is same as described in DBCA technique of developing database.

Page 215: Oracle db ebook

215

Step 02: Decide On Instance Identifier (SID) This will be name of your database instance. It will be unique on the machine running multiple Oracle instances. This is basically you set in the Step 01 when you set the environment variable ORACLE_SID. Step 03: Select Authentication Method For DBA Normal database users get authenticated by Oracle database only but DBA or in other words administrators have two methods through which they can get authenticated one is Operating System and the other one is password file. For local or remote database administration you can use either method but you need a secure network connection for remote administration using operating system authentication. In operating system method you don’t have to give password and login to make a connection as SYSDBA. All you have to do is make yourself member of OSDBA or SYSOPER operating system group. For example when you tried to connect using OEM to Oracle instance you didn’t specify any login and password, all you did was selected the option Connect as SYSDBA. Once someone sends a connection request to Oracle as SYSDBA without login and password. Oracle contacts the operating system under which Oracle is running to

3) For normal users there is one method of authentication but for DBAs two methods.

4) In OS authentication

method, Oracle gets your credential from OS once you try to connect as SYSDBA or SYSOPER.

5) Our recommendations

are with OS based authentication.

6) Six states of Instance is

made possible using following six clauses of STARTUP command.

o NOMOUNT o MOUNT o OPEN o RESTICT o RECOVER o FORCE

7) Four ways to shutdown

the database is made possible using four clause of SHUTDOWN command.

o NORMAL o IMMEDIATE o TRANSACTIONAL o ABORT

Page 216: Oracle db ebook

216

get the login and password of the user submitted the request. Remember to reach to Oracle running on a server machine, first you have to login to server, so server already know who is currently logged in. If you are system Administrator or you are member operating system SYSOPER or SYSDBA then Oracle will let you connect as DBA role which has the power to perform all kinds of DBA tasks. Our recommendation is with letting the OS to authenticate the database administrator and not to create a separate external password file. The utility used to create password file is ORAPWD. Step 04: Create Initialization Parameter File In ORACLE_HOME/dbs directory you can find generic initialization files templates e.g. one for data warehouse database, one for Online Transactional Processing (OLTP) database and one for combination database. Select the one that suits your needs and change its contents or add new parameters. Finally save it with the name shown below. init<SID>.ora Where <SID> would be name you decided on in the beginning and which is also your environment variable ORACLE_SID. Edit this file and change the following parameters according to your needs.

Page 217: Oracle db ebook

217

DB_NAME It will be your database name and it should not be eight characters in length e.g., DB_NAME=mydb DB_DOMAIN It will be text string which identifies the network domain where the database is hosted e.g., DB_DOMAIN=takveen.com CONTROL_FILES This parameter defines the control file names and location to be used to create the new database. If you fail to specify the filename, Oracle creates a file with a default operating system dependent name e.g., CONTROL_FILES = (d:\oracle\databases\ora10\control01.ora, d:\oracle\databases\ora10\control02.ora, d:\oracle\databases\ora10\control03.ora) UNDO_MANGEMENT You can control undo management to be manual (MANUAL) or automatic (AUTO). Its recommended to set this parameter to AUTO in Oracle9i and Oracle 10g e.g., UNDO_MANAGEMENT=auto UNDO_TABLESPACE A default tablespace with the name SYS_UNDOTBS will get created automatically when you execute the CREATE DATABASE statement if you have set UNDO_MANGEMENT=auto

Page 218: Oracle db ebook

218

and no undo tablespace in the CREATE DATABASE statement. Oracle used this tablespace to store its undo information. DB_BLOCK_SIZE DB Block is the smallest building block of a database. This should be set to even multiple of OS block size to get high performance. Take care when determining its value as you cannot change it after the creation of database e.g., DB_BLOCK_SIZE=8192 Where 8192 represents bytes. The size of Database buffer cache is determined if we multiply DB_BLOCK_BUFFERS with DB_BLOCK_SIZE. Where DB_BLOCK_BUFFERS are the number of buffers in the database buffer cache of SGA (System Global Area). Step 05: Connect To And Start The Instance Starting SQL*Plus is very simple. No matter whether you are in Unix or Windows environment, all you have to do is to write the following command on the command prompt. E:\> sqlplus /nolog Then you will be taken to the SQL prompt. At the SQL prompt write the following command. SQL> connect / as sysdba

Page 219: Oracle db ebook

219

Once Oracle receives this command, it will immediately communicate with the underlying OS to make sure that you are member of operating system OSDBA or OSOPER group or you are the system administrator. Now you get connected with the Instance. Next step is to start the instance without mounting a database since there is no database. The following command is used to do so. SQL> startup nomount If your parameter file is not at the default location i.e. ORACLE_HOME/dbs then you need to use the PFILE clause in the STARTUP command. But this is an exceptional case. With the execution of this command the Oracle Instance will get alive without the database or in other words Genie will become alive without Genie’s notebook. There are several options for starting up the Oracle system. These options are discussed below. STARTUP NOMOUNT This command will erect the Instance with no database mounted on it. No user would be able to connect or interact with the instance except the logged in SYSDBA. This state is usually used to create the new database. STARTUP MOUNT As a result of this command, the instance will mount the database after being alive.

Page 220: Oracle db ebook

220

No user would be able to interact or access the information from the database. This state is usually used to recover the database in case of failures. STARTUP OPEN OPEN is the default clause of STARTUP command i.e. if you used execute STARTUP without any clause Oracle will take it as STARTUP OPEN. In this, Oracle Instance will open the database after making himself alive and then mounting the database in this brain and finally opens it. In this state users would be able to interact with the instance and access the data. STARTUP RESTRICT Starts the instance, mounts and opens the database but only users with restricted session privileges would be able to access or interact with the instance. STARTUP RECOVER Starts the instance but leaves the database closed and begins the recovery for whatever failure happened. STARTUP FORCE Forces the instance to shutdown abort and immediately startup open. This option is usually used with instance having problems either starting or stopping. Its like OS reboot or restart command. Like the STARTUP command, there is a command to shutdown the instance or in other words oracle system (instance and database) i.e. SHUTDOWN. Using this command we can shutdown the database

Page 221: Oracle db ebook

221

in four ways. Those four ways are discussed below. SHUTDOWN NORMAL The NORMAL clause in the SHUTDOWN command is default i.e. if you just execute SHUTDOWN Oracle will take it as SHUTDOWN NORMAL. As a result of this command, no new user is allowed to make a connection with Oracle Instance. The Instance will wait for each user to disconnect and then closes the database, dismounts it onto itself and shuts down itself. SHUTDOWN IMMEDIATE As a result of this command, no new user connections are accepted. All the connected users will get disconnected. Oracle Instance will roll back all uncommitted transactions. Closes the database, dismounts the database and shuts down itself. SHUTDOWN TRANSACTIONAL The result of this command is the same as SHUTDOWN IMMEDIATE except that Oracle Instance waits for the connected users to complete the transactions before disconnecting them. SHUTDOWN ABORT As a result of this command, everything shuts down abruptly. Oracle Instance terminates all connected users regardless of SQL statements under process. Terminates itself immediately and will not rollback uncommitted transactions. Instance recovery is needed when you start the database next time.

Page 222: Oracle db ebook

222

Step 06: Issue The CREATE DATABASE Statement CREATE DATABASE statement is used to create the database. It has lots of clauses and it can be better understood if we first look at its syntax and then real world examples. The syntax for CREATE DATABASE statement is provided below. CREATE DATABASE [database name] [CONTROLFILE REUSE] [LOGFILE [GROUP integer] file specification] [MAXLOGFILES integer] [MAXLOGMEMBERS integer] [MAXLOGHISTORY integer] [MAXDATAFILES integer] [MAXINSTANCES integer] [ARCHIVELOG|NOARCHIVELOG] [CHARACTER SET charset] [NATIONAL CHARACTER SET charset] [DATAFILE filespec [autoextend]] [DEFAULT TEMPORARY TABLESPACE filespec] [UNDO TABLESPACE tablespace DATAFILE files] [SET TIME_ZONE [time_zone_region]]; Let’s look at the items in this statement one by one. Database name you specified in the initialization parameter file using DB_NAME parameter. If you omit the name, the one you specified in the parameter file will be used. Control file reuse means that the control file specified in the initialization parameter file should be reused. Log file group is the place where we specify the redo log files and the group they belong. Things will get clearer to

Page 223: Oracle db ebook

223

you once we look at the real world example. MAXLOGFILES defines the maximum number of redo log files/redo log groups that can ever be created in the database. Similarly, MAXLOGMEMBERS defines the limit for redo log members per redo log group. MAXLOGHISTORY specifies the maximum number of archive redo log files for automatic media recovery. AUTOEXTEND enables or disables the automatic extension of SYSTEM tablespace data files. MAXDATAFILES defines the maximum number of data files that can ever be added to the database. MAXINSTANCES is basically optional and used specifically in Oracle RAC (Real Application Cluster). The default is 1, if not specified. ARCHIVELOG or NOARCHIVELOG With this clause you can make the database to run in archive log mode or no archive log mode. In archive log mode, Oracle Instance makes sure that redo log files get archived to ARCHIVE_LOG_DEST location before they get overwritten. You can also switch between these two modes later after the creation of database using the ALTER DATABASE statement. CHARACTER SET defines the character set that the database uses to store data.

Page 224: Oracle db ebook

224

NATIONAL CHARACTER SET this clause is optional and is used to store data in columns specifically defined as NCHAR, NCLOB or NVARCHAR2. Now let’s look at the real world example as provide below. CREATE DATABASE mydb1 CONTROLFILE REUSE LOGGILE GROUP 1 ('/mydatabases/mydb1/log01a.log', '/mydatabase02/mydb1/log01b.log') size 50M, GROUP 2 ('/mydatabases/mydb1/log02a.log', '/mydatabase02/mydb1/log02b.log') size 50M, GROUP 3 ('/mydatabases/mydb1/log03a.log', '/mydatabase02/mydb1/log03b.log') size 50M, DATAFLE '/mydatabases/mydb1/system.dbf' SIZE 250M AUTOEXTEND ON NEXT 10M MAXSIZE 500M UNDO TABLESPACE undotab DATAFILE '/mydatabases/mydb1/undotab.dbf' SIZE 50M DEFAULT TEMPORARY TABLESPACE temptbs TEMPFILE ‘/mydatabases/mydb1/temptbs.dbf' size 75M EXTENT MANGEMENT LOCAL CHARACTER SET US7ASCII MAXLOGFILES 20 MAXLOGMEMBERS 20 MAXLOGHISTORY 1 MAXDATAFILES 200; A database must have at least two redo log groups and it’s recommended that all redo log groups should be of same size. Where we have specified 500M for system tablespace, if you use UNLIMITED then there would not be any limit. Actually in that case the limit comes from the space available on your hard disk. Up till this point our database would be having data files, control file, redo log files and obviously the PFILE. Also we would be having SYS and SYSTEM accounts.

Page 225: Oracle db ebook

225

If you haven’t changed the default password of SYS (change_on_install) and SYSTEM (manager), it’s the best time to do it using the following ALTER USER statement. ALTER USER username IDENTIFIED BY password; Data Dictionary tables would also be available but not the data dictionary views. Step 07: Create Additional Tablespaces Create additional tablespaces that you need for your database using the CREATE TABALESPACE statement e.g., CREATE TABLESPACE payroll LOGGING DATAFLE '/mydatabases/mydb1/payroll.dbf' SIZE 250M AUTOEXTEND ON NEXT 10M MAXSIZE UNLIMITED EXTENT MANGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO; Create as many tablespace as you want in this manner. You can also use ALTER TABLESPACE to add more data files to the tablespace. There are two kinds of tablespaces based on the storage management. One is called “Locally Managed” tablespace and the other one is “Dictionary Managed” tablespace. In all pre-Oracle9i systems, there was only one tablespace type i.e. Dictionary Managed and you had to take care of segments and extents within the tablespace.

Page 226: Oracle db ebook

226

In Oracle9i all sorts of management of segments and extents are done by Oracle all you have to add is “EXTENT MANGEMENT LOCAL”. If you want a locally managed tablespace to have the segment’s free space managed by Oracle, you must include the clause, SEGMENT SPACE MANAGEMENT AUTO. Once done then you don’t even have to worry about PCTUSED and FREELISTS used in pre-Oracle9i releases. Step 08: Run Scripts To Build Data Dictionary Views Run the necessary scripts required to build data dictionary view. They are located under the directory ORACLE_HOME/rdbms/admin. Remember SYS is the owner of all the data dictionary view so you have to connect to Oracle Instance as SYS as shown below. CONNECT SYS/password AS SYSDBA The scripts you need to run are CATALOG.sql and CATPROC.sql. Once done all the data dictionary view will be created for you. Step 09: Run Scripts To Install Additional Options (Optional) This is optional step. If you need to install any of the optional products like InterMedia or Spatial, you can run the scripts to have these products added to your database. Consult the Oracle

Page 227: Oracle db ebook

227

documentation for procedure and installation. Step 10: Create Server Parameter File (Highly Recommended) It is highly recommended to have a server parameter file created or in other words SPFILE from PFILE. SPFILE give you the ability to make dynamic changes to the initialization parameters using ALTER SYSTEM statement without the need to restart the Oracle Database. The statement to create the SPFILE given below. C:\>sqlplus /nolog SQL>CONNECT SYS/password AS SYSDBA SQL>CREATE SPFILE FROM PFILE; SQL>SHUTDOWN C:\>sqlplus /nolog SQL>CONNECT SYS/password AS SYSDBA SQL>STARTUP SQL>EXIT Step 11: Create Additional Users, Grant Them Quotas On Tablespaces, Privileges/Roles This is an optional step. Now you can create new users, grant them quotas on tablespaces, assign them privileges, profiles or Roles.

Page 228: Oracle db ebook

228

Step 12: Back Up The Database Shutdown the database as NORMAL and take a cold backup of the database. We learn about back-up in great detail in the backup and recovery chapter.

Page 229: Oracle db ebook

229

We Did It!

In this chapter we learnt about design of Oracle Database (or precisely Oracle Instance & Oracle Database). Two different methods are presented to achieve this: DBCA (Database Configuration Assistant) and Manual Technique using SQL statements. DBCA is a software wizard and it takes the database administrators to the step by step process to the development of the database without requiring any SQL knowledge. In the background, DBCA use the same SQL statements that we use in the Manual technique for the development of database.

Page 230: Oracle db ebook

230

Page 231: Oracle db ebook

231

CChhaapptteerr 88

Oracle Network Administration

• Oracle Network Concepts • Oracle Network Implementation

Page 232: Oracle db ebook

232

Page 233: Oracle db ebook

233

The purpose of making Oracle network is to have an environment where users from all over the city or country or even world can access the database as if it’s residing on their machine. Establishing an Oracle network is very simple. All you have to do is to configure Oracle Net layer on both server and client side. Each of the clients that need to make a connection with Oracle server requires an Oracle Net installation. Before Oracle 9i, Oracle Net was called as SQL*Net. Oracle Net gets installed on the server at the time of installation of Oracle Server software but for each client we have to select the option of client installation in Oracle Universal Installer. Once we have configured client and server side Oracle Net layer, the connected client can easily send SQL commands to Oracle server

Key Concepts

1) Oracle Net has to be there in both clients and server.

2) Configuration of Oracle

Net on server side is actually the development of listener.ora file.

3) Configuration of Oracle Net on the client side is actually the development of tnsnames.ora file.

Page 234: Oracle db ebook

234

and can get the response back. Oracle Net takes care of all different types of Network protocols. Oracle server knows how to communicate with Oracle Net and Oracle Net knows all different Network languages/protocols including world famous TCP/IP. The whole internet runs on TCP/IP protocol based communication and even in LAN (Local Area Network), WAN (Wide Area Network) etc. TCP/IP is now a standard. Protocol is nothing but the “grammar” e.g. right now I am using English protocol to speak with you and you are understanding whatever I am saying since I following English grammar by default. Imagine if I stop following grammar rules than it would be really impossible for you to understand what I am saying. Similarly, when two machines (computers) talks with each other, both of them needs to know the common language and both of them have to follow the grammar (protocol) of the language (TCP/IP). The way it works is first we create the listener.ora file on the server then we start the listener. The development of listener.ora file is actually the configuration of Oracle Net for the server. Once we have started the listener process, it listens to the users connection requests. The job of the listener process is to bequeath the connection to the dispatcher in case the Oracle server is running under shared server mode or to the dedicated server process in case Oracle is running under dedicated mode.

4) Oracle Net takes care of

all different types of Network protocols.

5) Server and clients know how to speak with Oracle Net and Oracle Net knows all different Network languages.

6) The sole job of the listener process is to bequeath the connection to the dispatcher or dedicated server process.

7) tnsnames.ora file contains the resolution of “Net Service Name” or “Connect String” that we will use to connect to Oracle Server.

Page 235: Oracle db ebook

235

On the client side, we have to first install the Oracle Net software layer. It can be done using the same Oracle installation CD and selecting the option of “client side installation” in Oracle Universal Installed. Once done we have to create the tnsnames.ora file. Creating tnsnames.ora file is actually the configuration of Oracle Net on the client side. tnsnames.ora is nothing but contains the resolution of “service name” or “connect string” that we will use to connect to Oracle server running on the same machine or on some other machine located in even different country. All the client side (SQL*Plus) and remote database administration (Oracle Enterprise Manger) tools require Net Service Name or Connect Sting or in other words tnsnames.ora file. This way of name resolution of Oracle server is called “Local Naming” method. This is highly recommended by Oracle and widely used all over the world. There are two other ways too i.e. Host Naming method and Oracle Internet Directory. The study of these two methods are totally on your likeness and information can be retrieved my making a search on “google.com” with the key words “Oracle Host Naming” or “Oracle Internet Directory”. Host naming method requires the existence of external DNS (Domain Name Server) where as Oracle Internet Directory is advantageous when we are dealing with complex Oracle networks

Page 236: Oracle db ebook

236

having many Oracle Servers running. In this book we have used the local naming method as it’s widely used.

Page 237: Oracle db ebook

237

Creation of listener.ora file and starting/making sure listener process is running is mandatory on the server end whereas on the client side creating tnsnames.ora file is mandatory. There is on optional thing I like to mention, if you plan to manage Oracle Server using OEM running on the same machine then you need to create tnsnames.ora file on the server too as it is better to use the “Net Service Name” with the OEM. Before starting configuring server and clinet side Oracle Net make sure to ping the server using the following command. Ping <ip address of Oracle Server> This command is equally good for Unix as well as windows environments. If you are running or using external DNS (Domain Name Server) then you can use

Key Concepts

1) Before starting the configuration process on clients and server make sure ping commands result in success.

2) Creation of

tnsnames.ora file on the server end is only required if you want to run OEM on the server.

3) The purpose of tnsnames.ora file is that it contains the name resolution of Net Service Name.

Page 238: Oracle db ebook

238

domain name instead of ip address of the machine. Upon ping command’s successful response, we should go ahead with the configuration of Oracle Net on the server. Ping command’s success means that no packets should be lost, out of 4 sent from the machine executed the ping command. In other words the machine executed the ping command can clearly see the other machine on the network.

For both client and server side Oracle Net configuration we use ONM (Oracle Network Manger). ONM helps facilitates the development of listerner.ora file on the server end and tnsnames.ora file on the client end.

4) Net Service Name is

basically the alias given to the set of TCP/IP required connection parameters that can lead OEM, SQL*Plus etc. to the Oracle Server.

Page 239: Oracle db ebook

239

After installation of Oracle Server and creating database we will run OEM on the server machine to see whether we connect to the Oracle Server or not. In Unix environment we can launch it using the following command. $ oemapp console Whereas in Windows the same software can be accessed under Start>All Programs>Oracle> Launch the application as standalone and click the OK button.

Key Concepts

1) In the Unix environment use the command <oemapp console> to start the same OEM which you can start in windows under start>all programs menu.

Page 240: Oracle db ebook

240

Expand the <Databases> item under the Network. Once you expand the item under the <Databases> (e.g. in our case we just created the database with the SID as TEST which is not the Net Service Name for this database.), you will see the login screen. Select the option “SYSDBA” next to Connect as. There is no need to specify login and password as here we are using Externally Authenticated Database Administration method. Remember database users get authenticated by Oracle server only whereas for database administrator we can use this type of authentication.

Key Concepts

1) To connect as SYSDBA you have to be the member of operating system OSOPER or OSDBA group.

2) Only DBA can be

authenticated by Oracle with this externally authenticated method.

3) Regular database users are verified through Oracle Server only.

Page 241: Oracle db ebook

241

After selecting SYSDBA, click the OK button. You will get the “TNS could not resolve the service name”. As mentioned earlier that we have not created the listener.ora file and tnsnames.ora file. TEST in the OEM tree is not the Net Service name but it’s the name of the database running on the same machine on which we are running OEM. Just to keep in mind that here we are running OEM on the same machine on which we are running Oracle Server.

Key Concepts

1) Net Service Name gets resolved by tnsnames.ora file.

Page 242: Oracle db ebook

242

The next step would be to start the Oracle Net Manger. There is one more tool called Oracle Network Configuration Assistant or ONCA used for basic configuration of Oracle network. For more detailed control ONM is recommended by Oracle and is widely used. With this single tool we can create both listener.ora and tnsnames.ora files. ONM can be accessed under Unix using the netmgr command under ORACLE_HOME/bin directory and the same software can be found in window under Start>All Programs>Oracle.

Key Concepts

1) Oracle Network Manager should be preferred over Oracle Network Configuration as recommended by Oracle.

2) In Unix environment,

ONM can be accessed by executing netmgr command and the same software can be accessed in windows under start>all programs menu.

Page 243: Oracle db ebook

243

Now you can clearly see the cause of error. Once you expand the <Service Naming> and <Listeners> items in the tree, you will see nothing. There is no listener running moreover there is no tnsnames.ora file. As we are running Oracle Network Manager (ONM) on the same machine on which we are running ONM, let’s first start by creating listener.

Key Concepts

1) We will first create the listener on the machine running Oracle Server using ONM.

Page 244: Oracle db ebook

244

Select the Listener item from the tree in ONM and then click the ‘+’ sign shown in the slide to create the listener. In the popped up window write the Listener Name. If you use the default name of the listener than the Oracle Instance registers itself with the listener dynamically and its called dynamic service registration. That’s why we are sticking with the default name of the listener i.e. LISTENER. Not only listener name has to be default but we have to use default protocol i.e. TCP/IP and default port i.e. 1521 or 1520 if we want to go with dynamic service registration.

Key Concepts

1) Dynamic Service Registration happens only if we select the default listener name, protocol and port.

Page 245: Oracle db ebook

245

After selecting listener name as LISTENER you have to click the OK button. Then the listener name will appear under the Listeners item in the tree on the left pane of ONM. Select the listener name and click “Add Addresses” button on the right pane of ONM. Once you click this button Oracle adds the default values for Host (i.e. name of the machine running Oracle Server or its IP address in case no name resolution service like DNS is available), Protocol (i.e. TCP/IP) and port (i.e. 1521).

Key Concepts

1) Dynamic Service Registration happen only if we use default Listener Name, Protocol, Port.

Page 246: Oracle db ebook

246

After you have finalized Host, Protocol and port, select the drop down menu from the top as shown in the slide and select the option “Database Service”. Although the Oracle Instance will register itself with the listener as we have selected all default values but if you want to manually register the database you can do it this way.

Key Concepts

1) Manual registration of Oracle Instance is also possible using ONM.

Page 247: Oracle db ebook

247

After selecting “Database Serives” from the drop down menu click the “Add Database” button on the right pane of ONM (Oracle Network Manger). Just to keep you on track and for your information we are running ONM on the same server which is running Oracle Server. Moreover, we are working on the development of listener.ora file. Once we start the listener process it will configure it self according to the configuration contained in the listener.ora file.

Key Concepts

1) Its not recommended that you create the listener.ora file using text editor. Always use ONM as it’s programmed to produce listener.ora file exactly in same format required by listener process.

Page 248: Oracle db ebook

248

Now provide the Database name i.e. Global Database Name. In our case it is test.takveen. Moreover provide the location of Oracle Home directory and the name of SID i.e. test in our case. The information you provide in these three text boxes should be exactly the same when you created the database. Once done do not forget to save the configuration as it’s only when you save, ONM creates or updates the listener.ora file. Next thing is to start the listener. The listener process can be started using lsnrctl utility both in Windows Server and Unix environment. You can also run in windows environment by clicking

Key Concepts

1) Global database name, Oracle Home and SID is required to complete the manual registration of Oracle database.

2) The sole purpose of the

listener process is to listen to the connection request.

3) Listener process never

processes the request rather it bequeath the connection to dispatcher or dedicated server process and then gets back to listening state.

Page 249: Oracle db ebook

249

Start>Settings>Control Panel>Services And then selecting the listener service for Oracle and then pressing the “play” on the tool bar of services console.

C:\>lsnrctl start This command will start the listener whereas if you replace “start” with “stop” you can stop the listener. To check the status of the listener use the following command. C:\>lsnrctl status What if you don’t want to have default listener or in other words you don’t want the listener name to be LISTENER or port to be 1521 or even protocol to be TCP/IP. In this case you have to define three more initialization parameters viz., INSTANCE_NAME, SERVICE_NAMES and LOCAL_LISTENER for Oracle to register with the listener dynamically e.g. if you want to run the listener on port other than 1521 then create the listener.ora file suing that port number and mention this information in the LOCAL_LISTENER parameter in the init.ora file. For Oracle Instance to register dynamically with the listener you have to set the INSTACNE_NAME to name of the Instance in our case it should be TEST and the SERVICE_NAMES

4) Do not forget to save the configuration as it only after saving, things get written to listener.ora file.

5) Two ways to do

Dynamic service registration namely, use the default listener or specify the following three init.ora parameters LOCAL_LISTENER, INSTANCE_NAME & SERVICE_NAMES

Page 250: Oracle db ebook

250

should be set to global database name, in our case it is test.takveen. Summary Of Creating listener.ora File: Step 1: Select The Tool We have two options either to use ONCA (Oracle Network Configuration Assistant) or to use ONM (Oracle Network Manger). Oracle recommends using ONM whereas ONCA is used for basic configuration of Oracle Network files. Step 2: Create The Listener Input three things to ONM viz., Host name, Protocol and Port. Host will be the name or ip address of the machine running Oracle Server, protocol will be TCP/IP and port will be 1521 in case we want to use dynamic service registration. In Windows environment the name of the machine can be found by right clicking “My Computer” icon and then clicking “Properties”. In the properties window select the “Network Identification” tab and click “Properties” to locate or update the computer name. Whereas the ip address of the machine can be found by using ipconfig command at the DOS prompt.

Page 251: Oracle db ebook

251

Step 3: Add the Database Service Input three things to ONM viz., Global Database Name, Oracle Home directory and SID. Step 4: Save Network Configuration In ONM click “File” on the menu bar and then click “Save Network Configuration”. Only when you save the configuarion settings get written to listener.ora file. Step 5: Start The Listener Listener process can be started using lsnrctl utility located in bin directory under ORACLE_HOME both in Unix and Windows Server environments. In windows you can also start this service from the Services console by double clicking “Services” under Control Panel. C:\>lsnrctl start This command will start the listener whereas if you replace “start” with “stop” you can stop the listener. To check the status of the listener use the following command. C:\>lsnrctl status

Page 252: Oracle db ebook

252

Since now we have created the listener.ora file and started the listener process. Next thing we will do is to create the Net Service Name on this machine. Net Service Name is basically a short & easy to remember name for a long set of parameters that helps client applications to connect to server it includes host name, protocol, port, instance name etc. Select the “Service Naming” item in the tree on the left pane of ONM and click the “+” sign to add the Net Service Name. In the popped up window write the Net Service Name as “asim”. Now we are going to define what the word “asim” means on this machine by clicking “Next” button.

Key Concepts

1) Net Service Name is the short description or Alias for the long set of connection parameters that help client applications to connect to Oracle Server.

2) Net Service Names get

resolved through tnsnames.ora file.

Page 253: Oracle db ebook

253

Select protocol as TCP/IP and click “Next” button. Then write down the host name of the machine running Oracle Server or in other words the listener’s machine name. Remember that client applications send a connection request to the listener and listener bequeath the connection to the dispatcher in a shared server mode or dedicated server process in a dedicated server mode. After the connection has been bequeathed to the Oracle Instance then the communication takes place between Oracle Instance and the connected client without any listener process involvement. Write down the port on which listener is running on the Host and click “Next”.

Key Concepts

1) Here we will define the meaning of the Net Service Name “asim” and save it in tnsnames.ora file.

2) Whenever any client

uses the word “asim” Oracle will decode or resolve the word using tnsnames.ora file as it contains the name resolution of “asim”.

Page 254: Oracle db ebook

254

The parameters defined in the previous slide will take your client application to the listener running on the machine “guru” at port 1521. Your client application also has to convey to that listener that which database on that machine you want him to bequeath the connection. Since there are chances that you might be having more than one Oracle databases running on the same machine. Service Name over here is the parameter where you specify the global database name of Oracle database you what the listener to bequeath the connection in our case it is test.takveen. Click the “Next” button. The next popped up window will give you the opportunity to test the connection and see that your configured Net Service

Key Concepts

1) Service Name over in this slide is the Global Database Name of the Oracle Database your connection will be bequeathed to.

Page 255: Oracle db ebook

255

Name “asim” is resolving properly using tnsname.ora. Once you click the “Test” button ONM (Oracle Network Manger) will use the “scott/tiger” account to test the connection. If you don’t have this account in your database or if you would like to use some other account you can do that too. Upon successful connection don’t forget to save the Network Configuration. The tnsnames.ora file will get created or updated only when you save the configuration by clicking “File” and then “Save Network Configuration”.

Page 256: Oracle db ebook

256

Once you save the network configuration as shown in the slide then the Net Service Name that you just created will appear under Service Naming item in the tree on the left pane of ONM. ONM also creates the tnsname.ora file on saving or update it in case you changed some settings. The default location of tnsnames.ora file is %ORACLE_HOME%\network\admin on Windows Server 2003/2000/NT plateform and $ORACLE_HOME/network/admin under Unix environment.

Key Concepts

1) ONM generates the tnsnames.ora file once you save the configuration.

Page 257: Oracle db ebook

257

Summary Of Creating tnsnames.ora File: Step 1: Choose The Naming Method There are three way namely, Host Naming, Local Naming and Oracle Internet Directory. We have selected Local Naming method. Moreover we are using ONM (Oracle Network Manager) as a tool to create tnsnames.ora file. Step 2: Choose Net Service Name Here we used “asim” as Net Service Name and it will be used by client applications especially OEM to make a connection to Oracle Server. Step 3: Choose Network Protocol We will select TCP/IP as its widely used and also internet runs on this protocol. Step 4: Host Name & Port It’s the name of the machine running listener process whereas port number defines the port on which the listener process is listening for connection requests.

Page 258: Oracle db ebook

258

Step 5: Service Name It’s the global database name of the Oracle database running on the same machine you are running the listener. In our case its test.takveen. Step 6: Test The Connection Step 7: Save Your Network Configuration Once you save Network Configuration by clicking File and then Save Network Configuration, ONM creates or updates the tnsnames.ora file.

Page 259: Oracle db ebook

259

Now you can run OEM if it’s not already running. Remember that you have created listener.ora file and you have also started the listener. Moreover you have also created the Net Service Name to be used with client applications. Net Service Name or tnsnames.ora file has to be created for each of the machines from where you want to launch any of Oracle’s client applications like SQL*Plus, OEM etc. Delete any other previously mentioned items under “Databases” tree on the left pane of OEM. Its very important point to note over here that the Net Service Name on one machine (e.g. “asim”) needs not to be similar to the Net Service Name on the

Key Concepts

1) Net Service Name has to be created on each of the machines from where you want to run client application.

2) Net Service Name on

one machine need not to be similar to Net Service Name on the other machine although both will point to the same Oracle database.

3) You can connect to and

manage multiple databases scattered all over the world using the same OEM window.

Page 260: Oracle db ebook

260

other machine (e.g. lets suppose “cow”) although both Net Service Names will resolves to the same Oracle database (e.g. test.takveen). You can connect to or manage multiple databases using OEM remotely. All you need to do is to create the Net Service Name for each of the databases you want to manage the same way explained for “asim” Net Service Name above. Once you click the “Save Network Configuration” under “File” on the menu bar after adding each Net Service Name, ONM will append the Net Service Name resolution configuration in the same tnsnames.ora file. In other words all the Net Service Names we will create, all reside in one tnsnames.ora file on that machine running OEM. Click the “Navigator” on the menu bar and then click “Add Database To Tree”.

4) You need to create Net Service Name for each of these databases in the same way.

5) All the Net Service Names for all the databases you want to connect to through OEM will be defined under one tnsnames.ora file one the machine running OEM.

Page 261: Oracle db ebook

261

Select the option “Add selected databases from your local tnsnames.ora file …” and make sure that the service name “ASIM” is checked. Click “OK” once done. Now you can see the Net Service Name under the Databases item on the left pane of OEM. Note that you are seeing the Net Service Name of the database having global database name as test.takveen. So in OEM Database item contains the Net Service Names of the databases and not the database names.

Key Concepts

1) Under Databases item in the tree OEM contains the Net Service Names of the databases and not the databases names or global database names.

Page 262: Oracle db ebook

262

Once you try to expand the “ASIM” tree, the login and password window will pop up. Connect to ASIM as SYSDBA since we are using external authentication method or in other words OS authentication. Remember that you have to be member of operating system SYSOPER or SYSDBA for connecting as SYSDBA. Both local and remote administrations are possible with operating system authentication but it requires a secure connection to the Oracle Server when you want to do remote database administration using operating system authentication. Once you select the option connect as SYSDBA then click OK button.

Key Concepts

1) Two types of Administrator Authentication viz., OS authentication or password file.

2) We can use either of the

methods for local as well as remote administration.

Page 263: Oracle db ebook

263

Now the Net Service Name ASIM will expand to show all next level of items. The complete management of database is possible using OEM whether the database is residing on the same machine or even in some other state or country. Moreover we can manage more than one database using single OEM console by adding Net Service Names for each in the tnsnames.ora file utilizing ONM. Before the development of OEM DBAs’ used to do the stuff using SQL statements but now OEM communicates with Oracle Instance using same SQL statements for each of the task we perform on OEM by clicking.

Key Concepts

1) More than one distributed databases can be managed using OEM.

2) OEM communicates

with Oracle Server using same SQL statements that we use in manual database management or before the development of OEM.

Page 264: Oracle db ebook

264

In the next chapter we will learn how to manage Oracle database manually as well as using OEM.

Page 265: Oracle db ebook

265

We Did It!

In this chapter we learnt about how to make a Oracle Network so that clients from different locations or even different parts of the world can access the Oracle Server as if it’s residing on their machine. We learnt how to use Oracle Network Manager (ONM) to create listener.ora file on the Oracle Server and tnsnames.ora file for the client applications running either on client side or server side.

Page 266: Oracle db ebook

266

Page 267: Oracle db ebook

267

CChhaapptteerr 99

Oracle Database Management

• SQL Statements Based Management • OEM Based Management

• Setting Up Automatic Archiving

Page 268: Oracle db ebook

268

Page 269: Oracle db ebook

269

You can control the availability of tablespaces using the ALTER TABLESPACE statement e.g. you can make them READ ONLY or ONLINE/OFFLINE. Once you put the tablespace READ ONLY then no operation besides SELECT statement is allowed on that tablespace. By default once you start the Oracle database, all the tablespaces would be online unless you take a tablespace offline using the OFFLINE clause in the ALTER TABLESPACE command. The application of each of these will come in the back up and recovery chapter then their purpose will get clearer. Let’s discuss each of these statements in the following lines one by one. ALTER TABLESPACE users OFFLINE NORMAL;

Key Concepts

1) Availability of tablespace to the users can be controlled using ALTER TABLESPACE statement.

2) SYSTEM tablespace can

never be placed in the OFFLINE mode as Oracle utilizes the tables relating to data dictionary views and dynamic performance view in its normal operation.

Page 270: Oracle db ebook

270

With NORMAL clause, Oracle writes all the dirty buffer blocks to the data files associated with that tablespace before taking it offline. All the data files associated with the tablespace needs to be online. No recovery is necessary when bringing the tablespace ONLINE again using the following statement. ALTER TABLESPACE users ONLINE; If you use the TEMPORARY clause instead of NORMAL no data files are checked whether they are online or not. Oracle performs a checkpoint whenever you use the ALTER TABLESPACE with TEMPORARY clause. Media recovery is necessary when you try to bring the tablespace ONLINE using the above-mentioned statement. Whereas with IMMEDIATE as shown below, Oracle takes the tablespace offline immediately without performing even Checkpoint. ALTER TABLESPACE users OFFLINE IMMEDIATE; Where as with FOR RECOVER clause in the ALTER TABLESPACE statement, Oracle places the tablespace in recovery mode. Like I said before, the applications of these commands are discussed in Back up and Recovery chapter. Remember that the SYSTEM tablespace can never be placed in OFFLINE mode, as the data dictionary has to be ONLINE all the time.

Page 271: Oracle db ebook

271

If your tablespace is running out of space than you can either increase the side of existing data files or you can add more data files to it using the ALTER TABLESPACE statement as shown in the slide above. Did you ever thought that why in the definition of “Relationship Between Physical & Logical Architecture of Database”, we said, “Each tablespace can reside in one or more data files but each data file can never be associated with more than one tablespace.”

Key Concepts

1) Each tablespace can reside in one or more data files but each data file can never belong to more than one tablespace.

2) Oracle tablespace size is

unlimited and it doesn’t depends on the OS maximum file size limit rather it depends on the available resources (e.g. Hard Disk Drives)

3) You cannot drop

SYSTEM tablespace.

Page 272: Oracle db ebook

272

What’s wrong with the following definition, if Oracle would have defined this relationship as, Each tablespace can reside in one data file only, that’s it. Wouldn’t our life be much simpler? We never would have to worry about adding more data files. Whenever we run out of space in tablespace we can just increase it size. But let me tell you one thing which we are neglecting over here i.e. every OS has a file size limitation without going into details e.g. in Windows 98 you cannot have file size greater than 2 GB where as in Windows XP/Server you cannot have file size greater than 4 GB. So if Oracle would have said that a tablespace can reside in only one data file than it means Oracle tablespace size is limited to the OS maximum file size. That’s the reason, Oracle has lifted this limitation by saying that you can add as many data files as you like. It all depends on your resource limit (e.g. Hard Disks size etc.) but not on OS file size limit. Bottom line is that Oracle tablespace size is unlimited and is not bounded by OS maximum file size limit. DROP TABLESPCE statement helps in dropping of tablespace as shown below. DROP TABLESPACE users;

4) Oracle updates the control files once you drop a tablespace.

5) The datafile(s)

associated with the tablespace can be renamed or relocated by following 4 steps procedure.

Page 273: Oracle db ebook

273

If the tablespace is not empty then we have to add the clause INCLUDING CONTENTS. DROP TABLESPACE users INCLUDING CONTENTS; Remember that if you have named the data files according to file naming convention defined in OMF (Oracle Managed Files) then once dropping a tablespace with DROP TABLESPACE statement will drop/delete the data files associated with that tablespace too otherwise you have to remove the data files yourself at the OS level relating to that tablespace or use the following statement which will remove the data files too. DROP TABLESPACE users INCLUDING CONTENTS AND DATAFILES; Remember that you cannot drop SYSTEM tablespace. As the tables related to data dictionary and dynamic performance Views have to be up all the times during normal Oracle operation. If the tablespace that you are deleting has referential integrity (Primary/Foreign Key relationship) constraints with the objects in other tablespace then include the clause CASCADE CONTRAINS as shown below. DROP TABLESPACE users INCLUDING CONTENTS CASCADE CONSTRAINTS;

Page 274: Oracle db ebook

274

Now let’s see how to change the name and location of all the data files belonging to a tablespace. It’s always recommended to take full backup of the database before making any change pertaining to the structure of the database. It’s better to have the hard copy of the location and names of data files associated with the tablespace in front of you by using the DBA_DATA_FILES view. This view will give you the name of the tablespaces and the associated data files. Changing The Name/Location Of Complete Tablespace or Data File(s) If the tablespace is comprised of two data files you can change the name and location of one data file or all the data files. Changing the name and/or location of all the data files associated with the tablespace means that you are changing the name and/or location of complete tablespace. Step 1: Take the tablespace offline ALTER TABLESPACE payroll OFFLINE; OR ALTER TABLESPACE payroll OFFLINE NORMAL;

Page 275: Oracle db ebook

275

Step 2: Rename the data files at OS Level Use OS commands (cp in Unix, copy in Windows) to change the name/location of data files associated with the tablespace. Step 3: Inform Oracle About The Change Use the ALTER TABLESPACE statement with RENAME DATAFILE clause to inform Oracle regarding the change you made to the data files at the OS level. ALTER TABLESPACE payroll RENAME DATAFILE ‘/disk1/oradata/test/payroll_data01.dbf’, ‘/disk1/oradata/test/payroll_data02.dbf’ TO ‘/disk2/oradata/test/payroll_data01.dbf’, ‘/disk2/oradata/test/payroll_data02.dbf’; Step 4: Make Tablespace ONLINE Use the following statement to bring the tablespace online again and that’s it. ALTER TABLESPACE payroll ONLINE; In case you want to change the name and/or location of data file(s) belonging to SYSTEM tablespace than you have to shutdown Oracle database follow step 2 and then STARTUP MOUNT, this command will make the Instance alive and the Instance will mount the database but it will not be open to the public. Then follow step 3 and finally open the database using following SQL statement. ALTER DATABASE OPEN;

Page 276: Oracle db ebook

276

We can perform many of the database management tasks now using OEM just by clicking and OEM communicates with Oracle Instance using SQL statements in the background.

Page 277: Oracle db ebook

277

Oracle Enterprise Manager (OEM) is a java based client application. As its java based that’s why it’s completely independent of OS. The only difference is how you invoke it through Unix and Windows platform. In Windows Server 2003/XP environment you can invoke in by clicking Start > All Programs > Oracle > Enterprise Manger Console Where as in Unix environment it can be invoked using the following command at the OS command prompt. $ oemapp console

Key Concepts

1) OEM is java based client side application for complete management of Oracle database(s) locally or remotely without using any SQL statements.

2) OEM on the other hand

communicates with Oracle using SQL in the background.

Page 278: Oracle db ebook

278

As explained before, connect to the database as SYSDBA when asked to provide login, password and privilege level. There is no need to specify the login and password as its would be the externally authenticated OSDBA or OSOPER account. Once you get connected to the Oracle database you would be able to expand the tree on the left pane of OEM window. Expand the “Storage” item; you will see “Tablespaces”. When you select the Tablespaces item, on the right pane you will see the list of all the Tablespaces with that database along with associated data files and related information.

Key Concepts

1) Complete management of Oracle Database(s) is possible using OEM.

2) Under Storage item lies

the “Tablespaces” information.

3) Complete management

of Tablespaces within the database can be done using this “Tablespaces” item without using any ALTER/CREATE TABLESPACE statements.

Page 279: Oracle db ebook

279

We can create new tablespace by right clicking any one of the tablespace mentioned in the list and then selecting the option “Create …” in the popped up window as shown in the slide above. Once we click the “Create …” option then the Create Tablespace window will pop up as shown in the next slide.

Key Concepts

1) Right click on the right pane of OEM after selecting “Tablespaces” item on the left pane to see the complete list of management information.

Page 280: Oracle db ebook

280

In this Create Tablespace window you can give your tablespace a name and you can have as many data files as you like. You can also make the tablespace ONLINE/OFFLINE. If you create the tablespace with OFFLINE option then the tablespace will not be available to users you will grant access unless you make it ONLINE Under the storage tab make sure it’s selected as “Locally Managed” otherwise you would have to take care of Extents management as discussed before. It’s a nice feature available in Oracle 9i and 10g, which free up DBAs to do other task and Oracle does this management magnificently.

Key Concepts

1) Locally Managed tablespaces are well managed by Oracle Instance at the Extent and DB Block level.

2) Its recommended to

select this (Locally Managed) option with Oracle 9i or higher.

Page 281: Oracle db ebook

281

Under the “Datafiles” item you will see the list of all the data files associated to the database along with the corresponding tablespaces. We can create a new data file and assign it to the tablespace. Its similar to adding data file using the “Tablespaces” item. Right Clicking on any one of the data file on the right pane of OEM after selecting “Datafiles” item will bring up another window having all the data files management options including “Create...”

Key Concepts

1) Creating a data file and assigning it to the tablespace is the same as if we have added a new data file to a tablespace.

Page 282: Oracle db ebook

282

If we expand the “Redo Log Groups” item under “Storage” we can see the all the redo log groups and associated members. We can add new members in all the redo log groups as well as their location after selecting the group number and then add new member information on the right pane of OEM window.

Key Concepts

1) Complete Redo Log Files management is possible using OEM.

Page 283: Oracle db ebook

283

Using OEM, management of database is not just limited to Storage but you can do complete management of users utilizing the “Users” item under the “Security”. “Create User” user window will pop up once you right click any user and select the option “Create …” as shown in the slide above.

Key Concepts

1) Complete management of Users, Privileges, Roles, and Profiles are possible utilizing stuff under “Security” item.

Page 284: Oracle db ebook

284

We can also manage the Oracle Instance related stuff by first selecting the “Configuration” item on the left pane of OEM and changing values in the right pane.

Key Concepts

1) Complete Oracle Instance management is possible using the “Configuration” item on the left pane of OEM.

Page 285: Oracle db ebook

285

Under the “Memory” tab we can have control over all the SGA related stuff. We can also have a graphical view in the form of pie chart of each quantity’s share in the SGA (Shared Global Area). Always remember the definition of an Oracle Instance i.e. “Background processes and memory architecture constitute an Oracle Instance.” We can also change the values for Maximum Concurrent Users and the Sort Area Size depending upon the needs.

Key Concepts

1) Complete management of Oracle Instance’s Memory Architecture is possible through the “Memory” tab of “Configuration” item under “Instance” node.

2) Background processes

and Memory architecture constitute an Oracle Instance.

Page 286: Oracle db ebook

286

If you want to run the database is ARCHIVELOG mode or NOARCHIVELOG mode you can control that using “Recovery” tab as shown in the slide. The “Recovery” tab can be accessed by first expanding the “Instance” node and then selection “Configuration” item underneath it. If you check the box “Archive Log Mode” then the “Archiver” process will get alive and will start archiving your redo log files. Under archive log mode Oracle Instance makes sure that redo log files gets archived before they get over written. You can also specify Archive Log Destination in the same “Recovery” tab at

Key Concepts

1) Database can be made in ARCHIVELOG mode or NOARCHIVELOG mode using the “Recovery” tab.

2) Also using the

“Recovery” tab, Archive Log Destination can be changed or multiplexed.

Page 287: Oracle db ebook

287

the bottom. If you specify more than one location than it will be called “Multiplexing of Redo Log files” or in others words you would be having mirror copies of redo log files at two or three different locations. The sole purpose of multiplexing is to increase the fault tolerance factor of your information system and it all depends on how critical the data is and how much resources are available. Setting Up Automatic/Manual Archiving We have learnt how to set up the ARCHIVELOG mode using OEM. Now lets see how we can implement manually. The ARCHIVELOG mode can either be set during the creation or database or using the following statement. ALTER DATABASE ARCHIVELOG; Make sure that the database was in mounted state and not in open state before the execution of above statement. The Redo log files will get archived to the destination path defined using LOG_ARCHIVE_DEST. Once executed this command will stays effective unless you execute the following statement. ALTER DATABASE NOARCHIVELOG;

Page 288: Oracle db ebook

288

Steps For Manual Archiving:

1. To setup manual archiving we set the LOG_ARCHIVE_START initialization parameter to False.

2. Put the database in

ARCHIVELOG mode by first shutting down the database as NORMAL and then starting it in mount state using STARTUP MOUNT statement. Then executing the following statement.

ALTER DATABASE ARCHIVELOG;

3. Finally open the database using

the following statement.

ALTER DATABASE OPEN; Now you are ready to perform manual archiving of Redo log files using the following statements. SQL> archive log all; Or SQL> archive log next; With ALL clause you would be able to archive all Redo log files available for archiving whereas with NEXT clause the next group of redo logs will get archived.

Page 289: Oracle db ebook

289

Steps For Automatic Archiving: Follow the same steps as in the manual method described above except change LOG_ARCHIVE_START to True. To make sure that the archiving is happening, execute the following statement and then check the directory defined in LOG_ARCHIVE_DEST initialization parameter file. You should see the archived Redo log files ALTER SYSTEM SWITCH LOGFILE;

Page 290: Oracle db ebook

290

We Did It!

In this chapter we have learnt how ALTER TABLESPACE and other ALTER statements can help us in doing database management. Not only this we have looked into OEM (Oracle Enterprise Manger) in detail. With OEM, all we have to do is “Select & Click” and the task will get completed. OEM on the background communicates with Oracle Instance using SQL statements and frees up the user’s brain from remembering SQL statements syntax.

Page 291: Oracle db ebook

291

CChhaapptteerr 1100

Oracle Backup & Recovery

• User Managed • RMAN Based

• Logical Backup & Recovery

Page 292: Oracle db ebook

292

Page 293: Oracle db ebook

293

In this chapter we will be studding different ways to perform backup and recovery operations in Oracle. There are basically three paths and each has its own application. User Managed Backup & Recovery RMAN Based Backup & Recovery Logical Backup & Recovery Backups taken in the good time or when the database is up and running perfectly fine really help in time of failure or when files get corrupted. Its exactly like what they say, Make Hay While The Sun Shines …

Key Concepts

1) There are three different paths available to perform backup and recovery, each having its own application.

Page 294: Oracle db ebook

294

We are starting the discussion with User Managed path. Using the User-Managed path we can do both Oracle database backup as well as recovery. Let first start with learning User-Managed Backups. User-Managed Backups can be taken either by first making the database “offline” or even when the database is up and running. The steps of implementation are different in both the cases. There is very useful SQL statement and I usually call it a tool than just a SQL statement. This statement will give you the output with all the tablespaces within the database along with all the data files associated with each of these tablespaces.

Key Concepts

1) User-Managed Backups can be taken when the database is SHUTDOWN NORMAL or even when it’s running.

2) There is a difference

between Restore & Recovery.

3) Restore basically means

putting the stuff back from backed up location to original location using OS level copy command

Page 295: Oracle db ebook

295

SELECT a.TS#, a.Name, b.Name FROM V$TABLESPACE a, V$DATAFILE b WHERE a.TS# = b.TS#; I call it a “tool” since with any other “tool” in real world you need not to know how its designed and what were mathematical formulae used to develop that tool e.g., with ranch all you need to know how to use it and when to use it. That’s it. Unless you want to open up your own Ranch design factory.

Similarly, in software world all you need to know how to use the application and not how it’s being developed unless you want to open up a software company or you’re a programmer. In Oracle world there are certain queries, I call as “Tools”. All you need to know what information it provides and when to use it. User-Managed Offline Backup: The other names given to this kind of backup are Closed/Cold/Consistent Database backup. No recovery is needed in this kind of backup during the restore process, as the SCN (System Change Number) is consistent.

4) Recovery process is performed after the Restoration of data files from backed up location to original location happens.

5) Recovery process is a

must if the backup was an online/hot backup.

Page 296: Oracle db ebook

296

Step 1: Shut down the database Connect to SQL as SYSDBA and then use the SHUTDOWN statement with NORMAL, IMMEDIATE or TRANSACTIONAL clause. C:\> sqlplus /nolog SQL>connect / as sysdba SQL> SHUTDOWN NORMAL; Remember that if there is any single user connected with the database and you have used the NORMAL clause with the SHUTDOWN statement then database will never shutdown unless the user closes the session. Step 2: Backup all the data files Once the database gets completely shutdown backup all the data files associated with the database at the operating system level or in other worlds using operating system commands e.g. copy in Windows and cp in case of Unix. Step 3: Backup Parameter File, Redo Log Files and Archived Redo Log Files Backup all these files too using operating system command. It’s highly recommended and provides greater fault tolerance in case of database failures.

Page 297: Oracle db ebook

297

Step 4: Startup Oracle Database with MOUNT option Start the oracle instance and mount the database but don’t open. C:\>sqlplus /nolog SQL>connect / as sysdba SQL>startup mount; Step 5: Backup the Control File Control file can be backed up using the following statement and at the desired location. ALTER DATABASE BACKUP CONTROLFILE TO ‘c:\staging\ctl.bak’; The backup created in this fashion will be the binary copy of the control file. We can also take the backup of control file, which will be in text format in the following manner. ALTER DATABASE BACKUP CONTROLFILE TO TRACE; As a result of this statement the text version of control file will get copied to the location/folder specified in USER_DUMP_DEST init.ora parameter. Step 6: Open the database Now you have full database backup or in other words Whole Database Backup. Its time to make the database available to the

Page 298: Oracle db ebook

298

users you executing the following statement. ALTER DATABASE OPEN; This completes our User-Managed Offline/Cold Database Backup. Let’s see now how we can perform User-Managed Online/Hot Database Backup. User-Managed Online Backup: The other names given to this kind of backup are viz., Open/Hot/Inconsistent database backup. The SCN will be different in all the backed up data files that’s why once you restore all the data files, recovery of database is required before making it available to users. There is very useful SQL statement and I usually call it a tool than just a SQL statement. This statement will give you the output with all the tablespaces within the database along with all the data files associated with each of these tablespaces. SELECT a.TS#, a.Name, b.Name FROM V$TABLESPACE a, V$DATAFILE b WHERE a.TS# = b.TS#; Always keep this tools output handy before proceeding with the backup. Step 1: Put the tablespace in backup mode Once connected to Oracle as SYSDBA, use the following statement to put the tablespace in the backup mode.

Page 299: Oracle db ebook

299

ALTER TABLESPACE users BEGIN BACKUP; Step 2: Copy the associated data files Copy all the data files associated with the tablespace we placed in backup mode at the operating system level or in other words using operating system copy command in Window and cp in Unix. Step 3: Take the tablespace out of Backup mode Once we have copied all the associated data files then its time to take the tablespace out of backup mode using the following SQL statement. ALTER TABLESPACE users END BACKUP; Note: Repeat Step 1 to Step 3 for each of the tablespace with the database. Step 4: Backup the Control File Control file can be backed up using the following statement and at the desired location. ALTER DATABASE BACKUP CONTROLFILE TO ‘c:\staging\ctl.bak’; The backup created in this fashion will be the binary copy of the control file. We can also take the backup of control file, which will be in text format in the following manner. ALTER DATABASE BACKUP CONTROLFILE TO TRACE;

Page 300: Oracle db ebook

300

As a result of this statement the text version of control file will get copied to the location/folder specified in USER_DUMP_DEST init.ora parameter. Note: It’s recommended to backup Archived Redo Log files, Redo log files and parameter file to get a complete database backup.

Page 301: Oracle db ebook

301

User-Managed Complete Recovery is done in two different ways and it all depends on the mode in which you are running the Oracle server. You can run Oracle server either in ARCHIVELOG mode or NOARCHIVELOG mode. The way we do the recovery in NOARCHIVELOG mode is different from the way we do recovery in ARCHIVELOG mode. Lets have a look on each procedure as follows. User-Managed Complete Recovery In NOARCHIVELOG Mode In NOARCHIVELOG mode the recovery is very simple. All you have to do is to restore the database files from your backup to the original location and start the database.

Key Concepts

1) In ARCHIVELOG mode, there will be Archiver process whose job is to archive Redo Log files before they get over written by the Redo Log writer process (LGWR.

2) The data entered/

changed after the last backup will not be recovered in NOARCHIVELOG mode whereas no data will get lost if you are running Oracle server in ARCHIVELOG mode.

Page 302: Oracle db ebook

302

Let’s suppose one of your data file got corrupted. The time try to start the Oracle Instance, it will give you an error. In this case shutdown the database. Restore all the data files; redo log files and control file back to their original location from your last whole/complete cold backup. Then start the Oracle database again by first running SQL and then connecting as SYSDBA. This time you will not get any error. User-Managed Complete Recovery In ARCHIVELOG Mode Let’s suppose one of your data file got corrupted/deleted. The time you try to start the Oracle Instance, it will give you an error with the name and location of the data files. If the Oracle server is up and running and you don’t want to shut it down then follow the following steps. Step 1: Take the corrupted data file offline Use the following SQL statement to take the corrupted/deleted data file offline. ALTER DATABASE DATAFILE ‘c:\oracle\oradata\test\users01.dbf’ OFFLINE; Step 2: Restore the corrupted data file Restore the corrupted data file by copy it from the backed up location and pasting it to the original location at the operating system level or in other words using

Page 303: Oracle db ebook

303

operating systems commands like ‘copy’ in windows or ‘cp’ in Unix/Linux. Step 3: Recover Data File Recover the restored data file using the RECOVER DATAFILE statement. Then apply all the available Redo Log files by pressing “Enter” button on the keyboard whenever asked. C:\> sqlplus /nolog SQL>connect / as sysdba SQL>recover datafile ‘c:\oracle\oradata\test\users01.dbf’ This will complete the recovery process and you will realize that no data will get lost i.e. everything that you entered or changed after the last whole database backup will get recovered by Oracle by applying all the redo log files. Difference Between Recovery In ARCHIVELOG mode & NOARCHIVELOG mode In NOARCHIVELOG mode, the transactions that got processed after the last complete backup, will get lost where as in ARCHIVELOG mode no data will get lost i.e. everything that you entered or changed after the last whole database backup will get recovered by Oracle by applying all the redo log files including the ones got archived by the Archiver process.

Page 304: Oracle db ebook

304

RMAN or in other words the recovery manager is the utility that comes with Oracle software and helps in the backup and recovery process. What it does is like OEM, it communicates with Oracle Server using SQL in the background. RMAN requires a repository or storage area to store information about the database that needs to be backed up or recovered. That repository is called RMAN repository and remember it does not contain the backup itself rather it contains the information regarding target database. There are two places you can use as a mean for RMAN repository. First is the target database control file and the second is the Recovery Catalog Database. You can use RMAN with either of these

Key Concepts

1) RMAN can run either by using target database control file or having its own database called Recovery Catalog database.

2) We can create Backup

Sets and Image Copies using RMAN.

3) Oracle recommendation

is to use RMAN with Recovery Catalog Database and not with target database control file.

Page 305: Oracle db ebook

305

repositories. It means that you can run RMAN by using target database control file as RMAN’s repository or you can run it having its own separate database called RMAN Recovery Catalog database. Whatever RMAN needs to write it appends to the control file and would not overwrite or delete any stuff on it. It would be having its own section at the end of target database control file. Oracle recommendation is that one should prefer Recovery Catalog way instead of target database control file. Connecting To RMAN: Here are the steps you need to perform to connect to RMAN with or without recovery catalog. Step 1: Set ORACLE_SID ORACLE_SID is the operating system environment variable and we use the ‘set’ command to set it to the database SID we want RMAN to backup. C:\> SET ORACLE_SID = test In Unix, its done as follows $ oraenv ORACLE_SID = test

4) RMAN backup can only be performed when the database is either open or mount state.

Page 306: Oracle db ebook

306

Step 2: Run RMAN Go to the Oracle bin directory and at the command prompt write rman RMAN> no matter its Unix or Windows Server 2003/NT. Step 3: Issue CONNECT TARGET Using DBA privileges issue CONNECT TARGET as follows. RMAN> connect target / The above mentioned command will make RMAN utilize Recovery Catalog database but before doing that you have to make sure that you have created Recovery Catalog database otherwise run RMAN with NOCATALOG option as shown below. RMAN> connect target / nocatalog This command will make RMAN utilize control file of database we want to backup, as repository for RMAN. Once you are connected to RMAN then the next thing would be to select whether you want to create Backup Sets or Image Copies.

Page 307: Oracle db ebook

307

Creating Backup Sets: After connecting to RMAN and at the RMAN prompt execute the following command to create the backup sets. RMAN> RUN { ALLOCATE CHANNEL ch1 TYPE DISK; BACKUP DATABASE FORMAT ‘/bkup/%U’; BACKUP (ARCHIVELOG ALL) FORMAT ‘/bkarc/arc_%p’; RELEASE CHANNEL ch1; } This command will perform the complete database backup including archive log files. The BACKUP DATABASE clause backs up all the data files, control file and server parameter file (spfile) if used. In the above command %U defines the format of the backup sets name. Where %U is the abbreviation for %u_%p_%c. Where %u stands for 8 character name, %p stands for backup piece number within the backup set and %c means the copy number of backup piece within the backup set. We can also do the backup of tablespace and control file separately by using the following commands after connection with RMAN. RMAN> RUN { ALLOCATE CHANNEL ch1 TYPE DISK; BACKUP TABLESPACE users INCLUDE CURRENT CONTROLFILE; RELEASE CHANNEL ch1; }

Creating Image Copies: After connecting to RMAN and at the RMAN prompt execute the following command to create the image copy of data file and control file.

Page 308: Oracle db ebook

308

RMAN> RUN { ALLOCATE CHANNEL ch1 TYPE DISK; COPY DATAFILE 1 TO ‘c:\backup\system01.dbf’, CURRENT CONTROLFILE TO ‘c:\backup\control.ctl’; RELEASE CHANNEL ch1; } In the command above data file 1 will get copied to the backup location. Oracle assigns each data file a unique number in the database. To find out tablespaces names and the associated data files numbers use the following query which is basically a tool for DBA as explained before. SELECT b.name, a.file#, a.ts#, status FROM v$datafile a, v$tablespace b WHERE a.ts# = b.ts# ORDER BY file#; NAME FILE# TS# STATUS ------------------------ ---------- ---------- ------ SYSTEM 1 0 SYSTEM UNDOTBS 2 1 ONLINE CWMLITE 3 2 ONLINE DRSYS 4 3 ONLINE EXAMPLE 5 4 ONLINE INDX 6 5 ONLINE TOOLS 7 7 ONLINE USERS 8 8 ONLINE 8 rows selected. If you want to copy more than one data file use the following command at the RMAN prompt. RUN { ALLOCATE CHANNEL ch1 TYPE DISK; COPY DATAFILE 1 TO '/bkup/system01.dbf', DATAFILE 2 TO '/ bkup /undotbs01.dbf', DATAFILE 3 TO '/ bkup /cwmlite01.dbf', DATAFILE 4 TO '/ bkup /drsys01.dbf', DATAFILE 5 TO '/ bkup /example01.dbf', DATAFILE 6 TO '/ bkup /indx01.dbf',

Page 309: Oracle db ebook

309

DATAFILE 7 TO '/ bkup /tools01.dbf', DATAFILE 8 TO '/ bkup /users01.dbf', CURRENT CONTROLFILE TO '/tmp/control01.ctl'; RELEASE CHANNEL ch1; } To copy archived redo log files use the following command. We can also merge the ARCHIVELOG clause mentioned in the RUN command below, in the RUN command mentioned above. RUN { ALLOCATE CHANNEL ch1 TYPE DISK; COPY ARCHIVELOG '/oradata/test/arch/archive1_1.dbf' TO '/bkup/archive1_1.dbf', ARCHIVELOG '/oradata/test/arch/archive1_2.dbf' TO '/bkup/archive1_2.dbf'', CURRENT CONTROLFILE TO '/tmp/control01.ctl'; RELEASE CHANNEL ch1; } Image copies generated using RMAN COPY command are similar to images copies generated using operating system commands like copy in Windows Server2004/XP/NT and no recovery is needed after restoring the files back from the backed up location.

Page 310: Oracle db ebook

310

There are two types of recoveries that are possible with RMAN. Recovery of Database running under NOARCHIVELOG mode and the other one is the Recovery of Database running under ARCIVELOG mode. Recovering the database running under NOARCHIVE mode is very simple. All you have to make sure that the target database is first shunt down NORMAL, IMMEDIATE or TRANSACTIONAL. Then start the instance with database mounted on it by first connecting as SYSDBA. Then run RMAN and issue the RESTORE DATABASE command. Since the database was running under NOARCHIVELOG mode that why no recovery is need. The following RMAN command can be used to restore the

Key Concepts

1) No recovery of database is need if it was running under NOARCHIVELOG mode.

2) Two key statements for

RMAN recovery process are RESTORE DATABASE and RECOVER DATABASE.

3) For the database in

ARCIVELOG mode, we have to first restore the database files and then start the recovery process.

4) In the recovery process, all the redo logs will get applied to the database.

Page 311: Oracle db ebook

311

database. RMAN> RUN { ALLOCATE CHANNEL ch1 TYPE DISK; RESTORE DATABASE; } This command will restore all the database files from the backed up location to original position. Once the restore process complete we can open the database using the following statement. ALTER DATABASE OPEN; If the database was running under ARCHIVELOG mode then first we have restore the database files and then issue the RECOVER DATABASE statement so that all the redo logs will be applied to the data files. Here “redo logs applied” means that all those redo logs that were archived by the archiver process. The database has to be in MOUNT state. Then start and connect the RMAN with the target database. The following would be used at the RMAN prompt to recover the database that was running under ARCHIVELOG mode before the failure detected. RMAN> RUN { ALLOCATE CHANNEL ch1 TYPE DISK; RESTORE DATABASE; RECOVER DATABASE; }

Page 312: Oracle db ebook

312

Once the recover process complete, open the database using the following statement. ALTER DATABASE OPEN; It’s recommended that at this point shutdown the database normal and reopens it to make sure that the restore process completed successfully. It everything went well the database will open with no error.

Page 313: Oracle db ebook

313

When the database was running under ARCHIVELOG mode all the redo log files archived before they gets over written. If something wrong happens to the database then we first have to restore the files and then use the RECOVER DATABASE statement to inform Oracle to start applying all the redo log files including those archived. The purpose of Redo Log files is now clear that it helps Oracle in bringing the database to the last committed transaction before the time failure occurred otherwise if there would not be any Redo Logs then would have recovered the database to the last good backup and things or in other words transactions that took place after the backup would then be lost. If you use just RECOVER DATABASE, then this is considered as complete

Key Concepts

1) All the redo log files including the ones got archived will get applied to the database when RECOVER DATABAE statement is executed.

2) We can stop the process

of recovery in the middle by using either of the clauses i.e. UNTIL CHANGE, UNTIL CANCEL or UNTIL TIME with the RECOVER DATABASE statement.

3) A complete back must be

performed after incomplete recovery.

Page 314: Oracle db ebook

314

recovery where as if we use the clause UNTIL TIME/UNTIL CANCEL/UNTIL CHANGE with the RECOVER DATABASE statement then we can stop the process of recovery in the middle. This concept applies to both User-Managed and RMAN based. After any type of incomplete recovery, a complete database backup must be performed. In case of time based recovery we can stop the recovery process at any point in time after the last backup but before the time of failure. So instead of just using RECOVER DATABASE, add the UNTILL TIME clause to it as shown below. RECOVER DATABASE UNTIL TIME ‘2004-2-15 11:00:00’; The complete name of such type of recovery is “User-Managed Time-Based Incomplete Recovery” or simple “Time Based” recovery. In “Cancel-Based” recovery, you Oracle will ask you each time its going to apply the redo log file whether to apply it or not. It’s more like a yes/no question. If you press the Enter key, the redo log will get applied and Oracle will ask you again regarding the next redo log file i.e. whether you want to apply the redo log file or not. If you press Enter, Oracle will apply that redo log file again. This process is repeated again and again unless you enter the CANCEL command or all the redo log gets applied. If you respond with the CANCEL command then Oracle will not apply that redo log file and will

4) Each transaction in Oracle has been assigned a unique number called SCN or System Change Number.

5) After performing

incomplete recovery of any type, the database would then be opened with the RESETLOGS clause.

Page 315: Oracle db ebook

315

stop the recovery process. The statement used to perform such type of recovery is as follows. RECOVER DATABASE UNTIL CANCEL; The last case under User-Managed Incomplete recovery is “Changed-Based Recovery”. The word “Change” means SCN or System Change Number. SCN is the unique number that gets written to the header of data files, control files and redo log files. It gets written whenever Checkpoint happens. Oracle assigns each committed transaction a unique identification number called SCN. So Change Based recovery gives us the most control. We can stop the recovery process at any SCN. Remember that this SCN should be after the last back but before the point of failure. The statement used to perform such type of recovery is as follows. RECOVER DATABASE UNTIL CHANCE 64575343; After performing any type of incomplete recovery the database must be opened with the RESETLOG clause and then back up the complete database. ALTER DATABASE RESETLOGS; RESETLOGS clause makes sure that redo logs applied in the recovery process will never be used by resetting the log sequence and rebuilding the redo log system. The next step would be to perform complete database backup.

Page 316: Oracle db ebook

316

The information about the SCN can be acquired by querying the V$LOG_HISTORY dynamic performance view.

Page 317: Oracle db ebook

317

The command used to perform RMAN “Time-Based” Incomplete Recovery is as follows. RMAN> RUN { ALLOCATE CHANNEL ch1 TYPE DISK; SET UNTIL TIME ‘2003-3-4 11:00:00’; RESTORE DATABASE; RECOVER DATABASE; } For “Change-Based” Incomplete Recovery use the following command at the RMAN prompt. The information about the SCN can be acquired by querying the V$LOG_HISTORY dynamic performance view.

Key Concepts

1) After any type of incomplete recovery, open the database using the RESETLOGS clause.

2) After any type of

incomplete recovery, complete database backup is required.

Page 318: Oracle db ebook

318

RMAN> RUN { ALLOCATE CHANNEL ch1 TYPE DISK; SET UNTIL SCN=6563452; RESTORE DATABASE; RECOVER DATABASE; } After the completion of any type of RMAN Incomplete Recovery process, open the database using RESETLOGS clause as shown below. ALTER DATABASE OPEN RESETLOGS; RESETLOGS clause makes sure that redo logs applied in the recovery process will never be used by resetting the log sequence and rebuilding the redo log system. The next step would be to take the complete database backup using one of the techniques discussed earlier in this chapter.

Page 319: Oracle db ebook

319

We can perform logical backup and recovery using Oracle’s Export and Import utilities. The major benefits of this import/export are logical backup/recovery, moving data from one Oracle database to another Oracle database and reorganization of database by using COMPRESS=Y. We export table or complete schema (i.e. all the objects within the schema like tables, views, sequences etc.) to an external binary file called Dump file. The dump file is having extension of .dmp and a default name of expdata.dmp. As it’s a binary file so it is not possible to read/understand that file. We have to use the Import utility to dump this file into any Oracle database.

Key Concepts

1) We can take the backup of table or complete schema using Export utility.

2) We cannot only recover

the same table back into the database but we can also import that table into some other Oracle database.

3) Export/Import utilities

are only for Oracle databases and not for any other database.

Page 320: Oracle db ebook

320

There are two ways to do export of data from Oracle database using Export utility. One is called the “Conventional Path Export” and other is called the “Direct Path Export”. Conventional path export is the default mode of the Export utility where as Direct path export can be achieved by setting DIRECT=Y. Conventional path export takes more time for large amount of data being transferred then the direct path export but it works fine for everyday uses. In this method data is extracted by Export utility from the database using SQL statement (SELECT * FROM emp) and the result is dumped into the Database Buffer Cache. From Buffer Cache, the data then gets evaluated in the evaluating buffer then it gets dumped into the external binary dump file. In Direct path export there is no “evaluation of data” step involved and the data gets transferred to the external dump file from Database Buffer Cache. This elimination of one step results in speedy transfer. There are three ways we can run the Export utility. One is in the “Interactive Mode”, second is the “Command-line” and the third is by using parameter file i.e. PARFILE. PARFILE method is basically used for nightly or scheduled or automatic jobs to do the logical backup of data. In Command line way, we describe all the parameters to run Export utility in a

4) There are three ways to perform Export

o Interactive o Command-Line o PARFILE

5) Import utility works the

same way as export the only difference is that it imports the data exported earlier by Export utility into any Oracle database.

6) This Export/Import is

not applicable to any non-Oracle database.

Page 321: Oracle db ebook

321

single command where as in the Interactive mode, the Export utility ask questions to get input from the user regarding the parameters required to extract data. Interactive Export: Step 1: Set ORACLE_SID Set the ORACLE_SID to the name of the Oracle Instance with which you want the Export utility to communicate to extract the information. Step 2: Run & provide your Oracle credentials to Export utility You must be having this utility available under $ORACLE_HOME/bin directory having the name exp. At the Unix or Windows NT/Server/XP prompt write exp. C:\> exp Then provide your Oracle credentials i.e. login/password that you use to connect to Oracle database. Actually Export utility will use your credentials to make a connection with the Oracle database whose SID you have assigned to ORACLE_SID operating system environment variable in step 1. Username: system Password: manager

Page 322: Oracle db ebook

322

Step 3: Answer questions asked by Export Utility After getting your credentials (login/password) Export utility will ask you certain question before exporting data out to external dump file. The first question would be the buffer size you want to use to facilitate the fetching of data. The default size would be 4096K. If you want to stick with this default size then simply press Enter key. Enter array fetch buffer size: 4096> Then the Export utility will ask for the external dump file name. Provide the full qualified name including the path where you want to store this file. The extension of this dump file should be .dmp whereas the file name could be any name, in our case we are using the file name same as that of table name we want to export i.e. emp table. The default name of dump file is expdat.dmp. If you want to stick with this press the Enter key. Export file: expdat.dmp> emp.dmp Next question will ask for Users or Tables. If you want to export table then select the table option whereas if we want to export the complete schema i.e. all the object under the user account then select the user option. By default its users. (2) U(sers), or (3)T(ables): (2)U> 3 Then the next prompt would be to have extents compressed by default its yes.

Page 323: Oracle db ebook

323

Compress extents (yes/no): y Lastly, it will ask for the table or partition that needs to be exported. Table or Partition to be exported> emp With the answer to this question, the export utility will start exporting emp data out from Oracle in the binary dump file and once done it will terminates with the success and you will be back on the OS prompt. PARFILE Export: First set the ORACLE_SID operating system environment variable then create a separate text file having all the parameters lined up as follows and then run Export utility specifying PARFILE parameter. > exp system/manager parfile=param.dat Where param.dat file contains the following information: DIRECT=Y FILE=expdat.dmp TABLES=(scott.emp,blake.dept) GRANTS=y INDEXES=y

Page 324: Oracle db ebook

324

Command-Line Export: The Export utility can be used in the command line as follows where we provide all the parameters in the single command. Before running this command make sure you set the ORACLE_SID operating system environment variable. > exp system/manager tables=(emp, dept) grants=Y indexes=Y direct=y You can also get a detailed help on Export utility by running the following command at the OS prompt. >exp –help The Import utility is located under ORACLE_HOME/bin directory in Unix and ORACLE_HOME\bin directory in Windows NT/XP/Server 2003. The utility comes with a detailed help too and can be accessed by running the following command at the operating system prompt. >imp –help With the help of Import utility we can import any previously exported logical objects using Export utility in any Oracle database. The step by step process of importing any .dmp file into the Oracle database is given below. Step 1: Set ORACLE_SID Make sure that ORACLE_SID is set to the SID of Oracle database to which you want to import the file.

Page 325: Oracle db ebook

325

Step 2: Run & provide your Oracle credentials to Import utility The Import utility is located under ORACLE_HOME/bin directory and can be executed simple by writing imp. Then supply your Oracle username/password. The Import utility will use your credentials to make a connection with the Oracle database and will import the data from external binary file to your schema. Username: system Password: manager Step 3: Answer questions asked by Import Utility Following questions will be asked by import utility and most of them are self-explanatory. The very first prompt would be the name of file you would like to import into your Oracle schema. Over here provide the full qualified dump file name i.e. file name as well as path where it is located. Import file: expdata.dmp> emp.dmp Similarly as with Export utility, the Import utility will ask for the buffer size. We are choosing 8K the minimum one. Enter insert buffer size (minimum is 8192) 30720 > 8192 Next, the utility asks for whether to list the contents of the dump file or not. The default is no, which will do the import without listing the contents. Choose the default i.e. no.

Page 326: Oracle db ebook

326

List contents of import file only (yes/no): no> n Next prompt would be as follows and it asks for whether to ignore the CREATE ERROR if the object already exits. Choose yes. Ignore create error due to object existence (yes/no): no> y The next question as for whether to import grants related to object that we are trying to import. Import grants (yes/no): yes > y Next prompt would be to import the data in the table instead of just table structure. Choose yes. Import table data (yes/no): yes > y Lastly, it will prompt for whether you would like to import the entire file or not. Choose yes. Import entire export file (yes/no): no> y Once you finishing answering the last question the import process will start and the utility will notify you as it completes. Once done the utility terminates and bring you back to your OS prompt. Then you can query the table just got imported to your database just to make sure that everything is in perfect shape.

Page 327: Oracle db ebook

327

We Did It!

In this chapter we learnt in great detail different methods of doing backup and recovery. We looked into each and every method and provided the steps of implementation for each method. There are basically three ways to do back and recovery of Oracle database. One is called User Managed backup and recovery, second one is called RMAN based backup and recovery and lastly Logical backup and recovery.

Page 328: Oracle db ebook

328

Page 329: Oracle db ebook

329

CChhaapptteerr 1111

Oracle Performance Tuning

• Alert Log Files • Trace Files

o Background Trace Files o Event Trace File o Users Trace File

• Dynamic Performance Views$ • Data Dictionary Views • Oracle Supplied Utilities

o UTLBSTAT.SQL/UTLESTAT.SQL o STATSPACK

Page 330: Oracle db ebook

330

Page 331: Oracle db ebook

331

Oracle Performance Tuning, the topic is not that scary as it sounds. Actually it’s very simple if you understand the performance tuning concept –the bigger picture or in other words bird’s eye view. Performance tuning is nothing but the collection of statistics regarding the current status of the system and then change system values based on the statistics to enhance the performance. In other words performance tuning is

• Collection of system statistics • Changing system based on the

statistics Now we have to look into what are different sources that can provide us the information regarding the current health of the system. Although there are a lot of

Key Concepts

1) Performance tuning is nothing but a two step process.

o Collection of system

statistics o Changing system

based on statistics

2) Two major sources of system statistics collection are following Oracle supplied utilities.

o UTLBSTAT.SQL/

UTLESTAT.SQL o STATSPACK

Page 332: Oracle db ebook

332

sources and we look into each one by one but the two most important one are as follows.

• UTLBSTAT.SQL/ UTLESTAT.SQL

• STATSPACK We will talk about these two in more detail when we come to that point. Besides these two, the other sources, which can provide us the Oracle system information, are alert log file, trace files, dynamic performance view, data dictionary views etc. Let’s start learning about each of these sources one by one. Alert Log File: The location of Alert log file is described by BACKGROUND_DUMP_DEST initialization parameter. For Oracle systems that follow OFA (Oracle Flexible Architecture) or in other words standard directory architecture of Oracle files, the location of Alter Log file will be as follows. In Windows Server 2003/NT/XP, %ORACLE_BASE%\admin\SID\bdump whereas in Unix it would be $ORACLE_BASE/admin/SID/bdump. Where SID is the name of the Oracle Instance. You might have noticed that in Unix environment we use “forward slash” (/) whereas in Windows we user “backward slash” (\). Oracle writes all the error messages and other alerts to this file whenever that

3) Don't shut down the database in between the running of UTLBSTAT and UTLESTAT otherwise, the results will be invalid.

4) Do not use SYSTEM

tablespace as default tablespace for PERFSTAT schema.

5) STATSPACK will take

you one step further into the performance tuning of the Oracle system.

6) With STATSPACK

snapshots of database performance can be collected through two different ways. Once is by using STATSPACK.SNAP procedure and other by using SPAUTO.SQL

7) SPAUTO.SQL way is

called Automatic Technique whereas STATSPACK.SNAP way is called Manual technique.

Page 333: Oracle db ebook

333

message occurs during it normal operations. Background Process, Event & User Trace Files Oracle trace files contain information pertaining to certain event that happens in the regular operation of Oracle. The location of these trace files is described by BACKGROUND_DUMP_DEST initialization parameter. For Oracle systems that follow OFA (Oracle Flexible Architecture) or in other words standard directory architecture of Oracle files, the location of these trace files will be as follows. In Windows Server 2003/NT/XP, %ORACLE_BASE%\admin\SID\bdump whereas in Unix it would be $ORACLE_BASE/admin/SID/bdump. Where SID is the name of the Oracle Instance. All trace files have file extension of .trc no matter whether it is Background process, Event or User trace file. In Windows environment, the background processes will generate the following files. Process Monitor (PMON): sidPMON.trc System Monitor (SMON): sidSMON.trc Database Writer (DBW0): sidDBW0.trc Archive Process (ARC0): sidARC0.trc Log Writer (LGWR): sidLGWR.tc

Page 334: Oracle db ebook

334

If we have two Database Writer processes (DBW0, DBW1) then you may see one more trace file sidDBW1.trc besides sidDBWR0.trc, this applies to Archiver Process too. Similarly for Checkpoint Process (CKPT) it is sidCKPT.trc whereas in Unix environment the trace files pertaining to each of these processes would include the name of that process in the file names e.g. for PMON the trace file name would be PMON_nnnn.trc etc. Remember that user trace file will get generated when there is an error in user’s server process which contains the details regarding that error. We can limit the size of User trace file by specifying the MAX_DUMP_FILE_SIZE init.ora parameter e.g. specifying the value of this parameter as follows in init.ora file will the user trace file size to 20MB. MAX_DUMP_FILE_SIZE=20M Dynamic Performance Views (V$) & Data Dictionary Views Dynamic Performance Views or in other words all the views that start with V$ and Data Dictionary View are views created on Oracle base tables. These tables are Oracle Internal tables and are utilized by Oracle. They reside in the SYSTEM tablespace and if you remember SYS is the owner of these tables as these tables get created when we logged in to Oracle as SYS and we ran two scripts which resulted in these Oracle base tables and views.

Page 335: Oracle db ebook

335

Dynamic Performance Views as the name says are dynamic in nature i.e. data inside these tables keeps changing and it depends on the current state of Oracle system. Some of the very important Dynamic Performance Views along with the brief description of the stuff they contain are provided below. V$SGASTAT Contains information about the size of SGA (System Global Area) and the each of each of its major components. V$SESSTAT Contains the statistics information regarding the each connected session or in other words connected user. V$SESSION Contains the current connection information of each session or in other words each connected user. Whereas Data Dictionary Views are static in nature and the values inside these tables change only when you change the structure of the database. Some of the very important Data Dictionary Views along with the brief description of the stuff they contain are provided below. DBA_TABLES Contains information about the tables within the database, their row and block

Page 336: Oracle db ebook

336

information. DBA_DATA_FILES Contains the information about all the data files within the database, their names, size etc. UTLBSTAT.SQL/UTLESTAT.SQL UTLBSTAT.SQL and UTLESTAT.SQL are two scripts provided by Oracle to get a detailed report regarding the complete statistics of Oracle database. It is recommended that before starting this process of generating performace report, Oracle server should be up and running for couple of hours. The location of these script files are provided below. In Windows Server 2003/XP/NT, these two scripts are located in %ORACLE_HOME%\rdbms\admin whereas in Unix, these two scripts are located in $ORACLE_HOME/rdbms/admin The way utilize these scripts is that first we run UTLBSTAT.SQL and then after a significant amount of time we run UTLESTAT.SQL. “B” in UTLBSTAT.SQL represents “Begin” and “E” in UTLESTAT.SQL represents “End”. When Oracle finishes executing the UTLESTAT.SQL script, it will generate a

Page 337: Oracle db ebook

337

detail report in Text format and the name of that file would be REPORT.TXT. Before running make sure you set the init.ora parameter TIME_STATISTICS=True or if you already have spfile instead of pfile then the situation is much simpler, just use the following ALTER SESSION to make this change sustain only for the session or ALTER SYSTEM statement to make this change sustains forever unless you change it to False. When we run UTLBSTAT.SQL, what it does it creates certain temporary tables by queries dynamic performance views (V$) but before creating new tables it makes sure that there should not be any previous same tables. So by the time when you execute this script and you see the error message that “Table or View Does Not Exists” then don’t get worried, it’s normal. After making sure that there should not be any previously created same tables, UTLBSTAT.SQL script then creates the tables where required snapshot of dynamic performance views’ (V$) is retained. After reasonable amount of time run the UTLESTAT.SQL script. Same as that of UTLBSTAT.SQL, UTLESTAT.SQL will first make sure that there are no same tables existing in the Oracle database. So by the time when you execute this script and you see the error message that “Table or View Does Not Exists” then don’t get worried, it’s normal. It then creates series

Page 338: Oracle db ebook

338

of tables to hold the snapshot of the Oracle database for that instance. It then compare the resultant tables generated by UTLBSTAT.SQL with the one generated by UTLESTAT.SQL. The report that will get generated at the end of UTLESTAT.SQL contains the statistics information for the duration when we ran UTLBSTAT.SQL to UTLESTAT.SQL. Just to refresh your memory, you should be connected to Oracle as SYSDBA account. Summary of the concepts explained above are provided below in easy steps. Steps 1: Set ORACLE_SID You should set the ORACLE_SID to the name of the Instance you want to connect to. Remember it’s a operating system environment variable for both Unix and Windows Server 2003/NT/XP operating systems. Step 2: Set TIME_STATISTICS = True Set the TIME_STATICSTICS=True in init.ora file also known as Initialization parameter file. Another name used for this file is PFILE. If you already have created SPFILE from PFILE then first perform step 3 and after connected to Oracle Instance as SYSDBA, use ALTER SESSION statement to set this parameter for the current session or ALTER SYSTEM to make this change permanent. ALTER SESSION SET TIMED_STATICSTICS=True

Page 339: Oracle db ebook

339

Or ALTER SYSTEM SET TIMED_STATICSTICS=True Step 3: Connect / As SYSDBA Connect to Oracle Instance as SYSDBA as explained many times before. Make sure that you have the database in Mounted or Open state. Step 4: Run UTLBSTAT.SQL Run the UTLBSTAT.SQL and don’t worry if you see errors like “Table or View doesn’t exits” as the utility first tries to delete any previously created tables before creating new ones. Step 5: Run UTLESTAT.SQL After a significant amount of time run the UTLESTAT.SQL and don’t worry if you see errors like “Table or View doesn’t exits” as the utility first tries to delete any previously created tables before creating new ones. The significant amount of time could be hour, day, week etc. but make sure that the server never shuts down in between the time when you ran UTLBSTAT.SQL and UTLESTAT.SQL. At the end of execution of UTLESTAT.SQL script, REPORT.TXT file be generated having very detailed information regarding complete Oracle System statistics. Step 6: Open & Analyze REPORT.TXT You can open this report in any text editor and analyze the data. Based on the

Page 340: Oracle db ebook

340

information you have take decisions to enhance the performance. The resulting report.txt file will provide the following information: Shared Pool Statistics System Statistics System Wide Wait Events Dictionary Cache Statistics I/O statistics Latch statistics Rollback Segment Statistics Shared Pool Statistics Following is the section of the report.txt file generated as a result of UTLBSTAT.SQL/UTLESTAT.SQL, which deals with Shared Pool.

Let’s talk about each of these columns one by one. LIBRARY: It’s the particular section of Shared Pool which deals with SQL statements and PL/SQL procedures/blocks. GETS: The total number of requests made for objects in the library between the interval of UTLBSTAT.SQL and UTLESTAT.SQL execution. GETHITRATIO: It should be greater

Page 341: Oracle db ebook

341

than 0.9 for all the records and it’s the percentage of time the requested object was found in the library. PINS: The total number of times a requested object in the cache was executed. PINHITRATIO: It should be greater than 0.9 for all the records and its total number of times (in between the execution of UTLBSTAT.SQL and UTLESTAT.SQL) a requested object being executed is found in the library cache. RELOADS: Ideally it should be zero but not more than 1% of Pins. It’s the total number of times (in between the execution of UTLBSTAT.SQL and UTLESTAT.SQL) a requested object was reloaded in the memory as it got aged out. INVALIDATIONS: It’s the total number of times (in between the execution of UTLBSTAT.SQL and UTLESTAT.SQL) an object in the library became invalid. If you have lots of Reloads and GetHitRatio is less than 0.9 than increase the SHARED_POOL_SIZE initialization parameter. System Statistics Following is the section of the report.txt file generated as a result of UTLBSTAT.SQL/UTLESTAT.SQL, which deals with system statistics.

Page 342: Oracle db ebook

342

Formula for calculating HIT RATIO is as follows. LOGICAL READS = (CONSISTENT GETS) + (DB BLOCK GETS) HIT RATIO = (LOGICAL READS – PHYSICAL READS) / LOGICAL READS Any value of cache HIT RATIO less than 85% is considered unacceptable and can be increased by increasing DB_BLOCK_BUFFERS parameter in initialization parameter file. Background Checkpoints completed and Background Checkpoint started should not be more than one. Redo size is the total amount of Redo information written to redo log files (in between the execution of UTLBSTAT.SQL and

Page 343: Oracle db ebook

343

UTLESTAT.SQL). Formula for calculating Non-Index Lookups Ratio is as follows. Non-Index Lookups Ratio = table scans (long tables) / (table scans (long tables) + table scans (short tables)) Non-Index Lookups Ratio should be as zero ideally and it means whatever columns/fields we are using in the WHERE clause, are all indexed or in other words no one is submitting any SELECT statement to Oracle Instance with using any non-indexed column in the WHERE clause. The formula to figure out the percentage of Disk Sorts or in other words the Sort operation happening in the disks due to lack of RAM or SORT_AREA_SIZE, is given below. %age Disk Sort = Sorts (Disk) / (Sorts(Disks) + Sorts(Memory))

This percentage should not be more than 5% otherwise increase the SORT_AREA_SIZE init.ora parameter. Before doing this makes sure that you have plenty of RAM available. Because if you don’t than even though you increase the size of this parameter OS is going to create a virtual RAM on your hard disk drive. Again the sorts will be performed using the disk. System Wide Wait Events Following is the section of the report.txt file generated as a result of UTLBSTAT.SQL/UTLESTAT.SQL,

Page 344: Oracle db ebook

344

which deals with system wide wait events statistics. A) For Non-Background Processes

B) For Background Processes

Although many of the above mentioned waits are considered normal in regular operation of Oracle system but there are some that you would not want to see. One of the most important ratios that help in tuning the system is Buffer Busy Waits Ratio. The formula to calculate this ratio is given below. Buffer Busy Waits Ratio = (Buffer Busy Waits) / (Logical Reads) The Buffer Busy Waits Ratio should be 0 ideally or as close to zero as possible otherwise you have to tune the DB_BLOCK_BUFFERS initialization parameter. Dictionary Cache Statistics Following is the section of the report.txt file generated as a result of UTLBSTAT.SQL/UTLESTAT.SQL, which deals with data dictionary statistics.

Page 345: Oracle db ebook

345

If GET_MISS to GET_REQS ratio is greater than 10% than it’s a clear indication to increase SHARED_POOL_SIZE. I/O Statistics Following is the section of the report.txt file generated as a result of UTLBSTAT.SQL/UTLESTAT.SQL, which deals with I/O statistics.

READS: It is the total number of physical I/O requests that got generated (in between the execution of UTLBSTAT.SQL and UTLESTAT.SQL). BLKS_READ: It is the total number of blocks that is being read in between the execution of UTLBSTAT.SQL and UTLESTAT.SQL. Keeping in mind these two fields we can determine the factor called “Blocks Per I/O” as follows. Blocks Read Per I/O = BLKS_READ / READS For example if your generated report shows READS = 6000 and the BLK_READ = 40,000 for the SYSTEM tablespace then the Blocks Read Per I/O would be 6.67 (40,000/6000). Then you

Page 346: Oracle db ebook

346

should set DB_FILE_MULTIBLOCK_READ_COUNT equal to 7. This initialization parameter determines how many blocks should be read by Oracle in one I/O. Latch Statistics Following is the section of the report.txt file generated as a result of UTLBSTAT.SQL/UTLESTAT.SQL, which deals with latch statistics. Latch is basically a lock on the memory or in other words memory buffer. There are some processes that can wait to acquire a latch whereas others can’t. Processes that are willing to wait to acquire latch go into sleep mode and wait for the latch to become free.

Page 347: Oracle db ebook

347

A) Willing To Wait

B) Not Willing To Wait

A low HIT_RATIO for a given latch shows clearly problem areas. Our goal should be to have HIT_RATIO for all latches not less than 0.99. Rollback Segment Statistics Following is the section of the report.txt file generated as a result of UTLBSTAT.SQL/UTLESTAT.SQL, which deals with rollback segment statistics.

Page 348: Oracle db ebook

348

The waits-to-gets ratio should not be higher than 5%. If it does consider adding rollback segments. waits-to-gets = TRANS_TBL_WAITS / TRANS_TBL_GETS Besides the information discussed above, there are other stuff available in report.txt file e.g., the queries UTLESTAT.SQL used to generate these statistics, locations and utilization of data files, data files and their associated with tablespace etc. STATSPACK STATSPACK is another tool for the collection of system statistics. Its improved version of UTLBSTAT.SQL/UTLESTAT.SQL in a sense that we can take the statistics between over desired any two interval in time. On the contrary to UTLBSTAT.SQL/UTLESTAT.SQL where UTLESTAT.SQL deletes all the tables and statistics after generating the report, in STATSPACK all the statistics stays there and its totally up to you make a which two snapshots analysis you want in your report. Setting up STATSPACK Before you start using STATSPACK to capture the snapshot of your database performance, you have to create the PERFSTAT schema that owns all the STATSPACK PL/SQL packages, tables and indexes.

Page 349: Oracle db ebook

349

How to create PERFSTAT schema First connect to Oracle database as SYSDBA as discussed and shown many times before in the book. Execute the SPCREATE.SQL script. This script will create all the objects required by STATSPACK under the PERFSTAT schema. During the execution process you will be asked for default and temporary tablespace for the schema. It is recommended to have a separate tablespace for PERFSTAT. Initially it requires a tablespace of 80MB but its better to have one created for 300MB size. The password for PERFSTAT is PERFSTAT and it should be changed as soon as login. SPCREATE.SQL script is located in the following folder under Windows Server 2003/XP/2000. %ORACLE_HOME%\rdbms\admin\ Whereas in Unix\Linux operating system its located under following directory. $ORACLE_HOME/rdbms/admin/ The SPCREATE.SQL script generates three log files viz., SPCUSR.LIS, SPCTAB.LIS and SPCPKG.LIS which should be examined at the end of execution of script just to make sure everything went well smoothly.

Page 350: Oracle db ebook

350

Methods to collect statistics Once we have STATSPACK configured as described in the above article then step would be to collect the statistics. There are two options. Either to collect the statistics manually or do it automatically. Manual Technique Manual technique requires the execution of STATSPACK.SNAP procedure whereas the automatic technique uses SPAUTO.SQL scheduling script. Lets discuss both of these techniques one by one. STATSPACK.SNAP procedure can be executed by first logging in as PERFSTAT and then executing this procedure at the SQL prompt as shown below. SQL> execute statspack.snap Each time you execute this procedure, a snapshot of the database current performance statistics gets saved in the tables under PERFSTAT schema. Unlike UTLBSTAT.SQL/UTLESTAT.SQL, snapshots are not deleted and report can be generated for the statistics collected between any two captured database snapshots using SPREPORT.SQL script located under the following folder under Windows Server 2003/XP/2000. %ORACLE_HOME%\rdbms\admin\

Page 351: Oracle db ebook

351

Whereas in Unix/Linux its located under the following directory. $ORACLE_HOME/rdbms/admin/ Once you run this script it will first display the taken snapshots each with an ID associated with it. Then it will ask for the following question. Enter the value for begin_snap: Against this question spypply the snapshot id number then it will ask for end_snap id and finally it ask for report name. Provide the name and location for the report file. Automatic Technique For scheduling of snapshot collection regarding Oracle system statistics, we use the SPAUTO.SQL script. This script is located under the following folder in Windows 2003/2000/XP. %ORACLE_HOME%\rdbms\admin\ Whereas in Unix/Linux its located under the following directory. $ORACLE_HOME/rdbms/admin/ With scheduling power we can automate the collection of snapshots at regular interval of time. That interval of time could be from days, weeks to even months. The SPAUTO.SQL script basically uses the DBMS_JOBS package to do this kind of scheduling. For this scheduling thing to work set the

Page 352: Oracle db ebook

352

JOB_QUEUE_PROCESSES to a non-zero value. This initialization parameter tells Oracle how many background processes to use to handle DBMS_JOBS scheduled jobs. You can easily end the SPAUTO.SQL script file to edit the interval between two snapshots provide you have a knowledge of SQL. The method of generating the report is exactly the same as described with the Manual technique above i.e. using spreport.sql script.

Page 353: Oracle db ebook

353

We Did It!

In this chapter we learn about basically the different sources of collection of Oracle system statistics. Out of many two are widely used. One is called the UTLBSTAT.SQL/UTLESTAT.SQL and the other one is STATSPACK. We have discussed both of these methods in this chapter in great detail besides others. The report generated at the end, helps in identifying problem areas to be addressed. In short, Oracle performance tuning is basically collection of system statistics and then changing the parameters of the system accordingly to gain performance.

Page 354: Oracle db ebook

354

Page 355: Oracle db ebook

355

TThhee BBooookk EExxttrraacctt

The complete extract of the whole book is described in this section. Slides mentioned here are actually the extracts of whole Oracle subject. The summary of tools/techniques learnt to implement different Oracle tasks is also described at the end.

Summaries like this can really help in consolidating and lining up the knowledge in the brain so to make things further clear.

Page 356: Oracle db ebook

356

Page 357: Oracle db ebook

357

Page 358: Oracle db ebook

358

Page 359: Oracle db ebook

359

Page 360: Oracle db ebook

360

Page 361: Oracle db ebook

361

Page 362: Oracle db ebook

362

Page 363: Oracle db ebook

363

Page 364: Oracle db ebook

364

Page 365: Oracle db ebook

365

Page 366: Oracle db ebook

366

Techniques/Tools Learnt in this book are as follows: To Develop Oracle Database (including Instance)

1. DBCA (Database Configuration Assistant) 2. Manual Technique

To Develop Oracle Network

1. ONM (Oracle Network Manager) To Manage Oracle Database (Local/Remote)

1. OEM (Oracle Enterprise Manger) 2. Manual

Oracle Database Backup & Recovery

1. RMAN 2. User-Managed 3. Logical (Import/Export)

Oracle Database Performance Tuning

1. Alter Log File 2. Trace Files 3. Data-Dictionary Views 4. Dynamic Performance Views (V$) 5. UTLBSTAT.SQL/UTLESTAT.SQL 6. STATSPACK

Page 367: Oracle db ebook

367

Free Oracle Guidance/Support

Ask Asim Oracle related questions on "Oracle

Made Simple" Google group.

Oracle Made Simple

Browse Archives at groups.google.com You can also request for soft copy (PDF Version) of the

book. The PDF version contains all the pictures/slides in

color.

Page 368: Oracle db ebook

368

Page 369: Oracle db ebook

369

IINNDDEEXX

%

%age Disk Sort · 331

A

ABS · 40 ADD_MONTHS · 40 Aladdin · 13 Alert log · 331 ALTER DATABASE · 271 ALTER INDEX · 88 ALTER SEQUENCE · 88 ALTER SYSTEM · 198 ALTER TABLE · 74 ALTER TABLESPACE · 269, See ANY/SOME · 34 ARCHIVE_LOG_DEST · 215 Archived Redo Log Files · 111 ARCHIVELOG · 195, 301 ARCn · 142, 149 ASCII · 40 AVG · 40

B

background processes · 129 BACKGROUND_DUMP_DEST ·

331 Backup & Recovery · 293 Backup Sets · 304 BFILE · 69 BLOB · 69 broadband · 174 Buffer Busy Waits · 331

C

Cartesian Product · 60 CEIL · 40 CHAR · 69 CHARACTER SET · 215 CHR · 40 CKPT · 149 CLOB · 69 CONCAT · 40 connect · 215 Connect Sting · 233 CONNECT TARGET · 304 CONSTRAINT · 76 Control file · 16 Control File · 111 CONTROL_FILES · 215 COUNT · 40 CREATE DATABASE · 215 CREATE INDEX · 88 CREATE PROFILE · 96 CREATE ROLE · 96 CREATE SEQUENCE · 88 CREATE SYNONYM · 88 CREATE TABLE · 69 CREATE USER · 96 CREATE VIEW · 88

D

Data Consistency · 84 Data Dictionary Cache · 136 Data files · 16 Data Files · 111 Data Integrity · 84 Data Types · 69 Data Warehouse · 190 Database · 18 Database Buffer Cache · 136 Database Service · 246 DATE · 69

Page 370: Oracle db ebook

370

DB_BLOCK_SIZE · 115, 215 DB_KEEP_CACHE_SIZE · 136 DB_RECYCLE_CACHE_SIZE · 136 DBA_DATA_FILES · 331 DBA_TABLES · 331 dbca · 186 DBCA · 169, 174, 181 DBWn · 149 DCL · 27 DDL · 27 Dedicated Server · 149 Dedicated Server mode · 142 DEFAULT · 136 Dirty Buffers · 136 Dispatcher · 157 Division · 34 DML · 27 Dnnn · 149 DQL · 27 DROP INDEX · 88 DROP SEQUENCE · 88 DROP SYNONYM · 88 DROP TABLESPCE · 271 DUAL · 40

E

Enterprise Edition · 171 Equijoin · 63 EXISTS · 34 Export · 319

F

FLOOR · 40 FOREIGN KEY · 76 Free Buffers · 136 Functions · 40

G

General Purpose · 190 Genie · 13 Genie’s Notebook · 109 GET_MISS · 331 Global Database Name · 191 GRANT · 96 GUI · 208

H

HIT RATIO · 331 Host Naming · 233 Human brain · 131

I

Image Copies · 304 Import · 319 IN · 34 init.ora · 111 INITCAP · 40 INSERT · 67 Instance · 13, 18 INSTR · 40 INSTRB · 40

J

Java Pool · 136 JOB_QUEUE_PROCESSES · 331 Joins · 60

K

KEEP · 136

L

LAN · 233 Large Pool · 136 LARGE_POOL_SIZE · 136 LAST_DAY · 40 Latch · 331 LCKn · 149 LD_LIBRARY_PATH · 214 LENGTH · 40 LENGTHB · 40 LGWR · 142, 149 Library Cache · 136 licenses · 171 LIKE · 34 listener.ora · 233 Local Naming · 233 LOGICAL READS · 331 LONG · 69 LONG RAW · 69

Page 371: Oracle db ebook

371

LOWER · 40 LPAD · 40 lsnrctl · 248 LTRIM · 40

M

MAX · 40 MAX_DUMP_FILE_SIZE · 331 MAX_SHARED_SERVERS. · 149 MAXDATAFILES · 215 MAXINSTANCES · 215 MAXLOGFILES · 215 MAXLOGHISTORY · 215 memory architecture · 129 MIN · 40 MOD · 40 MONTHS_BETWEEN · 40 Multiplexing · 123 Multiplication · 34

N

National Character Set · 196 Net Service Name · 233 Network Manger · 237 NOARCHIVELOG · 195, 301 NOCATALOG · 304 NOT NULL · 76 NOVALIDATE · 76 NULL · 34 NUMBER · 69 NVL · 40

O

Object Privileges · 96 OEM · 208, 283 oemapp · 239 OFA · 169, 198 OLTP · 215 OMF · 169, 198 ONCA · 242 ONM · 237 ORA_NLS33 · 214 Oracle Database · 109 Oracle Enterprise Manager · 208 Oracle Instance · 109, 129 Oracle Internet Directory · 233 Oracle Net · 233

Oracle Universal Installer · 174 ORACLE_BASE · 183 ORACLE_HOME · 183 ORACLE_SID · 183 OracleRAC · 136 organizational chart · 164 OSDBA · 215 OUI · 174 Outer Join · 65

P

PATH · 183 PERFSTAT · 331 pfile · 198 PGA · 136 physical architecture · 117 Ping · 237 Pinned Buffers · 136 PMON · 142 port · 244 PRIMARY KEY · 76 protocol · 233

Q

QMNn · 149

R

RAM · 131 RAW · 69 RECO · 149 RECOVER DATABASE · 313 RECOVER DATAFILE · 301 Recovery · 293 RECYCLE · 136 Redo Log Buffer Cache · 136 Redo log files · 16 Redo Log Files · 111 redo log group · 123 REPLACE · 40 REPORT.TXT · 331 Restaurant · 149 REVOKE · 96 RMAN Based · 293 ROUND · 40 RPAD · 40 RTRIM · 40 RUN · 304

Page 372: Oracle db ebook

372

S

Segments · 119 SELECT · 32 Self Join · 60 Service Naming · 252 Services · 248 Session · 18 SGA · 136 Shared Memory · 136 Shared Pool · 136 Shared Server · 142 Shared Server Mode · 149 SHARED_POOL_SIZE · 136 SHARED_SERVERS · 149 SHUTDOWN · 215 SID · 191 SIGN · 40 SMON · 142, 149 SOUNDEX · 40 SPAUTO.SQL · 331 spfile · 198 SQL*Net · 233 sqlplus · 215 SQRT · 40 stack space · 136 Standard Edition · 171 STARTUP FORCE · 215 STARTUP MOUNT · 215 STARTUP NOMOUNT · 215 STARTUP OPEN · 215 STARTUP RECOVER · 215 STARTUP RESTRICT · 215 STATSPACK · 331 STATSPACK.SNAP · 331 Sub-Query · 58 SUBSTR · 40 SUBSTRB · 40 SUM · 40 SYS · 207 SYSDATE · 40 SYSDBA · 96, 215 SYSOPER · 96, 215 SYSTEM · 207 System privileges · 96

T

Tablespace · 121 TCL · 27 TCP/IP · 233 TIME_STATISTICS · 331 tnsname.ora · 256 tnsnames.ora · 233 TO_CHAR · 40 TO_DATE · 40 TO_NUMBER · 40 trace files · 331 TRIM · 40 TRUNC · 40

U

UNDO_MANGEMENT · 215 UNDO_TABLESPACE · 215 UNIQUE · 76 UPDATE · 67 UPPER · 40 User Managed · 293 UTLBSTAT.SQL · 331 UTLESTAT.SQL · 331

V

V$LOG_HISTORY · 313, 317 V$SESSION · 331 V$SGASTAT · 331 VALIDATE · 76 VARCHAR2 · 69 Views · 88 VSIZE · 40

W

waits-to-gets · 331 WAN · 233 WHERE · 34

Page 373: Oracle db ebook

373

Page 374: Oracle db ebook

374

Page 375: Oracle db ebook

375

Page 376: Oracle db ebook

376

Page 377: Oracle db ebook

377

One of Mr. Abbasi’s research areas is to realize the logic and to

improve certain processes that happens and we take them for

granted and to make its relationship with the Supreme Creator or

the originator of those processes to eventually break down

(analyze) His mode of thinking and taking actions so to unfold

the code behind His wonderful “Unattended Automated System”

called Nature…

Page 378: Oracle db ebook

378

ISBN: 0-9770739-2-0

Buy from

www.amazon.com or www.lulu.com