mysql type reference

mysql database content summary

Number type:

Tinyint(M) unsigned zerofull

     1 byte is signed -128~127 by default

     unsigned: unsigned integer range 0~255

     M: represents the width (meaning only when zerofull), just a display effect, will not affect the actual data length

     zerofull: zero padding (if a column is zerofull, the default is unsigned) because there will be no case of 0000-1

     Zero padding (eg: 0005, how many zeros is the M limit width)

     Columns are generally not null

          The first reason is that null is not easy to compare (the equal sign comparison is not available, only is null or is not null);

          The second reason is that the execution efficiency of null is lower, and the default value can be specified by not null default 0

Smallint and tinyInt usage

     2 bytes default signed range -32768~32767; unsigned range 0~65535

Mediumint is the same as tinyInt usage

     3 bytes default signed range -8388608~8388607; unsigned range 0~16777215

Int is the same as tinyInt usage

     4 bytes default signed range -2147483648~2147483647; unsigned range 0~4294967295

BigInt and tinyInt usage

     8 bytes default signed range -9223372036854775808-9223372036854775807; unsigned range 0~18446744073709551615

Float(m,d) float 

     four bytes

     float(6, 2) means a total of six digits (without a decimal point), with two decimal places.

     The number of decimal places can be specified in mysql, but it is not allowed to specify the number of decimal places in sql server and oracle

Double(m,d) float 

     eight bytes

     double(6, 2) means that there are six digits (without decimal points), and the decimals occupy two digits.

     The number of decimal places can be specified in mysql, but it is not allowed to specify the number of decimal places in sql server and oracle

Decimal(m,d) Fixed-point type

     Use the same float, just more precise.

     Example: float(9,2) decimal(9,2) is stored in 1234567.23, float will display 1234567.25, and decimal will display 1234567.23, which is more precise.

     Floating point and fixed point are four or eight bytes

Text Type:

Char char(M), M represents the width between 0 and 255 characters

     , the number of characters that can be accommodated (not bytes, one byte occupies eight bits, one Chinese character occupies two bytes, and utf-8 Chinese characters occupy three bytes), char(8), which can store eight Chinese characters

Varchar variable length varchar(M) M represents the width between 0 and 65535 bytes, which can store about 22000 Chinese characters

     There are 1-2 bytes to mark the real length

     Difference between Char and Varchar:

     1, char is fixed length, varchar is variable length

     2. The maximum storage length of char is 255 characters, which has nothing to do with the setting of the character set; while the maximum length of varchar is related to the setting of the character set

          Therefore, if the set character set is

          latin1: a character is a byte -----> the maximum number of characters that can be stored is 65532

          gbk: a character is two bytes -------> the maximum number of characters that can be stored is 65532/2

          utf-8: a character is three bytes -------> the maximum number of characters that can be stored is 65532/3

          char fixed length: M characters, if the stored value is less than M characters, it will occupy M characters and fill in with spaces

          Varchar variable length: M characters, less than M characters stored, set to N, N <= M, actually occupy N characters

     3. Utilization

          Type Width Stored characters Real storage characters (i<=M) space utilization

          char(M)  M          M               i                    M                         i/M <= 100%

          varchar M M i i characters + (1-2) bytes i/(i + 1-2) <= 100%

          char may be equal to 100%, for example: four-digit char(4), the utilization rate is 100%; the utilization rate of varchar is always less than 100%

     4, char is faster than varchar

text: It is a text type that can store relatively large data, but the search speed is relatively slow, there is no full-text index, and it is useless to add no width.

    Therefore, if it is not a relatively large data, it is recommended to use char or varchar to query

Date Type:

     year year

     date date

     time time

     datetime dateTime

     timestamp timeStamp

     One byte of year type represents 1901-2155[0000, which means select when wrong]

          If you enter 2 digits, '00-69' means 2000-2069 plus 2000 The first year of the computer starts from 1970

                         '70-99' means 1970-1999 plus 1900

          Four digits are recommended

     Typical format of Date type1992-08-12

          Stored range: '1000-01-01'-->'9999-12-31'

     Time time type typical format hh-mm-ss

          Storage range: '-838:59:59'-->'838:59:59'

     dateTime datetime type typical format '1992-09-28 12:23:23'

          Stored range: '1000-01-01 00:00:00'-->'9999-12-31 23:59:59'

     timeStamp timestamp type

          is 1970-01-01:00:00:00, the number of seconds to the current

          The calculation is very accurate and can also be easily formatted

Note: In real development, a date and time type is rarely used to represent a column that needs to be accurate to seconds, but a timestamp type is used to represent it

Reason: Although the date and time type can be accurate to the second and is easy to view, it is very troublesome and inaccurate when encountering boundary date calculations, such as February in normal years and leap years

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326700816&siteId=291194637