Created
August 5, 2022 12:46
-
-
Save icarofreire/19e48d4cc3c45790a714089080bbc772 to your computer and use it in GitHub Desktop.
queryes para listar as chaves estrangeiras e suas colunas, de todas as tabelas do banco.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- lista as chaves estrangeiras de todas as tabelas; | |
| SELECT TABLE_NAME | |
| , CONSTRAINT_NAME | |
| , COLUMN_NAME | |
| , ORDINAL_POSITION | |
| FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE | |
| WHERE CONSTRAINT_NAME IN ( | |
| SELECT CONSTRAINT_NAME | |
| FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS) | |
| ORDER BY TABLE_NAME, ORDINAL_POSITION; | |
| -- lista as chaves estrangeiras e suas colunas, de todas as tabelas do banco; | |
| SELECT | |
| KCU.CONSTRAINT_NAME AS FK_CONSTRAINT_NAME | |
| , KCU.TABLE_NAME AS FK_TABLE_NAME | |
| , KCU.COLUMN_NAME AS FK_COLUMN_NAME | |
| , KCU.ORDINAL_POSITION AS FK_ORDINAL_POSITION | |
| , RC.UNIQUE_CONSTRAINT_NAME | |
| FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC | |
| JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU | |
| ON KCU.CONSTRAINT_CATALOG = RC.CONSTRAINT_CATALOG | |
| AND KCU.CONSTRAINT_SCHEMA = RC.CONSTRAINT_SCHEMA | |
| AND KCU.CONSTRAINT_NAME = RC.CONSTRAINT_NAME | |
| -- \/ buscar tabelas que possuem o mesmo relacionamento com uma tabela específica, | |
| -- por seu nome único gerado com chave estrangeira; | |
| WHERE RC.UNIQUE_CONSTRAINT_NAME = 'atendimento_pkey' | |
| ORDER BY KCU.CONSTRAINT_NAME, KCU.ORDINAL_POSITION |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment