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 1hp0yD-0008Kh-FL for pgsql-admin@arkaria.postgresql.org; Sun, 21 Jul 2019 01:49:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hp0yB-0004nG-Ta for pgsql-admin@arkaria.postgresql.org; Sun, 21 Jul 2019 01:48:59 +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 1hp0yB-0004jY-Fk for pgsql-admin@lists.postgresql.org; Sun, 21 Jul 2019 01:48:59 +0000 Received: from mail-ot1-x344.google.com ([2607:f8b0:4864:20::344]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hp0y8-0005ur-Jh for pgsql-admin@lists.postgresql.org; Sun, 21 Jul 2019 01:48:59 +0000 Received: by mail-ot1-x344.google.com with SMTP id j19so36673587otq.2 for ; Sat, 20 Jul 2019 18:48:56 -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=FHhlDIIGOZHwKyUf6WU8BXcmdfx/CfDeasPbdnufGEI=; b=AY+FB8dE+avsG3c8ODbNyXJjnb6aJAPOR7n7fTd8sLp8h6L9uhy2PnvN82YN8+47TI GdAW6j+MhgAY0Ijr5BDg2UUnWa5nG3XfZh6U8JSmdYf0YRoVbARIcvcNPR2XB+G1ZOsD 3k7KDrJRk8P5T6dbhKx7LH4MeFzejKCQeQjOFQDIMjuEkP8tSKSDAgYJyz0N5PbBJs2x omnQHw61nP3WMikLU2ns/xCD1OnoR06Yzvz66AwSl2T3fR8S+QLiy62uhyKB+3hkF+xn 8Td6zMLMDZl1CotyZ6hu+uPHrG75eP6M4MGBIUqBTtMm468UQ5J9yR2Yd9Sb120EM6j8 UYyQ== 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=FHhlDIIGOZHwKyUf6WU8BXcmdfx/CfDeasPbdnufGEI=; b=s1zdL+RJF8QO0FY2rpP3hsj/ignqPm3kC4quDgH3lPWGfwT/wVFLbHsBI7WN8sb+AL bfbuud3/GSPTNosal+5jahzkXwpYuBG0RiyiOdE6/lEzVcrochxwsPBrat84gOjKXWo7 PrYfKEMte5Qr0HEfY4fC/82SjZxYtCnzwACgbthoIxl+NhqgJfV+J7vFShQTQwgfMQa4 XaY2kpKxqsjIaeEleVUrn7Iik1BXQEANJVqm8yY0BUk93XXHCuOsRka1ewRNwebtJZYF eVA28m10icHk9hxnALOZin1Clw3+7kEkH3A+zEUczvv7xZC/NIcT0VyYIJrq/JKvXnju zR5Q== X-Gm-Message-State: APjAAAVUCERIFn40kJDvwmCt9RLVdp9aeXVRid07bVG1awO3ICCY+PVG nigaK+6o+MctJX2IgbWYJa1dYj6zAjQ= X-Google-Smtp-Source: APXvYqz+28PTuSykSu7/NpW4Ej+FVoFY0J1i/P/XsgItQzg5/iy0DIug8cl6V2J+sVzTripO7Dk4xw== X-Received: by 2002:a9d:76ce:: with SMTP id p14mr24576457otl.342.1563673733777; Sat, 20 Jul 2019 18:48:53 -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 f84sm12343230oig.43.2019.07.20.18.48.52 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 20 Jul 2019 18:48:53 -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> From: Ron Message-ID: <8da1d777-b5d3-30e1-e942-424ece88c4de@gmail.com> Date: Sat, 20 Jul 2019 20:48:52 -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: <1813907165.2572427.1563672699363@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 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. > > 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.