X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id D7F59D1B265 for ; Tue, 8 Jun 2004 19:55:56 -0300 (ADT) 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 36493-08 for ; Tue, 8 Jun 2004 22:55:55 +0000 (GMT) Received: from jalapeno.crazydogs.org (jalapeno.jellybean.co.uk [212.78.70.100]) by svr1.postgresql.org (Postfix) with ESMTP id 9451BD1B18F for ; Tue, 8 Jun 2004 19:55:51 -0300 (ADT) Received: from rp by jalapeno.crazydogs.org with local (Exim 3.36 #1 (Debian)) id 1BXpVX-00016O-00 for ; Tue, 08 Jun 2004 23:55:51 +0100 Date: Tue, 8 Jun 2004 23:55:51 +0100 From: Richard Poole To: pgsql-general@postgresql.org Subject: Re: [SQL] simple addition of constraints problem Message-ID: <20040608225550.GA26592@guests.deus.net> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.4i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200406/340 X-Sequence-Number: 61723 On Tue, Jun 08, 2004 at 05:51:22PM -0400, Michelle Murrain wrote: > I'd like to make 'courseid' in this second table a foreign key, > referencing 'courseid' in the first table. So I did this command: > > charter_dev2=# ALTER TABLE coursesevaluations ADD CONSTRAINT rc_ce > FOREIGN KEY (courseid) REFERENCES rcourseinfo (courseid); > > And I get this result: > > NOTICE: ALTER TABLE will create implicit trigger(s) for FOREIGN KEY > check(s) > ERROR: rc_ce referential integrity violation - key referenced from > coursesevaluations not found in rcourseinfo What this is telling you is not that there is any problem with your formulation of the constraint (which as far as I can tell is what you want), but that the existing data in the tables violates the constraint you're trying to impose. If you do something like SELECT rcoursesevaluations.courseid FROM rcoursesevalutations NATURAL LEFT JOIN rcourseinfo WHERE rcourseinfo.courseid IS NULL you should see which courseids appear in rcoursesevaluations but not in rcourseinfo. Richard