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

From: Ron <ronljohnsonjr(at)gmail(dot)com>
To: pgsql-admin(at)lists(dot)postgresql(dot)org
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 01:25:33
Message-ID: 24840698-956d-31fe-96dd-9e9427890e39@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin pgsql-sql

On 7/20/19 7:58 PM, Karen Goh wrote:
> Hi all,
>
> I used to write a script in MYSQL and foreign and primary key will be created.
>
> With PG4Admin, I am lost.
>
> I realised now that the keys are not created and perhaps that is why the join query is not working out.
>
> 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)

What error message do you get?

Does tutor_id already exist in tutor_subject?

What foreign table are you referencing?  (I don't see that referenced in
your example.)

Have you read the documentation?
https://www.postgresql.org/docs/9.6/sql-altertable.html
http://www.postgresqltutorial.com/postgresql-primary-key/
http://www.postgresqltutorial.com/postgresql-foreign-key/

--
Angular momentum makes the world go 'round.

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Karen Goh 2019-07-21 01:31:39 Re: How do I alter an existing column and add a foreign key which is a Primary key to a table?
Previous Message Karen Goh 2019-07-21 00:58:05 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 Karen Goh 2019-07-21 01:31:39 Re: How do I alter an existing column and add a foreign key which is a Primary key to a table?
Previous Message Karen Goh 2019-07-21 00:58:05 How do I alter an existing column and add a foreign key which is a Primary key to a table?