Re: How do I alter an existing column and add a foreign key which is a Primary key to a table?

From: Karen Goh <karenworld(at)yahoo(dot)com>
To: pgsql-admin(at)lists(dot)postgresql(dot)org, Ron <ronljohnsonjr(at)gmail(dot)com>
Subject: Re: How do I alter an existing column and add a foreign key which is a Primary key to a table?
Date: 2019-07-21 14:24:35
Message-ID: 1745586001.2633224.1563719075867@mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin pgsql-sql

On Sunday, July 21, 2019, 10:12:33 PM GMT+8, Ron <ronljohnsonjr(at)gmail(dot)com> wrote:

On 7/21/19 8:53 AM, Karen Goh wrote:
>
> On Sunday, July 21, 2019, 3:28:10 PM GMT+8, Holger Jakobs
> <holger(at)jakobs(dot)com> wrote:
>
> Am 21. Juli 2019 02:58:05 MESZ schrieb Karen Goh <karenworld(at)yahoo(dot)com>:
>
[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';

Hi Ron,

You can't Select s_tutor.zip_code from subject_tutor as zip_code belongs to s_tutor table.

Hence, I wrote my last message about my confusion.

--
Angular momentum makes the world go 'round.

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Tom Lane 2019-07-21 14:36:06 Re: Database.Schema.Table
Previous Message Ron 2019-07-21 14:12:12 Re: How do I alter an existing column and add a foreign key which is a Primary key to a table?

Browse pgsql-sql by date

  From Date Subject
Next Message David G. Johnston 2019-07-21 14:54:54 Re: How do I alter an existing column and add a foreign key which is a Primary key to a table?
Previous Message Ron 2019-07-21 14:12:12 Re: How do I alter an existing column and add a foreign key which is a Primary key to a table?