| 1 | SELECT table_name |
| 2 | FROM information_schema.tables |
| 3 | WHERE table_schema = 'public' |
| 4 | AND table_type = 'BASE TABLE' |
| 5 | ORDER BY table_name; |
ℹ
information_schema é o catálogo interno do PostgreSQL — ele mostra toda a estrutura do banco.
| 1 | SELECT |
| 2 | ordinal_position AS ordem, |
| 3 | column_name AS coluna, |
| 4 | data_type AS tipo, |
| 5 | character_maximum_length AS tamanho, |
| 6 | is_nullable AS aceita_nulo, |
| 7 | column_default AS valor_padrao |
| 8 | FROM information_schema.columns |
| 9 | WHERE table_name = 'servidor' |
| 10 | ORDER BY ordinal_position; |
ℹ
USER-DEFINED geralmente significa ENUM (tipo personalizado).
| 1 | -- ver dados |
| 2 | SELECT * FROM servidor LIMIT 5; |
| 3 | |
| 4 | -- contagem total |
| 5 | SELECT COUNT(*) FROM servidor; |
| 6 | |
| 7 | -- valores únicos |
| 8 | SELECT DISTINCT situacao FROM servidor; |
⚠
Sempre use LIMIT no início. Em produção, tabelas podem ter milhões de registros.
| 1 | SELECT |
| 2 | tc.table_name, |
| 3 | kcu.column_name, |
| 4 | ccu.table_name AS ref_table, |
| 5 | ccu.column_name AS ref_column |
| 6 | FROM information_schema.table_constraints tc |
| 7 | JOIN information_schema.key_column_usage kcu |
| 8 | ON tc.constraint_name = kcu.constraint_name |
| 9 | JOIN information_schema.constraint_column_usage ccu |
| 10 | ON ccu.constraint_name = tc.constraint_name |
| 11 | WHERE tc.constraint_type = 'FOREIGN KEY'; |
ℹ
Isso mostra exatamente como fazer JOIN entre tabelas.
| 1 | SELECT |
| 2 | COUNT(*) AS total, |
| 3 | COUNT(salario_atual) AS preenchidos, |
| 4 | COUNT(*) - COUNT(salario_atual) AS nulos, |
| 5 | MIN(salario_atual), |
| 6 | MAX(salario_atual), |
| 7 | ROUND(AVG(salario_atual), 2) |
| 8 | FROM servidor; |
| 1 | SELECT situacao, COUNT(*) |
| 2 | FROM servidor |
| 3 | GROUP BY situacao |
| 4 | ORDER BY COUNT(*) DESC; |
| 1 | -- enums do banco |
| 2 | SELECT t.typname, e.enumlabel |
| 3 | FROM pg_type t |
| 4 | JOIN pg_enum e ON e.enumtypid = t.oid; |
| 1 | -- listar views |
| 2 | SELECT table_name |
| 3 | FROM information_schema.views; |
| 1 | -- código da view |
| 2 | SELECT view_definition |
| 3 | FROM information_schema.views |
| 4 | WHERE table_name = 'vw_servidores'; |
ℹ
Views já possuem JOINs prontos — use-as como ponto de partida.
01 → Descobrir tabelas ·
02 → Entender colunas ·
03 → Ver dados ·
04 → Entender relações
Depois disso você já consegue fazer análises reais.