Neuronworks Blog, WebMethods, Oracle, Bea, Java Dev2Dev, Arch2Arch Neuronworks Make IT Easy, Let's Play the Game » Oracle

Archive

Archive for the ‘Oracle’ Category

Backup dan Restore Schema Oracle Menggunakan exp dan imp

December 31st, 2008

Oracle menyediakan perintah untuk membackup schema dan merestore schema hasil backup tersebut. Kedua perintah tersebut yaitu exp untuk membackup schema dan imp untuk merestore schema hasil backup sebelumnya.
Kita coba dengan backup terlebih dahulu. Pertama kita buat direktori backup untuk menampung hasil backup schema yang kita lakukan, kemudian kita gunakan direktori tersebut sebagai base direktori pekerjaan kita:

oracle@NeuronServer:~> mkdir schema-backup
oracle@NeuronServer:~> cd schema-backup/
oracle@NeuronServer:~/schema-backup>

Misalnya kita ingin membackup schema train yang memiliki password train yang ada di server oracle dengan service id neuron, file backup schema-nya kita simpan dengan nama train.dmp. Penggunaan perintah exp-nya seperti dibawah ini:

oracle@NeuronServer:~/schema-backup> exp train/train@neuron file=train.dmp    
 
Export: Release 10.2.0.1.0 - Production on Fri Jan 2 16:17:32 2009
 
Copyright (c) 1982, 2005, Oracle.  All rights reserved.
 
 
Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses WE8ISO8859P1 character set (possible charset conversion)
. exporting pre-schema procedural objects and actions
. exporting foreign function library names for user TRAIN
. exporting PUBLIC type synonyms
. exporting private type synonyms
. exporting object type definitions for user TRAIN
About to export TRAIN's objects ...
. exporting database links
. exporting sequence numbers
. exporting cluster definitions
. about to export TRAIN's tables via Conventional Path ...
. . exporting table                          TRAIN          2 rows exported
. exporting synonyms
. exporting views
. exporting stored procedures
. exporting operators
. exporting referential integrity constraints
. exporting triggers
. exporting indextypes
. exporting bitmap, functional and extensible indexes
. exporting posttables actions
. exporting materialized views
. exporting snapshot logs
. exporting job queues
. exporting refresh groups and children
. exporting dimensions
. exporting post-schema procedural objects and actions
. exporting statistics
Export terminated successfully without warnings.
oracle@NeuronServer:~/schema-backup>

File train.dmp tersebut dapat kita gunakan untuk merestore schema tersebut dikemudian hari atau untuk menduplikat schema tersebut ke server yang lain.
Sebagai contoh kita gunakan file tersebut untuk menduplikat schema di mesin yang berbeda tapi memiliki user yang sama yaitu train. Penggunaan imp:

oracle@linuxbox:~> imp train/train@xe file=/home/alam/train.dmp
 
Import: Release 10.2.0.1.0 - Production on Fri Jan 2 15:43:10 2009
 
Copyright (c) 1982, 2005, Oracle.  All rights reserved.
 
 
Connected to: Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
 
Export file created by EXPORT:V10.02.01 via conventional path
import done in US7ASCII character set and AL16UTF16 NCHAR character set
import server uses AL32UTF8 character set (possible charset conversion)
. importing TRAIN's objects into TRAIN
. . importing table                        "TRAIN"          2 rows imported
Import terminated successfully without warnings.
oracle@linuxbox:~>

Pastikan bahwa schema yang dimaksud (train) ada di server yang hendak direstore. Perlu diingat bahwa perintah imp ini akan menimpa schema yang sudah ada di server, jadi pastikan bahwa anda memang ingin menimpa schema tersebut.

UPDATE

Ada banyak opsi-opsi yang dapat kita gunakan dalam menggunakan exp dan imp. Untuk mengetahui apa saja opsi-opsi yang tersedia jalankan perintah berikut:

oracle@NeuronServer:~> exp help=y
 
Export: Release 10.2.0.1.0 - Production on Fri Jan 2 14:06:04 2009
 
Copyright (c) 1982, 2005, Oracle.  All rights reserved.
 
 
 
You can let Export prompt you for parameters by entering the EXP
command followed by your username/password:                     
 
     Example: EXP SCOTT/TIGER
 
Or, you can control how Export runs by entering the EXP command followed
by various arguments. To specify parameters, you use keywords:          
 
     Format:  EXP KEYWORD=value or KEYWORD=(value1,value2,...,valueN)
     Example: EXP SCOTT/TIGER GRANTS=Y TABLES=(EMP,DEPT,MGR)         
               or TABLES=(T1:P1,T1:P2), if T1 is partitioned table   
 
USERID must be the first parameter on the command line.
 
Keyword    Description (Default)      Keyword      Description (Default)
--------------------------------------------------------------------------
USERID     username/password          FULL         export entire file (N)
BUFFER     size of data buffer        OWNER        list of owner usernames
FILE       output files (EXPDAT.DMP)  TABLES       list of table names
COMPRESS   import into one extent (Y) RECORDLENGTH length of IO record
GRANTS     export grants (Y)          INCTYPE      incremental export type
INDEXES    export indexes (Y)         RECORD       track incr. export (Y)
DIRECT     direct path (N)            TRIGGERS     export triggers (Y)
LOG        log file of screen output  STATISTICS   analyze objects (ESTIMATE)
ROWS       export data rows (Y)       PARFILE      parameter filename
CONSISTENT cross-table consistency(N) CONSTRAINTS  export constraints (Y)
 
OBJECT_CONSISTENT    transaction set to read only during object export (N)
FEEDBACK             display progress every x rows (0)
FILESIZE             maximum size of each dump file
FLASHBACK_SCN        SCN used to set session snapshot back to
FLASHBACK_TIME       time used to get the SCN closest to the specified time
QUERY                select clause used to export a subset of a table
RESUMABLE            suspend when a space related error is encountered(N)
RESUMABLE_NAME       text string used to identify resumable statement
RESUMABLE_TIMEOUT    wait time for RESUMABLE
TTS_FULL_CHECK       perform full or partial dependency check for TTS
VOLSIZE              number of bytes to write to each tape volume
TABLESPACES          list of tablespaces to export
TRANSPORT_TABLESPACE export transportable tablespace metadata (N)
TEMPLATE             template name which invokes iAS mode export
 
Export terminated successfully without warnings.
oracle@NeuronServer:~> imp help=y               
 
Import: Release 10.2.0.1.0 - Production on Fri Jan 2 14:08:51 2009
 
Copyright (c) 1982, 2005, Oracle.  All rights reserved.
 
 
 
You can let Import prompt you for parameters by entering the IMP
command followed by your username/password:                     
 
     Example: IMP SCOTT/TIGER
 
Or, you can control how Import runs by entering the IMP command followed
by various arguments. To specify parameters, you use keywords:          
 
     Format:  IMP KEYWORD=value or KEYWORD=(value1,value2,...,valueN)
     Example: IMP SCOTT/TIGER IGNORE=Y TABLES=(EMP,DEPT) FULL=N      
               or TABLES=(T1:P1,T1:P2), if T1 is partitioned table   
 
USERID must be the first parameter on the command line.
 
Keyword  Description (Default)       Keyword      Description (Default)
--------------------------------------------------------------------------
USERID   username/password           FULL         import entire file (N)
BUFFER   size of data buffer         FROMUSER     list of owner usernames
FILE     input files (EXPDAT.DMP)    TOUSER       list of usernames
SHOW     just list file contents (N) TABLES       list of table names
IGNORE   ignore create errors (N)    RECORDLENGTH length of IO record
GRANTS   import grants (Y)           INCTYPE      incremental import type
INDEXES  import indexes (Y)          COMMIT       commit array insert (N)
ROWS     import data rows (Y)        PARFILE      parameter filename
LOG      log file of screen output   CONSTRAINTS  import constraints (Y)
DESTROY                overwrite tablespace data file (N)
INDEXFILE              write table/index info to specified file
SKIP_UNUSABLE_INDEXES  skip maintenance of unusable indexes (N)
FEEDBACK               display progress every x rows(0)
TOID_NOVALIDATE        skip validation of specified type ids
FILESIZE               maximum size of each dump file
STATISTICS             import precomputed statistics (always)
RESUMABLE              suspend when a space related error is encountered(N)
RESUMABLE_NAME         text string used to identify resumable statement
RESUMABLE_TIMEOUT      wait time for RESUMABLE
COMPILE                compile procedures, packages, and functions (Y)
STREAMS_CONFIGURATION  import streams general metadata (Y)
STREAMS_INSTANTIATION  import streams instantiation metadata (N)
VOLSIZE                number of bytes in file on each volume of a file on tape
 
The following keywords only apply to transportable tablespaces
TRANSPORT_TABLESPACE import transportable tablespace metadata (N)
TABLESPACES tablespaces to be transported into database
DATAFILES datafiles to be transported into database
TTS_OWNERS users that own data in the transportable tablespace set
 
Import terminated successfully without warnings.
oracle@NeuronServer:~>

Sebelumnya kita mengekspor schema yang memiliki dua user yang sama di dua sistem yang berbeda. Bagaimana jika database yang akan mengimpor menggunakan username yang berbeda? Sebagai contoh saya akan mencoba mengimpor schema train yang sebelumnya dibackup untuk direstore di server yang ada di mesin yang berbeda, dimana user yang digunakan yaitu neurondev dengan password neurondev yang ada di service xe. Logisnya saya akan menggunakan command dibawah ini:

oracle@linuxbox:~> imp neurondev/neurondev@xe file=/home/alam/train.dmp
 
Import: Release 10.2.0.1.0 - Production on Fri Jan 2 13:48:33 2009
 
Copyright (c) 1982, 2005, Oracle.  All rights reserved.
 
 
Connected to: Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
 
Export file created by EXPORT:V10.02.01 via conventional path
 
Warning: the objects were exported by TRAIN, not by you
 
import done in US7ASCII character set and AL16UTF16 NCHAR character set
import server uses AL32UTF8 character set (possible charset conversion)
. importing TRAIN's objects into NEURONDEV
Import terminated successfully without warnings.
oracle@linuxbox:~>

Kita tidak bisa mengimpor schema yang dibuat oleh user yang lain yang berbeda dengan yang kita gunakan untuk mengimpor schema tersebut, dalam hal ini backup schema tersebut dimiliki oleh user train sedangkan kita menggunakan user neurondev. Bagaimana solusinya? Gunakan opsi fromuser dan touser yang disediakan oleh imp seperti ini:

oracle@linuxbox:~> imp neurondev/neurondev@xe file=/home/alam/train.dmp fromuser=train touser=neurondev
 
Import: Release 10.2.0.1.0 - Production on Fri Jan 2 14:57:59 2009
 
Copyright (c) 1982, 2005, Oracle.  All rights reserved.
 
 
Connected to: Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
 
Export file created by EXPORT:V10.02.01 via conventional path
 
Warning: the objects were exported by TRAIN, not by you
 
import done in US7ASCII character set and AL16UTF16 NCHAR character set
import server uses AL32UTF8 character set (possible charset conversion)
. . importing table                        "TRAIN"          2 rows imported
Import terminated successfully without warnings.
oracle@linuxbox:~>

Opsi fromuser digunakan untuk memberitahu oracle user awal yang memiliki backup schema tersebut (owner) dan opsi touser digunakan untuk menentukan user mana yang akan memiliki schema hasil impor tersebut. Jadi kita isi train untuk opsi fromuser karena backup schema tersebut dimiliki oleh user train dan dibackupnya pun oleh user train. Sedangkan opsi fromuser kita isi dengan neurondev karena user ini yang akan memiliki schema hasil impor tersebut dan juga user yang melakukan proses impornya.
Sekarang mari kita coba mengimpornya menggunakan user yang sama sekali berbeda:

oracle@linuxbox:~> imp admin/admin@xe file=/home/alam/train.dmp fromuser=train touser=neurondev
 
Import: Release 10.2.0.1.0 - Production on Fri Jan 2 15:23:23 2009
 
Copyright (c) 1982, 2005, Oracle.  All rights reserved.
 
 
Connected to: Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
 
Export file created by EXPORT:V10.02.01 via conventional path
 
Warning: the objects were exported by TRAIN, not by you
 
import done in US7ASCII character set and AL16UTF16 NCHAR character set
import server uses AL32UTF8 character set (possible charset conversion)
IMP-00007: must be a DBA to import objects to another user's account   
IMP-00000: Import terminated unsuccessfully                            
oracle@linuxbox:~>

Kita membutuhkan user dengan grant DBA jika ingin melakukan hal diatas.

oracle@linuxbox:~> imp system@xe file=/home/alam/train.dmp fromuser=train touser=neurondev
 
Import: Release 10.2.0.1.0 - Production on Fri Jan 2 15:24:55 2009
 
Copyright (c) 1982, 2005, Oracle.  All rights reserved.
 
Password:
 
Connected to: Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
 
Export file created by EXPORT:V10.02.01 via conventional path
 
Warning: the objects were exported by TRAIN, not by you
 
import done in US7ASCII character set and AL16UTF16 NCHAR character set
import server uses AL32UTF8 character set (possible charset conversion)
. importing TRAIN's objects into NEURONDEV
. . importing table                        "TRAIN"          2 rows imported
Import terminated successfully without warnings.
oracle@linuxbox:~>

Semoga bermanfaat and have a lot of fun!

Oracle , ,

Me-Resize ukuran Temporary Tablespace

December 29th, 2008

Suatu ketika saya kaget melihat ukuran temporary tablespace saya yang mencapai 2 GB, kenapa bisa sebesar ini, ternyata setelah di lihat memang di konfigurasinya autoextends, ya sudah akhirnya saya mencoba untuk meresize ukuran tersebut.

berikut kronologi melakukan resize temp tablesapce :

langkah pertama yang saya lakukan adalah :

SQL> ALTER DATABASE tempfile '/app/oradata/NEURON/temp01.dbf' resize 250M;
ALTER DATABASE tempfile '/app/oradata/NEURON/temp01.dbf' resize 250M
*
ERROR AT line 1:
ORA-03297: FILE contains used data beyond requested RESIZE VALUE

ooo , ternyata error berarti tidak bisa menggunakan cara ini, cara berikutnya adalah Drop dan Create ulang tablesapce temp

berikut langkah-langkahnya :

SQL> DROP TABLESPACE temp;
 
TABLESPACE dropped.

Create ulang temp tablespace

SQL> CREATE TEMPORARY TABLESPACE TEMP
  2  TEMPFILE '/app/oradata/NEURON/temp01.dbf' SIZE 500M REUSE
  3  AUTOEXTEND ON NEXT 100M MAXSIZE unlimited
  4  EXTENT MANAGEMENT LOCAL UNIFORM SIZE 1M;
 
TABLESPACE created.

perintah diatas akan berhasil jika, oracle yang kita gunakan adalah oracle 8i atau oracle 9i dengan temp tablespace bukan default temporary tablesapace.

SQL> DROP TABLESPACE temp;
DROP TABLESPACE temp
*
ERROR AT line 1:
ORA-12906: cannot DROP DEFAULT TEMPORARY TABLESPACE

jika kita ingin merubah ukuran temp tablespace yang menjadi default temp tablespace kita harus meggunakan menggunakan cara berikut :

misalkan tablespace yang akan dirubah adalah temp dan merupakan default tablesapce, langkah pertama kita harus membuat tablespace baru misal temp2, kemudian merubah default tablespace dari temp menjadi temp2, kita rubah kembali default tablespace ke temp dan hapus temp2

SQL> CREATE TEMPORARY TABLESPACE temp2
  2  TEMPFILE '/app/oradata/NEURON/temp2_01.dbf' SIZE 5M REUSE
  3  AUTOEXTEND ON NEXT 1M MAXSIZE unlimited
  4  EXTENT MANAGEMENT LOCAL UNIFORM SIZE 1M;
 
TABLESPACE created.
 
 
SQL> ALTER DATABASE DEFAULT TEMPORARY TABLESPACE temp2;
 
DATABASE altered.
 
 
SQL> DROP TABLESPACE temp INCLUDING CONTENTS AND DATAFILES;
 
TABLESPACE dropped.
 
SQL> CREATE TEMPORARY TABLESPACE temp
  2  TEMPFILE '/app/oradata/NEURON/temp01.dbf' SIZE 500M REUSE
  3  AUTOEXTEND ON NEXT 100M MAXSIZE unlimited
  4  EXTENT MANAGEMENT LOCAL UNIFORM SIZE 1M;
 
TABLESPACE created.
 
 
SQL> ALTER DATABASE DEFAULT TEMPORARY TABLESPACE temp;
 
DATABASE altered.
 
 
SQL> DROP TABLESPACE temp2 INCLUDING CONTENTS AND DATAFILES;
 
TABLESPACE dropped.

untuk oracle 9i ke atas perubahan ukuran tablespace akan lebih mudah dilakukan
berikut cara untuk meresize temp tablespace :

SQL> SELECT tablespace_name, file_name, bytes
  2  FROM dba_temp_files WHERE tablespace_name = 'TEMP';
 
TABLESPACE_NAME   FILE_NAME                                 BYTES
----------------- -------------------------------- --------------
TEMP              /app/oradata/NEURON/temp01.dbf   2,107,200,000
 
 
SQL> ALTER DATABASE TEMPFILE '/app/oradata/NEURON/temp01.dbf' DROP INCLUDING DATAFILES;
 
DATABASE altered.
 
 
SQL> ALTER TABLESPACE temp ADD TEMPFILE '/app/oradata/NEURON/temp01.dbf' SIZE 512m
  2  AUTOEXTEND ON NEXT 250m MAXSIZE UNLIMITED;
 
TABLESPACE altered.
 
 
SQL> SELECT tablespace_name, file_name, bytes
  2  FROM dba_temp_files WHERE tablespace_name = 'TEMP';
 
TABLESPACE_NAME   FILE_NAME                                 BYTES
----------------- -------------------------------- --------------
TEMP              /app/oradata/NEURON/temp01.dbf      536,870,912

terkadang di sistem operasi windows command DROP INCLUDING DATAFILES tidak menghapus file fisik, untuk memastikannya anda perlu mencek terlebih dahulu, jika belum terhapus, hapus file dengan os Command.

Oracle ,

Startup dan Shutdown Oracle

December 24th, 2008

Sebelum melakukan startup atau shutdown cek terlebih dahulu apakah service oracle sudah jalan atau belum :

1. Login ke OS dengan user oracle
2. check listener

#lsnrctl stat

jika hasilnya sepertin dibawah ini :

TNS-12541: TNS:no listener
TNS-12560: TNS:protocol adapter error
TNS-00511: No listener
Linux Error: 111: Connection refused
Connecting TO (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=NeuronServer.site)(PORT=1521)))
TNS-12541: TNS:no listener
TNS-12560: TNS:protocol adapter error
TNS-00511: No listener
Linux Error: 111: Connection refused</code>

berarti listener oracle tidak jalan.

untuk menjalankan ketik

#lsnrctl start

listener bisa dijalankan jika service oracle sudah hidup.

3. Start Up Oracle

1
2
3
#sqlplus /nolog
SQL>conn / AS sysdba
SQL>startup
ORACLE instance started.
 
Total SYSTEM Global Area  608174080 bytes
Fixed SIZE                  1220820 bytes
Variable SIZE             121638700 bytes
DATABASE Buffers          478150656 bytes
Redo Buffers                7163904 bytes
DATABASE mounted.
DATABASE opened.

Kemudian hidupkan listener dan cek kembali listener

#lsnrctl START
LSNRCTL FOR Linux: Version 10.2.0.1.0 - Production ON 24-DEC-2008 09:36:57
 
Copyright (c) 1991, 2005, Oracle.  ALL rights reserved.
 
Starting /app/oracle/product/10.2/bin/tnslsnr: please wait...
 
TNSLSNR FOR Linux: Version 10.2.0.1.0 - Production
SYSTEM parameter FILE IS /app/oracle/product/10.2/network/admin/listener.ora
LOG messages written TO /app/oracle/product/10.2/network/LOG/listener.LOG
Listening ON: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1)))
Listening ON: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=NeuronServer.site)(PORT=1521)))
 
Connecting TO (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1)))
STATUS OF the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR FOR Linux: Version 10.2.0.1.0 - Production
START DATE                24-DEC-2008 09:36:59
Uptime                    0 days 0 hr. 0 MIN. 0 sec
Trace LEVEL               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter FILE   /app/oracle/product/10.2/network/admin/listener.ora
Listener LOG FILE         /app/oracle/product/10.2/network/LOG/listener.LOG
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=NeuronServer.site)(PORT=1521)))
Services Summary...
Service "PLSExtProc" has 1 instance(s).
Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) FOR this service...
The command completed successfully

Oracle , ,

Create Tokenize Function in Oracle

December 24th, 2008

Fungsi tokenizer sering sekali digunakan , jiak kita menggunakan java fungsi itu akan relatif mudah di gunakan karena disudah disediakan oleh java, nah bagaimana kita melakukan fungsi tokenizer di oracle.
berikut ada sample procedure untuk melakukan tokenizer di oracle

The pl/sql Oracle version for the Tokenizer procedure

CREATE OR REPLACE PROCEDURE tokenizer (
         iStart     IN       NUMBER,
         sPattern   IN       VARCHAR2,
         sBuffer    IN       VARCHAR2,
         sResult    OUT      VARCHAR2,
         iNextPos   OUT      NUMBER
        )
AS
     nPos1   NUMBER;
     nPos2   NUMBER;
BEGIN
     nPos1 := INSTR (sBuffer, sPattern, iStart);
    IF nPos1 = 0
    THEN
            sResult := NULL;
    ELSE
           nPos2 := INSTR (sBuffer, sPattern, nPos1 + 1);
          IF nPos2 = 0
          THEN
                 sResult := RTRIM (LTRIM (SUBSTR (sBuffer, nPos1 + 1)));
                 iNextPos := nPos2;
          ELSE
                sResult := SUBSTR (sBuffer, nPos1 + 1, nPos2 - nPos1 - 1);
                iNextPos := nPos2;
          END IF;
     END IF;
END tokenizer;

untuk test nya gunakan script dibawah ini

– a Procedure to Test the Tokenizer

CREATE OR REPLACE PROCEDURE sp_test_tokenizer
AS
      sepr VARCHAR2(1);
      sbuf VARCHAR2(200);
      sres VARCHAR2(200);
      pos NUMBER;
      istart NUMBER;
BEGIN
 
      sbuf := ‘@0@11@222@3333@44444@555555@6666666@77777777@888888888?;
      sepr := ‘@’;
      istart := 1;
      tokenizer (istart ,sepr,sbuf,sres,pos);
      IF (pos &lt;&gt; 0) THEN
          dbms_output.put_line (sres);
      END IF;
     WHILE (pos &lt;&gt; 0)
     LOOP
             istart := pos;
             tokenizer (istart ,sepr,sbuf,sres,pos );
             dbms_output.put_line (sres);
     END LOOP;
END sp_test_tokenizer;
/

dari SQLPLUS

SQL&gt; SET serveroutput ON
SQL&gt;exec sp_test_tokenizer;

silakan mencoba semoga bermanfaat
<sumber dari oracle.com>

Oracle ,