Thursday, July 23, 2015

Virtual Columns in oracle 11g

Virtual Columns:

Oracle 11g introduced the concept of ‘Virtual Column’ within a table. Virtual Columns are similar to normal table’s columns but with the following differences:

They are defined by an expression. The result of evaluation of this expression becomes the value of the column. The values of the virtual column are not stored in the database. Rather, it’s computed at run-time when you query the data. You can’t update (in SET clause of update statement) the values of virtual column. These are read only values, that are computed dynamically and any attempt to modify them will result into oracle error.

The syntax for defining a virtual column is:

column_name [datatype] [GENERATED ALWAYS] AS [expression] [VIRTUAL]

where the parameters within [ ] are optional and can be omitted. If you don’t mention the datatype, Oracle will decide it based on the result of the expression.

Excepting the above points, a virtual column, exists just like any other column of a normal table and the following points apply to it:

Virtual columns can be used in the WHERE clause of UPDATE and DELETE statement but they can’t be modified by DML.
Statistics can be collected on them.
They can be used as a partition key in virtual column based partitioning.
Indexes can be created on them. As you might have guessed, oracle would create


  • Function based indexes as we create on normal tables. 
  • Constraints can be created on them.


For creating a virtual column, use the syntax mentioned above. Consider the following example:

CREATE TABLE VTEST
(
    emp_id        NUMBER,
    emp_no        VARCHAR2(50),
    monthly_sal    NUMBER(10,2),
    bonus          NUMBER(10,2),
    tot_sal      NUMBER(10,2) GENERATED ALWAYS AS (monthly_sal*10 + bonus)
);

Here we have defined a virtual column “total_sal” whose value would be dynamically calculated using the expression provided after the “generated always as” clause. Please note that this declaration is different than using “default” clause for a normal column as you can’t refer column names with “default” clause.

Lets check the data dictionary view:

SELECT column_name, data_type, data_length, data_default, virtual_column
  FROM user_tab_cols
 WHERE table_name = 'VTEST';

COLUMN_NAME | DATA_TYPE | DATA_LENGTH | DATA_DEFAULT | VIRTUAL_COLUMN
EMP_ID                   | NUMBER      | 22         | null                     | NO           
EMP_NO                 | VARCHAR2  | 50         | null                     | NO           
MONTHLY_SAL   | NUMBER       | 22         | null                     | NO           
BONUS                   | NUMBER       | 22         | null                     | NO           
TOT_SAL           | NUMBER      | 22         | "MONTHLY_SAL"*12+"BONUS" | YES 


Lets test this :

CREATE TABLE VTEST
(
    emp_id        NUMBER,
    emp_no        VARCHAR2(50),
    monthly_sal    NUMBER(10,2),
    bonus          NUMBER(10,2),
    tot_sal      NUMBER(10,2) GENERATED ALWAYS AS (monthly_sal*10 + bonus)
);

Table created.

when you insert data into table you only insert for non virtual columns. See below


SQL> INSERT INTO VTEST (emp_id, emp_no, monthly_sal, bonus) values (1,'arvind',10000,1000);

1 row created.

SQL> INSERT INTO VTEST (emp_id, emp_no, monthly_sal, bonus) values (2,'reddy',20000,2000);

1 row created.

SQL> commit;

Commit complete.


SQL> select * from vtest;

   EMPL_ID EMPL_NM     MONTHLY_SAL BONUS   TOT_SAL
---------- ---------- ---------- ------------ ----------
         1 arvind              10000 1000     121000
         2 reddy   20000 2000     242000

Configure Putty tunnel

WinSCP Config

The WinSCP Config is quite simple and utilizes its “tunnel” feature. Open WinSCP and configure a saved session for the final destination host as follows:

On the Session page, fill in the hostname and user name for the final destination host. Leave the password blank.
Check the “Advanced options” box in the login dialog.
Select the Connection –> Tunnel page.
Check the “Connect through SSH tunnel” box.
Fill in the Host name and user name of the intermediate host. Leave the password blank.
Save the session using the button in the lower right-hand corner of the window.

When you log in using the new profile, you will be prompted for two passwords. The first is for your account on the intermediate host, and the second is for your account on the final-destination host. After login, the bounce is entirely transparent and WinSCP works as if you had connected directly to the final-destination host. The connection process can be made even more transparent and secure by using public key authentication with Pageant instead of passwords.


Putty Config

The Putty setup is slightly more complicated and requires that public key authentication be used on the intermediate host. It utilizes Putty’s “local proxy” feature, which allows you to specify an arbitrary command on the local machine to act as a proxy. Instead of creating a TCP connection, PuTTY will communicate using the proxy program’s standard input and output streams. Our local proxy will be plink, which is a command-line ssh connection program included in the Putty default installation. Plink’s -nc option provides functionality similar to the ProxyCommand/netcat approach, but does so using the ssh server’s native port-forwarding interface and does not require that netcat be installed on the intermediate system. To set things up, configure a saved session for the final destination host:

Configure public key authentication for the intermediate host and make sure it works.

Start putty and on the “Session” page of the “Putty Configuration Dialog” that appears, fill in the host name and user name for the final destination host.

Switch to the Connection –> Proxy page, select “Local” as the proxy type enter the following as the local proxy command: plink.exe intermediate.proxy.host -l username -agent -nc %host:%port

Save the session.

C:\Program Files (x86)\PuTTY\plink.exe 10.194.0.0210 -l username -agent -nc %host:%port


connect %host %port\n

PRVF-7532 : Package "libaio-0.3.105 (i386)" is missing - Oracle installation requires old rpm versions, what to do?

Below packages can usually be ignored as oracle installer thinks they are not available and are still needed while they already exists.



[root@rac2 Desktop]# yum install libaio-0.3.105-2.i386.rpm

Loaded plugins: refresh-packagekit, security

Setting up Install Process

No package libaio-0.3.105-2.i386.rpm available.

Error: Nothing to do

[root@rac2 Desktop]# rpm -ivh compat-libstdc++*

error: File not found by glob: compat-libstdc++*

[root@rac2 Desktop]# ps -ef |grep compat-libstdc++*

root     11837  7817  0 19:27 pts/0    00:00:00 grep compat-libstdc++*

[root@rac2 Desktop]# rpm -qa|grep compat-libstdc++*

compat-libstdc++-296-2.96-144.el6.i686

compat-libstdc++-33-3.2.3-69.el6.x86_64


Reference :

You can also see below notes from this link:

Installing Oracle Software

Install Oracle software without clicking through Oracle Universal Installer (OUI). Use instead smart swInst action of install Manager which installs Oracle SW with OUI silently. Add your own response files if you prefer another ORACLE_HOME destination.
$ bash
$ cd $INSTALL_CONF
$ cp sample/swInstEeSrv1-linux-x86_64.cfg .
$ installManager swInst swInstEeSrv1-linux-x86_64.cfg
$ su -
# /opt/oracle/eesrv/11.2.0/db1/root.sh
# exit
Oracle Universal Installer prints beginning with 11g R2 also warnings for optional requirements. Identify the errors and decide whether they can be ignored or not. The next example shows errors which can usually be ignored since OUI does not recognize newer packages.
20101205_185530: Info: Grepping for PRVF- pattern in /opt/oracle/oraInventory/logs/installActions2010-12-05_06-40-15PM.log
INFO: Error Message:PRVF-7532 : Package "libaio-0.3.105 (i386)" is missing on node "<node_name>"
INFO: Error Message:PRVF-7532 : Package "compat-libstdc++-33-3.2.3 (i386)" is missing on node "<node_name>"
INFO: Error Message:PRVF-7532 : Package "libaio-devel-0.3.105 (i386)" is missing on node "<node_name>"
INFO: Error Message:PRVF-7532 : Package "libgcc-3.4.6 (i386)" is missing on node "<node_name>"
INFO: Error Message:PRVF-7532 : Package "libstdc++-3.4.6 (i386)" is missing on node "<node_name>"
INFO: Error Message:PRVF-7532 : Package "unixODBC-2.2.11 (i386)" is missing on node "<node_name>"
INFO: Error Message:PRVF-7532 : Package "unixODBC-devel-2.2.11 (i386)" is missing on node "<node_name>"
INFO: Error Message:PRVF-7532 : Package "pdksh-5.2.14" is missing on node "<node_name>"

Monday, June 29, 2015

Create Control file manually - Oracle


When to create control files ?

=>  Create control files in situations when :

1. You have lost all your control files.
2. When you want to rename your database name (db_name);

Note :although we can use utility called DBNEWID to change the DBNAME. DBNEWID can be used to change :

-- Only the DBID of a database
-- Only the DBNAME of a database
-- Both the DBNAME and DBID of a database

 How to create control files.

You need a create controlfile script for recreating control files.
Code:
SQL*Plus: Release 11.2.0.0 - Production on Fri Feb 28 17:04:00 2003

Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.

Enter user-name: sys as sysdba
Enter password:

Connected to:
 Enterprise Edition Release 11.2.0.0 - Production
With the Partitioning, Oracle Label Security, OLAP and Oracle Data Mining options


SQL> alter database backup controlfile to trace;

Database altered.

SQL>

This will create a trace file in the udump directory.

In my case it was

E:\oracle\admin\ORCL\udump\orcl_ora_20327.trc

Edit the file to point the path of the datafiles and redologfiles.

Code:
CREATE CONTROLFILE REUSE DATABASE "ORCL" RESETLOGS  ARCHIVELOG
    MAXLOGFILES 5
    MAXLOGMEMBERS 3
    MAXDATAFILES 14
    MAXINSTANCES 1
    MAXLOGHISTORY 226
LOGFILE
  GROUP 1 'C:\ORACLE\ORADATA\ORCL\REDO01.LOG'  SIZE 100M,
  GROUP 2 'C:\ORACLE\ORADATA\ORCL\REDO02.LOG'  SIZE 100M,
  GROUP 3 'C:\ORACLE\ORADATA\ORCL\REDO03.LOG'  SIZE 100M
DATAFILE
  'C:\ORACLE\ORADATA\ORCL\SYSTEM01.DBF',
  'C:\ORACLE\ORADATA\ORCL\UNDOTBS01.DBF',
  'C:\ORACLE\ORADATA\ORCL\EXAMPLE01.DBF',
  'C:\ORACLE\ORADATA\ORCL\INDX01.DBF',
  'C:\ORACLE\ORADATA\ORCL\TOOLS01.DBF',
  'C:\ORACLE\ORADATA\ORCL\USERS01.DBF',
CHARACTER SET WE8MSWIN1252
;

ALTER DATABASE OPEN RESETLOGS;

and rename it to control_file.sql

Then startup the database in nomount mode and run the control_file.sql file as sys as sysdba user

Code:
SQL*Plus: Release 11.2.0.0 - Production

Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.

Enter user-name: sys as sysdba
Enter password:
Connected to an idle instance.

SQL> startup nomount
ORACLE instance started.

Total System Global Area   80812648 bytes
Fixed Size                   453224 bytes
Variable Size              54525952 bytes
Database Buffers           25165824 bytes
Redo Buffers                 667648 bytes

SQL> @C:\oracle\ORCL\udump\control_file.sql

Control file created.


Database altered.

SQL> select status from v$instance;

STATUS
------------------------------------
OPEN

SQL> select name from v$controlfile;

NAME
--------------------------------------------------------
C:\ORACLE\ORADATA\ORCL\CONTROL01.CTL
C:\ORACLE\ORADATA\ORCL\CONTROL02.CTL
C:\ORACLE\ORADATA\ORCL\CONTROL03.CTL


To rename the database change reuse to set in the create control file script as shown below

Code:
CREATE CONTROLFILE SET DATABASE "ORCL" RESETLOGS  ARCHIVELOG
    MAXLOGFILES 5
    MAXLOGMEMBERS 3
    MAXDATAFILES 14
    MAXINSTANCES 1
    MAXLOGHISTORY 226
LOGFILE
  GROUP 1 'C:\ORACLE\ORADATA\ORCL\REDO01.LOG'  SIZE 100M,
  GROUP 2 'C:\ORACLE\ORADATA\ORCL\REDO02.LOG'  SIZE 100M,
  GROUP 3 'C:\ORACLE\ORADATA\ORCL\REDO03.LOG'  SIZE 100M
DATAFILE
  'C:\ORACLE\ORADATA\ORCL\SYSTEM01.DBF',
  'C:\ORACLE\ORADATA\ORCL\UNDOTBS01.DBF',
  'C:\ORACLE\ORADATA\ORCL\EXAMPLE01.DBF',
  'C:\ORACLE\ORADATA\ORCL\INDX01.DBF',
  'C:\ORACLE\ORADATA\ORCL\TOOLS01.DBF',
  'C:\ORACLE\ORADATA\ORCL\USERS01.DBF',
CHARACTER SET WE8MSWIN1252
;

ALTER DATABASE OPEN RESETLOGS;

Thursday, June 25, 2015

CREATE ORACLE DATABASE SERVICE

dbms_service.create_service :  

We can call dbms_service.create_service procedure to create new service names , then start these service names for user connections. The procedure dbms_service.create_service requires the service name and service network name, use the service name to manage the service name.


oracle@LINUX201:[~] $ sqlplus /"As sysdba"

SQL*Plus: Release 11.2.0.4.0 Production on Tue Apr 5 13:17:50 2016

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> select SERVICE_ID,name from V$SERVICES;

SERVICE_ID NAME
---------- ----------------------------------------------------------------
         4 ORCL
         1 SYS$BACKGROUND
         2 SYS$USERS

************************************************************

LEST CREATE A NEW DB SERVICE 

************************************************************


SAMPLE :

BEGIN

DBMS_SERVICE.CREATE_SERVICE(service_name=>'QPDEV',

 network_name=>'QPDEV.WORLD'); (or)  network_name=>'QPDEV');

 END;

 /


SQL> begin
 dbms_service.create_service('ORCLTEST','ORCLTEST');
end;
/

PL/SQL procedure successfully completed.

SQL> select SERVICE_ID,name from V$SERVICES;

SERVICE_ID NAME
---------- ----------------------------------------------------------------
         4 ORCL
         1 SYS$BACKGROUND
         2 SYS$USERS

SQL> begin
 DBMS_SERVICE.START_SERVICE('ORCL');
end;
/  2    3    4

PL/SQL procedure successfully completed.

************************************************************

CHECK IF THE SERVICE IS CREATED AND STARTED

************************************************************


SQL> select SERVICE_ID,name from V$SERVICES;

SERVICE_ID NAME
---------- ----------------------------------------------------------------
         7 ORCLTEST
         4 ORCL
         1 SYS$BACKGROUND
         2 SYS$USERS

SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

oracle@LINUX201:[~] $ cd $ORACLE_HOME/network/admin

oracle@LINUX201:[/u01/app/oracle/product/11.2.0/db_1/network/admin] $ ls
samples  shrept.lst  tnsnames.ora

************************************************************

LETS ADD THIS NEW SERVICE IN TO TNSNAMES FILE:

************************************************************


oracle@LINUX201:[/u01/app/oracle/product/11.2.0/db_1/network/admin] $ vi tnsnames.ora
# tnsnames.ora Network Configuration File: /u01/app/oracle/product/11.2/db_1/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.

ORCL =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = LINUX201.world.com)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = ORCLTEST)
    )
  )

************************************************************

LETS TEST IT :

************************************************************



oracle@LINUX201:[/u01/app/oracle/product/11.2.0/db_1/network/admin] $ tnsping ORCLTEST

TNS Ping Utility for Linux: Version 11.2.0.4.0 - Production on 05-APR-2016 13:21:31

Copyright (c) 1997, 2013, Oracle.  All rights reserved.

Used parameter files:


Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = LINUX201)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = ORCLTEST)))
OK (10 msec)
oracle@LINUX201:[/u01/app/oracle/product/11.2.0/db_1/network/admin] $ sqlplus atoorpu@ORCLTEST

SQL*Plus: Release 11.2.0.4.0 Production on Tue Apr 5 13:21:46 2016

Copyright (c) 1982, 2013, Oracle.  All rights reserved.

Enter password:

Wednesday, June 24, 2015

Trigger to disable create objects in database starting with TMP or BAK

---- DISABLE TABLE NAMES STARTING WITH TMP OR BAK in Database ----

Intially :

create table tmp_test (fname varchar2(20));

After enabling below trigger, no more tables can be created in database starting with TMP or BAK:

create or replace TRIGGER NO_TMP_TABS_TRIG
BEFORE CREATE
ON DATABASE

DECLARE
 x user_tables.table_name%TYPE;
BEGIN
  SELECT ora_dict_obj_name
  INTO x
  FROM DUAL;

  IF SUBSTR(x, 0, 4) = 'TMP_' or SUBSTR(x, 0, 4) = 'BAK_' THEN
    RAISE_APPLICATION_ERROR(-20099, 'TABLE NAMES CAN NOT START WITH THE WITH TMP% OR BAK%');
  END IF;
END NO_TMP_TABS_TRIG;


Lets test it :

create table tmp_test (fname varchar2(20));


Error :

Error starting at line : 15 in command -
create table tmp_test (fname varchar2(20))
Error at Command Line : 15 Column : 1
Error report -
SQL Error: ORA-00604: error occurred at recursive SQL level 1
ORA-20099: TABLE NAMES CAN NOT START WITH THE WITH TMP% OR BAK%.

Active Session History (ASH) performed an emergency flush



ARC1: STARTING ARCH PROCESSES COMPLETE
ARC2: Becoming the heartbeat ARCH
Sun Jun ** 10:46:08 ****

Active Session History (ASH) performed an emergency flush. This may mean that ASH is undersized. If emergency flushes are a recurring issue, you may consider increasing ASH size by setting the value of _ASH_SIZE to a sufficiently large value. Currently, ASH size is 134217728 bytes. Both ASH size and the total number of emergency flushes since instance startup can be monitored by running the following query:
select total_size,awr_flush_emergency_count from v$ash_info;



SQL> select total_size/1024/1024,awr_flush_emergency_count from v$ash_info;

TOTAL_SIZE/1024/1024 AWR_FLUSH_EMERGENCY_COUNT
-------------------- -------------------------
                 128                         1


This is not a dynamic paramter :

sql > alter system set "_ash_size"=200M scope=spfile;

SQL> select total_size/1024/1024,awr_flush_emergency_count from v$ash_info;

TOTAL_SIZE/1024/1024 AWR_FLUSH_EMERGENCY_COUNT
-------------------- -------------------------
                 128                         1


Note : This will require a DB restart to take the above changes effective.