语言和开发规则: Difference between revisions

Jump to navigation Jump to search
Tag: 2017 source edit
Line 270: Line 270:
==SQL编码规则==
==SQL编码规则==


*Alias usage/Fields naming
*别名/字段命名的使用
 
在进行 select 时,可以使用别名来简化查询的编写和阅读:


When doing select, we can use alias to simplify writing/reading of requests:
<syntaxhighlight lang="sql">
<syntaxhighlight lang="sql">
select chp1, chpxxx2 as chp2 from table2 as t1, table2 as t2 where t1.chpx = t2.chpy
select chp1, chpxxx2 as chp2 from table2 as t1, table2 as t2 where t1.chpx = t2.chpy
</syntaxhighlight>
</syntaxhighlight>
However, we must not used alias for update request as they are not compatible with Mysql 3.1.


*Using SELECT * is forbidden ! When using SELECT you must define complete list of fields to get. This avoids confusion. Example:
但是,不应在 update 请求中使用别名,因为它们与 MySQL 3.1 不兼容。
 
*禁止使用 SELECT * !每个 SELECT 都必须指定要检索的字段的完整列表。这有助于避免混淆。最重要的是,这使代码的重新生成更加容易,并使变更对字段的影响分析成为可能。示例:


<syntaxhighlight lang="sql">
<syntaxhighlight lang="sql">
Line 284: Line 286:
</syntaxhighlight>
</syntaxhighlight>


*Into SQL requests, you must quote fields except the fields that contain amounts which must be stored as double or real type. Quotes on numbers may result in saving as a different value. For example 412.62 in an insert will be saved as value 412.61999512 into database (due to implicit conversion string to numeric) if the target field has type double(24,8). Only PHP see value 412.61999512. Other tools will see 412.62 giving a sense that there is no problem. But it's PHP that has the good vision. There is really a wrong value into database. By removing quotes on numbers, no problem occurs.
*在 SQL insert 时,必需给字段加引号,但不包括要存储在 double 或 real 类型字段中的金额数字。浮点数字上的引号有时会导致其存储了不同的值。例如,如果字段类型为 double(24,8),则插入412.62时实际上是将值412.61999512(由于隐式的字符串 - 数字转换)存储到数据库中。只有PHP可以看到412.61999512。其他工具将看到412.62,这使其看上去没有问题。但PHP是正确的,数据库中确实存在错误的值。通过删除数字上的引号,就不会出现此问题。
 
示例:


Example:
<syntaxhighlight lang="sql">
<syntaxhighlight lang="sql">
Good:    INSERT INTO table_1 (field_txt, field_num) VALUES ('txt', 412.62)
Good:    INSERT INTO table_1 (field_txt, field_num) VALUES ('txt', 412.62)
Line 292: Line 295:
</syntaxhighlight>
</syntaxhighlight>


Note, problem of float numbers is same problem on all langauges and not only when inserting data into database. It occurs also with any language when you work on "real" numbers, so numbers must be, as soon as they are affected, cleaned with function price2num with second parameter defined to :
请注意,浮点数的问题是普遍性的,不仅是在数据库访问上,而且在处理实数时,所有语言都存在浮点数问题。因此,一旦受到影响,就必须使用 price2num 函数清除浮点问题。根据需要,第二个参数设置为:“MU”(如果是单价)、“MT”(如果是总价)或“MS”(其他情况),具体取决于数字的使用情况。另请参阅下面的“实数、金额和计算”一章。
'MU' (for unit prices), 'MT' (for total prices) or 'MS' (otherwise) depending on usage of number. (see function documentation)


*Functions NOW, SYSDATE or DATEDIFF are forbidden inside SQL requests. If you must use the current date into a field, value must come from the PHP and not from the database engine. This is for better portability of code and correct management of TimeZone.
*SQL命令中不允许使用SQL日期函数NOW、SYSDATE、DATEDIFF。如果可能的话,还必须避免使用MONTH、YEAR。如果必须在字段中使用当前日期,则该值必须来自PHP,例如通过 dol_now() 函数,而不是来自数据库引擎。原因如下:
**better portability of code
**better management of TimeZone (reference timezone is the PHP timezone and timezone of database may differs so all dates functions must be on PHP side).
**better performance (see example and final comment)  For


For example, don't do:
For example, don't do:
714

edits

Navigation menu