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 1hpCHv-00084H-3B for pgsql-admin@arkaria.postgresql.org; Sun, 21 Jul 2019 13:54:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hpCHt-0003xm-QC for pgsql-admin@arkaria.postgresql.org; Sun, 21 Jul 2019 13:54:05 +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 1hpCHt-0003xf-Dr for pgsql-admin@lists.postgresql.org; Sun, 21 Jul 2019 13:54:05 +0000 Received: from sonic306-2.consmr.mail.bf2.yahoo.com ([74.6.132.41]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1hpCHp-0003Gd-Q6 for pgsql-admin@lists.postgresql.org; Sun, 21 Jul 2019 13:54:03 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1563717240; bh=5llJmLdkpPOV63Lz3KbuVC+rovx+icsXLYNQhapVJOM=; h=Date:From:To:In-Reply-To:References:Subject:From:Subject; b=XzGQLg2+hYv5U36lA9yQdtPhZZxRWg6fyifyE9QPNfiUo4DUUyK2Zwfn0W8Zzge9VviNFYAtV+eM6Fix04VPHF7DqzVOJfcu/w+ZuadsRF8R7HJdC2w7MyqJ4UMC8y2n5CSB6B4tBPsP+8qyZqFsao6roFcqyB6VqPf6XoyB5uc+BT9TOMC6AWDBf80mDVlI74jyJRGaNv3EnNTyDS28B/9E2Xbi5oUkwmh3EPtsX2CuhTNW6BOaA+Ej7EYn2mReOQC3kjfF9fzixGDMLgB5mVLmgKAPEbwmCiM8fUO21F+kQH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IPSFGR5E_z1sVdc_Pvr9VIc.m3Bax6O6 dcvT9eQSe5R9mzIbNajke3jUUuAkhPLTo3bbtNkKRIVsMCxPqKmZr1KY1Rnsm0ZVpS1S.AYGce4B I0eUTucaG4FloHM4cr7gM5np2BirLkh7dM_32ZWDGj_Rufcp9KJGy_vRa.s4l1kb6ilr8erYM1oq yhmNA6ygNSrWIo6zSsLW1Snw9.Y7el1LMYpaFpyR_kmTBK7jwRZfpBSj_iA9pKcsVbWswGxGxXkE 98B_1PALq.QlACIZRpUj17Q-- Received: from sonic.gate.mail.ne1.yahoo.com by sonic306.consmr.mail.bf2.yahoo.com with HTTP; Sun, 21 Jul 2019 13:54:00 +0000 Date: Sun, 21 Jul 2019 13:53:58 +0000 (UTC) From: Karen Goh To: pgsql-admin@lists.postgresql.org, Holger Jakobs Message-ID: <385857992.2643116.1563717238725@mail.yahoo.com> In-Reply-To: <5CE41E80-274A-44DA-9553-E2DCA260F751@jakobs.com> References: <820008379.2579287.1563670685484@mail.yahoo.com> <5CE41E80-274A-44DA-9553-E2DCA260F751@jakobs.com> Subject: Re: How do I alter an existing column and add a foreign key which is a Primary key to a table? MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_2643115_983167770.1563717238724" Content-Length: 4178 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------=_Part_2643115_983167770.1563717238724 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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 : >Hi all, > >With PG4Admin, I am lost. Since pgadmin4 is just a frontend, you're not "lost with pgadmin4", but maybe with postgresql. >I realised now that the keys are not created and perhaps that is why >the join query is not working out. Creating a foreign key doesn't magically create the necessary keys for it. >Please let me know what is the correct way to alter a column in a table >to have foreign key to a tutor_id which is also the primary key of that >table. > >So, meaning I need to create a foreign key as well as primary key for >tutor_id. > >So far, this is what I have attempted but it is not working. >ALTER TABLE tutor_subject > ADD CONSTRAINT tutor_subject_pk > PRIMARY KEY (tutor_id) > ADD CONSTRAINT tutor_subject_fk > FOREIGN KEY (tutor_id) 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 ? Regards, Holger -- Holger Jakobs, Bergisch Gladbach +49 178 9759012 - sent from mobile, therefore short - ------=_Part_2643115_983167770.1563717238724 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable





On Sunday, July 21, 2019, 3:28:10 = PM GMT+8, Holger Jakobs <holger@jakobs.com> wrote:



Am 21. Juli 2019 02:58:05 MESZ schrieb Karen Goh <karenworld@yahoo.com&= gt;:
>Hi all,
>
>With PG4Admin, I am lost.

Since p= gadmin4 is just a frontend, you're not "lost with pgadmin4", = but maybe with postgresql.

>I realised now that the keys are not = created and perhaps that is why
>the join query is not working out.
Creating a foreign key doesn't magically create the necessary key= s for it.


>Please let me know what is the correct way to alte= r a column in a table
>to have foreign key to a tutor_id which is als= o the primary key of that
>table.
>
>So, meaning I need t= o create a foreign key as well as primary key for
>tutor_id.
><= br>>So far, this is what I have attempted but it is not working.
>= ALTER TABLE tutor_subject
> ADD CONSTRAINT tutor_subject_pk
> = PRIMARY KEY (tutor_id)
> ADD CONSTRAINT tutor_subject_fk
>= ; FOREIGN KEY (tutor_id)


This looks like a 1:1 relationship w= hich 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 un= related 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_su= bject 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 so= me reference right ? If not, how does the database tell this zipcode belon= g to which tutor_id ?



Regards,

Holger

--
H= olger Jakobs, Bergisch Gladbach
+49 178 9759012
- sent from mobile, t= herefore short -
=20 ------=_Part_2643115_983167770.1563717238724--