Review Gadget

descriptions of all the gadgets specification.

12

Saving Files to Oracle database

In SQL * Plus Oracle 11g, we can perform storage and retrieval operations from and to file an Oracle database, which previously could not do. In previous versions, if we try to query the type of blob field or bfile, SQL * Plus will display the message "SP2-0678: Column or attribute type can not be displayed by SQL * Plus". Here's my experiment in Oracle 11g to perform operations with the type of file storage BFILE in Oracle 11g using SQL * Plus:

SQL> create table album(id number(5) primary key, keterangan varchar(1000), foto bfile);

SQL> select * from album;

no rows selected

SQL> create or replace directory ALBUMDIR as ‘/home/oracle/Pictures’;

(jika ada pesan ORA-00990: missing or invalid privilege, berikan hak CREATE ANY DIRECTORY kepada userAnda).

SQL> create sequence albumseq;

SQL> insert into album values (albumseq.nextval, ‘komik koran spiderman’, bfilename(‘ALBUMDIR’, ‘2.png’));

SQL> col keterangan format a30;

SQL> select * from album;

ID KETERANGAN
———- ——————————
FOTO

1 komik koran spiderman
bfilename(‘ALBUMDIR’, ‘2.png’)

Thus for example the use BFILE. For BLOB data type, we can try to enter the EMPTY_BLOB function () to see that the SQL * Plus in Oracle 11g can also be displayed.

For example the Java programming of reading the contents of files stored in a BFILE column, please refer kehttp: / / www.oracle.com / technology / sample_code / tech / java / sqlj_jdbc / files / advanced / BFILESample / Readme.html. Thanks.

AutoNumber in Oracle

Basically, Oracle does not have facilities like Numer Auto AutoIncrement in SQLServer or mySQL. But could diakalin using a combination of sequence and trigger. And the following example:

1. CREATE SEQUENCE nama-sequence;
2. CREATE OR REPLACE TRIGGER nama-trigger
3. BEFORE INSERT ON nama-tabel
4. FOR EACH ROW
5. WHEN (NEW.nama-field IS NULL OR NEW.nama-field = ‘’)
6. BEGIN
7. SELECT ‘PR-’ || nama-sequence.NEXTVAL INTO :NEW.nama-field FROM DUAL;
8. END;
9. /

Or it could be in the following ways:

1. CREATE TABLE contoh (
2. latihan_id INTEGER PRIMARY KEY,
3. );
4.
5. CREATE SEQUENCE contoh_seq;
6. CREATE TRIGGER contoh_seq_trigger
7. BEFORE INSERT ON contoh FOR EACH ROW
8.
9. BEGIN
10. IF (:new.latihan_id IS NULL) THEN
11. SELECT contoh_seq.nextval INTO :new.latihan_id
12. FROM DUAL;
13. END IF;
14. END;
15. /

Variable manipulation PL / SQL in Oracle heterogeneous System

Oracle heterogeneous system. My Oracle HS agaar short easy to remember. What's that? Oracle HS is a system that allows for manipulation of other databases outside the system databse being contacted. Other systems can be an Oracle database, or a non-Oracle databases, such as SQL Server or MySQL.

With Oracle HS system settings, we can do SQL query on other databases on our Oracle session. For example we want to fetch the data in a table in another database, then query it will be like this:

SELECT * FROM rfq@hsmsql

There are additional @ hsmsql which shows HS Oracle database link that has been set-up before (HS Oracle setup is not discussed in this post).

Well, the problem occurs when the Oracle HS in the script used in PL / SQL. Oracle HS can not read or fill in the variables PL / SQL. Consider queries PL / SQL the following:

SELECT Count( * ) INTO v_count FROM rfq@hsmsql

SELECT * FROM rfq@hsmsql WHERE rfqnum = v_rfqnum

In the normal query with no Oracle HS, the query above can work well. v_count row will contain the number obtained from table RFQ, whereas the second query will be filtered for rows that have the same rfqnum with v_rfqnum. But the Oracle did not HS. Oracle HS can not enter the Count (*) into a variable v_count and can not filter the content rfqnum record variabel v_rfqnum.

The solution, we can use a mechanism called-SQL Passthrough. There are built-in package from Oracle named special DBMS_HS_PASSTHROUGH used to obtain data and / or vice versa from / to Oracle HS. Here is how to get results from Oracle HS and stored on variables PL / SQL

con := DBMS_HS_PASSTHROUGH.open_cursor@hsmsql.world;

DBMS_HS_PASSTHROUGH.parse@hsmsql.world (con,

‘SELECT COUNT( * ) FROM rfq_mxm
WHERE rfqnum = ”’
|| vrfq.rfqnum
|| ””
);

rs := DBMS_HS_PASSTHROUGH.fetch_row@hsmsql.world (con);

DBMS_HS_PASSTHROUGH.get_value@hsmsql.world (con, 1, vcount);

DBMS_OUTPUT.put_line (’count: ‘ || vcount);

If the code is described conceptually above, then the steps are as follows:

1. Open the cursor HS (meaning here is different cursor with the understanding PL / SQL cursor)
2. Parse the query with the function
3. Pull the results of each line and enter into a variable and PL / SQL function get_value

It is much more complicated, but this is the safest way to manipulate the variables PL / SQL with heterogeneous systems (Oracle HS).

SQL Tuning: EXISTS Clause vs. IN

Still on the issue on Oracle performance tuning SQL Query. In the case of correlated subquery, we used to use IN and NOT IN. Eg

SELECT * FROM tabel1 a
WHERE a.kolom1 NOT IN
( SELECT b.kolom1 FROM tabel2 b)

In the same case, we can also use the EXISTS clause and NOT EXISTS. Eg

SELECT * FROM tabel1 a
WHERE a.kolom1 NOT EXISTS
( SELECT b.kolom1 FROM tabel2 b)

What is the difference of two this SQL statement? IN clause and NOT IN check if a value contained in the list (can be array, can be correlated subquery in the example above), while clause EXISTS and NOT EXISTS check only whether or not the existence of a list row. In performance, certainly much faster than IN EXISTS correlated subquery in the case.

When do we use IN? Wherever possible, use EXISTS, IN used in the case such as this example below:

SELECT * FROM tabel1 a
WHERE a.kolom1 IN (’1′, ‘2′, ‘3′)

SQL Tuning: String Functions in the WHERE clause

This is the Oracle SQL Performance Tuning tips and trick from Mas FF. Say we have a SQL like this,

SELECT *
FROM tabel t
WHERE LOWER(t.kol)
LIKE ‘%hehe%’ AND SUBSTR(t.kol2,2,4) = ‘AB’

Look familiar? This is a standard query kok. But do you know the implications and substr LOWER function in velocity problem? Apparently the LOWER and substr is to lower-case an entire row in the column and t.kol substring to an entire row in the column before comparing it with t.kol2 hehe and AB. If the number of line too much, of course, speed degradation will occur very significant (read: insignificant).

Solution: Use the sort function and substr LOWER wisely. If you are sure of a column in lowercase, then the right input (in this case hehe) should be changed to lowercase as well. Similarly to substr function.

Tips and tricks so short SQL Tuning. : D

copy Database to the Same Machine

copy of the database on another machine, it was the same as conventional backup & recovery. Copying a database on the same machine, there is little difference because in one machine may not have 2 (or more) the same database; a step to change the name of the database.

I have 1 database development, database name is ts2. For test purposes, I need another database on the development machine. New database content is the same as the previous database. How do I make it, there are two ways:

1. Creating a new database, export from the old database, then import into new database.
2. Copying the old database to the new one, as if the backup and recovery

The first way, of course, takes a long time, especially if a large enough data. Then I choose the latter; my first database backup, then I raise (restore & recovery) with a different name (I named tsrep). Here are the steps:

1. Prepare (copy) init file. Examples here, I use UNIX. In Windows, the location of the file init in% ORACLE_HOME% \ database

cd $ORACLE_HOME/dbs
cp -rp initts2.ora inittsrep.ora

Suppose inittsrep.ora content is the following:

### Parameter ini perlu disesuaikan ###
audit_file_dest='/data1/oracle/admin/ts2/adump'
background_dump_dest='/data1/oracle/admin/ts2/bdump'
core_dump_dest='/data1/oracle/admin/ts2/cdump'
user_dump_dest='/data1/oracle/admin/ts2/udump'
control_files='/oradata/oracle/ts2/control01.ctl', '/oradata/oracle/ts2/control02.ctl', '/oradata/oracle/ts2/control03.ctl'
log_archive_dest_1='LOCATION=/oradata/oracle/ts2/arc'
db_name='ts2'
####################################
log_archive_dest_state_1=enable
log_archive_format=%s_%t_%r.arc
compatible='10.2.0.3.0'
db_block_size=8192
db_domain=''
pga_aggregate_target=209715200
sga_target=1610612736
undo_management='AUTO'
undo_tablespace='UNDOTBS1'
remote_login_passwordfile='EXCLUSIVE'

I adjust some parameters, especially db_name and related directories. Here are the parameters I have been customized:

audit_file_dest='/data1/oracle/admin/tsrep/adump'
background_dump_dest='/data1/oracle/admin/tsrep/bdump'
core_dump_dest='/data1/oracle/admin/tsrep/cdump'
user_dump_dest='/data1/oracle/admin/tsrep/udump'
control_files='/oradata/oracle/tsrep/control01.ctl', '/oradata/oracle/tsrep/control02.ctl', '/oradata/oracle/tsrep/control03.ctl'
log_archive_dest_1='LOCATION=/oradata/oracle/tsrep/arc'
db_name='tsrep'

Prepare a directory for files and other data

mkdir /data1/oracle/admin/tsrep
mkdir /data1/oracle/admin/tsrep/adump
mkdir /data1/oracle/admin/tsrep/bdump
mkdir /data1/oracle/admin/tsrep/cdump
mkdir /data1/oracle/admin/tsrep/udump
mkdir /oradata/oracle/tsrep
mkdir /oradata/oracle/tsrep/arc

Prepare script to create database
In the old database (source):

SQL> alter database backup controlfile to trace resetlogs;

See trace file in the directory user_dump_dest, / data1/oracle/admin/ts2/udump
SQL> sho parameter user_dump_dest

Copy the trace file

cd /data1/oracle/admin/ts2/udump
cp ts2_ora_18762.trc /oradata/oracle/tsrep/crdbtsrep.sql

Edit crdbtsrep.sql file, remove "trace file entry", to be like this

STARTUP NOMOUNT
CREATE CONTROLFILE REUSE DATABASE "TS2" RESETLOGS ARCHIVELOG
MAXLOGFILES 16
MAXLOGMEMBERS 3
MAXDATAFILES 100
MAXINSTANCES 8
MAXLOGHISTORY 292
LOGFILE
GROUP 4 '/oradata/oracle/ts2/redo04.log' SIZE 5M,
GROUP 5 '/oradata/oracle/ts2/redo05.log' SIZE 5M,
GROUP 6 '/oradata/oracle/ts2/redo06.log' SIZE 5M
-- STANDBY LOGFILE
DATAFILE
'/oradata/oracle/ts2/system01.dbf',
'/oradata/oracle/ts2/undotbs01.dbf',
'/oradata/oracle/ts2/sysaux01.dbf',
'/oradata/oracle/ts2/users01.dbf'
CHARACTER SET WE8ISO8859P1
;

Edit crdbtsrep.sql file again.
Replace this entry

CREATE CONTROLFILE REUSE DATABASE “TS2″ RESETLOGS ARCHIVELOG

Become

CREATE CONTROLFILE SET DATABASE “TS2REP” RESETLOGS ARCHIVELOG

Adjust the directory-related directory. In this example I changed

/oradata/oracle/ts2/

become

/oradata/oracle/tsrep/

After some editing, crdbtsrep.sql files will be

STARTUP NOMOUNT
CREATE CONTROLFILE SET DATABASE "TSREP" RESETLOGS ARCHIVELOG
MAXLOGFILES 16
MAXLOGMEMBERS 3
MAXDATAFILES 100
MAXINSTANCES 8
MAXLOGHISTORY 292
LOGFILE
GROUP 4 '/oradata/oracle/tsrep/redo04.log' SIZE 5M,
GROUP 5 '/oradata/oracle/tsrep/redo05.log' SIZE 5M,
GROUP 6 '/oradata/oracle/tsrep/redo06.log' SIZE 5M
-- STANDBY LOGFILE
DATAFILE
'/oradata/oracle/tsrep/system01.dbf',
'/oradata/oracle/tsrep/undotbs01.dbf',
'/oradata/oracle/tsrep/sysaux01.dbf',
'/oradata/oracle/tsrep/users01.dbf'
CHARACTER SET WE8ISO8859P1
;

Copy (backup and restore) a database source
If NOARCHIVELOG database, do a cold (off line) backups. Because ts2 ARCHIVELOG database, then I can do is hot (on line) backups. In this example I use hotbackup.Lihat list datafile and tempfile that needs to be copied

select name as file_name from
(select name from v$tempfile union
select name from v$datafile);
/oradata/oracle/ts2/sysaux01.dbf
/oradata/oracle/ts2/system01.dbf
/oradata/oracle/ts2/temp01.dbf
/oradata/oracle/ts2/undotbs01.dbf
/oradata/oracle/ts2/users01.dbf

See tablespace to the backup

SQL> select distinct tablespace_name from dba_data_files;
TABLESPACE_NAME
------------------------------
SYSTEM
USERS
SYSAUX
UNDOTBS1

Before running BEGIN backup, see "Current log sequence". This information we need to see any archived logs are later required to raise the database. In this instance, "Current log sequence" is 17.

SQL> archive log list

Run BEGIN BackUp-tablespaces in the tablespace (if cold backup, this step is replaced by a database shutdown):

SQL> alter tablespace SYSTEM begin backup;
SQL> alter tablespace USERS begin backup;
SQL> alter tablespace SYSAUX begin backup;
SQL> alter tablespace UNDOTBS1 begin backup;

Copy datafile and tempfile

cd /oradata/oracle/ts2/
cp -rp sysaux01.dbf /oradata/oracle/tsrep
cp -rp system01.dbf /oradata/oracle/tsrep
cp -rp temp01.dbf /oradata/oracle/tsrep
cp -rp undotbs01.dbf /oradata/oracle/tsrep
cp -rp users01.dbf /oradata/oracle/tsrep

After a datafile copy process is complete, run the BackUp END tablespace tablespace-related (if cold backup, this step is replaced with a startup database):

SQL> alter tablespace SYSTEM end backup;
SQL> alter tablespace USERS end backup;
SQL> alter tablespace SYSAUX end backup;
SQL> alter tablespace UNDOTBS1 end backup;

When finished END backups, see the value of "Current log sequence". In this example the value was 18.

SQL> archive log list

Run "archive log current" to create archived logs from sequence 18.

SQL> alter system archive log current

Archived logs required to raise the database is archived log sequence before BEGIN END seteleh backups and backups. Thus, sequences 17 and 18.

Further copies of the required archived log is

cd /oradata/oracle/ts2/arc/
cp -rp 17_1_658171224.arc /oradata/oracle/tsrep/arc
cp -rp 18_1_658171224.arc /oradata/oracle/tsrep/arc

Preparation before raising database
Setting ORACLE_SID, in csh shell

setenv ORACLE_SID tsrep

Ksh shell

export ORACLE_SID=tsrep

In Windows

set ORACLE_SID=tsrep

Especially in Windows, for instance (service) oradim use. Make sure that the file was created inittsrep.ora.

cd %ORACLE_HOME%\database
oradim -NEW -SID tsrep

Raise (create) the database

SQL> @/oradata/oracle/tsrep/crdbtsrep.sql

Perform recovery. With automatic specify, Oracle will search for archived log files alone.

SQL> RECOVER AUTOMATIC DATABASE USING BACKUP CONTROLFILE;

In this example, the required archived log is sequence 17 to 18. After a sequence of 18-apply, Oracle still asking squence 19. Because the sequence 19 does not exist, then the error recovery. Ignore these errors. Next:

SQL> RECOVER DATABASE until cancel;

If the database in sync, the above command should work. If Oracle is still asked archived logs for recovery, copy it again from its source databases. If the source database archived log has not been create, yes run "alter system archive log current", or if you want, just stop using the logfile recover the source database (not archived log). Suppose still required archived log sequence 19, while sequence is not dicreate archived logs. Use the following command to get logfilenya

SQL> -- di database source (ts2)
SQL> select member from v$logfile where GROUP# in
(select GROUP# from v$log where SEQUENCE#=19);

Next, use the logfile above for recovery in tsrepSetelah database recovery process is successful, open the database:

SQL> ALTER DATABASE OPEN RESETLOGS;

Next, add the temp files:

SQL> ALTER TABLESPACE TEMP ADD
TEMPFILE '/oradata/oracle/tsrep/temp01.dbf'
SIZE 20971520 REUSE AUTOEXTEND OFF;
Posted by audio at 2:21 PM 0 comments
Labels: copy Database to the Same Machine
PHP with Oracle Connection 2: Sample Program
In this tutorial, we will use the following oracle table as its data source. Table name is test.

nama
kota
Prothelord
New York
Strawberi
Bangkok

Our goal is to display the name and city data from the table. But before you start, you need to seek prior information about the Oracle login information is usually called the TNS name (you can compare with the user, password and IP dariMySQL server). TNS name consists of:

- IP Server Oracle
- Port is in use (if MySQL 3306, remember?)
- SID
- User
- Password
Try asking the same Oracle admin at your place on such information.

If it can, try the following programs to edit the configuration TNS Name which you have from your Oracle admin. In this example, we use the TNS name configuration as follows:

- IP: 10.2.2.2
- Port: 1523
- SID: TEST
- User: your name
- Password: pwd

Here's an example program to access the Oracle tables:

$ db = "(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (HOST = 10.2.2.2) (PORT = 1523))) (CONNECT_DATA = (SID = TEST)))";
$ c1 = ocilogon ( "name", "pwd", $ db);
$ code = "select * from test";
$ stmt = ociparse ($ c1, $ code);
ociexecute ($ stmt, OCI_DEFAULT);

/ / Displaying data from test table

while (ocifetch ($ stmt)) (
ociresult echo ($ stmt, 0 ).",". ociresult ($ stmt, 1);
echo ( "
");
)
ocilogoff ($ c1);
?>
When finished, save the name and call coba_oracle.php from the browser. You should see the following results:


Prothelord, New York
Strawberry, Bangkok


Bye, cuman kok gitu doang. Easy yach?

Good luck, and hopefully useful.
Best regards.
Prothelord.