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 1hpCZZ-0000UB-Nt for pgsql-admin@arkaria.postgresql.org; Sun, 21 Jul 2019 14:12:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hpCZW-0004qG-E4 for pgsql-admin@arkaria.postgresql.org; Sun, 21 Jul 2019 14:12:18 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hpCZW-0004q9-2P for pgsql-admin@lists.postgresql.org; Sun, 21 Jul 2019 14:12:18 +0000 Received: from mail-ot1-x342.google.com ([2607:f8b0:4864:20::342]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hpCZU-0003lH-FO for pgsql-admin@lists.postgresql.org; Sun, 21 Jul 2019 14:12:17 +0000 Received: by mail-ot1-x342.google.com with SMTP id r21so31487823otq.6 for ; Sun, 21 Jul 2019 07:12:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=+gkfp6INIpN2kkK5SH6DVo/k9uDwoKHUYErVjUHojTA=; b=EaUQx+MM+ikmqDWFaueCwgDymeTE2yg3TSB7IHq+auk+OqIaeapLruggbkiA7aTvGE CZeKg7uIFNmWRO3a4HeDDJdBAN2N73tj7pC9KbBrAzavD0XcAKJCsQdWEFZTF/ptcjOL 93OzRDVifanjekrJQmNK4smW1z+VWTWF5l2nBmrwlQ4ukwA6YT97uTxurPgxdBodQo0G UjJfR0NEUkdtHIUwexglr9WjrMbvhhyo+Q0UkpnJ/ff+H1EiHai7PkUkjt6KqzXZqLk4 ofDybnAxeuyLUaZGpjLwuf+eUkAdbL34iKRkht6wwbZ0uzf5uKzvusSXsPQ+8B5xQXJd grTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=+gkfp6INIpN2kkK5SH6DVo/k9uDwoKHUYErVjUHojTA=; b=K23u6DL4OdJFeA3fcYtcAQehRTN1YbrR45m6vGSAuJ+3R/pOuoBtqUGegINw/mzGNU rC4yYINZsU5znG47nmKUhqM9feqJeWY6IpckHa4S1iymDIxfy0ZnOtc2hqPhtSPN8bEX 0d2qGNEOZNqkL8QCa6sQwpKXTHWROG3LXapiYZv0ofN0WWgS0OsdGLqU6wN+pvnOB2FG IMre4ySZqtkz9JO8eQlWxDQfprUWZbRST/YDdppbZgOcD7Lv0NUC9JxNvnh/awQW5x4o WdNAC5AFS9W7vDQAbMZeduFW4wyKmO8AX55XzV32RTBznlt7GmDtcRVD4vnM9nd4E4Gn tp+w== X-Gm-Message-State: APjAAAWI8cGFtO0PtcoAAEHHoE7/q5QFDe/hw47lPFHz01+kLFSn6ktY eVqvgHdzY8TZdo0Q6YxehDYix7sLFvw= X-Google-Smtp-Source: APXvYqwuOs+DFajnGZY3tleYiS50aG4qMkOZJ5LmA8c2OAgjVDxpExPbl2IUtFP87UhP/FJBR7KI+g== X-Received: by 2002:a9d:4d81:: with SMTP id u1mr1107200otk.221.1563718333710; Sun, 21 Jul 2019 07:12:13 -0700 (PDT) Received: from [192.168.1.10] (ip70-171-116-89.no.no.cox.net. [70.171.116.89]) by smtp.googlemail.com with ESMTPSA id f39sm14399833otb.57.2019.07.21.07.12.12 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 21 Jul 2019 07:12:12 -0700 (PDT) Subject: Re: How do I alter an existing column and add a foreign key which is a Primary key to a table? To: pgsql-admin@lists.postgresql.org References: <820008379.2579287.1563670685484@mail.yahoo.com> <5CE41E80-274A-44DA-9553-E2DCA260F751@jakobs.com> <385857992.2643116.1563717238725@mail.yahoo.com> From: Ron Message-ID: <923fb3b1-90e9-e0b5-bc5b-d896128c3f28@gmail.com> Date: Sun, 21 Jul 2019 09:12:12 -0500 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: <385857992.2643116.1563717238725@mail.yahoo.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 7/21/19 8:53 AM, Karen Goh wrote: > > On Sunday, July 21, 2019, 3:28:10 PM GMT+8, Holger Jakobs > wrote: > > Am 21. Juli 2019 02:58:05 MESZ schrieb Karen Goh : > [snip] > > This looks like a 1:1 relationship which is to be avoided. > > One ALTER TABLE command either adds a primary or a foreign key. There is > no FK without the keyword REFERENCES. > > FK relationships help in keeping the data consistent, but they are totally > unrelated to/useless for SELECT statements and their JOIN operations. This > is a common misconception. > > Hi Holger, > > After reading your reply, I am confused now. > > Cos without creating foreign key in my tutor_subject how am I going to > retrieve the zipcode at s_tutor table that meet the list of subject_name > and tutor_id in tutor_subject ? There should have some reference right ? > If not, how does the database tell this zipcode belong to which tutor_id ? The reference is in the SELECT statement, not in the FK definition. SELECT s_tutor.zip_code from subject_tutor, s_tutor where s_tutor.tutor_id = subject_tutor.tutor_id and subject_tutor.subject_name = 'Angela Merkel'; -- Angular momentum makes the world go 'round.