CREATE TABLE addressbook ( lastname varchar(25),
);
The new table mimics the existing data file that we had; included were a couple of int columns and several varchar columns of varying size. The design of the table was overall very simple.
The next step was to populate the table with the data from the text file. This was easy to do by using the
COPY command, which loads large amounts of data
(either character or Binary) from flat-text files. You tell
COPY which file to load by specifying FROM '/directory/file'. By default, COPY uses a tab ("\t") character as a delimiter between fields; we changed this behavior by including USING
DELIMETERS ','
The
COPY command, however, has a few potential pitfalls. Such as if you don't have enough columns in the file, you will get an error, but if you have too many columns you will get a warning only and the extra columns are ignored. Also remember that
COPY is executed as a transaction, meaning that a single error in the data causes an undo of the entire import operation.
As always, it is good practice to read over the intricacies of the
COPY command in the PostgreSQL help docs, which are online at http://www.postgresql.org/idocs
It is entirely possible to access, modify and remove the data from your database within the psql tool, but an alternative method is to use PHP access the database via the web. PHP, when compiled with the -with pgsql directive, comes with a vast library of functions for interacting with PostgreSQL (much the same as with mySQL).
Overall, the migration to PostgreSQL went smoothly without any unwarranted surprises. Of course this would not have been possible without the help of a few resources. Kudos to the PostgreSQL team for supplying the online documentation files on their website.
We also used two books to help us:
"PostgreSQL Developer's Handbook" by Evald Geschwinde and Hans-Jurgen
Schonig. Published by Sams, 2002.
"Beginning Databases with PostgreSQL" by Richard Stones and Neil Matthew.
Published by Wrox Press, 2001
PostgreSQL Data Types
The datatypes in PostgreSQL look a lot like Oracle datatypes.
HP 3000 Evolution Migrating a 3000 Application
• 275
Character Datatypes:
For character data, the most common datatype is VARCHAR(n) with a specified maximum number of characters. If all data values will be the same length, you can use CHAR(n) instead. For unlimited length text fields, use TEXT, which does not have a declared maximum size. Although the type text is not in the SQL standard, many other RDBMS packages have it as well. The longest possible character string that can be stored is about 1 GB.
There are no performance differences between these three types, apart from the increased storage size when using the blank-padded type (CHAR(n)).
Multi-byte Characters such as UNICODE are supported, but you may have to recompile the database (no problem, since the source code is freely available).
Numeric Datatypes:
Smallint, int2. An integer of 2 bytes. Use for TurboIMAGE I1 or J1 or K1 fields.
Integer, int4. An integer of 4 bytes. Use for I2, J2 or K2 fields. This is the default datatype for integer fields.
Bigint, int8. An integer of 8 bytes (equivalent to I4 "quad" integers on the HP
3000). Use for TurboIMAGE I4 or J4 fields. Oracle does not have this datatype, but
SQL Server does.
The bigint type may not function correctly on all platforms, since it relies on compiler support for eight-byte integers. On a machine without such support, bigint acts the same as integer (but still takes up eight bytes of storage). However, the
PostgreSQL team is not aware of any reasonable platform where this is actually the case.
You may want to use type Integer for indeces, rather than Smallint or Bigint.
Otherwise you must escape constants to avoid type casting errors. See the user manual for details.
Real. A 4-byte floating point field for approximate numeric values. Use for an E2 in TurboIMAGE.
Double Precision. An 8-byte floating point field for approximate numeric values. Use for an E4 in TurboIMAGE.
Numeric (p,s) - precise numbers with possible decimal places. Similar to the
Numeric datatype in Oracle and SQL Server, except that there is no limit to the precision!
You specify a precision and scale for the values. Precision is the maximum total digits in the values. Scale is the number of places to the right of the decimal. The maximum number of digits that can be placed to the left of the decimal is precision minus scale. For example, DEC(7,2) means the same as S9(5)V9(2) in COBOL.
NUMERIC in PostgreSQL is similar to NUMERIC in Oracle, although it does not have the odd "negative scale factors" of Oracle (scale factor-3 in Oracle actually multiplies the value by 1000!).
276
• Migrating a 3000 Application
HP 3000 Evolution