Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id DD90263315A for ; Mon, 14 Sep 2009 10:28:22 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 02834-08-2 for ; Mon, 14 Sep 2009 13:28:03 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from rotring.dds.nl (rotring.dds.nl [85.17.178.138]) by mail.postgresql.org (Postfix) with ESMTP id EFE3363280F for ; Mon, 14 Sep 2009 10:28:11 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by rotring.dds.nl (Postfix) with ESMTP id 27B3857DE8; Mon, 14 Sep 2009 15:28:06 +0200 (CEST) Received: from [192.168.2.100] (unknown [190.12.51.7]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) by rotring.dds.nl (Postfix) with ESMTP id 7B2E65813B; Mon, 14 Sep 2009 15:27:58 +0200 (CEST) Cc: pgsql-sql@postgresql.org Message-Id: <24123985-9812-49FE-B422-31E7AC963758@rvt.dds.nl> From: Ries van Twisk To: Mario Splivalo In-Reply-To: <4AAE43F8.4000107@megafon.hr> Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Mime-Version: 1.0 (Apple Message framework v936) Subject: Re: CHECK constraint on multiple tables Date: Mon, 14 Sep 2009 08:27:53 -0500 References: <4AAE43F8.4000107@megafon.hr> X-Mailer: Apple Mail (2.936) X-Virus-Scanned: clamav-milter 0.95.2 at rotring X-Virus-Status: Clean X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=AWL=0.000, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200909/57 X-Sequence-Number: 33122 can't you solve it creating a reference between the tables? Ries On Sep 14, 2009, at 8:24 AM, Mario Splivalo wrote: > I have two tables, tableA and tableB: > > CREATE TABLE tableA (idA integer primary key, email character varying > unique); > CREATE TABLE tableB (idB integer primary key, email character varying > unique); > > Now, I want to create check constraint in both tables that would > disallow records to either table where email is 'mentioned' in other > table. > > If CHECK constraints supported SUBSELECTS, I could write: > > ALTER TABLE tableA ADD CONSTRAINT tableA_chk CHECK > (email NOT IN (SELECT email FROM tableB)); > > Unfortunatley, postgres won't allow me to do so. > > Now, i could create function, check_for_email, that would return > TRUE if > email is mentioned in either table, and then call that function when > creating a check constraint. > > Or I could add separate table, emails, like this: > > CREATE TABLE emails (email_id integer primary key, email character > varying unique) > > And then replace 'email' column in tables tableA and tableB with > 'email_id' that would be foreign key refference to the emails table. > > I could, also, write functions for inserting data to the tableA and > tableB tables. > > What would be the best approach to solve the problem I have? Could I > use > rules on insert to help me? > > Mario > > -- > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-sql regards, Ries van Twisk ------------------------------------------------------------------------------------------------- tags: Freelance TYPO3 Glassfish JasperReports JasperETL Flex Blaze-DS WebORB PostgreSQL DB-Architect email: ries@vantwisk.nl web: http://www.rvantwisk.nl/ skype: callto://r.vantwisk Phone: +1-810-476-4196 Cell: +593 9901 7694 SIP: +1-747-690-5133