PG UPDATE question - Mailing list pgsql-general

From Jeff Butera
Subject PG UPDATE question
Date
Msg-id Pine.LNX.4.21.0103210121060.21956-100000@linus.highpoint.edu
Whole thread Raw
Responses Re: PG UPDATE question  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
I'm new to postgres and am having an UPDATE problem when
using a join.  I've got two tables like so:

CREATE TABLE QUESTION_ANSWERS (
  QUESTIONS_ID   int4  NOT NULL,  -- question ID
  ANSWERS_ID     int4  NOT NULL,  -- answer ID
  CORRECT        boolean
);

CREATE TABLE STUDENT_ANSWERS (
  STUDENTS_ID    int4   NOT NULL,  -- student ID
  QUESTIONS_ID   int4   NOT NULL,  -- question ID
  ANSWERS_ID     int4   NOT NULL,  -- answer ID
  CORRECT        boolean
);

The table QUESTION_ANSWERS is completely populated.  The table
STUDENT_ANSWERS has all fields except CORRECT populated.  I want to
write an update that'll copy CORRECT from QUESTION_ANSWERS into
STUDENT_ANSWERS when the IDs match.  Here's what I wrote:

UPDATE STUDENT_ANSWERS SET CORRECT=QA.CORRECT
  FROM QUESTION_ANSWERS QA,STUDENT_ANSWERS SA
  WHERE SA.CORRECT=null
    AND SA.QUESTIONS_ID=QA.QUESTIONS_ID
    AND SA.ANSWERS_ID=QA.ANSWERS_ID;

It executes without an error and correctly returns the count of the
records in the join, but the CORRECT field in STUDENT_ANSWERS is
populated entirely with false regardless of what CORRECT is
QUESTION_ANSWERS.

Can anyone shed light on what I'm missing?

--
Jeff Butera, Ph.D.                               Junk e-mail is like
Department of Mathematics & Computer Science     plastic grocery bags:
High Point University                            they just seem to
jbutera@linus.highpoint.edu                      multiply at will...
http://linus.highpoint.edu/~jbutera                        Anonymous


pgsql-general by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: Really Dumb Question...(Dumping db)
Next
From: "Richard Huxton"
Date:
Subject: Re: after trigger question