Interpretation of "Alibaba Java Development Manual-v1.1" (1) sql specification

1. [Mandatory] Do not use count (column name) or count (constant) instead of count(*). count(*) is the standard syntax for counting rows defined by SQL92. It has nothing to do with the database, and has nothing to do with NULL and non-NULL.  

     Description: count(*) will count rows with NULL values, while count(column name) will not count rows with NULL values ​​in this column.  

 

2. [Mandatory] count(distinct col) calculates the number of unique rows except NULL in this column, pay attention to count(distinct col)  

     col1, col2) Returns 0 if one of the columns is all NULL, even if the other column has a different value.  

 

3. [Mandatory] When the value of a column is all NULL, the return result of count(col) is 0, but the return result of sum(col) is NULL, so you need to pay attention to the NPE problem when using sum().  

     Positive example: The NPE problem of sum can be avoided by using the following method: SELECT IF(ISNULL(SUM(g)),0,SUM(g)) FROM table;  

 

 4. [Mandatory] Use ISNULL() to determine whether the value is NULL. Note: A direct comparison of NULL with any value is NULL.  

     illustrate:  

      1) The return result of NULL<>NULL is NULL, not false.  

      2) The return result of NULL=NULL is NULL, not true.  

      3) The return result of NULL<>1 is NULL, not true.  

 

 5. [Mandatory] When writing paging query logic in the code, if count is 0, it should be returned directly to avoid executing subsequent paging statements.  

 

 6. [Mandatory] Foreign keys and cascades are not allowed. All foreign key concepts must be resolved at the application layer.  

     Note: (concept explanation) The student_id in the student table is the primary key, then the student_id in the grade table is the foreign key. 

     If you update the student_id in the student table and trigger the update of the student_id in the grade table at the same time, it is a cascading update. 

     Foreign keys and cascading updates are suitable for single-machine low concurrency, but not suitable for distributed and high-concurrency clusters; cascading updates are strong blocking, and there are many 

     Risk of database update storms; foreign keys affect database insert speed.  

 

 7. [Mandatory] The use of stored procedures is prohibited. Stored procedures are difficult to debug and extend, and have no portability.  

 

 8. [Mandatory] When revising data, when deleting or modifying records, you must select first to avoid accidental deletion, and then execute the update statement after confirmation.  

 

9. [Recommendation] Avoid the in operation if it can be avoided. If it cannot be avoided, you need to carefully evaluate the number of set elements behind in, and control it within 1000.  

 

10. [Reference] If there is a need for globalization, all character storage and representation are encoded in utf-8, then the character counting method

     Notice:  

     illustrate:

         SELECT LENGTH("Easy work"); returns 12  

         SELECT CHARACTER_LENGTH("Easy work"); returns 4  

         If you want to use expressions, then use utfmb4 for storage, pay attention to the difference between it and utf-8 encoding. 

 

 11. [Reference] TRUNCATE TABLE is faster than DELETE and uses less system and transaction log resources, but TRUNCATE 

     There is no transaction and no trigger is triggered, which may cause accidents, so it is not recommended to use this statement in the development code.  

     Description: TRUNCATE TABLE is functionally identical to the DELETE statement without the WHERE clause.

 

12. [Mandatory] In table query, never use * as the field list of the query, which fields must be clearly stated. 

     Description: 1) Increase the parsing cost of the query analyzer. 2) Adding or subtracting fields is easy to be inconsistent with the resultMap configuration.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326396819&siteId=291194637