Mysql field type

integer type
type name illustrate storage requirements
TINYINT very small integer 1 byte
SMALLINT small integer 2 space knots
MEDIUMINT medium-sized integer 3 bytes
INT (INTEGHR) normal sized integer 4 bytes
BIGINT big integer 8 bytes
floating point number
type name illustrate storage requirements
FLOAT single precision floating point 4 bytes
DOUBLE double precision floating point 8 bytes
DECIMAL (M, D),DEC Compressed "strict" fixed-point numbers M+2 bytes

The value range of the FLOAT type is as follows:

  • Signed value range: -3.402823466E+38 to -1.175494351E-38.
  • Unsigned value range: 0 and -1.175494351E-38~-3.402823466E+38.

The value range of the DOUBLE type is as follows:

  • Signed value range: -1.7976931348623157E+308 to -2.2250738585072014E-308.
  • Unsigned value range: 0 and -2.2250738585072014E-308~-1.7976931348623157E+308.

Tip: Whether it is a fixed-point or a floating-point type, if the precision specified by the user exceeds the precision range, it will be rounded up for processing.

When FLOAT and DOUBLE do not specify the precision, the default will follow the actual precision (determined by the computer hardware and operating system). If DECIMAL does not specify the precision, the default is (10, 0).

The advantage of floating-point numbers over fixed-point numbers is that floating-point numbers can represent a larger range when the length is fixed; the disadvantage is that it will cause precision problems.

Finally, I would like to emphasize again: in MySQL, fixed-point numbers are stored in the form of strings. When the precision requirements are relatively high (such as currency and scientific data), it is better to use the DECIMAL type, and the other two floating-point numbers are used for subtraction and comparison operations. It is also prone to problems, so you need to pay attention when using floating-point numbers, and try to avoid doing floating-point number comparisons.

3. Date/Time Type

There are many date data types in MySQL: YEAR, TIME, DATE, DTAETIME, TIMESTAMP. When only year information is recorded, only the YEAR type can be used.

Each type has a legal range of values. When specifying an illegal value, the system will insert a "zero" value into the database.

The date and time types in MySQL are listed in the following table.

type name date format date range storage requirements
YEAR YYYY 1901 ~ 2155 1 byte
TIME HH:MM:SS -838:59:59 ~ 838:59:59 3 bytes
DATE YYYY-MM-DD 1000-01-01 ~ 9999-12-3 3 bytes
DATETIME YYYY-MM-DD HH:MM:SS 1000-01-01 00:00:00 ~ 9999-12-31 23:59:59 8 bytes
TIMESTAMP YYYY-MM-DD HH:MM:SS 1980-01-01 00:00:01 UTC ~ 2040-01-19 03:14:07 UTC 4 bytes

YEAR type

The YEAR type is a single-byte type used to represent the year and requires only 1 byte for storage. YEAR can be specified using various formats, as follows:

  • YEAR expressed in 4-digit string or 4-digit number format, ranging from '1901' to '2155'. The input format is 'YYYY' or YYYY, for example, if you input '2010' or 2010, the value inserted into the database is 2010.
  • YEAR in 2-digit string format, in the range '00' to '99'. Values ​​in the range '00'~'69' and '70'~'99' are converted to YEAR values ​​in the range 2000~2069 and 1970~1999, respectively. '0' has the same effect as '00'. Values ​​inserted outside the range of values ​​will be converted to 2000.
  • YEAR represented by 2 digits, ranging from 1 to 99. Values ​​in the range of 1 to 99 and 70 to 99 are converted to YEAR values ​​in the range of 2001 to 2069 and 1970 to 1999, respectively. Note that a value of 0 will be converted to 0000 here, not 2000.

Tip: The two-digit integer range is slightly different from the two-digit string range. For example, to insert the year 3000, the reader might use a numeric format of 0 for YEAR, and in fact, the value inserted into the database is 0000, not 3000 as expected. Only '0' or '00' in string format can be correctly interpreted as 3000, and illegal YEAR values ​​will be converted to 0000.

TIME type

The TIME type is used for values ​​that only require time information, and requires 3 bytes for storage. The format is HH:MM:SS. HH indicates hours, MM indicates minutes, and SS indicates seconds.

The value range of the TIME type is -838:59:59~838:59:59. The reason why the hour part is so large is that the TIME type can not only be used to represent the time of day (must be less than 24 hours), but also an event Elapsed time or time interval between two events (can be greater than 24 hours, or even negative).

The TIME value can be specified in various formats, as follows.

  • String in 'D HH:MM:SS' format. These "non-strict" syntaxes can also be used: 'HH:MM:SS', 'HH:MM', 'D HH' or 'SS'. Here D represents the day and can take a value between 0 and 34. When inserting into the database, D is converted to hours and saved in the format "D*24+HH".
  • 'HHMMSS' format, a string with no breaks, or a value in HHMMSS format, assumed to be a meaningful time. For example, '101112' is understood as '10:11:12', but '106112' is illegal (it has a nonsensical minutes part) and becomes 00:00:00 when stored.

Tip: Be careful when assigning shorthand values ​​to TIME columns: without the colon, MySQL interprets the value assuming the rightmost two digits represent seconds. (MySQL interprets TIME values ​​as past times rather than current times). For example, a reader may think that '1112' and 1112 mean 11:12:00 (that is, 12 minutes after 11 o'clock), but MySQL interprets them as 00:11:12 (that is, 11 minutes and 12 seconds). Likewise '12' and 12 are interpreted as 00:00:12. Conversely, a colon in a TIME value must be interpreted as the time of day, that is, '11:12' means 11:12:00, not 00:11:12.

DATE type

The DATE type is used when only a date value is required, without a time part, and requires 3 bytes for storage. The date format is 'YYYY-MM-DD', where YYYY is the year, MM is the month, and DD is the day.

When assigning a value to a field of type DATE, you can insert data of type string or number, as long as it conforms to the date format of DATE. As follows:

  • The date expressed in the format of 'YYYY-MM-DD' or 'YYYYMMDD' characters, the value range is '1000-01-01'~'9999-12-3'. For example, if you enter '2015-12-31' or '20151231', the date inserted into the database will be 2015-12-31.
  • Express the date in the string format 'YY-MM-DD' or 'YYMMDD', where YY represents the two-digit year value. MySQL interprets the rules for the two-digit year value: the year value in the range of '00~69' is converted to '2000 2069', and the year value in the range of '70 99' is converted to '1970~1999'. For example, if you enter '15-12-31', the date inserted into the database is 2015-12-31; if you enter '991231', the date inserted into the database is 1999-12-31.
  • The date represented by the YYMMDD number format is similar to the above, the year value in the range of 00~69 is converted to 2000~2069, and the year value in the range of 80~99 is converted to 1980~1999. For example, if you enter 151231, the date inserted into the database is 2015-12-31, and if you enter 991231, the date inserted into the database is 1999-12-31.
  • Use CURRENT_DATE or NOW() to insert the current system date.

Tip: MySQL allows "lax" syntax: any punctuation mark can be used as a spacer between date parts. For example, '98-11-31', '98.11.31', '98/11/31', and '98@11@31' are equivalent, and these values ​​will also insert correctly into the database.

DATETIME type

The DATETIME type is used for values ​​that need to contain both date and time information, and requires 8 bytes for storage. The date format is 'YYYY-MM-DD HH:MM:SS', where YYYY is the year, MM is the month, DD is the day, HH is the hour, MM is the minute, and SS is the second.

When assigning a value to a field of type DATETIME, you can insert data of type string or number, as long as it conforms to the date format of DATETIME, as shown below.

  • The date expressed in the string format of 'YYYY-MM-DD HH:MM:SS' or 'YYYYMMDDHHMMSS', the value range is '1000-01-01 00:00:00'~'9999-12-3 23:59 : 59'. For example, if you enter '2014-12-31 05:05:05' or '20141231050505', the DATETIME value inserted into the database is 2014-12-31 05:05:05.
  • A date in the string format 'YY-MM-DD HH:MM:SS' or 'YYMMDDHHMMSS', where YY represents the two-digit year value. As before, year values ​​in the range '00~79' are converted to '2000~2079', and year values ​​in the range '80~99' are converted to '1980~1999'. For example, input '14-12-31 05:05:05', the DATETIME inserted into the database is 2014-12-31 05:05:05; input 141231050505, the DATETIME inserted into the database is 2014-12-31 05:05:05 .
  • Date and time in YYYYMMDDHHMMSS or YYMMDDHHMMSS numeric format. For example, if you enter 20141231050505, the DATETIME inserted into the database is 2014-12-31 05:05:05; if you enter 140505050505, the DATETIME inserted into the database is 2014-12-31 05:05:05.

Tip: MySQL allows "lax" syntax: any punctuation character can be used as a spacer between date parts or time parts. For example, '98-12-31 11:30:45', '98.12.31 11+30+35', '98/12/31 11 30 45' and '98@12@31 11 30 45' are equivalent , the values ​​are inserted correctly into the database.

TIMESTAMP type

The display format of TIMESTAMP is the same as that of DATETIME, the display width is fixed at 19 characters, the date format is YYYY-MM-DD HH:MM:SS, and 4 bytes are required for storage. But the value range of TIMESTAMP column is smaller than that of DATETIME, which is '1970-01-01 00:00:01' UTC~'2038-01-19 03:14:07' UTC. When inserting data, ensure that it is within the legal value range.

Tip: Coordinated Universal Time (English: Coordinated Universal Time, French: Temps Universel Coordonné) is also known as Universal Unified Time, Universal Standard Time, and International Coordinated Time. English (CUT) and French (TUC) abbreviations are different, as a compromise, referred to as UTC.

In addition to the different storage bytes and supported ranges between TIMESTAMP and DATETIME, there is another biggest difference:

  • When DATETIME stores date data, it is stored according to the actual input format, that is, it stores whatever is input, regardless of the time zone;
  • The TIMESTAMP value is stored in UTC (Coordinated Universal Time) format, the current time zone is converted when stored, and converted back to the current time zone when retrieved. That is, when querying, the displayed time value is different according to the current time zone.

Tip: If you assign a DATE value to a DATETIME or TIMESTAMP object, the time portion of the resulting value is set to '00:00:00', so the DATE value does not contain time information. If a DATE object is assigned a DATETIME or TIMESTAMP value, the time portion of the resulting value is removed, so the DATE value does not contain time information.

4. String type

The string type is used to store string data, and can also store binary data of pictures and sounds. Strings can be case-sensitive or case-insensitive string comparisons, and regular expression matching searches can also be performed.

The string types in MySQL include CHAR, VARCHAR, TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT, ENUM, SET, etc.

The following table lists the string data types in MySQL, and the M in brackets indicates that the length can be specified for it.

type name illustrate storage requirements
CHAR(M) fixed-length non-binary string M bytes, 1<=M<=255
VARCHAR(M) variable-length non-binary string L+1 bytes, here, L<=M and 1<=M<=255
TINYTEXT very small non-binary string L+1 bytes, here, L<2^8
TEXT small non-binary string L+2 bytes, here, L<2^16
MEDIUMTEXT Medium-sized non-binary string L+3 bytes, here, L<2^24
LONGTEXT large non-binary string L+4 bytes, here, L<2^32
ENUM Enumeration type, can only have one enumeration string value 1 or 2 bytes, depending on the number of enumeration values ​​(maximum is 65535)
SET A SET, String object can have zero or more SET members 1, 2, 3, 4 or 8 bytes, depending on the number of set members (up to 64 members)

The VARCHAR and TEXT types are variable-length types whose storage requirements depend on the actual length of the column value (indicated by L in the preceding table), not on the largest possible size of the type.

For example, a VARCHAR(10) column can store a string with a maximum length of 10 characters, and the actual storage needs the length L of the string plus one byte to record the length of the string. For the character "abcd", L is 4 and storage requires 5 bytes.

CHAR and VARCHAR types

CHAR(M) is a fixed-length string, and the length of the string column is specified when it is defined. When saving, pad with spaces on the right to the specified length. M represents the length of the column, ranging from 0 to 255 characters.

For example, CHAR(4) defines a fixed-length string column containing a maximum of four characters. Trailing spaces are removed when CHAR values ​​are retrieved.

VARCHAR(M) is a variable-length character string, M represents the length of the largest column, and the range of M is 0-65535. The maximum actual length of VARCHAR is determined by the size of the longest line and the character set used, while the actual space occupied is the actual length of the string plus one.

For example, VARCHAR(50) defines a string with a maximum length of 50, if the inserted string has only 10 characters, the actual stored string will be 10 characters and an end-of-string character. VARCHAR Trailing spaces are preserved when saving and retrieving the value.

TEXT type

The TEXT column holds non-binary strings, such as article content, comments, etc. Trailing spaces are not removed when saving or querying the value of a TEXT column.

There are 4 TEXT types: TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT. The storage space and data length of different TEXT types are different.

  • TINYTEXT represents a TEXT column with a length of 255 (28-1) characters.
  • TEXT means a TEXT column with a length of 65535 (216-1) characters.
  • MEDIUMTEXT represents a TEXT column with a length of 16777215 (224-1) characters.
  • LONGTEXT represents a TEXT column with a length of 4294967295 or 4GB (232-1) characters.

ENUM type

ENUM is a string object whose value is a column of values ​​enumerated in the column specification when the table was created. Its syntax format is as follows:

<字段名> ENUM( '值1', '值1', …, '值n' )

The field name refers to the field to be defined, and the value n refers to the nth value in the enumeration list.

Fields of ENUM type can be obtained in the specified enumeration list when taking values, and only one can be taken at a time. If there are spaces in the created member, the trailing spaces will be automatically removed.

The ENUM value is internally represented by an integer, and each enumeration value has an index value; the member values ​​allowed by the list value are numbered from 1, and MySQL stores this index number, and the enumeration can have up to 65535 elements.

SET type

SET is a string object, which can have zero or more values, and the SET column can have up to 64 members, and the value is a column of values ​​specified when the table is created. When specifying a SET column value that includes multiple SET members, separate each member with a comma, and the syntax is as follows:

SET( '值1', '值2', …, '值n' )

Like ENUM types, SET values ​​are represented internally as integers, and each value in the list has an index number. Trailing spaces in SET member values ​​are automatically removed when the table is created.

However, unlike the ENUM type, the field of the ENUM type can only select one value to insert from the defined column values, while the SET type column can select a union of multiple characters from the defined column values.

Tip: If the column values ​​inserted into the SET field are duplicated, MySQL will automatically delete the duplicated values; the order of the values ​​inserted into the SET field is not important, and MySQL will display them in the defined order when they are stored in the database; Correct value, by default, MySQL will ignore these values ​​and give a warning.

5. Binary type

MySQL supports two types of character data: text strings and binary strings. In the previous section "MySQL String Type", we talked about text strings, and in this section we will explain binary strings.

Binary string types are sometimes simply referred to as "binary types".

Binary strings in MySQL are BIT, BINARY, VARBINARY, TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB.

The binary data types in MySQL are listed in the following table, and the M in brackets indicates that the length can be specified for it.

type name illustrate storage requirements
BIT(M) bit field type About (M+7)/8 bytes
BINARY(M) fixed-length binary string M bytes
VARBINARY (M) variable length binary string M+1 bytes
TINYBLOB (M) very small blob L+1 bytes, where L<2^8
BLOB (M) Small BLOB L+2 bytes, where L<2^16
MEDIUMBLOB (M) Medium BLOB L+3 bytes, where L<2^24
LUNG BLOB (M) very large blob L+4 bytes, where L<2^32

BIT type

Bit field type. M represents the number of digits for each value, ranging from 1 to 64. If M is omitted, the default value is 1. If the value assigned to a BIT(M) column is less than M bits long, the value is padded with 0s on the left. For example, assigning a value of b'101' to a BIT(6) column has the same effect as assigning b'000101'.

The BIT data type is used to store bit field values, for example, to store data 13 in binary form, and the binary form of 13 is 1101, where a BIT type with at least 4 bits is required, that is, the column type can be defined as BIT(4). Data greater than binary 1111 cannot be inserted into a field of type BIT(4).

Tip: By default, MySQL cannot insert values ​​beyond the allowable range of the column, so when inserting data, ensure that the inserted values ​​are within the specified range.

BINARY and VARBINARY types

The BINARY and VARBINARY types are similar to CHAR and VARCHAR except that they contain binary byte strings. The syntax format used is as follows:

列名称 BINARY(M) 或者 VARBINARY(M)

The length of the BINARY type is fixed. After the specified length, if the length is less than the maximum length, "\0" will be padded to the right of them to reach the specified length. For example, if the column data type is specified as BINARY(3), when inserting a, the stored content is actually "\a0\0", when inserting ab, the actual stored content is "ab\0", no matter whether the stored content is When the specified length is reached, the storage space is the specified value M.

The length of the VARBINARY type is variable. After specifying the length, the length can be between 0 and the maximum value. For example, if the column data type is specified as VARBINARY(20), if the length of the inserted value is only 10, the actual storage space is 10 plus 1, and the actual occupied space is the actual length of the string plus 1.

BLOB type

A BLOB is a binary object used to store variable amounts of data. There are 4 BLOB types: TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB

Guess you like

Origin blog.csdn.net/s_frozen/article/details/129031385