Java Coding Specification 12 (MySQL-Table Building Protocol)

MySQL-table building protocol


Other related articles
Java Coding Specification 1 (Programming Specification - Naming Style)
Java Coding Specification 2 (Programming Specification - Constant Definition)
Java Coding Specification 3 (Programming Specification - Code Format)
Java Coding Specification 4 (Programming Specification - OOP Specification)
Java Coding Specification 5 (Programming Specification - Collection Processing)
Java Coding Specification 6 (Programming Specification - Concurrent Processing)
Java Coding Specification 7 (Programming Specification - Control Statement)
Java Coding Specification 8 (Programming Specification - Annotation Specification and Others)
Java Coding Specification 9 (Exception Log )
Java Coding Specification 10 (Unit Test)
Java Coding Specification 11 (Security Specification)
Java Coding Specification 12 (MySQL-Table Building Specification)
Java Coding Specification 13 (MySQL-Index Specification)
Java Coding Specification 14 (MySQL-SQL Statement and ORM Mapping )
Java Coding Specification 15 (Project Structure)


  1. [Mandatory] Fields that express the concept of yes or no, must be is_xxxnamed in a way that the data type is unsigned tinyint(1 for yes, 0 for no).

    • Description: If any field is non-negative, it must be unsigned.
    • Positive example: Express the field name of the logical deletion is_deleted, 1 means deleted, 0 means not deleted.
  2. [Mandatory] The table name and field name must use lowercase letters or numbers. It is forbidden to start with a number, and it is forbidden to have only numbers between two underscores. The modification of database field names is expensive, because pre-release cannot be performed, so field names need to be carefully considered.

    • Note: MySQL is case-insensitive under Windows, but is case-sensitive by default under Linux. Therefore, the default under the database is case-sensitive. Therefore, database names and table fields are not allowed to appear any capital letters to avoid extraneous branches.
    • Normal example:aliyun_admin,rdc_config,level3_name
    • Counterexample:AliyunAdmin,rdcConfig,level_3_name
  3. [Mandatory] Table names do not use plural nouns.

    • Note: The table name should only represent the entity content in the table, and should not represent the number of entities. The corresponding DO class name is also singular, which is in line with the expression habit.
  4. [Mandatory] Disable reserved words, such desc、range、match、delayedas, please refer to MySQL official reserved words.

  5. [Mandatory] The name of the primary key index; the name pk_字段名of the unique index uk_字段名; the name of the common index idx_字段名.

  6. [Mandatory] The decimal type is decimal, and the use of float and double is prohibited.

    • Note: When float and double are stored, there is a problem of precision loss, and it is very likely that incorrect results will be obtained when comparing values.
    • If the range of stored data exceeds the range of decimal, it is recommended to split the data into integers and decimals and store them separately.
  7. [Mandatory] If the stored strings are nearly equal in length, use charthe fixed-length string type.

  8. [Mandatory] varchar is a variable-length string, no storage space is allocated in advance, and the length should not exceed 5000. - If the storage length is greater than this value, define the field type as text, separate a table, and use the primary key to correspond to avoid affecting other fields Index efficiency.

  9. [Mandatory] The table must have three fields: id, gmt_create, gmt_modified.

    • Note: Among them , it idmust be the primary key, the type is unsigned bigint, the single table is incremented automatically, and the step size is 1.
    • gmt_create, gmt_modifiedThe types of are all datetimetypes, the former present tense means active creation, and the latter past participle means passive update.
  10. [Recommendation] The best way to name the table is to add "business name_function of the table".

    • Normal example:alipay_task / force_project / trade_config
  11. [Recommended] The library name should be as consistent as the application name.

  12. [Recommended] If the meaning of the field is modified or the state indicated by the field is added, the field comment needs to be updated in time.

  13. [Recommended] Fields allow for appropriate redundancy to improve query performance, but data consistency must be considered.

    • Redundant fields should follow:
      • Not a frequently modified field.
      • It is not a varchar long field, let alone a text field.
    • Positive example: The commodity category name is frequently used, the field length is short, and the name is basically unchanged. The category name can be redundantly stored in the associated table to avoid associated queries.
  14. [Recommended] Database sharding is recommended only when the number of rows in a single table exceeds 5 million or the capacity of a single table exceeds 2 GB.

    • Note: If it is expected that the data volume in three years will not reach this level at all, please do not split the database and the table when creating the table.
  15. [Recommendation] Appropriate character storage length not only saves database table space and index storage, but more importantly, improves retrieval speed.

    • Positive example: the following table, in which unsigned values ​​can avoid false negative numbers, and expand the range of representation.

      object age range Types of byte Indicates the range
      people within 150 years unsigned tinyint 1 Unsigned value: 0 to 255
      turtle hundreds of years old unsigned smallint 2 Unsigned value: 0 to 65535
      dinosaur fossil tens of millions of years unsigned int 4 Unsigned value: 0 to about 4.29 billion
      Sun about 5 billion years unsigned bigint 8 Unsigned value: 0 to about 10 to the 19th power

Guess you like

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