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 1hp1Ge-0000bB-RE for pgsql-admin@arkaria.postgresql.org; Sun, 21 Jul 2019 02:08:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hp1Gc-00068b-Da for pgsql-admin@arkaria.postgresql.org; Sun, 21 Jul 2019 02:08:02 +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 1hp1Gc-00068U-2H for pgsql-admin@lists.postgresql.org; Sun, 21 Jul 2019 02:08:02 +0000 Received: from mail-oi1-x244.google.com ([2607:f8b0:4864:20::244]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hp1GV-00063s-6d for pgsql-admin@lists.postgresql.org; Sun, 21 Jul 2019 02:08:01 +0000 Received: by mail-oi1-x244.google.com with SMTP id m202so27136514oig.6 for ; Sat, 20 Jul 2019 19:07:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=oER1di+7bCFJzNG/L70QnQM4rqSopNpxMugk7dYnxQI=; b=reCwoc2JtIIK9oMCxKHzSfUUoj51LuywBl2huxBQQnZPJUh+oxdELNuFD5XZKocrO4 y7QLOPKDWg4AcYytfwocxCW92WyP/kx1f8SwIbvlcadC8DwUMu24Y2/bXnvbKfEmBCiy PVoUlQSJ4eegNQ7kTRn6XLSuZoDv9Q7SHR2FJfGXd3lT05IWeSGVpeKo10sCIFjGsoTG TbnlLbYD2T6EEHLdmHS/tR8eYXlqmZCHCHi27gVlIzckTzI9Q6cbVZTF/Y0p2hGi7/ZJ A9cNe5r/v186XuyEh1lKlPoCadzQ1SNzNVY97pyx5CSySd+0/0SnHQ8EmXWobxKdUN5r TR2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=oER1di+7bCFJzNG/L70QnQM4rqSopNpxMugk7dYnxQI=; b=tlnPY+h6Unqc2+4iJXXNJ+HACbDZg2esmqo2I7QxqFUx2aP3zr97ciUEuwcnbG85cF 1o2EGT/nljUAvtbXUDzZ7tPAWGC40EsbS3YclJI58DIL8ZSJewVwRCAQT+aZopHj15Rk NWBsHZrdThHoP3I5KBtXqVo5Tf4zLpr++a4zPDbUMhOGUv81zvK62QhgavmEldmHu1wZ 8Z36aKo/i1j/GTTGNtNx1O6XnLrlwlmuQo1vseRhsbHIzVjoBVTQK+BYRU5MFNe3/bap HHzhYW/nI0R13TqU9qynERxb0YYlpbHLsgBObs1byY2g0oXHRr3eS/LOnmG94G3DHSRY ISHA== X-Gm-Message-State: APjAAAV5Vrh8/7ONLKRJzJfkKZ5ym07z/0I12DH0Q0XZUI7+sfLhlWtV qX7w0aKf06T5jH4DOaU8cHWJ1IZRBcs= X-Google-Smtp-Source: APXvYqyggo0Il2sMMUyDVp97dXWylo9xhvd2ONzNuwnGc50nihSZ/6qfBT/Ug3b4FGXKWJItFAeF8Q== X-Received: by 2002:aca:f40a:: with SMTP id s10mr29503755oih.51.1563674871042; Sat, 20 Jul 2019 19:07:51 -0700 (PDT) Received: from [192.168.1.10] (ip70-171-116-89.no.no.cox.net. [70.171.116.89]) by smtp.googlemail.com with ESMTPSA id j8sm11821784otl.9.2019.07.20.19.07.49 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 20 Jul 2019 19:07:49 -0700 (PDT) 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 References: <820008379.2579287.1563670685484@mail.yahoo.com> <24840698-956d-31fe-96dd-9e9427890e39@gmail.com> <1813907165.2572427.1563672699363@mail.yahoo.com> <8da1d777-b5d3-30e1-e942-424ece88c4de@gmail.com> <337537607.2613319.1563674408215@mail.yahoo.com> From: Ron Message-ID: Date: Sat, 20 Jul 2019 21:07:48 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: <337537607.2613319.1563674408215@mail.yahoo.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 7/20/19 9:00 PM, Karen Goh wrote: > > On Sunday, July 21, 2019, 9:49:13 AM GMT+8, Ron > wrote: > > > On 7/20/19 8:31 PM, Karen Goh wrote: > > > > 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? > > Naturally. You can't have a unique index with duplicate keys. > > Sorry Ron. I just realised that my tutor_id needs to contain duplication > becuase of my use case. > Basically, tutor_subject is a 'JOIN' table so it will have duplicate > tutor_id as it is a many-to-many relationship design. > > In this case, what should I do then since I can't make tutor_id a Primary > key but yet it has to reference s_tutor.tutor_id as foreign key? Only you know your data and use cases.  Is there another column you can add to make it a compound PK?  Or create a synthetic key? > > > > > 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. > > You can't just say "tutor_id is a foreign key"; you've got to tell it the > name of the foreign table. > > > > > > 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. > > > > > > > > -- > Angular momentum makes the world go 'round. > > -- Angular momentum makes the world go 'round.