Archive

Archive for August, 2008

Mac Shortcuts

August 31st, 2008 rupert Comments off
Categories: mac, mysql Tags:

Postgres PostGIS CheatSheet v2

August 29th, 2008 rupert Comments off

This is a quick-command list of Postres. If you want detailed instructions, please visit the Postgres Manual.

How do I Show all databases?
1. Using “psql -l”

2. Using

postgres=# \l
List of databases
Name       |  Owner   | Encoding
------------------+----------+----------
postgis          | postgres | UTF8
postgres         | postgres | UTF8
template0        | postgres | UTF8
template1        | postgres | UTF8
template_postgis | postgres | UTF8
(5 rows)

Note: Do not drop template databases if not necessary.

How do I run a script from the prompt?
psql -d cybersoftbj -u user -f myfile.sql

Its very useful in reloading user-defined functions.

How do I create a user/role?

CREATE ROLE lbs WITH LOGIN PASSWORD 'mypassword' SUPERUSER INHERIT CREATEDB CREATEROLE;

How do I change the password for a user/role?

ALTER ROLE lbs PASSWORD 'mynewpassword';

How to provide/restrict access privileges to tables?

GRANT SELECT ON TABLE table TO user;
REVOKE SELECT ON TABLE table FROM user;

How to dump database in a text file?

pg_dump -U lbs -d cybersoftbjv1 -h 127.0.0.1 -W > cybersoftbjv1.sql

How to dump database cleanly?

pg_dump -c -d -E UTF8 -h 127.0.0.1 -U lbs -W platform_v1 > platform_v1.sql

How to rename a database?

ALTER DATABASE beijing_app RENAME TO beijing_app_20080801;

How to update using two tables?

UPDATE road_for_update u
SET the_geom = r.the_geom
FROM roads r
WHERE r.rd_id = u.rd_id;

How to change a column type with Cast?

ALTER TABLE roads ALTER COLUMN class_new TYPE integer USING class_new::integer;

How to add a geometry column to a table?
EXAMPLE: SELECT AddGeometryColumn(‘public’, ‘poi’, ‘the_geom’, 4326, ‘POINT’, 2)

Changing column names with spaces?
ALTER TABLE class_aroundme RENAME "level 1" TO level_1;

Setting kernel shmmax for postgres
sysctl -w kernel.shmmax=134217728
Note: For permanent changes see /etc/sysctl.cfg11. How to backup table(s) from pg_dump?
pg_dump poi_beijing -t class -t poi_class -f $BACKUPDIR/test_$MYDATE.sql

Categories: postgis, postgres Tags: ,

Oracle SQL CheatSheet

August 27th, 2008 rupert 1 comment

References:
Comparison of different SQL implementations

Oracle DataTypes

Part 1: Oracle Misc Information

*Oracle Services Running on Windows?
Oracle Services Running on Windows.png

* 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

Picture 1.png

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
Categories: oracle Tags: ,

Using SDO_WITHIN_GEOM

August 27th, 2008 rupert Comments off

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.

Categories: oracle Tags:

Oracle Text Notes

August 27th, 2008 rupert Comments off

Just a couple of notes from studying Oracle Text…

1. What is the default index?
Its CONTEXT. The text column can be of type CLOB, BLOB, BFILE, VARCHAR2, or CHAR.

CREATE INDEX idx_ft_meta_en_name ON poi_app(ft_meta_en_name) INDEXTYPE IS CTXSYS.CONTEXT;

2. When you perform inserts or updates on the base table, you must explicitly synchronize the index with CTX_DDL.SYNC_INDEX.

SQL> EXEC CTX_DDL.SYNC_INDEX(‘idx_docs’, ’2M’);

3. CONTAINS Phrase Queries

If multiple words are contained in a query expression, separated only by blank spaces (no operators), the string of words is considered a phrase and Oracle Text searches for the entire string during a query.

4. Logical Operators
Picture 1.png

5. Some sample SQL queries:

-- Simple Query
SELECT SCORE(1) AS myscore, en_name, en_visname, py_name FROM poi_app WHERE CONTAINS(ft_meta_en_name, 'grammy center', 1) > 0 ORDER BY myscore DESC;
 
SELECT SCORE(1) AS myscore, en_name, en_visname, py_name FROM poi_app WHERE CONTAINS(ft_meta_en_name, 'cybersoft', 1) > 0;
 
-- Query Rewrite
SELECT en_name, en_visname, py_name
FROM poi_app
WHERE CONTAINS (ft_meta_en_name,
'<query>
<textquery lang="ENGLISH" grammar="CONTEXT"> international hotel boya
<progression>
<seq><rewrite>transform((TOKENS, "{", "}", "AND"))</rewrite></seq>
<seq><rewrite>transform((TOKENS, "{", "}", "ACCUM"))</rewrite></seq>
</progression>
</textquery>
<score datatype="INTEGER" algorithm="COUNT"/>
</query>')>0;
 
-- Query 'About'
SELECT en_name, en_visname, py_name, score(1)
FROM poi_app
WHERE CONTAINS(ft_meta_en_name, 'about(italian restaurants)', 1) > 0
ORDER BY SCORE(1) DESC;
 
-- Query logical
SELECT en_name, en_visname, py_name, score(1)
FROM poi_app
WHERE CONTAINS(ft_meta_en_name, 'beijing, international, hotel', 1) > 0
ORDER BY SCORE(1) DESC;
Categories: oracle Tags: