The meaning and application scenarios of Mysql common data types


The meaning and application scenarios of common data types

Let me talk about the application scenarios of various data types in MySQL in detail:


Number type:

a. TINYINT: used to store very small positive and negative integers, ranging from -128 to 127, occupying 1 byte. Usually used to store 0 or 1, indicating status switch, tombstone, gender, etc. For example: to store gender, you can use 0 to represent female and 1 to represent male.

b. SMALLINT: used to store small positive and negative integers, ranging from -32768 to 32767, occupying 2 bytes. Usually used to store the year, month, etc., the range is small, which can save space. For example: user age, etc.

c. MEDIUMINT: used to store medium-sized integers, the general range is -8388608 ~ 8388607, occupying 3 bytes. It is often used to represent medium-scale data such as page views, downloads, and user statistics.

d. INT (INTEGER): used to store positive, negative, and zero integers, ranging from -2147483648 to 2147483647, occupying 4 bytes. It is usually used to store integer data such as primary key ID, quantity, and amount. Generally, it is a data type commonly used in our development.

e. BIGINT: used to store longer integers, ranging from -9223372036854775808 to 9223372036854775807, occupying 8 bytes. Usually used to represent large values, such as phone numbers, ID card numbers, and so on.


Character type:

a. CHAR: Used to store shorter text strings, fixed length, generally used to store character arrays of limited length, such as provinces, brands, etc.

b. VARCHAR: used to store longer text strings, variable length, usually used to store longer strings, such as addresses, article content, and so on.

c. TEXT: used to store longer text strings, up to 65535 characters, suitable for storing very long content.

d. ENUM: Used to store a set of predefined character values, usually used to represent status values, gender, etc. If there are only two cases of men and women, an enumeration type can be defined to replace the implementation method represented by numbers.

e. SET: Used to store a set of predefined character sets, suitable for storing options, flags, etc. Such as the multi-choice situation of user preferences.


Date and time type:

a. DATE: used to store data in year-month-day format, usually used to store birthday, card opening time, etc. The format is 'YYYY-MM-DD'. .

b. TIME: It is used to store the data in hour, minute and second format, the format is 'hh:mm:ss'. Usually used to store work time, get off work time, etc.

c. DATETIME: used to store data in date and time format, the format is 'YYYY-MM-DD hh:mm:ss'. Date and time from '1000-01-01 00:00:00' to '9999-12-31 23:59:59' can be stored. Usually used to store order time, contract time, etc.

d. TIMESTAMP: used to store data in Unix timestamp format, stored from January 1, 1970 00:00:00 (coordinated universal time) to January 19, 2038 03:14:07 (coordinated universal time) The number of seconds between, occupying 4 bytes. Typically used to record the timestamp of the last modification or update.

e. YEAR: Used to store the year. YEAR is a special data type that only takes 1 byte and can store the years from 1901 to 2155. Typically not used for precise data storage, such as annual events, etc.

It should be noted that different MySQL versions may have different data types, so you need to check and select when using. In addition, it is also necessary to choose according to actual needs. Generally, you can consider the length of business data, make full use of storage space, and read and write efficiency, and choose the data type that matches the application scenario.


The meaning of the characters


What does the 10 in varchar(10) mean?

In English, a character is usually a letter or a punctuation mark, and 10 in varchar(10) refers to 10 English characters.

In the MySQL database, the 10 in VARCHAR(10) means that the field can store up to 10 characters. VARCHAR is a variable-length string type that can store a string with a maximum length of 65535 characters, but if we know that the maximum storage value of the field will not exceed a specific length, we can use VARCHAR(n) to specify the field The maximum length to control the storage space of the data. n indicates that the field can store up to n characters; if the length of the inserted string is less than n, the actual stored value will be the same length as the inserted string. If the inserted string is longer than n, the field will be automatically truncated to a string of n length. Therefore, VARCHAR(10) means that the field can store up to 10 characters, and when storing a string with a length less than or equal to 10 characters, the string will be stored in its entirety. If the length of the stored string is greater than 10, only the first 10 characters will be stored in this field, and the latter characters will be truncated. For example: when VARCHAR(10) stores the string "Hello World", it will store the string data of "Hello Worl".


1 character is how many bytes?

A character is usually composed of multiple bytes, depending on the character encoding used. In MySQL, commonly used character encoding methods include UTF-8, GBK, GB2312 and BIG5, etc.

Generally speaking, in UTF-8 encoding, an ASCII character (including English letters, numbers, special symbols, etc.) occupies 1 byte, and a Chinese character occupies 3 bytes. In GBK encoding, an ASCII character occupies 1 byte, and a Chinese character occupies 2 bytes. In BIG5 encoding, an ASCII character occupies 1 byte, and a Chinese character occupies 2 bytes. Therefore, to determine how many bytes a character occupies, you need to consider the character encoding used.

In MySQL, when defining a character data type, you need to specify the character length occupied by the data type. For example, when defining a VARCHAR(10) data type, it means that the field can store a string of up to 10 characters long, and this character length also depends on the character encoding used. If UTF-8 encoding is used, the VARCHAR(10) data type can store up to 30 bytes (a Chinese character occupies 3 bytes).

The length of VARCHAR can be any length from 1 to the maximum storage range. When selecting the length of VARCHAR, you need to select the character type and length according to actual needs to control the storage space and data storage characteristics.


Guess you like

Origin blog.csdn.net/m0_37742400/article/details/130170259