X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 8EC1F47668A for ; Thu, 15 May 2003 07:43:40 -0400 (EDT) Received: from postgresql.org ([64.49.215.8]) by localhost (postgresql.org [64.49.215.8:10024]) (amavisd-new) with ESMTP id 87421-04 for ; Thu, 15 May 2003 07:43:39 -0400 (EDT) Received: from comploteur.gibbonie.creolie (m174.net195-132-195.noos.fr [195.132.195.174]) by postgresql.org (Postfix) with ESMTP id AF1CD475458 for ; Thu, 15 May 2003 07:43:38 -0400 (EDT) Received: from edouard by comploteur.gibbonie.creolie with local (Exim 4.10) id 19GHiM-00023S-00 for pgsql-sql@postgresql.org; Thu, 15 May 2003 14:20:02 +0200 Date: Thu, 15 May 2003 14:20:01 +0200 From: edouard.boucher@free.fr To: pgsql-sql@postgresql.org Subject: check with select Message-ID: <20030515122001.GA7851@comploteur.gibbonie.creolie> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.4i X-Virus-Scanned: by amavisd-new X-Archive-Number: 200305/312 X-Sequence-Number: 13477 Hi, 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, 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). i would like something i can use like this : begin; insert into services ... insert into employesdsservices ... end; is there a way to do this, a check after each insert, update, delete in both tables? i know my constraint is anyway very slow, but this is for a scolar project about theory and performance realy dont matters. thank you for help Edouard Boucher