SQL 风格

SQL 中级

Jasmin Ludolf

Data Science Content Developer, DataCamp

SQL 格式化

  • 格式化非必需
  • 但缺少格式会带来问题
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 中级

最佳实践

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 中级

风格指南

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 中级

风格指南

 

Holywell 的风格指南: https://www.sqlstyle.guide/

一串涂鸦变成直线的序列

编写清晰可读的代码

SQL 中级

分号

SELECT title, release_year, country
FROM films 
LIMIT 3;

 

  • 最佳实践
  • 更易在不同 SQL 方言间迁移
  • 表示查询结束
SQL 中级

处理非标准字段名

  • release year 代替 release_year
  • 将非标准字段名用双引号括起
SELECT title, "release year", country
FROM films 
LIMIT 3;
SQL 中级

为何要格式化?

  • 便于协作
  • 清晰可读
  • 专业美观
  • 更易理解
  • 更易调试
SQL 中级

Vamos praticar!

SQL 中级

Preparing Video For Download...