Re: [SQL] simple addition of constraints problem - Mailing list pgsql-general

From Richard Poole
Subject Re: [SQL] simple addition of constraints problem
Date
Msg-id 20040608225550.GA26592@guests.deus.net
Whole thread Raw
List pgsql-general
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

pgsql-general by date:

Previous
From: CSN
Date:
Subject: using regexp backreferences
Next
From: DeJuan Jackson
Date:
Subject: Re: using regexp backreferences