X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 24FB0476705 for ; Thu, 15 May 2003 10:49:44 -0400 (EDT) Received: from postgresql.org ([64.49.215.8]) by localhost (postgresql.org [64.49.215.8:10024]) (amavisd-new) with ESMTP id 18111-05 for ; Thu, 15 May 2003 10:49:13 -0400 (EDT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id CC5E747670D for ; Thu, 15 May 2003 10:49:12 -0400 (EDT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 50EE3D618; Thu, 15 May 2003 07:49:17 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 46A925C04; Thu, 15 May 2003 07:49:17 -0700 (PDT) Date: Thu, 15 May 2003 07:49:17 -0700 (PDT) From: Stephan Szabo To: Cc: Subject: Re: check with select In-Reply-To: <20030515122001.GA7851@comploteur.gibbonie.creolie> Message-ID: <20030515074312.V60859-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new X-Archive-Number: 200305/315 X-Sequence-Number: 13480 On Thu, 15 May 2003 edouard.boucher@free.fr wrote: > I have a table referancing services of a company : > CREATE TABLE Services ( > id INTEGER NOT NULL PRIMARY KEY, > nom VARCHAR(30) NOT NULL -- name of the service > ); > > each of them can have many workers and many boss > but at least 1 boss > > So i have a table referancing workers and boss : > > CREATE TABLE EmployesDsServices ( > service INTEGER REFERENCES Services (id) > ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, > employe INTEGER REFERENCES Employes (id) > ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, > niveau CHAR(1), -- R -> boss, S -> worker > CONSTRAINT niveau_valide CHECK (NIVEAU IN ('R','S')), > PRIMARY KEY (service, employe) > ); > > to check that at any time a service always have at least one boss, > i would like : > > CREATE ASSERTION chaque_service_a_au_moins_un_responsable CHECK > (NOT EXISTS (SELECT id FROM Services LEFT OUTER JOIN > (SELECT * FROM employesdsservices WHERE niveau='R') AS resp ON > id=service GROUP BY id HAVING COUNT(niveau)<1)); > > the problem is that this is not valid in postgreSQL, Noone's done assertions yet, and PostgreSQL also doesn't support subqueries in check constraints at this point either AFAIK. > so i tryed to use a trigger, but it seems that it is not possible to > make a trigger deferrable (as the 2 tables are mutualy dependent). It should be possible, but you have to use a not meant for general use (and barely documented) CREATE CONSTRAINT TRIGGER, or you may be able to twiddle the entries for a normal trigger in pg_trigger after the fact, but I haven't tried the latter. CREATE CONSTRAINT TRIGGER was effectively mean for references constraints in dumps before they started being dumped as alter statements.