| This page is user contributed documentation. See the bottom of the page for information about the author. | |
In general, PostgreSQL makes a strong effort to conform to existing database standards, where MySQL has a mixed background on this. If you're coming from a background using MySQL or Microsoft Access, some of the changes can seem strange (such as not using double quotes to quote string values).
The important things (for me, at least) are more than just 'how do I do this MySQL thing in PostgreSQL?', but 'is there a much better way to think about this, which MySQL never even supported?'
For example:
Imagine you're building a dynamic website for human resources. You want to list every current senior staff member's name, some info about them, and a list of their goals.
With MySQL, you'd do something like this:
(this is generic pseudo-code, it would
easily translate to PHP, Zope,
EmbPerl, etc.)
<in sql="SELECT staffid, firstname,
lastname FROM Staff
WHERE
datefired ISNULL and seniorstaff = TRUE">
<h1>$firstname $lastname</h1>
<ul>
<in
sql="SELECT goalinfo FROM Goals WHERE staffid=$staffid">
<li>$goalinfo
</in>
</ul>
</in>
That's great, and it works fine. You can easily translate this to PostgreSQL.
Would you want to, though? PostgreSQL has many features MySQL doesn't, like:
For instance, rather than coding in
the web front end the logic of
is-not-fired and is-senior-staff, in PostgreSQL,
I'd make a VIEW
of all
staff for which we want to show goals:
CREATE VIEW staff_having_goals AS
SELECT staffid, firstname || lastname as fullname
FROM Staff
WHERE datefired ISNULL and seniorstaff = TRUE
ORDER BY lastname, firstname
Now, my web programming doesn't have
to worry about the lower level
concerns. Imagine if this same list of
people and goals appeared dozens of times on your
site -- I've moved from having this scattered in
many places, to having it encapsulated in one place.
PostgreSQL also allows procedural languages
(perl, tcl, python [alpha],
and an Oracle-alike, PL/pgSQL). These
allow you to create functions in your database (and
even non-sysadmins can use them, as the functions
fit in the PostgreSQL security model).
[ Yes, MySQL has user functions, which
last time I checked, had to be
written in C, and linked into the database. A
nice feature, to be sure,
but VERY different from having high-level procedural
languages usable w/o root privileges! ]
We might use these procedural languages to create lists, handle database events (if a record is added here, automatically track this there, and so on. You might have a function to calculate a staff member's hourly compensation from their salary, which, IMHO, *should* be a database function, not a function coded for every different web project or front-end project you had.)
PostgreSQL also has transactions, which can remove some of the hairy if-an-error-happened-back-out-all-database-work code. (MySQL, to its credit, has transactions in their new MaxSQL thingie.)
In addition, PostgreSQL supports many standard parts of SQL that MySQL doesn't, such as subqueries, unions, intersections, etc. While you can often program around these, either with more SQL, or more logic in the front-end, the best (fastest, more portable, most abstracted) solution is to integrate this thinking into your query writing and database design.
So:
The things that are handled differently are fairly small, and can generally be handled without too much pain. Especially since you can easily create PostgreSQL user functions that mimic any from MySQL.
The real lesson is to learn about what
features PostgreSQL has and figure out *why* to
use them!
I'd start w/the five above (views, procedural languages,
triggers, customizable aggregates, transactions)
and make sure that you understand exactly what they
are, how to use them, and how wonderful they are.
I hope this helps. I moved to PostgreSQL from using MySQL, and for several months after first playing with it, I just thought it was a bigger, more complicated database that did 'the same stuff' as MySQL. It took me a while to really realize how great the 'other' features are.
Good luck!