💥 Consultar tipo de dado em uma tabela:
SELECT
COLUMN_NAME,
DATA_TYPE,
CHARACTER_MAXIMUM_LENGTH
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = 'nomedatabela';
Saída:
💥 Consultar chave primária de uma tabela:
Opção 1
Opção 2
-- Chave Primária
SELECT
COLUMN_NAME,
DATA_TYPE,
CHARACTER_MAXIMUM_LENGTH
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = 'pfunc'
AND COLUMN_NAME IN (
SELECT
COLUMN_NAME
FROM
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
TABLE_NAME = 'pfunc'
AND CONSTRAINT_NAME LIKE 'PK_%'
);
Saída:
💥 Consultar chave estrangeira de uma tabela:
-- Chaves Estrangeiras
SELECT
CONSTRAINT_NAME,
COLUMN_NAME,
TABLE_NAME
FROM
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
TABLE_NAME = 'pfunc'
AND CONSTRAINT_NAME LIKE 'FK_%';