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 1hp0he-0007h6-PO for pgsql-admin@arkaria.postgresql.org; Sun, 21 Jul 2019 01:31:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hp0hc-00018h-Dh for pgsql-admin@arkaria.postgresql.org; Sun, 21 Jul 2019 01:31:52 +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 1hp0hc-00017H-2A for pgsql-admin@lists.postgresql.org; Sun, 21 Jul 2019 01:31:52 +0000 Received: from sonic312-22.consmr.mail.bf2.yahoo.com ([74.6.128.84]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hp0hU-0005gK-Lv for pgsql-admin@lists.postgresql.org; Sun, 21 Jul 2019 01:31:51 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1563672701; bh=7+77QB64Stm9NZnbspKbudqLNNONG6bzZ0eaM0MDIGw=; h=Date:From:To:In-Reply-To:References:Subject:From:Subject; b=N2QhJLyIsIDD9Td6LeJeA7EEcnvNSpLzWapjggq3ChuzZlQIseN/JCGgd/0OkNX6eDcl04nfkuEzR8MLH7N3r8LVzfQw4gJM4Cv/PvmQHLYtMMeINPvmImGWKDiz9Unlmz3FXuPZcnRAYMM0t7ZHgsunjVISVfJXN3bWE+lh9BoM7gbVUxZ+5j4ZQ7AocVhgnoqlDBxBEWJLa7AE76we0QushqaQ8rNgTEjWte/yvsEBymQCApXrHaOcTlSaNDCGEhocZz9elmaOjrKMZn9xN+Qx2X+AxXL3b+XVdRgyzkflBEov+3Tas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oXP37 gP7cq1c.ex105gcAajkyFKEyGecAsfRglZN1Vkl9Qr0gNewesfDXClTwW4LR7RzBXk3Bgki0uVil 4BAaV08SiUANvThKM8upcn7B9D7B.LfLPnc5Z9_sIONEXsHWgZpCvXxjHIgyy0YexMEJPc1TDDMw Uvdl77CqtmV1ieg0hCdNaiLFIBTocqOhh80XdAGnUcg5NmRLgjtfLNyxZQ9euhoNpsVRJcF5rDR3 U8DfXgVPo_KKG Received: from sonic.gate.mail.ne1.yahoo.com by sonic312.consmr.mail.bf2.yahoo.com with HTTP; Sun, 21 Jul 2019 01:31:41 +0000 Date: Sun, 21 Jul 2019 01:31:39 +0000 (UTC) From: Karen Goh To: pgsql-admin@lists.postgresql.org, Ron Message-ID: <1813907165.2572427.1563672699363@mail.yahoo.com> In-Reply-To: <24840698-956d-31fe-96dd-9e9427890e39@gmail.com> References: <820008379.2579287.1563670685484@mail.yahoo.com> <24840698-956d-31fe-96dd-9e9427890e39@gmail.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_2572426_1731224003.1563672699362" Content-Length: 4738 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------=_Part_2572426_1731224003.1563672699362 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On Sunday, July 21, 2019, 9:25:54 AM GMT+8, Ron wrote: 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? Yes. It is already there but it is the first time I used pgAdmin4 so I just used the add column to put in the infor. Now, I just tried want to do one thing first which is to alter the tutor_id in tutor_subject to a primary key. ALTER TABLE tutor_subject ADD CONSTRAINT tutor_subject_pk PRIMARY KEY (tutor_id) But, am receiving error messagte : ERROR: could not create unique index "tutor_subject_pk" DETAIL: Key (tutor_id)=(0) is duplicated. SQL state: 23505 I noticed several of the rows has 0 at tutor_id. It must have attributed to the table not created properly. How do I resolve this ? delete those rows? What foreign table are you referencing? (I don't see that referenced in your example.) The foreign table will be s_tutor which has a tutor_id as well. So, the tutor_id in tutor_subject will be both primary key as well as foreign key. 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. ------=_Part_2572426_1731224003.1563672699362 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable





On Sunday, July 21, 2019, 9:25:54 = AM GMT+8, Ron <ronljohnsonjr@gmail.com> wrote:


On 7/20/19 = 7:58 PM, Karen Goh wrote:

> Hi all,
>
> I used to wri= te a script in MYSQL and foreign and primary key will be created.
>> With PG4Admin, I am lost.
>
> I realised now that the ke= ys are not created and perhaps that is why the join query is not working ou= t.
>
> Please let me know what is the correct way to alter a co= lumn 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 wh= at I have attempted but it is not working.
> ALTER TABLE tutor_subjec= t
> ADD CONSTRAINT tutor_subject_pk
> PRIMARY KEY (tuto= r_id)
> ADD CONSTRAINT tutor_subject_fk
> FOREIGN KEY (= tutor_id)


What error message do you get?

Does tutor_id al= ready exist in tutor_subject?

Yes. It is already there but it is th= e first time I used pgAdmin4 so I just used the add column to put in the in= for.

Now, I just tried want to do one thing first which is to alter = the tutor_id in tutor_subject to a primary key.

ALTER TABLE tutor_su= bject
ADD CONSTRAINT tutor_subject_pk
PRIMARY KEY (tutor_id)
But, am receiving error messagte :

ERROR: could not create un= ique index "tutor_subject_pk"
DETAIL: Key (tutor_id)=3D(0) is= duplicated.
SQL state: 23505

I noticed several of the rows has 0= at tutor_id. It must have attributed to the table not created properly.
How do I resolve this ? delete those rows?

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

The foreign table will be s_tutor which has a tutor_id as well.
=
So, the tutor_id in tutor_subject will be both primary key as well as f= oreign key.

Have you read the documentation?
https://www.postgres= ql.org/docs/9.6/sql-altertable.html
http://www.postgresqltutorial.com/po= stgresql-primary-key/
http://www.postgresqltutorial.com/postgresql-forei= gn-key/


--
Angular momentum makes the world go 'round.


=20 ------=_Part_2572426_1731224003.1563672699362--