订购时可获得多场外贸

时间:2018-04-24 07:55:35

标签: sql postgresql

我正在寻找依赖于外键链接的表的依赖,但是在多字段的情况下,我需要它们之间的顺序。

例如,PgAdmin当你选择一个表时,他会给你约束等等...... 喜欢:

enter image description here

我已经有了查询,但是当多字段外键

时它出错了
SELECT
  tc.constraint_name as nom_fk,
  tc.table_name as table_out,
  kcu.column_name as colonne_out, 
  ccu.table_name AS table_in,
  ccu.column_name AS colonne_in 
FROM information_schema.table_constraints AS tc 
JOIN information_schema.key_column_usage AS kcu
  ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage AS ccu
  ON ccu.constraint_name = tc.constraint_name
WHERE constraint_type = 'FOREIGN KEY'
  AND ccu.table_name='table';

1 个答案:

答案 0 :(得分:0)

有关信息,我们可以通过以下方式执行此操作:

                            SELECT
                                kcu_foreign.COLUMN_NAME AS key_out,
                                kcu_primary.COLUMN_NAME AS key_in
                            FROM
                                INFORMATION_SCHEMA.TABLE_CONSTRAINTS fks
                                INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu_foreign 
                                  ON fks.TABLE_CATALOG = kcu_foreign.TABLE_CATALOG
                                  AND fks.TABLE_SCHEMA = kcu_foreign.TABLE_SCHEMA
                                  AND fks.TABLE_NAME = kcu_foreign.TABLE_NAME
                                  AND fks.CONSTRAINT_NAME = kcu_foreign.CONSTRAINT_NAME
                                INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc 
                                  ON rc.CONSTRAINT_CATALOG = fks.CONSTRAINT_CATALOG
                                  AND rc.CONSTRAINT_SCHEMA = fks.CONSTRAINT_SCHEMA
                                  AND rc.CONSTRAINT_NAME = fks.CONSTRAINT_NAME
                                INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS pks 
                                  ON rc.UNIQUE_CONSTRAINT_CATALOG = pks.CONSTRAINT_CATALOG
                                  AND rc.UNIQUE_CONSTRAINT_SCHEMA = pks.CONSTRAINT_SCHEMA
                                  AND rc.UNIQUE_CONSTRAINT_NAME = pks.CONSTRAINT_NAME
                                INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu_primary
                                  ON pks.TABLE_CATALOG = kcu_primary.TABLE_CATALOG
                                  AND pks.TABLE_SCHEMA = kcu_primary.TABLE_SCHEMA
                                  AND pks.TABLE_NAME = kcu_primary.TABLE_NAME
                                  AND pks.CONSTRAINT_NAME = kcu_primary.CONSTRAINT_NAME
                                  AND kcu_foreign.ORDINAL_POSITION = kcu_primary.ORDINAL_POSITION 
                            WHERE fks.TABLE_SCHEMA = 'your_shema'
                                AND fks.TABLE_NAME = 'foreign_table'
                                AND pks.TABLE_NAME = 'primary_table'
                                AND fks.CONSTRAINT_TYPE = 'FOREIGN KEY'
                                AND pks.CONSTRAINT_TYPE = 'PRIMARY KEY'
                                ORDER BY fks.constraint_name, kcu_foreign.ORDINAL_POSITION;

但如果你想要订单,你需要使用pg_catalog.pg_constraint:

https://www.postgresql.org/docs/9.1/static/catalog-pg-constraint.html

参见专栏" conkey"和" confkey"