当前位置:首页 >> 编程语言 >> 【SQL语句】SQL编写规范,vx535hd(sql语句编写格式规范)

【SQL语句】SQL编写规范,vx535hd(sql语句编写格式规范)

cpugpu芯片开发光刻机 编程语言 1
文件名:【SQL语句】SQL编写规范,vx535hd 【SQL语句】SQL编写规范 简介

本文编写原因主要来于XC迁移过程中修改SQL语句时,发现大部分修改均源自于项目SQL编写不规范,以此文档做以总结。

注:此文档覆盖不甚全面,大体只围绕迁移遇到的修改而展开。 正文

1、【字段引号】 列名、表名如无特殊情况,不要添加任何引号(` ’ 等)。若返回列名有可能是关键字,例如month、year等,必须使用双引号包裹。

# 错误SELECT book_name as bookMame, year as year;# 正确SELECT book_name as bookMame, year as "year";

2、【除0兼容】 存在除法计算的时候(/),要确保除数为0的兼容。MYSQL一定程度上兼容这种计算,但是对于其他数据库则不一定,最好的办法是使用函数nullif对被除数进行零值判断。

# 错误SELECT a/b# 正确select a/nullif(b, 0)函数解释:NULLIF(表达式1,表达式2)NULLIF函数比较表达式1和表达式2。如果两个表达式相等就返回空值(NULL)。如果不等就返回表达式1。

3、【严格分组】 在对表进行聚合查询的时候,只能在 SELECT 子句中写下面 3 种内容:通过 GROUP BY 子句指定的聚合键、聚合函数(SUM 、AVG 等)、常量。但通常可以通过设置数据库的sql_mode,移除only_full_group_by解决此问题,但是若无特殊情况,还是应保障遵守严格模式标准。若能保证某个字段在业务中每个分组中只会有一个值,并且也不想用作group by字段语句,可通过max(特殊字段)方式达到类似的效果。

# 不规范写法SELECT a, bfrom tbgroup by a;# 兼容写法SELECT a, max(b)from tbgroup by a;# 规范写法,既然b没有参与分组,就不该出现在select子句中SELECT afrom tbgroup by a;

4、【类型区别】 日期不要和字符串类型进行比较

update safety_manage_organ set spread_certificate_status = if(spread_end_time is null || spread_end_time >= now(), '有效', '无效'), chief_engineer_certificate_status = if(chief_engineer_end_time is null || chief_engineer_end_time >= now(), '有效', '无效'), deputy_safe_certificate_status = if(deputy_safe_end_time is null || deputy_safe_end_time >= now(), '有效', '无效'), electric_certificate_status = if(electric_end_time is null || electric_end_time >= now(), '有效', '无效'), produce_certificate_status = if(produce_end_time is null || produce_end_time >= now(), '有效', '无效')WHERE enterprise_id = 260;update safety_manage_organ set spread_certificate_status = if(spread_end_time is null || spread_end_time >= date_format(now(), '%Y-%m-%d'), '有效', '无效')WHERE enterprise_id = 260;

5、【union规范】 union 连接的所有数据行要保证列顺序一致且字段类型相同。

# 错误写法select 0 entId, 0 province, 0 city, 0 district from dualunion select '10164564' entId, '520000' province, '522000' city, '522022' district from dual# 正确写法select '0' entId, '0' province, '0' city, '0' district from dualunion select '10164564' entId, '520000' province, '522000' city, '522022' district from dual

6、【多表关联】 多表关联时,最好为每张表指定别名,第一复杂查询时方便定位,第二某些数据库会报字段不明确的错误情况。

# 错误写法: 字段关联 "industry_type" 是不明确的SELECT A.industry_type AS industry_type,SUM ( A.predict_count ) AS predict_count,( SUM ( A.predict_count ) - SUM ( b.real_count ) ) AS predict_change_value,(ROUND(( ( SUM ( A.predict_count ) - SUM ( b.real_count ) ) / SUM ( b.real_count ) ) * 100,2 ) ) AS predict_change_rate,SUM ( error_count ) AS error_count FROM( SELECT SUM ( predict_count ) AS predict_count, error_count, industry_type FROM electricity_produce_predict_info WHERE YEAR = '2022' GROUP BY industry_type )AS A JOIN ( SELECT real_count, industry_type FROM electricity_produce_predict_info WHERE YEAR = '2022' - 1 GROUP BY industry_type ) AS b ON A.industry_type = b.industry_type GROUP BY industry_type;# 正确写法,group by 跟上A.industry_typeSELECT A.industry_type AS industry_type,SUM ( A.predict_count ) AS predict_count,( SUM ( A.predict_count ) - SUM ( b.real_count ) ) AS predict_change_value,(ROUND(( ( SUM ( A.predict_count ) - SUM ( b.real_count ) ) / SUM ( b.real_count ) ) * 100,2 ) ) AS predict_change_rate,SUM ( error_count ) AS error_count FROM( SELECT SUM ( predict_count ) AS predict_count, error_count, industry_type FROM electricity_produce_predict_info WHERE YEAR = '2022' GROUP BY industry_type )AS A JOIN ( SELECT real_count, industry_type FROM electricity_produce_predict_info WHERE YEAR = '2022' - 1 GROUP BY industry_type ) AS b ON A.industry_type = b.industry_type GROUP BY A.industry_type; 结语

本文暂时列举此几大类,其主要应用于PG数据库,但也可代表所有,因为PG几乎是所有数据库中要求比较严格的一种。

协助本站SEO优化一下,谢谢!
关键词不能为空
同类推荐
«    2025年12月    »
1234567
891011121314
15161718192021
22232425262728
293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接