Re: [SQL] simple addition of constraints problem

From: Richard Poole <rp(at)guests(dot)deus(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: [SQL] simple addition of constraints problem
Date: 2004-06-08 22:55:51
Message-ID: 20040608225550.GA26592@guests.deus.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Stephan Szabo 2004-06-08 23:18:07 Re: simple addition of constraints problem
Previous Message Michelle Murrain 2004-06-08 21:51:22 simple addition of constraints problem

Browse pgsql-sql by date

  From Date Subject
Next Message Stephan Szabo 2004-06-08 23:18:07 Re: simple addition of constraints problem
Previous Message Jaime Casanova 2004-06-08 22:05:38 a query question