Estilo SQL

SQL Intermediário

Jasmin Ludolf

Data Science Content Developer, DataCamp

Formatação do SQL

  • A formatação não é obrigatória
  • Mas a falta de formatação pode causar problemas
select title, release_year, country from films limit 3
|title                                           |release_year|country|
|------------------------------------------------|------------|-------|
|Intolerance: Love's Struggle Throughout the Ages|1916        |USA    |
|Over the Hill to the Poorhouse                  |1920        |USA    |
|The Big Parade                                  |1925        |USA    |
SQL Intermediário

Práticas recomendadas

SELECT title, release_year, country
FROM films 
LIMIT 3;
|title                                           |release_year|country|
|------------------------------------------------|------------|-------|
|Intolerance: Love's Struggle Throughout the Ages|1916        |USA    |
|Over the Hill to the Poorhouse                  |1920        |USA    |
|The Big Parade                                  |1925        |USA    |
  • Coloque as palavras-chave em maiúsculas
  • Adicionar novas linhas
SQL Intermediário

Guias de estilo

SELECT  
    title, 
    release_year, 
    country
FROM films 
LIMIT 3;
|title                                           |release_year|country|
|------------------------------------------------|------------|-------|
|Intolerance: Love's Struggle Throughout the Ages|1916        |USA    |
|Over the Hill to the Poorhouse                  |1920        |USA    |
|The Big Parade                                  |1925        |USA    |
SQL Intermediário

Guias de estilo

 

Guia de estilo Holywell: https://www.sqlstyle.guide/

Sequência de rabiscos que se transformam em uma linha reta

Escreva código claro e legível

SQL Intermediário

Ponto e vírgula

SELECT title, release_year, country
FROM films 
LIMIT 3;

 

  • Prática recomendada
  • Mais fácil de traduzir entre as variantes de SQL
  • Indica o fim de uma consulta
SQL Intermediário

Como lidar com nomes de campo não padronizados

  • release year em vez de release_year
  • Coloque nomes de campos não padronizados entre aspas duplas
SELECT title, "release year", country
FROM films 
LIMIT 3;
SQL Intermediário

Por que formatamos?

  • Facilita a colaboração
  • Código claro e legível
  • Aparência profissional
  • Mais fácil de entender
  • Mais fácil de depurar
SQL Intermediário

Vamos praticar!

SQL Intermediário

Preparing Video For Download...