Re: Foreign Key for multi PK or design question

From: Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>
To: PostgreSQL Admin <postgres(at)productivitymedia(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Foreign Key for multi PK or design question
Date: 2007-12-11 19:42:46
Message-ID: 20071211194246.GK10710@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

PostgreSQL Admin wrote:
> This is my layout so far:
>
> CREATE TABLE users (
> id serial NOT NULL,
> --question REFERENCES questions(id) ON DELETE CASCADE ## ON REMOVED##
> );
>
> CREATE TABLE questions (
> id serial NOT NULL,
> questions varchar(450) NOT NULL
> );
>
> CREATE TABLE answers (
> id serial NOT NULL,
> question_id int REFERENCES questions(id) ON DELETE CASCADE,
> user_id int REFERENCES users(id) ON DELETE CASCADE,
> answer varchar(450) NOT NULL,
> created timestamptz NOT NULL
> );
>
> Originally I wanted to have a foreign key that would be the pk of the
> question table. So if the user answered Q2, 5 and 6 - the user.fk would
> store values 2,5,6 - but I have passed most of logic to the answer table.

That would have made no sense.

> Does this look correct? or most efficient?

Yeah it seems sane, however you have forgotten to add NOT NULL to the FK
fields.

--
Alvaro Herrera http://www.PlanetPostgreSQL.org/
"No me acuerdo, pero no es cierto. No es cierto, y si fuera cierto,
no me acuerdo." (Augusto Pinochet a una corte de justicia)

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Pavel Stehule 2007-12-11 19:46:27 Re: join on three tables is slow
Previous Message PostgreSQL Admin 2007-12-11 19:12:38 Re: Foreign Key for multi PK or design question