X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 472C7564A1 for ; Mon, 28 Feb 2005 19:30:46 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 14753-09 for ; Mon, 28 Feb 2005 19:30:29 +0000 (GMT) Received: from mm02snlnto.son.sandia.gov (mm02snlnto.sandia.gov [132.175.109.21]) by svr1.postgresql.org (Postfix) with ESMTP id B3354563FA for ; Mon, 28 Feb 2005 19:30:27 +0000 (GMT) Received: from 132.175.109.4 by mm02snlnto.son.sandia.gov with ESMTP ( Tumbleweed MMS SMTP Relay 01 (MMS v5.6.3)); Mon, 28 Feb 2005 12:30:11 -0700 X-Server-Uuid: 914ACFB1-8EBC-470E-882B-54A00EED9786 Received: from ES22SNLNT.srn.sandia.gov (ec03snlnt.sandia.gov [134.253.164.158] (may be forged)) by mailgate2.sandia.gov ( 8.12.11/8.12.11) with ESMTP id j1SJTst6024765 for ; Mon, 28 Feb 2005 12:30:09 -0700 (MST) Received: from 134.252.13.164 ([134.252.13.164]) by ES22SNLNT.srn.sandia.gov ([134.253.164.160]) with Microsoft Exchange Server HTTP-DAV ; Mon, 28 Feb 2005 19:29:56 +0000 Received: from boomerang by ES22SNLNT.srn.sandia.gov; 28 Feb 2005 11:28: 30 -0800 Subject: table constraints From: "Casey T. Deccio" To: pgsql-sql@postgresql.org Organization: Sandia National Laboratories Date: Mon, 28 Feb 2005 11:28:30 -0800 Message-ID: <1109618910.32166.0.camel@boomerang.ran.sandia.gov> MIME-Version: 1.0 X-Mailer: Evolution 2.0.3 X-PMX-Version: 4.7.0.111621, Antispam-Engine: 2.0.1.0, Antispam-Data: 2005.2.28.9 X-WSS-ID: 6E3DAEC91JG910118-01-01 Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.897 tagged_above=0 required=5 tests=AWL, RCVD_NUMERIC_HELO X-Spam-Level: X-Archive-Number: 200502/365 X-Sequence-Number: 20475 Hi, I am running PostgreSQL 7.4.7. I am having some issues with a constraint for one of my database tables. The code snippet below outlines the code and its output (output is commented). In this case each bldg has an owner associated to it, and each animal lives in some bldg. Each owner has exactly one own favorite animal out of all the bldgs owned by him. So the constraint added to each zoo row is that the boolean field 'favorite' is true for exactly once for each group of animals in the zoo that have a common owner. The unique_favorite(text) function is created to help with this constraint. The function returns what it is supposed to (see select statement in the code), but doesn't hold when used as a constraint. There may be a better way to do this, but I'm not looking for that right now. I would like to know if I am using this constraint wrong, or if there is there something wrong with the table constraints in the database system. Thanks, Casey ------ Code snippet ------- CREATE TABLE owner (owner varchar(50)); -- CREATE TABLE INSERT INTO owner VALUES ('steve'); -- INSERT 13193166 1 CREATE TABLE bldg (bldg varchar(50), owner varchar(50)); -- CREATE TABLE INSERT INTO bldg VALUES ('bldg1', 'steve'); -- INSERT 13193169 1 CREATE TABLE zoo (animal varchar(50), bldg varchar(50), favorite boolean); -- CREATE TABLE CREATE OR REPLACE FUNCTION unique_favorite(text) RETURNS BOOLEAN AS ' DECLARE temp RECORD; BEGIN SELECT into temp * FROM (SELECT b.owner, sum(CASE WHEN favorite = TRUE THEN 1 ELSE 0 END) AS num_favorites FROM (SELECT * FROM zoo WHERE animal = $1) z INNER JOIN bldg b USING(bldg) GROUP BY b.owner) sub WHERE sub.num_favorites <> 1; RETURN NOT FOUND; END; ' LANGUAGE plpgsql; -- CREATE FUNCTION ALTER TABLE zoo ADD check(unique_favorite(animal)); -- ALTER TABLE INSERT into zoo VALUES ('monkey', 'bldg1', false); -- INSERT 13193173 1 -- (This shouldn't be allowed!!!) SELECT *, unique_favorite(animal) FROM zoo; -- animal | bldg | favorite | unique_favorite -- --------+-------+----------+----------------- -- monkey | bldg1 | f | f INSERT into zoo VALUES ('monkey', 'bldg1', false); -- ERROR: new row for relation "zoo" violates check constraint "$1" INSERT into zoo VALUES ('monkey', 'bldg1', true); -- ERROR: new row for relation "zoo" violates check constraint "$1"