MySQL table design specifications

Specification summary

  • All database object names must be lowercase letters and underlined division

  • All MySQL database object names prohibit the use of reserved keywords [one investigation after the design table]

  • All tables must use Innodb storage engines, databases and tables using the UTF8 character set uniform

  • All tables and fields need to add a comment

  • Prohibit pictures stored in the database, such as large binary data files

    [ Usually stored in a file server, a database stores only address information file ]

  • Preferentially select the smallest data type can meet the storage needs of the integers not use string [] [] ip string into an integer

  • Avoid using text, blob type, will greatly reduce the efficiency of SQL execution

  • Avoid using ENUM type, you can not use the index, low query efficiency

  • As far as possible all columns defined as NOT NULL [it is not given a default value]

    [Index NULL columns require additional space to keep them, so to take up more space]

  • Use TIMESTAMP (4 bytes) or DATETIME type (8 bytes) storage time avoid the use of string []

  • With the financial data related to the amount of the class must use decimal precision floating-point data type [type, double and float non-precise]

Three Forms

  • The first paradigm: with atomic indecomposable.

    id name age address

    Where the address can be subdivided into national, provincial, regional.

    1567935331875

  • The second paradigm: the need to ensure that each row in the database table and the primary key related

    1567935427407

  • The third paradigm: do not allow data redundancy

    1567935958350

Database naming convention

1. All database object names must be lowercase letters and underlined division

表名:users,users_like-videos,users_fans

表头:id,face_image,nickname

why??

MySQL default object name specified case-sensitive, and MySQL in a production environment typically run on Linux systems, Linux system itself is case sensitive.

2. All MySQL database object names prohibit the use of reserved keywords (if the table name contains the keyword query, it needs to be in single quotes)

About MySQL reserved keyword

WHERE,INSERT,UPDATE,SET,SELECT

https://dev.mysql.com/doc/mysqld-version-reference/en/keywords-8-0.html

It recommended one investigation after the design data table has no keywords.

The basic database design specifications

1. All tables must use Innodb storage engine

No special requirements (ie Innodb not meet features such as: column storage, data storage, etc.) in case, all tables must use Innodb storage engine (default Myisam before MySQL5.5, after 5.6 as the default Innodb).

Innodb support services, support row-level locking, and better recovery, better performance under high concurrency.

MyISAM supports only table-level locking for insert infrequent, frequent query scene.

  • About storage engine

1568941874788

2. databases and tables using the UTF8 character set uniform

More compatible, unified character set can be avoided due to the garbled character set conversion produced a different character set requires a conversion will result in failure of the index before, and if there is need to store emoji expression database character set requires the use of character sets utf8mb4 .

3. All the tables and columns you need to add a comment

Use the comment clause adding tables and notes columns on the maintenance of the data dictionary from the beginning

/*表: user_info*/------------------

/*列信息*/-----------

Field     Type         Collation        Null    Key     Default  Extra           Privileges                       Comment       
--------  -----------  ---------------  ------  ------  -------  --------------  -------------------------------  --------------
id        int(10)      (NULL)           NO      PRI     (NULL)   auto_increment  select,insert,update,references  用户id      
username  varchar(10)  utf8_general_ci  YES             (NULL)                   select,insert,update,references  用户姓名  
password  varchar(20)  utf8_general_ci  YES             (NULL)                   select,insert,update,references  用户密码  
age       int(5)       (NULL)           YES             (NULL)                   select,insert,update,references  年龄        
email     varchar(20)  utf8_general_ci  YES             (NULL)                   select,insert,update,references  邮箱        

/*索引信息*/--------------

Table      Non_unique  Key_name  Seq_in_index  Column_name  Collation  Cardinality  Sub_part  Packed  Null    Index_type  Comment  Index_comment  
---------  ----------  --------  ------------  -----------  ---------  -----------  --------  ------  ------  ----------  -------  ---------------
user_info           0  PRIMARY              1  id           A                   99    (NULL)  (NULL)          BTREE                               

/*DDL 信息*/------------

CREATE TABLE `user_info` (
  `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '用户id',
  `username` varchar(10) DEFAULT NULL COMMENT '用户姓名',
  `password` varchar(20) DEFAULT NULL COMMENT '用户密码',
  `age` int(5) DEFAULT NULL COMMENT '年龄',
  `email` varchar(20) DEFAULT NULL COMMENT '邮箱',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=100 DEFAULT CHARSET=utf8 COMMENT='用户信息表'

4. Prohibition of pictures stored in the database, such as large binary data files

Usually file is large, the amount of data in a short time can cause the rapid growth of the database when the database is read, usually a lot of random IO operations, when the file is large, IO operation is time-consuming.

Usually stored in a file server, a database stores only file address information

Database design specifications field

1. meet the preferences stored minimum required data type

the reason:

The larger columns field, the greater the space required for indexing, the index number of nodes can be stored in such a page the less the less, the more the number of IO while traversing needed the performance index also worse.

method:

. A string converted into a digital type memory, such as: converting the IP address into integer data

MySQL provides two methods to deal with ip address

  • inet_aton ip into the unsigned integer (4-8)
  • inet_ntoa into the integer ip address

When data is inserted before the first use inet_aton the ip address into an integer, space can be saved, the display data, using the integer inet_ntoa ip address into the address can be displayed.

b. For non-negative data (e.g., ID incremented, Integer IP), the priority is stored using unsigned integer

the reason:

Unsigned relative to the symbol may have more than double the storage space

SIGNED INT -2147483648~2147483647
UNSIGNED INT 0~4294967295

The VARCHAR (N) N represents the number of characters, rather than bytes, characters with UTF8 store 255 Varchar (255) = 765 bytes. Excessive length will consume more memory.

2. Avoid using TEXT, BLOB data types, the most common type of TEXT data can be stored in the 64k

It suggested isolated into separate extension table BLOB or TEXT column

MySQL memory temporary table does not support TEXT, BLOB data types such as large, if the query contains such data, when sorting and other operations, you can not use memory temporary tables, you must use a temporary disk tables. And for this kind of data, MySQL or to secondary query, sql performance will become poor, but not saying that we can not use such data types.

If you must use, it is recommended to separate BLOB or TEXT columns to a separate extension table, must not use the query select * only need to remove the necessary columns, the columns do not need to query the data TEXT columns.

3. Avoid using ENUM type

Modify ENUM values ​​need to use ALTER statement

ORDER BY ENUM type of low operational efficiency, the need for additional operations

Prohibit the use of value as ENUM enumeration value

4. As far as possible all columns defined as NOT NULL

the reason:

Index NULL columns require additional space to keep them, so to take up more space

To do special handling of NULL values ​​are compared and calculated

5. Use TIMESTAMP (4 bytes) or DATETIME type (8 bytes) storage time

TIMESTAMP time stored 1970-01-01 00:00:01 ~ 2038-01-19-03: 14:07

TIMESTAMP 4 bytes and occupies the same INT, INT but higher than the readability

TIMESTAMP beyond the range of use of memory type DATETIME

Often someone with a string data type to store date (not the right way)

  • One disadvantage: comparison can not be calculated and date functions
  • 2 shortcomings: string stored date to take up more space

6. At the same amount of financial-related class must use decimal data types

  • Non-precision floating point: float, double
  • Precision floating point: decimal

Decimal precision floating-point type, without losing accuracy in the calculation of

Determined by the width of the space defined, it may be stored every four bytes of 9 digits, and one byte to a decimal point

Be used for storing integer data is larger than bigint

About storage engine

MySQL storage using plug-in engine, can select the most suitable storage engine according to the application scenarios.

MySQL default MyISAM before 5.5, after 5.5 Innodb use the default.

  • View the current engine used [5.7]

    show engines;

    1568967606406

Innodb

  • Row-level locking, support for higher concurrency
  • Support Services (ACID)
  • It has the cache function, index and cache data to improve query efficiency
  • Support online hot backup

[Scenarios]

  • The need to support business transactions (eg transfers, payments)
  • High concurrency
  • Comparative data are frequently read and update scenarios, such as: BBS, SNS, Twitter, micro-letters.

MyISAM storage engine

  • It does not support transactions
  • Caching-only index, does not cache data
  • Table-level locking, concurrency weak
  • Server downtime, data is easily damaged and difficult recovery

MEMORY storage engine

  • Data stored in memory, to understand
  • Table-level locking, concurrency weak

Data type selection

char 与 varchar

char: fixed-length string, but faster memory space is wasted

varchar: variable-length character string.

Use Innodb storage engine is recommended to use varchar

text and blob

To avoid the use, if must use, or proposed to BLOB TEXT column separation to separate extension table.

[MySQL memory temporary table does not support TEXT, BLOB data types such as large, if the query contains such data, when sorting and other operations, you can not use memory temporary tables, you must use a temporary disk tables. And for this kind of data, MySQL or to secondary query, sql performance will become poor, but not saying that we can not use such data types. ]

Floating-point and fixed-point

If the accuracy of data is inserted above the defined accuracy, the value is rounded insertion. [Float, double]

[Decimal fixed-point number is stored as a string]

  • With related financial amounts must use the decimal data type classes

Date type selection

  • DATETIME: year, month, day, hour recording, the maximum time indicated range
  • If the date is not recorded at the same time to let people use zones, use TIMESTAMP

Character Set - solve the garbage problem

  • UTF-8 is usually used
  • Applications involving import existing data, consider compatibility
  • Usually only need to support Chinese, the data capacity, high performance requirements, the use of GBK (a character is two bytes, UTF-8 occupy three bytes)
  • If the application mainly deal with English characters, using the UTF-8

Guess you like

Origin www.linuxidc.com/Linux/2019-09/160758.htm