Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hfvf3-0001Mg-Tz for pgsql-general@arkaria.postgresql.org; Wed, 26 Jun 2019 00:19:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hfvf2-0002WG-O8 for pgsql-general@arkaria.postgresql.org; Wed, 26 Jun 2019 00:19:40 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hfvf0-0002T8-UQ for pgsql-general@lists.postgresql.org; Wed, 26 Jun 2019 00:19:40 +0000 Received: from wout4-smtp.messagingengine.com ([64.147.123.20]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hfvex-0003Nv-O0 for pgsql-general@postgresql.org; Wed, 26 Jun 2019 00:19:37 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id CBF383E9; Tue, 25 Jun 2019 20:19:33 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Tue, 25 Jun 2019 20:19:33 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=E nVWGdTZFwXoLtUvrV6cgovXR9yo7R8cBP3zXugfNPg=; b=QmSfYunPC2mDf94+S XAikBtKw2hOI+CpD40FklQy3kt94EHluwR82Fa2JnJe/ie0VWhJgbKYp9F6Mxu1J Isi82E4qUl261Txu6x03+pC4itwbLL2UEWeVq2dDhcfnAZ0q632LsZjzqK7s/74w JJXtplM5nCuYA2xohMT6LjZDTqIlGivXe49TLjND4SHWH8LgWU73UKcEeO3F7PCH AAse0lxRmsjP1Gj8DH8NnMbc2ga/4s4t3oHy+5bt0f3DGUqxv//wRiiEdYrVvPp5 O0AUfCZ5yAJ6nS37rzMi3Zq8Q4O/dQorWvQO+pkLW2wOiWEAP8vigkVlg5lXRFdZ McYPA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=EnVWGdTZFwXoLtUvrV6cgovXR9yo7R8cBP3zXugfN Pg=; b=Lx0XQ4OMX5v1Kx9DkRHpP3e5i+uVwvcG3YAyjhAlgnU9uHZcCS28SW9Dn UmiiMNr/G9jb8uktgyhnH9kWlSkL2P2YDByfKVuyFc1QfJRFzenSVx52rxoUojA5 665ge16tYaRnR7wNptw+eVzPRsoKXpf+1/tHzaU3/0NcFkj80j6Jr5cygSj6O8k3 dg0y1ybAYLewyUQPQhLaLiHaPcEhaP5sijDXmewuLtQUHlBpHtBjvSwbZjYiUfFv GuzZucT63adYqFwLajBqOVwHCBGSNkQ57RmSF7oZGHuSLA4Q58jp1ZamCBiZCumP gSu1QaFzjoaRhLBr9PWvqEKSxg/wA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduvddrudehgddvlecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesthekre dttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhmqeenucfkphepjedurddvuddvrdduhedtrddvtd dvnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhl rghvvghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id B5F06380075; Tue, 25 Jun 2019 20:19:32 -0400 (EDT) Subject: Re: Need a referential constraint to a non-unique record To: David Gauthier , pgsql-general@postgresql.org References: From: Adrian Klaver Message-ID: <37267c00-86b5-e17d-f667-99cb2968c2a9@aklaver.com> Date: Tue, 25 Jun 2019 17:19:31 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 6/25/19 2:58 PM, David Gauthier wrote: > I need to create a constraint on a column of a table such that it's > value is found in another table but may not be unique in that other > table.  Example... > > Let's say the DB is about students and the grades they got for 4 > subjects... Math, English, Science, History.  But instead of creating 4 > records in the "grades" table for every record in the "students" table, But you are. > I storing each unique combination of grades in the "grades" table, those > records tied together with a common "id" field... > > grade_id   subject      grade > 1          math         A > 1          english      A > 1          science      A > 1          history      A > 2          math         B > 2          english      A > 2          science      C > 2          history      B > > etc...  Each unique combination of the 4 subject/grades gets a new "id" > and those 4 records are written to the grates table. Why not?: grade_id student_id subject grade 1 1 math A 2 1 english A 3 1 science A 4 1 history A 5 2 math B 6 2 english A 7 2 science C 8 2 history B Where grade_id is the PK and student_id is FK to students > > Now, in the "students" table I have a "grad_id" column which points to > the set of grades for that student.  The "grade_id" value in the > "students" table must also exist in the "grades" table.  But the > grade_id value is pointing to 4, not 1 record in the "grades" table. > And "grade_id" in the "grades" table can't (obviously) be a PK. > > There are no primary keys in this scenario so I don't think I can set up > a traditional primary-foreign key relationship. > > I could do this with a check constraint.  But I want the ER view in the > DBeaver tool to recognize the constraint and depict it. > > I suppose I could create a bridge table between the "students" and > "grades" table which has only the "grades_id" column as a primary key, > and then set up 2 traditional primary/foreign key constraints (one > between this new table and "grades", and the other between this new > table and "students").  But it's kinda unnecessary and am looking for > something more direct, without the bridge. > > Any ideas ? > psql (9.6.7, server 9.5.2) on linux > -- Adrian Klaver adrian.klaver@aklaver.com