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 1hp6HX-0006R9-TZ for pgsql-admin@arkaria.postgresql.org; Sun, 21 Jul 2019 07:29:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hp6GX-0004w2-7H for pgsql-admin@arkaria.postgresql.org; Sun, 21 Jul 2019 07:28:17 +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 1hp6GW-0004s5-Ru for pgsql-admin@lists.postgresql.org; Sun, 21 Jul 2019 07:28:16 +0000 Received: from jakobs.com ([85.214.83.89] helo=rs.plausibolo.de) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hp6GQ-0000Kq-Kb for pgsql-admin@lists.postgresql.org; Sun, 21 Jul 2019 07:28:16 +0000 Received: from localhost (localhost [127.0.0.1]) by rs.plausibolo.de (Postfix) with ESMTP id E65AABE0C1D; Sun, 21 Jul 2019 09:28:08 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at rs.plausibolo.de Received: from rs.plausibolo.de ([127.0.0.1]) by localhost (h2367442.stratoserver.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id d58VblX4wUeH; Sun, 21 Jul 2019 09:28:07 +0200 (CEST) Received: from [192.168.178.194] (i577BCFB1.versanet.de [87.123.207.177]) by rs.plausibolo.de (Postfix) with ESMTPSA id B740ABE0662; Sun, 21 Jul 2019 09:28:06 +0200 (CEST) Date: Sun, 21 Jul 2019 09:28:04 +0200 User-Agent: K-9 Mail for Android In-Reply-To: <820008379.2579287.1563670685484@mail.yahoo.com> References: <820008379.2579287.1563670685484@mail.yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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,Karen Goh From: Holger Jakobs Message-ID: <5CE41E80-274A-44DA-9553-E2DCA260F751@jakobs.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Am 21=2E Juli 2019 02:58:05 MESZ schrieb Karen Goh : >Hi all, > >With PG4Admin, I am lost=2E Since pgadmin4 is just a frontend, you're not "lost with pgadmin4", but ma= ybe with postgresql=2E >I realised now that the keys are not created and perhaps that is why >the join query is not working out=2E Creating a foreign key doesn't magically create the necessary keys for it= =2E >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=2E > >So, meaning I need to create a foreign key as well as primary key for >tutor_id=2E > >So far, this is what I have attempted but it is not working=2E >ALTER TABLE tutor_subject > ADD CONSTRAINT tutor_subject_pk=20 > 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=2E One ALTER TABLE command either adds a primary or a foreign key=2E There is= no FK without the keyword REFERENCES=2E FK relationships help in keeping the data consistent, but they are totally= unrelated to/useless for SELECT statements and their JOIN operations=2E Th= is is a common misconception=2E Regards, Holger --=20 Holger Jakobs, Bergisch Gladbach +49 178 9759012 - sent from mobile, therefore short -