"Alibaba Java Development Manual": Five, MySQL database

1. The library name and the application name are as consistent as possible

2. Table names and field names must use lowercase letters or numbers, and the beginning of numbers is prohibited.

3. Do not use plural nouns for table names

4. It is best to add "business name_function of table" to the name of the table. For example, edu_teacher

5. The table must have three fields: id, gmt_create, gmt_modified

Description:

Among them, id must be the primary key, the type is bigint unsigned, the increment is for single table, and the step size is 1.

(If you use sub-database and sub-table cluster deployment, the id type is verchar, non-incremental, and the distributed id generator is used in the business)

The types of gmt_create and gmt_modified are both datetime types. The former means active creation in the present tense, and the past participle means passive update.

gmt_create is the creation time gmt_modified is

6. Only when the number of rows in a single table exceeds 5 million rows or the capacity of a single table exceeds 2GB, it is recommended to sub-database and table. Note: If it is estimated that the data volume after three years will not reach this level at all, please do not divide the database and tables when creating the tables.

7. The field that expresses the concept of yes or no must be named in the way of is_xxx, and the data type is unsigned tinyint (1 means yes, 0 means no).

Note: If any field is a non-negative number, it must be unsigned.

Note: Do not add the is prefix to any boolean variable in the POJO class. The database indicates the value of yes or no, using the tinyint type, and insisting on the naming method of is_xxx to clarify the meaning and range of its value.

Positive example: the field name is_deleted that expresses logical deletion, 1 means delete, 0 means not deleted.

8. 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 loss of precision. It is very likely that incorrect results will be obtained when comparing values. If the stored data range exceeds the decimal range, it is recommended to split the data into integers and decimals and store them separately.

9. If the lengths of the stored strings are almost equal, use the char fixed-length string type.

10. Varchar is a variable-length character 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, create a separate table, and use the primary key to correspond to avoid affecting other field indexes effectiveness.

11. The unique index name is uk_ field name; the common index name is idx_ field name.

Description: uk_ is unique key; idx_ is the abbreviation of index

12. Foreign keys and cascades must not be used. All foreign key concepts must be resolved at the application layer. 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 is a risk of database update storms; foreign keys affect database insertion speed.

Guess you like

Origin blog.csdn.net/he1234555/article/details/115028628