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 1hp0be-0007Pz-Qt for pgsql-admin@arkaria.postgresql.org; Sun, 21 Jul 2019 01:25:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hp0bd-0005xV-7A for pgsql-admin@arkaria.postgresql.org; Sun, 21 Jul 2019 01:25:41 +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 1hp0bc-0005xO-RX for pgsql-admin@lists.postgresql.org; Sun, 21 Jul 2019 01:25:40 +0000 Received: from mail-ot1-x341.google.com ([2607:f8b0:4864:20::341]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hp0bZ-0005dO-Ho for pgsql-admin@lists.postgresql.org; Sun, 21 Jul 2019 01:25:40 +0000 Received: by mail-ot1-x341.google.com with SMTP id r21so30592602otq.6 for ; Sat, 20 Jul 2019 18:25:36 -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=RmiqsXWhnVQk+fTbeCBFxKZkSaVMoCmk2cD8Li0gu1A=; b=XzoMiR/SGp/DWHvf0KEEH3W3tadMtpsVWCeJi3gwd+w/bUjYXoH/vihRwby//YGugv bNCq89rTuRIslrl/HAqIOFK89rVw4HnAt9dApW4cIrCFSCtWONRdCZo1NeOXi6KR7rmi kwGV6bNCAc5egOkaY2RtC2TO4g4K2NS/cvjj6N0uOkiaSO0omrwN9lg5FWYIfS/DQtBD U+3Mzs2yZ+TPE45R8K5RDpfoVrHLfVPJCYeI8Mvjk4ERce/166sSoaWNbJzHmWooqu/Q VHEsoIKR49UgLfrAcWSFAbfWxPa2lnFUsrpKYdn+6yYmQPoKclyhQmZ+/V/XxasNNAHU WbXQ== 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=RmiqsXWhnVQk+fTbeCBFxKZkSaVMoCmk2cD8Li0gu1A=; b=kowMARFFuaGaKmkhI3ZbQ/JN0gEb6HoOl4QotY16ecvSbzA1vezjwX5zQWaheS9LLx E07i7TPinoBwANqGcIdc01wul9QzVkDeqOpkOuuxXZ+2BBy9Es4L8BO+AvYkdZ0aXXsD 8Z08dzUpKbNNYpMfviJq+YFzkVAkTtWHcALnaUt5q8EXmKTkta8Unj5LSVTQYXuL2vYM I1GyAWh5ZMfkeTe0fkDRttCwfffm1e9KdK+iCVwk0cZB21NqxhqZcqSV5+E0g0nOMvNs BwKrJTIjtXw1/PHPubnL974nMiYv8RtiYqUpGuWa5YU5m258qLzlHEkQp3Rhr0D+OqRP xHxw== X-Gm-Message-State: APjAAAVF66iT9Z8A2r9FjhtJHy8AcgBID9sLEydIqa5VcSSuhmXE8YPU vls9dYDV5JV0CWOtmfBB06NN6VchqQU= X-Google-Smtp-Source: APXvYqxhjUF9x4jMTdj9M5Yx8BG5StdegT7++cK/ETwj2nUCECI/99DwGJ+QHpqYppAigyuiA60aew== X-Received: by 2002:a9d:460d:: with SMTP id y13mr25561998ote.368.1563672334980; Sat, 20 Jul 2019 18:25:34 -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 d22sm12170758oig.38.2019.07.20.18.25.33 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 20 Jul 2019 18:25:34 -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> From: Ron Message-ID: <24840698-956d-31fe-96dd-9e9427890e39@gmail.com> Date: Sat, 20 Jul 2019 20:25:33 -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: <820008379.2579287.1563670685484@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 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.