Re: [GENERAL] What is the proper query - Mailing list pgsql-general

From David G. Johnston
Subject Re: [GENERAL] What is the proper query
Date
Msg-id CAKFQuwZEMtgO6MEr3t8F39SCzB6vR9jF19Jpdpb=5Vp+3-6WEw@mail.gmail.com
Whole thread Raw
In response to [GENERAL] What is the proper query  (Igor Korot <ikorot01@gmail.com>)
List pgsql-general
On Mon, Aug 21, 2017 at 9:08 PM, Igor Korot <ikorot01@gmail.com> wrote:
Hi, ALL,
draft=# SELECT * FROM information_schema.key_column_usage 
​[...]​
 
There are 3 foreign keys in that table.

Is there a way to get values of 0, 1, 1, and 2 for the ordinal position?

Not using the key_column_usage view.  What that view is doing is basically saying (my understanding from reading the docs, not testing it out):

CREATE TABLE tbl_pk
UNIQUE (col1, col2)​

CREATE TABLE tbl_fk
FOREIGN (col2, col1) REFERENCES tbl_pk (col1, col2)

Now your ordinal/position rows would be:

(1, 2)
(2, 1)

instead of:

(1, 1)
(2, 2)

if you had defined the FK and PK with the same column names in the same order, like is done almost always and like you did in your example.

If you want to enumerate constraints you need to use a different information_schema view or, as Melvin showed, use pg_catalog.  I'm not fluent enough to provide examples.  If you provide the question/problem you are trying to resolve others will likely offer suggestions.

David J.

pgsql-general by date:

Previous
From: Melvin Davidson
Date:
Subject: Re: [GENERAL] What is the proper query
Next
From: Igor Korot
Date:
Subject: Re: [GENERAL] What is the proper query