Firebird Metadata - Consultando FKs e os campos que estão apontando
17/08/2018
0
Olá;
Fiz várias pesquisas pela internet procurando por uma solução para a minha dúvida, mas, como nenhuma fonte me passou informações satisfatórias, resolvi perguntar eu mesmo, por talvez se tratar de um caso mais específico;
Eu estou precisando desenvolver um script (seja ele um SELECT puro ou uma STORED PROCEDURE) que liste para mim o nome das Foreign Keys, a tabela de origem, os campos, as regras de atualização e exclusão e a tabela e campos a que são referenciados;
Não encontrei nem mesmo material para me ajudar a desenvolver essa solução. Poderiam me ajudar?
Grato;
Fiz várias pesquisas pela internet procurando por uma solução para a minha dúvida, mas, como nenhuma fonte me passou informações satisfatórias, resolvi perguntar eu mesmo, por talvez se tratar de um caso mais específico;
Eu estou precisando desenvolver um script (seja ele um SELECT puro ou uma STORED PROCEDURE) que liste para mim o nome das Foreign Keys, a tabela de origem, os campos, as regras de atualização e exclusão e a tabela e campos a que são referenciados;
Não encontrei nem mesmo material para me ajudar a desenvolver essa solução. Poderiam me ajudar?
Grato;
Rodrigo Gama
Curtir tópico
+ 0
Responder
Post mais votado
17/08/2018
Veja se isto te ajuda (Testei no FB 2.5)
select A.RDB$RELATION_NAME AS TABELA,
A.RDB$CONSTRAINT_NAME AS FK_NAME,
A.RDB$CONSTRAINT_TYPE AS TIPO,
B.RDB$CONST_NAME_UQ AS UQ_NAME,
B.RDB$UPDATE_RULE AS UPDATE_RULE,
B.RDB$DELETE_RULE AS DELETE_RULE,
C.RDB$RELATION_NAME as FK_Table,
A.RDB$INDEX_NAME AS IDX,
list(distinct trim(D.RDB$FIELD_NAME)) as FK_Field,
list(distinct trim(E.RDB$FIELD_NAME)) as OnField
from RDB$REF_CONSTRAINTS B, RDB$RELATION_CONSTRAINTS A, RDB$RELATION_CONSTRAINTS C,
RDB$INDEX_SEGMENTS D, RDB$INDEX_SEGMENTS E, RDB$INDICES I
where (A.RDB$CONSTRAINT_TYPE = 'FOREIGN KEY') and
(A.RDB$CONSTRAINT_NAME = B.RDB$CONSTRAINT_NAME) and
(B.RDB$CONST_NAME_UQ=C.RDB$CONSTRAINT_NAME) and (C.RDB$INDEX_NAME=D.RDB$INDEX_NAME) and
(A.RDB$INDEX_NAME=E.RDB$INDEX_NAME) and
(A.RDB$INDEX_NAME=I.RDB$INDEX_NAME)
--and (A.RDB$RELATION_NAME = 'VOTOS') filtro por nome da tabela
group by 1,2,3,4,5,6,7,8
order by A.RDB$RELATION_NAME, A.RDB$CONSTRAINT_NAME
select A.RDB$RELATION_NAME AS TABELA,
A.RDB$CONSTRAINT_NAME AS FK_NAME,
A.RDB$CONSTRAINT_TYPE AS TIPO,
B.RDB$CONST_NAME_UQ AS UQ_NAME,
B.RDB$UPDATE_RULE AS UPDATE_RULE,
B.RDB$DELETE_RULE AS DELETE_RULE,
C.RDB$RELATION_NAME as FK_Table,
A.RDB$INDEX_NAME AS IDX,
list(distinct trim(D.RDB$FIELD_NAME)) as FK_Field,
list(distinct trim(E.RDB$FIELD_NAME)) as OnField
from RDB$REF_CONSTRAINTS B, RDB$RELATION_CONSTRAINTS A, RDB$RELATION_CONSTRAINTS C,
RDB$INDEX_SEGMENTS D, RDB$INDEX_SEGMENTS E, RDB$INDICES I
where (A.RDB$CONSTRAINT_TYPE = 'FOREIGN KEY') and
(A.RDB$CONSTRAINT_NAME = B.RDB$CONSTRAINT_NAME) and
(B.RDB$CONST_NAME_UQ=C.RDB$CONSTRAINT_NAME) and (C.RDB$INDEX_NAME=D.RDB$INDEX_NAME) and
(A.RDB$INDEX_NAME=E.RDB$INDEX_NAME) and
(A.RDB$INDEX_NAME=I.RDB$INDEX_NAME)
--and (A.RDB$RELATION_NAME = 'VOTOS') filtro por nome da tabela
group by 1,2,3,4,5,6,7,8
order by A.RDB$RELATION_NAME, A.RDB$CONSTRAINT_NAME
Victor Mendes
Responder
Mais Posts
17/08/2018
Rodrigo Gama
Ivictorinoi, muito obrigado! Vou testar aqui e já te passo um feedback.
Responder
17/08/2018
Rodrigo Gama
Ivictorinoi, fiquei até emocionado com o SELECT, de tão preciso que ficou! Pra mim, esse script é arte! Muito obrigado!!!
Responder
Clique aqui para fazer login e interagir na Comunidade :)