postgres 8.3 데이터베이스를 검사하여 외래 키의 세부 정보를 검색하려고합니다.postgresql 8.3으로 외래 키를 찾습니다.
CREATE TABLE "a" (
"id" SERIAL PRIMARY KEY
);
CREATE TABLE "b" (
"one" integer,
"two" integer,
"a_id" integer REFERENCES "a",
PRIMARY KEY ("one", "two")
);
CREATE TABLE "c" (
"id" SERIAL PRIMARY KEY,
"a_id" integer REFERENCES "a",
"b_one" integer,
"b_two" integer,
FOREIGN KEY ("b_one", "b_two") REFERENCES "b"
);
가 그럼 난 다음 producued 쿼리를 실행하고 싶습니다 : 나는 다음과 같은 스키마를 가지고 상상
table | columns | foreign table | foreign columns
--------------------------------------------------------
b | {a_id} | a | {id}
c | {a_id} | a | {id}
c | {b_one, b_two} | b | {one, two}
나의 첫번째 노력은 나에게 쿼리
SELECT conrelid::regclass as "table",
conkey as columns,
confrelid::regclass as "foreign table",
confkey as "foreign columns"
FROM pg_constraint
WHERE contype = 'f' ;
table | columns | foreign table | foreign columns
-------+---------+---------------+-----------------
b | {3} | a | {1}
c | {2} | a | {1}
c | {3,4} | b | {1,2}
을 준 거의 다 왔어. 그러나 열 번호를 열 이름으로 변환하려는 노력으로 아직 원하는 결과를 얻지 못했습니다. 인터넷 검색은 저에게 다시 을주었습니다.
SELECT conrelid::regclass as "table",
a.attname as columns,
confrelid::regclass as "foreign table",
af.attname as "foreign columns"
FROM pg_attribute AS af,
pg_attribute AS a,
(SELECT conrelid,
confrelid,
conkey[i] AS conkey,
confkey[i] as confkey
FROM (SELECT conrelid,
confrelid,
conkey,
confkey,
generate_series(1, array_upper(conkey, 1)) AS i
FROM pg_constraint
WHERE contype = 'f'
) AS ss
) AS ss2
WHERE af.attnum = confkey
AND af.attrelid = confrelid
AND a.attnum = conkey
AND a.attrelid = conrelid ;
table | columns | foreign table | foreign columns
-------+---------+---------------+-----------------
b | a_id | a | id
c | a_id | a | id
c | b_one | b | one
c | b_two | b | two
마지막 단계를 도와 줄 수있는 사람이 있습니까?