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 1iVKB4-0005V9-IF for pgsql-general@arkaria.postgresql.org; Thu, 14 Nov 2019 18:49:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iVKB3-0001hm-00 for pgsql-general@arkaria.postgresql.org; Thu, 14 Nov 2019 18:49:09 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iVKB2-0001hd-Gq for pgsql-general@lists.postgresql.org; Thu, 14 Nov 2019 18:49:08 +0000 Received: from mail-ot1-x341.google.com ([2607:f8b0:4864:20::341]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iVKB0-0004c7-10 for pgsql-general@lists.postgresql.org; Thu, 14 Nov 2019 18:49:07 +0000 Received: by mail-ot1-x341.google.com with SMTP id l14so5768120oti.10 for ; Thu, 14 Nov 2019 10:49:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:date:user-agent:mime-version :content-transfer-encoding:content-language; bh=rXdZodk2HBaIFwmD7rW7XL1P/wvZoStGJdn4MedeCXc=; b=odgYZfDZRDZaXrUtR4rK7eDLtGyXc/ccymx+jm0SDWh7PGoyy1/I6daxkYyk6AF6LI ajJNCoNIyYFxPJa8TRf+hOnLcSOHBQxM0P//t5ALfol+cmokiu/0AHhTg8QAVLfiBq1o acAxqbR8xO5Ato4H4VCnw1SJcezqJ9dJhRFReVpbiicOA5DJtvGamTjfzWi+k9I6pFw+ NalHE8irWcr+lOG50SYPCkuAaMUv5eYCpT0uR7KS9B67VzWVXfrdYMTyvW7x8AEocxyb dkxeFa7HMFDrU6mcx1cHJWFdb7l22BWlOA6ogc1hnNDSVje0NJ22sy/eThH+D/4YILYe LAgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-transfer-encoding:content-language; bh=rXdZodk2HBaIFwmD7rW7XL1P/wvZoStGJdn4MedeCXc=; b=elpwx95aqYGV1ItnvYaCP0RvM/+VMsPnbis+ePxTr7cip9kCkMw85C03AKbuuk/4cW t/r/BjlOEkyFKPISZQy0sipJ51EBr6hjHl5nLisUt0FlZaJoRLak6sZTaPabZfhfHjV1 E+ZT3I21iPcCNirJbYBWfs/cJCzE5ev8GH/y6mr00ZRcJPpqpyd3JpZT9/BdcBXkkN/D WMxepLEd0+dJlOk7IM/Z4KYTd1XGkp0ZnCghujfgtHYfw4QuR+nLcCRbigkYPMvZpzhy N7NoeSE4lo7tg1zLXAMXFa/QaVuimIgO0MfE+IMjnVJR/A2KE9EcH48qgzsc4W3K17Je 36Qg== X-Gm-Message-State: APjAAAUSRDTtTSyc2TrmOKaSC+Y4JNn/GzmadM//Mv43EdFoC+R/mqSo PSUdN/TwKhH4Qt4h7q1fh577Se1lUeM= X-Google-Smtp-Source: APXvYqxqxLiuWnZ0u1boqkuTcn/4x+UwRxEpFr2krM4PPWif7M/NV+9qP0/sA8cDaAwYkQ2KyNwzIg== X-Received: by 2002:a05:6830:1e16:: with SMTP id s22mr8753174otr.364.1573757344948; Thu, 14 Nov 2019 10:49:04 -0800 (PST) Received: from [192.168.1.10] (ip70-186-126-197.no.no.cox.net. [70.186.126.197]) by smtp.googlemail.com with ESMTPSA id k135sm2070725oih.54.2019.11.14.10.49.03 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 14 Nov 2019 10:49:04 -0800 (PST) To: pgsql-general@lists.postgresql.org From: Ron Subject: Query which shows FK child columns? Message-ID: Date: Thu, 14 Nov 2019 12:49:03 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 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 v9.6.16 I have a query which shows the parents and children in FK relations, along with the parent column name, but can't seem to find the child column names. Is there a way to find the child column names without having to dig into pg_constraint? Thanks test=# select ccu.table_schema||'.'||ccu.table_name as parent_table, test-#        ccu.column_name as parent_column, test-#        tc.table_schema||'.'||tc.table_name as child_table, test-# ccu.constraint_schema||'.'||ccu.constraint_name as con_name test-# from information_schema.table_constraints tc, test-#      information_schema.constraint_column_usage ccu test-# where tc.constraint_type = 'FOREIGN KEY' test-#   and tc.constraint_schema = ccu.constraint_schema test-#   and tc.constraint_name = ccu.constraint_name test-# order by parent_table, child_table, ccu.column_name test-# ;     parent_table     | parent_column | child_table     |              con_name ---------------------+---------------+---------------------+------------------------------------  public.inventory    | inv_id        | public.sales_detail | public.sales_detail_sd_inv_id_fkey  public.sales_header | parent_id     | public.sales_detail | public.sales_detail_id_fkey (2 rows) test=# \d inventory              Table "public.inventory"    Column    |          Type          | Modifiers -------------+------------------------+-----------  inv_id      | integer                | not null  description | character varying(255) | Indexes:     "inventory_pkey" PRIMARY KEY, btree (inv_id) Referenced by:     TABLE "sales_detail" CONSTRAINT "sales_detail_sd_inv_id_fkey" FOREIGN KEY (sd_inv_id) REFERENCES inventory(inv_id) test=# \d sales_header    Table "public.sales_header"   Column   |  Type   | Modifiers -----------+---------+-----------  parent_id | integer | not null Indexes:     "sales_header_pkey" PRIMARY KEY, btree (parent_id) Referenced by:     TABLE "sales_detail" CONSTRAINT "sales_detail_id_fkey" FOREIGN KEY (child_id) REFERENCES sales_header(parent_id) test=# \d sales_detail    Table "public.sales_detail"   Column   |  Type   | Modifiers -----------+---------+-----------  child_id  | integer | not null  seq       | integer | not null  sd_inv_id | integer | Indexes:     "sales_detail_pkey" PRIMARY KEY, btree (child_id, seq) Foreign-key constraints:     "sales_detail_id_fkey" FOREIGN KEY (child_id) REFERENCES sales_header(parent_id)     "sales_detail_sd_inv_id_fkey" FOREIGN KEY (sd_inv_id) REFERENCES inventory(inv_id) -- Angular momentum makes the world go 'round.