Re: PL/pgSQL

From: Ian Turner <vectro(at)pipeline(dot)com>
To: Stephan Richter <srichter(at)cbu(dot)edu>
Cc: PGSQL General <pgsql-general(at)postgresql(dot)org>
Subject: Re: PL/pgSQL
Date: 2000-08-30 14:13:16
Message-ID: Pine.LNX.4.21.0008300704040.734-100000@crafter.house
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

> - If I delete a record, I want to delete all its references. -- Is it right
> that I have to use a function and a trigger; but how?

Just say ON DELETE CASCADE when creating the reference. If you want a
row's references to be updated when you change the original row, you
should do ON UPDATE CASCADE, as well.

> - I want to make sure an entry does not exist in the table, before adding a
> new entry.

Select for it first, then do IF NOT FOUND. Or use a UNIQUE index on the
table, to keep the entries indexed. But watch out -- if any statement of
the script fails, it stops there.

> - When adding an entry generate another table's entry using some default
> information.

Store the default in some table, then do an INSERT ... SELECT.

> - Automatically delete entries that are old.

use DELETE WHERE x, where x is some definition of 'old'.

If you wanted this cleanup to happen without any continued queries, the
easiest way would be to 1) create a view which excludes 'old' entries, and
2) cleanup the 'old' entries before you vacuum each night.

> - Update entries based on other tables' information.

Not sure what you mean here. If you mean subselects, joins, or unions in
an update statement, that should work. Alternatively, do the selects you
need to get your answer in a Result, and then update from the Result.

> I think all of these problems are not possible to solve with regular
> SELECT, UPDATE and DELETE statements.

I beg to differ.

> So I guess what I am asking is, where do I find some documentation on
> triggers and functions for PostgreSQL 7.0.2?

Erm, did you try the main postgresql documentation? Perhaps you should
search it for 'triggers'? :o

Pl/PgSQL does not support utility statements such as create trigger,
however it will in postgresql 7.1.

Ian Turner
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.1 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE5rRZ+fn9ub9ZE1xoRAh/6AKDJgeCq/7TUHWwgYmXFZpW4PWCAYgCgkUZv
Rh/lbMThWg8dgXjsehDNJN8=
=kw7x
-----END PGP SIGNATURE-----

In response to

  • PL/pgSQL at 2000-08-30 07:40:47 from Stephan Richter

Browse pgsql-general by date

  From Date Subject
Next Message Andrew Snow 2000-08-30 14:22:36 RE: Large selects handled inefficiently?
Previous Message Martin Neimeier 2000-08-30 13:53:35 Jobs with PostgreSQL