Porting from Oracle to PostgreSQL



Similar documents
Programming with SQL

Oracle SQL. Course Summary. Duration. Objectives

Oracle Database: SQL and PL/SQL Fundamentals

4 Logical Design : RDM Schema Definition with SQL / DDL

Objectives. Oracle SQL and SQL*PLus. Database Objects. What is a Sequence?

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database: SQL and PL/SQL Fundamentals NEW

How To Create A Table In Sql (Ahem)

Database Extensions Visual Walkthrough. PowerSchool Student Information System

Oracle Database 12c: Introduction to SQL Ed 1.1

3.GETTING STARTED WITH ORACLE8i

Oracle Database: SQL and PL/SQL Fundamentals NEW

Comparison of Open Source RDBMS

Porting Oracle Applications to PostgreSQL

CSC 443 Data Base Management Systems. Basic SQL

CS 377 Database Systems SQL Programming. Li Xiong Department of Mathematics and Computer Science Emory University

Oracle Database: Introduction to SQL

Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

Database Migration from MySQL to RDM Server

Oracle Database 10g: Introduction to SQL

A Brief Introduction to MySQL

Oracle Database: Introduction to SQL

Oracle Database: Introduction to SQL

JDBC (Java / SQL Programming) CS 377: Database Systems

Optimizing with Open Source Technology Postgres

Java and Databases. COMP514 Distributed Information Systems. Java Database Connectivity. Standards and utilities. Java and Databases

Database Programming with PL/SQL: Learning Objectives

Oracle Database 11g SQL

RNDr. Michal Kopecký, Ph.D. Department of Software Engineering, Faculty of Mathematics and Physics, Charles University in Prague

Tuning Derby. Version Derby Document build: August 10, 2009, 1:04:58 PM (PDT)

sqlite driver manual

Programming Database lectures for mathema

Introduction to Microsoft Jet SQL

IT2305 Database Systems I (Compulsory)

Using SQL Server Management Studio

SQL Server An Overview

Introduction to the Oracle DBMS

Linas Virbalas Continuent, Inc.

IT2304: Database Systems 1 (DBS 1)

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Firebird. Embedded SQL Guide for RM/Cobol

Oracle Rdb A Comparison of SQL Dialects for Oracle and Oracle Rdb

An Oracle White Paper June Migrating Applications and Databases with Oracle Database 12c

T-SQL STANDARD ELEMENTS

4 Simple Database Features

1 Stored Procedures in PL/SQL 2 PL/SQL. 2.1 Variables. 2.2 PL/SQL Program Blocks

Oracle to MySQL Migration

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

Advanced PostgreSQL SQL Injection and Filter Bypass Techniques

Information Systems SQL. Nikolaj Popov

Apache Cassandra Query Language (CQL)

SQL. Short introduction

Writing Control Structures

Managing Objects with Data Dictionary Views. Copyright 2006, Oracle. All rights reserved.

Recognizing PL/SQL Lexical Units. Copyright 2007, Oracle. All rights reserved.

Microsoft SQL Server Connector for Apache Hadoop Version 1.0. User Guide

Once the schema has been designed, it can be implemented in the RDBMS.

PL / SQL Basics. Chapter 3

The release notes provide details of enhancements and features in Cloudera ODBC Driver for Impala , as well as the version history.

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

The Advantages of PostgreSQL

COSC344 Database Theory and Applications. Java and SQL. Lecture 12

Oracle Database 12c Enables Quad Graphics to Quickly Migrate from Sybase to Oracle Exadata

Database Administration with MySQL

Netezza SQL Class Outline

news from Tom Bacon about Monday's lecture

Making Oracle and JDBC Work For You

Fundamentals of Database Design

2. Oracle SQL*PLUS Winter Some SQL Commands. To connect to a CS server, do:

Creating Database Tables in Microsoft SQL Server

XEP-0043: Jabber Database Access

ODBC Client Driver Help Kepware, Inc.

SQL Server to Oracle A Database Migration Roadmap

Migrating Non-Oracle Databases and their Applications to Oracle Database 12c O R A C L E W H I T E P A P E R D E C E M B E R

Database Query 1: SQL Basics

not at all a manual simply a quick how-to-do guide

database abstraction layer database abstraction layers in PHP Lukas Smith BackendMedia

SQL:2003 Has Been Published

5.1 Database Schema Schema Generation in SQL

Physical Design. Meeting the needs of the users is the gold standard against which we measure our success in creating a database.

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

Oracle Migration Workbench

SQL interview questions and answers

Instant SQL Programming

PL/SQL MOCK TEST PL/SQL MOCK TEST I

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

MyOra 3.0. User Guide. SQL Tool for Oracle. Jayam Systems, LLC

New SQL Features in Firebird 3

LOBs were introduced back with DB2 V6, some 13 years ago. (V6 GA 25 June 1999) Prior to the introduction of LOBs, the max row size was 32K and the

Beginning SQL, Differences Between Oracle and Microsoft

Advance DBMS. Structured Query Language (SQL)

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

B.1 Database Design and Definition

FileMaker 12. ODBC and JDBC Guide

Oracle For Beginners Page : 1

Geodatabase Programming with SQL

Microsoft Access 3: Understanding and Creating Queries

An Oracle White Paper August, Oracle JDBC Memory Management

Transcription:

by Paulo Merson February/2002 Porting from Oracle to If you are starting to use or you will migrate from Oracle database server, I hope this document helps. If you have Java applications and use JDBC, the Data types and JDBC section will be particularly useful. Oracle and both conform to standard SQL. However, they contain several extensions and implementation details that differentiate one from the other. The most important differences are listed in this document. If you have comments about this document, please email me: pmerson@cs.cmu.edu. 1. SQL Syntax, Functions, Sequences, Etc. Oracle select sysdate from dual CREATE SEQUENCE seqname [ INCREMENT BY integer ] [ MINVALUE integer ] [ MAXVALUE integer ] [ START WITH integer ] [ CACHE integer ] [ CYCLE NOCYCLE ] Oracle s create sequence has other arguments not listed here and not supported by, but the main difference is the need of by and with after increment and start. If you don t specify MAXVALUE or if you use the parameter NOMAXVALUE, then the actual limit is 10 27. select now ::datetime There is no dual table Unlike other RDBMS, allows a select without the from clause. This use does not affect portability because the syntax to get current time is already DBMS specific. CREATE SEQUENCE seqname [ INCREMENT increment ] [ MINVALUE minvalue ] [ MAXVALUE maxvalue ] [ START start ] [ CACHE cache ] [ CYCLE ] If you don t specify MAXVALUE, then the maximum value is 2147483647 for ascending sequences. 1

Oracle To return the current value and increment the counter: sequence_name.nextval; Possible usage in a select statement: select sequence_name.nextval from dual; To return the current value and increment the counter: nextval( sequence_name ); Possible usage in a select statement select nextval( sequence_name ); Note that unlike other RDBMS, allows a select without the from clause. This use does not affect portability because the sequence syntax is already DBMS specific. SELECT product_id, DECODE (warehouse_id, 1, Southlake, 2, San Francisco, 3, New Jersey, 4, Seattle, Non-domestic ) quantity_on_hand FROM inventories SELECT a, CASE WHEN a=1 THEN 'one' WHEN a=2 THEN 'two' ELSE 'other' END FROM test select employeeid, NVL(hire_date, sysdate) from employee where employeeid = 10; select employeeid, coalesce(hire_date, 'now'::datetime) from employee where employeeid = 10; Oracle also has a coalesce function that is a generalization of the commonly used NVL function. Outer join (+) Hierarchical queries CONNECT BY SELECT product_id FROM inventories MINUS SELECT product_id FROM order_items; select unique col1, col2 from table1 In Oracle distinct and unique keywords are synonymous in the select statement. Doesn t support outer join. The workaround is to use a union. Nothing similar SELECT product_id FROM inventories EXCEPT SELECT product_id FROM order_items; select distinct col1, col2 from table1 doesn t allow select unique. 2

Oracle Oracle relational operators may have a space between the characters. For example, the following select will work: select id,name from employee where id > = 10; // There are spaces and tabs between > and = To get the remainder of the division of 10 by 4 (modulo) use the mod function: select mod(10,4) from dual; The ROWNUM pseudo-column returns a number indicating the order in which Oracle selects the row. ROWNUM can be used to limit the number of rows returned by a query, for example: select * from employees where rownum < 10 order by name; ROWNUM can be used in the projection as one of the values returned by the query (first line has value 1, second line value 2, and so on): select rownum, name from employees order by name; relational operators doesn t allow spaces, the characters that compound an operator must be consecutive when the command is parsed: select id,name from employee where id >= 10; To get the remainder of the division of 10 by 4 (modulo) use the % operator. (And has many other arithmetic operators.) select 10 % 4; There isn t anything equivalent to Oracle ROWNUM. However, you can limit the number of rows returned by a query using the LIMIT clause: select * from employees order by name limit 10; In some cases, it s possible that the pseudo-column OID may substitute ROWNUM, although they have different behavior. OID is a unique identifier of each line per table, while ROWNUM is always 1, 2,, N for each different query. select oid, name from employees order by name; And the query that uses ROWNUM can have join tables. If your select is a join you ll have a different OID for each table, because each one has an OID column. 2. Database Server General Characteristics Oracle A view can be updatable if some conditions are satisfied. Views are read only. 3

Oracle Transactions are initiated implicitly. By default, the auto-commit behavior is disabled. Tables, views and other objects can be logically grouped in schemas. A schema usually maps to the user name of the user that created the objects (owner). Thus, a table can be referenced in a statement as schemaname.tablename. For example: select * from myschema.mytable; Interactive command prompt tool: SQL*Plus Oracle permissions are granted/revoked to/from users or roles. You can create roles and grant/revoke roles to/from users. But you can create and alter groups to insert and remove users. By default, a password is always required to connect to the database. BEGIN initiates a transaction disabling the default behavior, which is auto-commit enabled, i.e., a commit is performed after each user statement. In Java, we need to write: con.setautocommit(false) There is no schema support, but it s planned for a future version. The alternative is to use separate databases: You have to connect to the specific database and use that connection to execute your SQL command. However, if you have a SQL statement that uses tables in different (Oracle) schemas, you cannot use separate () databases; there is no direct workaround, you ll need to rewrite the code. Interactive command prompt tool: psql permissions are granted/revoked to/from users or groups. You can create groups and then alter the groups inserting/removing users. By default, you can connect to the database simply by specifying the database name, no user ID and password are required. You should follow the instructions in the Administrator s Guide to configure the pg_hba.conf file in order to use password authentication. 4

3. Data Types and JDBC Oracle JDBC * JDBC NUMBER(p) where p is the precision, i.e., the number of digits getbyte SMALLINT - 2 bytes getshort getshort getint INTEGER - 4 bytes getint getlong BIGINT - 8 bytes getlong NUMBER(p,s) where p is the total number of digits and s is the number of digits to the right of the decimal point getdouble getbigdecimal NUMERIC(p,s) REAL 4 bytes, 6 decimal places getbigdecimal getdouble DOUBLE PRECISION 8 bytes, 15 decimal places getdouble Nothing similar SERIAL - 0 to +2147483647, typically used to create unique identifiers. Generates an implicit sequence that is incremented when a line is inserted in the table. VARCHAR2(size) where maximum size is 4000 getstring CHARACTER VARYING(n) where maximum n is 1 GB VARCHAR(n) is an alias CHAR(size) where maximum size is 2000 getstring CHARACTER(n) where maximum n is 1 GB CHAR(n) is an alias It s suggested that you use TEXT if n > 10 MB getint getstring getstring LONG - Character data of variable length up to 2 GB getstring TEXT variable length up to 1 GB It s suggested that you use TEXT if n > 10 MB getstring DATE holds date and time getdate TIMESTAMP gettimestamp TIMESTAMP gettime gettimestamp oracle.sql. gettimestamp You still can use getdate to read a TIMESTAMP column, but you will loose the time portion of the data. Nothing similar DATE holds only the date (resolution is one day) Nothing similar TIME holds only the time (00:00:00.00 23:59:59.99) getdate gettime 5

Oracle JDBC * JDBC RAW(size) binary data of length size bytes (max 2000) LONG RAW binary data or variable length up to 2GB getbytes BYTEA getbytes Nothing similar BIT(n) fixed length string of 1 s and 0 s BIT VARYING(n) variable length string of 1 s and 0 s (?) CLOB character large object (max 4GB) getclob TEXT (max 1GB) getstring BLOB binary large object (max 4GB) getblob BYTEA (max 1GB) BYTEA is not documented in 7.1 but it s fully implemented; Jdbc 7.2-1.2 is required though in order to use getbytes and setbytes. Besides TEXT and BYTEA, supports large objects as separate files. They are stored in a separate table in a special format, and are referred to from regular tables by an OID value. More information: http://www.postgresql.org/idocs/index.php?larg eobjects.html http://www.postgresql.org/idocs/index.php?jdbc -lo.html getbytes ROWID oracle.sql. getrowid Nothing similar Nothing similar Typically, char(1) is used to store a value that is translated to Boolean in the application logic. If you store '0' and '1' in a varchar2(1) or char column, then the jdbc driver can correctly interpret these values as boolean false an true respectively using ResultSet.getBoolean and PreparedStatement.setBoolean BOOLEAN can have the value TRUE, FALSE or NULL If you store '0' and '1' in a varchar(1) or char(1) column, then the jdbc driver can correctly interpret these values as boolean false an true respectively using ResultSet.getBoolean. However, PreparedStatement.setBoolean simply does not work. If you use BOOLEAN, then your Java code can use getboolean and setboolean. Oracle Spatial features? Geometric data types: POINT, LINE, CIRCLE, etc. getboolean org. postgresql. geometric.* 6

Oracle JDBC * JDBC Nothing similar Network address data types: INET, MACADDR, CIDR. (?) * JDBC note: Typically PreparedStatement.setXxx is used to set the value of arguments (or host variables) inside SQL statements. And there is a correspondent ResultSet.getXxx method to read the value returned by a query into java variables. Each get/setxxx method has a specific Java data type or class associated to it (e.g. set/getint deals with int; set/getdate deals with java.sql.date, etc.). Further, you can use different methods to read the same database data type, but usually there is a recommended method. For example, a BIGINT column can be read with getshort, getint, getlong, getdouble, etc., but the recommended method is getlong. So, to indicate the proper way to use each data type in Java I simply listed the recommended getxxx JDBC method. 4. Other Considerations: The set of operators and SQL functions is very similar though Oracle has a richer set. For example, both DBMS have the concatenation operator, as well as substr, upper, to_char and other functions with the same syntax. However, any Oracle function that is being used must have its syntax compared to the equivalent function in, if such exists. lacks the ability to query across multiple databases. s PL/pgSQL is similar to Oracle PL/SQL and can be used to write stored functions. doesn t have packages or procedures (only functions). More about this: http://www.postgresql.org/idocs/index.php?plpgsql-porting.html Both DBMS have triggers and the create trigger statement is similar, but the code executed by the trigger for must be in a stored function written by the user, while in Oracle you have the option of writing the code in a PL/SQL block in the create trigger statement. has yet an additional resource called the rule system that allows the definition of business logic that is executed upon an event. The create table statement is similar in both DBMS. One noticeable difference is that doesn t have pctfree, pctused, inittrans, and maxtrans clauses. They also differ in the create database statement, mainly in the arguments and clauses that specify storage details. 7

5. References: Oracle 9i documentation - http://download-east.oracle.com/otndoc/oracle9i/901_doc/nav/docindex.htm documentation - http://www.postgresql.org/idocs/ Oracle to Postgre Conversion - http://openacs.org/doc/openacs/html/oracle-to-pg-porting.html JDBC 2.0 compliance - http://lab.applinet.nl/postgresql-jdbc/ An important source of information is the mailing lists: http://archives.postgresql.org/ 8