1. Login as root and edit /etc/oratab to reflect “Y”
orcl:/opt/oracle/product/11.1.0/db_1:Y
2. oracle startup script:
#!/bin/bash
#
# oracle Init file for starting and stopping
# Oracle Database. Script is valid for 10g and 11g versions.
#
# chkconfig: 35 80 30
# description: Oracle Database startup script
# Source function library.
. /etc/rc.d/init.d/functions
ORACLE_OWNER="oracle"
ORACLE_HOME="/opt/oracle/product/11.1.0/db_1"
case "$1" in
start)
echo -n $"Starting Oracle DB:"
su - $ORACLE_OWNER -c "$ORACLE_HOME/bin/dbstart $ORACLE_HOME"
su - $ORACLE_OWNER -c "$ORACLE_HOME/bin/lsnrctl start"
echo "OK"
;;
stop)
echo -n $"Stopping Oracle DB:"
su - $ORACLE_OWNER -c "$ORACLE_HOME/bin/dbshut $ORACLE_HOME"
su - $ORACLE_OWNER -c "$ORACLE_HOME/bin/lsnrctl stop"
echo "OK"
;;
*)
echo $"Usage: $0 {start|stop}"
esac
3. Oracle EMCTL
#!/bin/bash
#
# oraemctl Starting and stopping Oracle Enterprise Manager Database Control.
# Script is valid for 10g and 11g versions.
#
# chkconfig: 35 80 30
# description: Enterprise Manager DB Control startup script
# Source function library.
. /etc/rc.d/init.d/functions
ORACLE_OWNER="oracle"
ORACLE_HOME="/opt/oracle/product/11.1.0/db_1"
case "$1" in
start)
echo -n $"Starting Oracle EM DB Console:"
su - $ORACLE_OWNER -c "$ORACLE_HOME/bin/emctl start dbconsole"
echo "OK"
;;
stop)
echo -n $"Stopping Oracle EM DB Console:"
su - $ORACLE_OWNER -c "$ORACLE_HOME/bin/emctl stop dbconsole"
echo "OK"
;;
*)
echo $"Usage: $0 {start|stop}"
esac
4. chkconfig –add /etc/init.d/oracle
5. chkconfig –add /etc/init.d/oraemctl
EXPLAIN PLAN
1. Create the PLAN_TABLE
SQL> $ORACLE_HOME/rdbms/admin/utlxplan.sql
2. Run EXPLAIN PLAN for an SQL
SQL> EXPLAIN PLAN
2 SET STATEMENT_ID = ‘example’ FOR
3 SELECT ename,dname
4 FROM emp inner join dept
5 ON ( emp.deptno = dept.deptno )
6 /
Explained.
3. DISPLAY Results
SQL> $ORACLE_HOME/rdbms/admin/utlxpls.sql
AUTOTRACE is a SQL*Plus facility that may be enabled in your database. To get this up and running you need to have administration rights to the system and perform the following steps:
* Log into SQL*Plus as SYSDBA
* Run the script $ORACLE_HOME/sqlplus/admin/plustrce
* Grant PLUSTRACE to SPATIAL (or to specific users/roles)
To Use:
SQL> SET autotrace ON
SQL> SELECT ename,dname
2 FROM emp INNER JOIN dept
3 ON ( emp.deptno = dept.deptno )
4 /
ENAME DNAME
---------- --------------
SMITH RESEARCH
ALLEN SALES
WARD SALES
JONES RESEARCH
MARTIN SALES
BLAKE SALES
CLARK ACCOUNTING
SCOTT RESEARCH
KING ACCOUNTING
TURNER SALES
ADAMS RESEARCH
JAMES SALES
FORD RESEARCH
MILLER ACCOUNTING
14 rows selected.
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=3 Card=14 Bytes=252)
1 0 HASH JOIN (Cost=3 Card=14 Bytes=252)
2 1 TABLE ACCESS (FULL) OF 'DEPT' (Cost=1 Card=4 Bytes=44)
3 1 TABLE ACCESS (FULL) OF 'EMP' (Cost=1 Card=14 Bytes=98)
Statistics
----------------------------------------------------------
0 recursive calls
4 db block gets
3 consistent gets
0 physical reads
0 redo size
1132 bytes sent via SQL*Net TO client
503 bytes received via SQL*Net FROM client
2 SQL*Net roundtrips TO/FROM client
0 sorts (memory)
0 sorts (disk)
14 rows processed
Courtesy of Beginning Oracle Programming by Sean Dillon et al
Read more…
It was a gruesome day.. I have an oracle10g on my MacBookPro running on VMWare (WindowsXPSP3, let’s call this orawin). I have another setup box (CentOS5 + oracle11g = oralin) just this morning. I imported the tables from my orawin10g to my oralin11g, created the indexes for the tables in oralin11g. I ran my spatial test query on orawin10(0.692192571 secs); I ran the same query on oralin11g (14.89 secs). I viewed the execution plans and both are the same!
Eventually, whatever works.. I have to drop the oracle11g and install Oracle10g instead. I have updated the Tutorials Page to include this, as well as the Oracle CheatSheet.
References:
Comparison of different SQL implementations
Oracle DataTypes
Part 1: Oracle Misc Information
*Oracle Services Running on Windows?

* How to create a user?
CREATE USER "APPDEV" PROFILE "DEFAULT" IDENTIFIED BY "*******" DEFAULT TABLESPACE "USERS" TEMPORARY TABLESPACE "TEMP" ACCOUNT UNLOCK
GRANT UNLIMITED TABLESPACE TO "APPDEV";
GRANT "CONNECT" TO "APPDEV";
GRANT "RESOURCE" TO "APPDEV";
* How to load data in bulk?
SQLLDR username/password CONTROL=filename DATA=filename
Note:
1. You can specify the CHARACTERSET UTF8 in your control file for multilingual databases.
LOAD DATA
CHARACTERSET UTF8
INFILE *
REPLACE INTO TABLE LOADER_TEST
FIELDS TERMINATED BY ';'
TRAILING NULLCOLS (
USR_ID INTEGER EXTERNAL,
USR_NAME CHAR(50),
USR_LNK_NAME CHAR(50),
USR_LNK_ORDER INTEGER EXTERNAL
)
2. Sometimes SQLLDR does not display anything when loading. Be sure to issue a “commit” command before doing anything.
* How to use EXPORT / IMPORT?
EXP scott/tiger@instance FILE=geo_entities.dmp TABLES=geo_entities
IMP sysman/password FILE=/path/TO/geo_entities.dmp FROMUSE=scott TOUSER=appdev IGNORE=Y INDEXES=N TABLES=geo_entities
Note: Delete records first before importing
* What is the database encoding of my database?
SELECT * FROM V$NLS_PARAMETERS

Part 2: Oracle SQL
* ADD COLUMN in a TABLE
ALTER TABLE table_name ADD (column_name NUMBER);
* CHANGE COLUMN NAME in a TABLE
ALTER TABLE geo_entities MODIFY meta_name varchar(255)
* DROP COLUMN NAME in a TABLE
ALTER TABLE table_name DROP COLUMN column_name;
* ADD PRIMARY KEY CONSTRAINT on a COLUMN
ALTER TABLE table_name ADD CONSTRAINT table_name_col_pk PRIMARY KEY(column_name);
* Select Top N rows
SELECT * FROM TABLE_NAME WHERE ROWNUM <= 100
* CREATING AN AUTO INCREMENT COLUMN
CREATE SEQUENCE seq_table_name_pk INCREMENT BY 1 START WITH 10000;
INSERT INTO table_name (pid, en_name)
SELECT seq_table_name_pk.NEXTVAL, en_name FROM other_table
* Concatenating Strings
SELECT concat('hello', 'rupert') FROM DUAL;
SELECT 'hello' || 'rupert' FROM DUAL;
* UPDATE TABLE
UPDATE poi_temp pt
SET geom = (SELECT geom FROM poi_app WHERE poi_id = pt.poi_id)
WHERE EXISTS (SELECT 1 FROM poi_app WHERE poi_id = pt.poi_id)
UPDATE poi_app pa
SET (long_900913, lat_900913) = (SELECT pg.long_900913, pg.lat_900913 FROM poi_app_900913 pg WHERE pa.poi_id = pg.poi_id)
WHERE EXISTS (SELECT 1 FROM poi_app_900913 pg WHERE pa.poi_id = pg.poi_id)
Part 3: Oracle Spatial
* What is SDO_GEOMETRY?
SDO_GEOMETRY{
SDO_GTYPE, - GeometryType: D00T
D: Dimension (2: 2d, 3: 3d, 4:4d)
T:GeometryType(
0 - unknown
1 - point
2 - line
3 - polygon
4 - collection
5 - multipoint
6 - multiline
7 - multipolygon
)
SDO_SRID,
SDO_POINT, - NULL for line, polygon, etc.
SDO_ELEM_INFO,
SDO_ORDINATES
}
* How to know the geometry type?
THE_GEOM(SDO_GTYPE, SDO_SRID, SDO_POINT(X, Y, Z), SDO_ELEM_INFO, SDO_ORDINATES)
--------------------------------------------------------------------------------
SDO_GEOMETRY(3, 4326, NULL, SDO_ELEM_INFO_ARRAY(1, 3, 1), SDO_ORDINATE_ARRAY(116
.281578, 39.854501, 116.281491, 39.853828, 116.281236, 39.853181, 116.280821, ....39
.855174, 116.281578, 39.854501))
THE_GEOM(SDO_GTYPE, SDO_SRID, SDO_POINT(X, Y, Z), SDO_ELEM_INFO, SDO_ORDINATES)
--------------------------------------------------------------------------------
SQL> SELECT g.the_geom.sdo_gtype FROM geo_entities g WHERE rownum <= 1;
THE_GEOM.SDO_GTYPE
------------------
3
* How to create a spatial index?
INSERT INTO USER_SDO_GEOM_METADATA VALUES
(
'table_name', -- TABLE_NAME
'geom', -- COLUMN_NAME
SDO_DIM_ARRAY -- DIMINFO attribute for storing dimension bounds, tolerance
(
SDO_DIM_ELEMENT
(
'LONGITUDE', -- DIMENSION NAME for first dimension
-180, -- SDO_LB for the dimension
180, -- SDO_UB for the dimension
0.5 -- Tolerance of 0.5 meters
),
SDO_DIM_ELEMENT
(
'LATITUDE', -- DIMENSION NAME for second dimension
-90, -- SDO_LB for the dimension
90, -- SDO_UB for the dimension
0.5 -- Tolerance of 0.5 meters
)
),
4326 -- SRID value for specifying a geodetic coordinate system
);
CREATE INDEX idx_poi_app_geom ON poi_app(geom) INDEXTYPE IS MDSYS.SPATIAL_INDEX;
CREATE INDEX idx_poi_app_geom ON poi_app(geom)
INDEXTYPE IS MDSYS.SPATIAL_INDEX PARAMETERS('LAYER_GTYPE=POINT')
* How to VALIDATE a geometry?
SELECT SDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXT(GEOM, 0.000000005) FROM table_name
* SPATIAL INTERSECTION
SELECT pc.postcode, au.authority_name, au.state, au.authority_type
FROM econ_authorities_valid_temp au, econ_postcodes pc
WHERE pc.POSTCODE = 3128
AND SDO_ANYINTERACT(
pc.GEOM,
au.GEOM
) = 'TRUE
Note:
1. Both geometries should be in the same projection.
* Get POINTS in a POLYGON using VERTICES
SELECT SDO_UTIL.GETVERTICES( GEOM ) FROM ECON_AUTHORITIES WHERE AUTHORITYID = 90009
Experiment 1: Two POIs
SELECT
p1.poi_id,
p1.en_name,
p1.py_fullpoiadd,
p1.py_rdname,
p2.poi_id,
p2.en_name,
p2.py_fullpoiadd,
p2.py_rdname,
SDO_GEOM.SDO_DISTANCE(p1.geom, p2.geom, 0.5) AS dist
FROM poi_test p1, poi_test p2
WHERE
Upper(p1.en_name) LIKE '%PARKSON%'
AND Upper(p2.en_name) LIKE '%KFC%'
AND SDO_WITHIN_DISTANCE(p1.geom, p2.geom, 'DISTANCE=500 UNIT=METER' )='TRUE'
ORDER BY dist
Experiment 2: Road + Two POIs
SELECT
p1.poi_id,
p1.en_name,
p1.py_fullpoiadd,
p1.py_rdname,
p2.poi_id,
p2.en_name,
p2.py_fullpoiadd,
p2.py_rdname,
SDO_GEOM.SDO_DISTANCE(p1.geom, p2.geom, 0.5) AS dist
FROM poi_app p1, poi_app p2, geo_entities g
WHERE
Upper(p1.en_name) LIKE '%BAR BLU%'
AND Upper(p2.en_name) LIKE '%KOKOMO%'
AND Upper(g.meta_name) LIKE '%SANLITUN%'
AND SDO_WITHIN_DISTANCE(p1.geom, p2.geom, 'DISTANCE=500 UNIT=METER' )='TRUE'
AND SDO_WITHIN_DISTANCE(p1.geom, g.the_geom, 'DISTANCE=500 UNIT=METER' )='TRUE'
AND SDO_WITHIN_DISTANCE(p2.geom, g.the_geom, 'DISTANCE=500 UNIT=METER' )='TRUE'
Notes:
1. Significant improvement when Sorting is removed.
2. SDO_WITHIN_DISTANCE vs NN? The first finds the nearest geometry within a given distance while NN finds the nearest geometry regardless of the distance. NN could be costly when unused properly.