Limit A Table To 1 Row

From: <cnliou(at)eurosport(dot)com>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Limit A Table To 1 Row
Date: 2002-08-07 03:29:50
Message-ID: 200208070329.32fc@th00.opsion.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hello!

Corporate table, t1, should contain exact 1 row.
I strongly believe that there are cleaner versions
than the following combination of trigger and rule.

Thank you in advance for any improved version!

CNLIOU
--------

CREATE TABLE t1 (CorpId TEXT,CorpName TEXT);

CREATE FUNCTION f1() RETURNS OPAQUE AS '
DECLARE
rc SMALLINT;
BEGIN
SELECT COUNT(*) INTO rc FROM t1;

IF rc > 0 THEN
RETURN NULL;
END IF;

RETURN NEW;
END;' LANGUAGE 'plpgsql';

CREATE TRIGGER trigger1 BEFORE INSERT ON t1 FOR EACH
ROW EXECUTE PROCEDURE f1();

CREATE RULE rule1 AS ON DELETE TO t1 DO INSTEAD
NOTHING;

--------------------------------------------------------
You too can have your own email address from Eurosport.
http://www.eurosport.com

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2002-08-07 03:31:49 Re: SQL syntax
Previous Message Christopher Kings-Lynne 2002-08-07 02:02:09 Re: ALTER TABLE follow up