Skip to content

Instantly share code, notes, and snippets.

@thiago122
Last active November 15, 2019 14:38
Show Gist options
  • Save thiago122/36302da3ac740918aaab to your computer and use it in GitHub Desktop.
Save thiago122/36302da3ac740918aaab to your computer and use it in GitHub Desktop.
SELECT
TABLE_SCHEMA,
TABLE_NAME,
COLUMN_NAME,
REFERENCED_TABLE_NAME,
REFERENCED_COLUMN_NAME
FROM information_schema.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = 'DATABASE_NAME'
AND TABLE_NAME = 'TABLE'
AND REFERENCED_TABLE_NAME != 'NULL'
// Obtem os relacionamentos externos da tabela
SELECT
information_schema.KEY_COLUMN_USAGE.REFERENCED_TABLE_NAME,
information_schema.KEY_COLUMN_USAGE.REFERENCED_COLUMN_NAME,
information_schema.KEY_COLUMN_USAGE.TABLE_NAME,
information_schema.KEY_COLUMN_USAGE.COLUMN_NAME,
information_schema.KEY_COLUMN_USAGE.TABLE_SCHEMA,
information_schema.KEY_COLUMN_USAGE.CONSTRAINT_NAME,
information_schema.KEY_COLUMN_USAGE.POSITION_IN_UNIQUE_CONSTRAINT
FROM
information_schema.KEY_COLUMN_USAGE
WHERE
information_schema.KEY_COLUMN_USAGE.CONSTRAINT_SCHEMA = 'telecine_recomenda'
AND information_schema.KEY_COLUMN_USAGE.CONSTRAINT_NAME != 'PRIMARY'
AND information_schema.KEY_COLUMN_USAGE.REFERENCED_TABLE_NAME = 'nivel'
// obtem os tipos de chave
SELECT
information_schema.TABLE_CONSTRAINTS.CONSTRAINT_SCHEMA,
information_schema.TABLE_CONSTRAINTS.CONSTRAINT_NAME,
information_schema.TABLE_CONSTRAINTS.TABLE_SCHEMA,
information_schema.TABLE_CONSTRAINTS.TABLE_NAME,
information_schema.TABLE_CONSTRAINTS.CONSTRAINT_TYPE
FROM
information_schema.TABLE_CONSTRAINTS
WHERE information_schema.TABLE_CONSTRAINTS.TABLE_SCHEMA = 'telecine_recomenda'
AND information_schema.TABLE_CONSTRAINTS.TABLE_NAME = 'recurso_nivel'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment