Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 86CF82E0073 for ; Tue, 11 Dec 2007 15:42:56 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 65838-01 for ; Tue, 11 Dec 2007 15:42:51 -0400 (AST) Received: from lists.commandprompt.com (host-159.commandprompt.net [207.173.203.159]) by postgresql.org (Postfix) with ESMTP id C95292E0043 for ; Tue, 11 Dec 2007 15:42:51 -0400 (AST) Received: from perhan.alvh.no-ip.org (200-126-76-117.bk5-dsl.surnet.cl [200.126.76.117]) (authenticated bits=0) by lists.commandprompt.com (8.13.8/8.13.8) with ESMTP id lBBJglBS016531 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Tue, 11 Dec 2007 11:42:50 -0800 Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id CDD8847C87; Tue, 11 Dec 2007 16:42:46 -0300 (CLST) Date: Tue, 11 Dec 2007 16:42:46 -0300 From: Alvaro Herrera To: PostgreSQL Admin Cc: pgsql-sql@postgresql.org Subject: Re: Foreign Key for multi PK or design question Message-ID: <20071211194246.GK10710@alvh.no-ip.org> References: <863ECFDF04C8804ABDF3559D87F157C638E5D9@MAGPTCPEXC02.na.mag-ias.net> <475ED504.7080006@productivitymedia.com> <20071211185330.GA16159@a-kretschmer.de> <475EE126.5030302@productivitymedia.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <475EE126.5030302@productivitymedia.com> User-Agent: Mutt/1.5.17 (2007-11-01) X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.0 (lists.commandprompt.com [207.173.203.159]); Tue, 11 Dec 2007 11:42:50 -0800 (PST) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200712/55 X-Sequence-Number: 29747 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)