วันพุธที่ 28 มกราคม พ.ศ. 2552

ROWID


rowid เ็ป็น column ที่ unique เก็บ address ของ row นั้นๆ

SQL> select rowid, id from A;

ROWID                      ID
------------------ ----------
AAAUd2AAHAAAACTAAB          1
AAAUd2AAHAAAACTAAC          2

ตั้งแต่ Oracle Release 8i เราจะพบ Extended Rowids ซึ่งจะเก็บด้วย BASE64 Encoding
ซึ่่งแบ่งเป็น 4 ส่วน

- OOOOOO: เรียกว่า data object number จะเป็นตัวบอกเกี่ยวกับ segment 
- FFF: ตัวบอกกี่ยวกับ datafile number
- BBBBBB: เป็นตัวบอกว่าอยู่บน block number อะำไรของ datafile...
- RRR: row...

 SELECT ROWID, 
   SUBSTR(ROWID,1,6) "OBJECT", 
   SUBSTR(ROWID,7,3) "FIL", 
   SUBSTR(ROWID,10,6) "BLOCK", 
   SUBSTR(ROWID,16,3) "ROW" from A;

ROWID              OBJECT                   FIL          BLOCK                    ROW
------------------ ------------------------ ------------ ------------------------ ------------
AAAUd2AAHAAAACTAAB AAAUd2                   AAH          AAAACT                   AAB
AAAUd2AAHAAAACTAAC AAAUd2                   AAH          AAAACT                   AAC

อย่างไรก็ตามเราสามารถหาข้อมูล เกี่ยวกับ rowid โดยใช้ dbms_rowid

> เช็ค datafile ที่ rowid  อยู่

SQL> select file_name
  2   from dba_data_files
  3   where file_id = dbms_rowid.rowid_to_absolute_fno('AAAUd2AAHAAAACTAAB', user, 'A');

FILE_NAME
--------------------------------------------------------------------------------
+DATA1/test.274.673800173


> เช็ค block ที่  rowid อยู่

SQL> select dbms_rowid.rowid_block_number('AAAUd2AAHAAAACTAAB') from dual;

DBMS_ROWID.ROWID_BLOCK_NUMBER('AAAUD2AAHAAAACTAAB')
---------------------------------------------------
                                                147


> และสามารถเช็คได้ว่า อยู่ที่ row ไหน

SQL> select dbms_rowid.rowid_row_number('AAAUd2AAHAAAACTAAB') from dual;

DBMS_ROWID.ROWID_ROW_NUMBER('AAAUD2AAHAAAACTAAB')
-------------------------------------------------
                                                1

เรายังสามารถใช้ rowid ได้มากมาย...

วันเสาร์ที่ 10 มกราคม พ.ศ. 2552

เปลี่ยน PORT 8080 บน Oracle Express


เมื่อลง oracle express เราจะเข้าใช้งานเว็บ

http://localhost:8080/apex

แต่หลายคนจะเกิดปัญหาเพราะว่า Port นี้ถูใช้ด้วย Program อื่นๆๆ เราสามารถเปลี่ยนได้ ด้วย dbms_xdb package.

dbms_xdb.sethttpport('new_port'); ใช้เปลี่ยนport http

dbms_xdb.setftpport('new_port'); ใช้เปลี่ยนport ftp

ตัวอย่าง
sqlplus / as sysdba SQL> select dbms_xdb.gethttpport , dbms_xdb.getftpport from dual;
GETHTTPPORT GETFTPPORT
----------- ----------
8080 0

SQL> begin
2 dbms_xdb.sethttpport('8081');
3 end;
4 /
PL/SQL procedure successfully completed.
SQL> select dbms_xdb.gethttpport , dbms_xdb.getftpport from dual;
GETHTTPPORT GETFTPPORT
----------- ----------
8081 0

Test.... http://localhost:8081/apex/

วันพฤหัสบดีที่ 25 ธันวาคม พ.ศ. 2551

Export/Import จาก version สูงกว่า ไป version ต่ำกว่า

หากต้องการ เอาข้อมูลจาก oracle ที่ version สูงกว่า ไปลง version ต่ำกว่า
ใน version 10g ขึ้นไป ทุกคนจะรู้จัก DATA PUMP(expdp/impdp) 

ในบทความนี้ จะแสดงตัวอย่าง การนำ table จาก 10.2. to 10.1
option ที่สำคัญ คือ version, ซึ่งคือ version (compatible) ของ database ที่เราจะ import ลง

เตรียม parfile สำหรับ export

parfile:
DIRECTORY=DIR
dumpfile=01.dmp
logfile=01.log
tables=t01
VERSION=10.1.0

$ expdp parfile=parfile

Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, OLAP, Data Mining
and Real Application Testing options
Starting "USERNAME"."SYS_EXPORT_TABLE_01":  USERNAME/******** parfile=parfile 
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 15 MB
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
. . exported "USERNAME"."T01"                       4.921 KB       1 rows
Master table "USERNAME"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for USERNAME.SYS_EXPORT_TABLE_01 is:
  /tmp/01.dmp
Job "USERNAME"."SYS_EXPORT_TABLE_01" successfully completed at 16:45:05


เมื่อได้ dump file จากนั้นก็ เตรียม parfile สำหรับ import

parfile:
DIRECTORY=DIR
dumpfile=01.dmp
tables=t01
logfile=01.log

$ impdp parfile=parfile

Connected to: Oracle Database 10g Enterprise Edition Release 10.1.0.4.0 - Production
With the Partitioning, OLAP and Data Mining options
Master table "USERNAME"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded
Starting "USERNAME"."SYS_IMPORT_TABLE_01":  USERNAME/******** parfile=parfile 
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
. . imported "USERNAME"."T01"                       4.921 KB       1 rows
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Job "USERNAME"."SYS_IMPORT_TABLE_01" successfully completed at 16:53


ถ้าไม่ได้ทำ compatible (version) ตั้งแต่ export จะพบ error ตอน import

ORA-39001: invalid argument value
ORA-39000: bad dump file specification
ORA-39142: incompatible version number x.y in dump file ...