Saturday, September 29, 2012

Reset the Oracle SYSTEM password

Scenario: Forgot the SYSTEM password for the ORACLE database installed in your computer.

Solution: You can reset it by logging in to Windows as Administrator and connecting to Oracle as sysdba.


Steps:

  • Log in to the Windows Server that is running Oracle as Administrator.
  • Start SQL*Plus

  • At the login prompt, in the user-name field, type ”/as sysdba”, and press enter

  • You are now connected as system with full DBA rights.

  • Reset the password of the SYSTEM  with below query
alter user system identified by NewPassword;
  • enter
“User altered.”
 
Now you can login with your new password.

Tuesday, June 26, 2012

Find Table by providing the Field or Column Name

You can use below statement to look into those objects to which you have access

select * from user_tab_columns where column_name=''

else to check in those objects to which you dont have access as well, you can use the below queries

select * from all_tab_columns

select * from dba_tab_columns

Monday, May 28, 2012

How to append path to Environment variable in Linux

The current value set to my environment variable PATH is

oracle@mymachine currentdirctory]$ echo $PATH
/home/oracle/Program/jdk1.6.0_21/bin:/home/oracle/app/oracle/product/11.1.0/client_1/bin:.:/home/oracle/Program/jdk1.6.0_21/bin:/home/oracle/app/oracle/product/11.1.0/client_1/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/oracle/bin
Add new enviroment variable and set the path
[oracle@mymachine currentdirctory]$ export ANT_HOME=/home/oracle/myproject/apache-ant-1.8.4

Add the above path to PATH variable
[oracle@mymachine currentdirctory]$ export PATH=${PATH}:${ANT_HOME}/bin

[oracle@mymachine currentdirctory]$ echo $PATH
/home/oracle/Program/jdk1.6.0_21/bin:/home/oracle/app/oracle/product/11.1.0/client_1/bin:.:/home/oracle/Program/jdk1.6.0_21/bin:/home/oracle/app/oracle/product/11.1.0/client_1/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/oracle/bin:/home/oracle/myproject/apache-ant-1.8.4/bin

You can see that the environmet path is appended.

Thursday, May 17, 2012

Determine the port number for Database Control

On Linux and UNIX systems:

You can check the file in below metioned path
 Oracle_home/install/portlist.ini

On Microsoft Windows systems:
This can be checked from the Database Control Properties window.



Go to the Start menu, navigate to the Database Control entry in the Oracle home folder, then right-click this entry and select Properties.

Setting the GLOBAL_NAMES Initialization Parameter

1. Log in to Enterprise Manager as an administrative user who can change initialization parameters.
See "Access oracle enterprise manager home page"
2. Go to the Database Home page for the database instance.
 
3. Click Server to open the Server subpage.

4. Click Initialization Parameters in the Database Configuration section.

5. If you are using a server parameter file, then click SPFile. Otherwise, proceed to the next step.

6. On the Initialization Parameters page, enter GLOBAL_NAMES in the search tool.

7. Set the appropriate value.

Access Oracle Enterprise Manager Home Page

Accessing the Database Home Page

The Database Home page is the main database management page in Oracle Enterprise Manager Database Control (Database Control).
To access the Database Home page:
  1. Ensure that the dbconsole process is running on the database host computer.
  2. In your Web browser, enter the following URL:
    https://hostname:portnumber/em
    
    For example, if you installed the database on a host computer named computer.example.com, and the installer indicated that your Enterprise Manager Console HTTP port number is 1158, enter the following URL:
    https://comp42.example.com:1158/em
  1. When you access Database Control, if the database is running, it displays the Login page. If the database is down and needs to be restarted, Database Control displays the Startup/Shutdown and Perform Recovery page.

Monday, May 14, 2012

CREATE SCHEMA in Oracle Database


Oracle Database automatically creates a schema when you create a user.

Use the CREATE USER statement to create and configure a database user.


PREREQUISITES:
You must have the CREATE USER system privilege. For the user to login to the Oracle Database, must have the CREATE SESSION system privilege. Therefore, after creating a user, you should grant the user at least the CREATE SESSION system privilege.

EXAMPLE:

CREATE


USER DEVIDENTIFIED BY DEVDEFAULT TABLESPACE DATATEMPORARY TABLESPACE TEMPPROFILE DEFAULT

DEFAULT TABLESPACE Clause:

Specify the default tablespace for objects that the user creates. If you omit this clause, then the user's objects are stored in the database default tablespace. If no default tablespace has been specified for the database, then the user's objects are stored in the SYSTEM tablespace.

TEMPORARY TABLESPACE Clause :

Specify the tablespace or tablespace group for the user's temporary segments. If you omit this clause, then the user's temporary segments are stored in the database default temporary tablespace or, if none has been specified, in the SYSTEM tablespace.
ACCOUNT UNLOCK;
  • Specify tablespace to indicate the user's temporary tablespace.
  • Specify tablespace_group_name to indicate that the user can save temporary segments in any tablespace in the tablespace group specified by tablespace_group_name.

Wednesday, April 18, 2012

ORA-12514: TNS:listener does not currently know of service requested in connect descriptor

I tried to connect to DB from command prompt as below and recieved ORA-12514 error



ORA-12514:  TNS:listener does not currently know of service requested in connect descriptor

Cause: The listener received a request to establish a connection to a database or other service. The connect descriptor received by the listener specified a service name for a service (usually a database service) that either has not yet dynamically registered with the listener or has not been statically configured for the listener. This may be a temporary condition such as after the listener has started, but before the database instance has registered with the listener.

Action: - Wait a moment and try to connect a second time.
- Check which services are currently known by the listener by executing: lsnrctl services <listener name>
- Check that the SERVICE_NAME parameter in the connect descriptor of the net service name used specifies a service known by the listener.
- If an easy connect naming connect identifier was used, check that the service name specified is a service known by the listener.
- Check for an event in the listener.log file.

Analysis:
This form of the ORA-12541 error commonly happens when the database or the listener processes are in the middle of a startup, or when the database has not been registered with the listener or when the service name you provided in the connection string may be different from the one specified in tnsnames.ora file.

Conclusion:
I found the service name provided in tnsnames.ora file is orcl.mysystem.com and hence tried connecting as below



Result: Succesfully Connected.

Thursday, February 2, 2012

Oracle 11g: Additional Features

Oracle 11g has come up with new functionality which makes many common operations simple and fast. There are tones of newly added features in 11g. Let me take you through few of them.

DDL Wait Option:
You might have come across with the scenario that whenever you try to execute an alter statement on a particular table, instead of getting a success message on your window an error occurs as displayed below

ERROR at line 1:
ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired

This will be irritating most of the time.

The reason behind this error is, some other session is doing some activity on this table and has acquired a lock because of which an exclusive lock request for changing the structure of the table is not provided and hence the error message. The person trying to alter the table has to keep on hitting the alter statement until the next session complete the task and do a commit. This is a painful task. In a large system where the business activity happens very frequently the possibility of having the tables objects unlocked is comparatively less.

Oracle 11g comes up with a solution for this. Set the waiting period for the lock.
 
SQL> alter session set ddl_lock_timeout = 10;

Now, when the DDL statement is submitted, it will wait for 10 sec and in this 10 sec keep on checking for an exclusive lock to do the changes and if not found throws error.

This parameter value can be set at session level and system level as well.
ALTER SYSTEM SET DDL_LOCK_TIMEOUT = 10;

Adding columns with default value:
Most of the time a developer will come across a situation where he/she has to add one additional column to an exiting table having data. Say for example our table has 4 million data. And what if this column should have a NOT NULL constraint?

The only option available is to write the alter statement specifying the default value as below.

SQL> alter table emp add emp_spousename varchar2(20) default 'xxx' NOT NULL;

Now we have a issue here, if we proceed with this statement. Oracle will add the additional column and update default value for all the existing records. This process not only take a long time but fills up the undo segment, generates a large amount of redo and performance issue as well. So we have to go for a down time for the application to complete this task. But not, if we are using 11g.

In Oracle 11g, this particular column will be added but the column values are not updated with the  default value. Hence there is no risk of filling up of undo segment and generating redo files and no performance issue as well. For new records added the default value will be set and for existing records, when a user fires a select query Oracle refers the data dictionary to get info on this column and substitutes the default value.

Virtual Columns:
Scenario: A new col has to be added to a table for which the value is generated based on some business condition.

Here the business don't want to do any changes to the code to populate value into the column. Now only option left to the developer is to create a trigger to populate value into this particular column. This is a bad habit as it has performance impact due to switching context from and into the trigger codes.

Oracle 11 g comes up with a solution for this. Create a virtual column.

When we say virtual columm its really virtual. You have the column created in table but the functionality is different from normal cols.

SQL> create table sales
  2  (
  3     sales_id      number,
  4     cust_id       number,
  5     sales_amt     number,
  6     sale_category varchar2(6)
  7     generated always as
  8     (
  9        case
10           when sales_amt <= 10000 then 'LOW'
11           when sales_amt > 10000 and sales_amt <= 100000 then 'MEDIUM'
12           when sales_amt > 100000 and sales_amt <= 1000000 then 'HIGH'
13           else 'ULTRA'
14        end
15      ) virtual
16  );

Note lines 6-7; the column is specified as "generated always as", meaning the column values are generated at runtime, not stored as part of the table. That clause is followed by how the value is calculated in the elaborate CASE statement. Finally, in line 15,"virtual" is specified to reinforce the fact that this is a virtual column. Now, if you insert some records:
SQL> insert into sales (sales_id, cust_id, sales_amt) values (1,1,100);
 
1 row created.
 
SQL> insert into sales (sales_id, cust_id, sales_amt) values (2,102,1500);
 
1 row created.
 
SQL>insert into sales (sales_id, cust_id, sales_amt) values (3,102,100000);
 
1 row created.
 
SQL> commit;
 
Commit complete.
 
SQL> select * from sales;
 
SALES_ID   CUST_ID  SALES_AMT SALE_C
----------      ----------    ----------     ------
1          1        100       LOW
2          102      1500      LOW
3          102      100000    MEDIUM
 
3 rows selected.


You can't insert value in to this particular column.It will throw error.
You can create index and it will be a function-based index
You can also partition this particular column.

Invisible indexes
Indexing a column wont be helping us always. So we end up analyzing the impact of adding one index to a column.

You created an index on a column, ran the query which use this column in its WHERE clause and analyzed the performance. Now you want to test it without index and so you removed the index and later you need to recreate it. The recreation of an index is an expensive process.

Oracle 11g comes up with a solution. Make the index invisible. When you say the index to be invisible the optimizer wont be able to see this index and query is processed without index. If you want to use this invisible index in your query you have to specifally mention that using Hint.

SQL> alter index index_name invisible;
 
select /*+ index (table index_name) */ id form emp;

By giving this hint, Oracle optimizer will be able to see the index and it will make use of the index while generationg the data.

Alternatively, you can set a session-level parameter to use the invisible indexes:
SQL> alter session set optimizer_use_invisible_indexes = true;


You  can see details about indexes in user_indexes table.

Read only tables.
This is mainly use in DWH. There use to be a periodical updates on every table. At this point of time we may come across a condition that the users should not do any DML statement on the tables.

To achieve this we either revoke the access or triggers with alert or create VPD (virtual private database) policy on the tables.

Revoking access is not advisable, triggers has performance issue and VPD policy eventhough better than triggers gives a wrong error message to the user.

Oracle 11g comes up with a new solution: Change the read only status

SQL> alter table emp read only;
 
Table altered.

Now when a user tries to issue a DML such as that shown below:
SQL> delete trans;

Oracle Database 11g throws an error right away:
delete trans
       *
ERROR at line 1:
ORA-12081: update operation not allowed on table "SCOTT"."TRANS"

The error message does not reflect the operation to the letter but conveys the message as intended, without the overhead of a trigger or VPD policy.
When you want to make the table update-able, you will need to make it read/write, as shown below:
SQL> alter table trans read write;
 
Table altered.

Now DMLs will be no problem:
SQL> update trans set amt = 1 where trans_id = 1;
 
1 row updated.

With creating read only option an user is only blocked from DML statements but not from DDL, the user can perform DDLs like creating index, managing partition etc...

Fine-Grained Dependency Tracking

Create a new table, create a view on this table selecting few columns for information hiding. On checking the dependency status of the view you can see this view depends on above table and status in VALID.

Change the sturcture of the table, say add a new column. Check the dependency of view, you can see it as INVALID even though that column is not used in view, as it is a child depending on parent table. This happens only in 10g and all previous releases.

Your application go for toss, Recompile the view will help you to make it active.

Oracle 11g wont invalidate the view is such scenario. All dependent objects of the view, such as other views, packages and procedures are also valid.
Overall availability of entire application is ensured. No need to stop the app during some changes.

If the column involved in view is altered then view go inactive which is desirable.

This scenario is applicable with other DB objects like procedures and packages as well. For example you have created a package which is being called form one of your function. Here your function depend on the package. Now for some reason you modified your package to add one more procedure and your function goes invalid in 10g and all below versions but not in 11g.

Here there is one exceptional scenario where if the new procedure is added at first position the depending object, function will be invalid in 11g as well because the slot provided for the procedures in package is rearranged. If we are adding new procedure after the existing one, we merely add one additional slot.


****
Hope this is helpful. Phoenix

Thursday, January 5, 2012

Oracle Replace and Translate

Replace:
Replace funtion replaces a given string with another set of characters

Syntax:
replace( string1, string_to_replace, [ replacement_string ] )

Example:

SQL> select replace ('Hello World','World','Beutiful World') from dual;
REPLACE('HELLOWORLD'
--------------------
Hello Beutiful World

What will be the output, if we didnt provide the string to be replaced?
SQL> select replace ('Hello World','World') from dual;
REPLAC
------
Hello

The word World is removed.


What if, we need to replace "Hello" with "Hi" and "World" with "Beautiful World"?
Then we have to use TRANSLATE
TRANSLATE:

If we need to replace multiple set of characters with another given set of characters use TRANSLATE. Translate takes a list of characters to be replaced and a list of adjacent character which will replace the given one. It replaces by position, the first character of the given list is replaced with the first character of the given replacement list, second with second and so on.

As the above given example for replace, if there is no string in the equivalent position, then the character is dropped from the source text.
Examples:

Use translate to:

Replace a single character
SQL> select translate  ('Hello World','W','i') from dual;

TRANSLATE('
-----------
Hello iorld

Replace a multiple characters
SQL> select translate  ('Hello World','Wo','wO') from dual;
TRANSLATE('
-----------
HellO wOrld

Note: You have to make sure that at least one character should be given in the replacement list, else TRANSLATE wont give us expected result.

Use TRANSLATE to replace double quotes
SELECT TRANSLATE('"Darn double quotes "', 'A"', 'A')
FROM DUAL;

here if you hadn't given the character A, the value returned is null, ie everything is dropped.
SQL> SELECT TRANSLATE('"Darn double quotes "', '"', '')
  2  FROM DUAL;
T
-
Use TRANSLATE to get the count of any items in the given string
SQL> WITH data AS (SELECT 'Whose line is it anyway' line FROM DUAL)
  2  SELECT LENGTH(line)-LENGTH(TRANSLATE(line,'xaeiou','x')) Vowels
  3  FROM data;
    VOWELS
----------
         8


SQL> WITH data AS (SELECT 'Whose line is it anyway' line FROM DUAL)
  2  SELECT LENGTH(line)-LENGTH(TRANSLATE(line,'aeiou','')) Vowels
  3  FROM data;
    VOWELS
----------

Encryption and Decryption:
You can also use TRANSLATE for encryption and decryption

SQL> SELECT TRANSLATE('Market crashes down',
  2  'abcdefghijklmnopqrstuvwxyz', '0123456789qwertyuiop[kjhbv')
  3  FROM DUAL;
TRANSLATE('MARKETCR
-------------------
M0iq4p 2i0o74o 3tjr

Now let us use this encrypted value and try to decrypt

SQL> SELECT TRANSLATE('M0iq4p 2i0o74o 3tjr',
  2  '0123456789qwertyuiop[kjhbv', 'abcdefghijklmnopqrstuvwxyz')
  3  FROM DUAL;
TRANSLATE('M0IQ4P2I
-------------------
Market crashes down
Fine, works well...


Note: Translate always replace a single character at a time. Also it wont replace a single character to a multiple character as like we do with Replace function.

****
Hope this helps..Phoenix