[⑭MySQL | data type (2)] string | binary type

foreword

✨Welcome to Xiao K 's MySQL column , this section will bring you MySQL string | binary type sharing✨


insert image description here

5 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 data types in MySQL are listed in the following table, and Mthe representation in parentheses can specify the length for it.

type name illustrate length range storage space used
CHAR(M) Fixed length 0<=M<=255 M bytes
VARCHAR(M) variable length string 0<=M<=65535 M+1 bytes
TINYTEXT very small string 0<=L<=255 L+1 bytes
TEXT small string 0<=L<=65535 L+2 bytes
MEDIUMTEXT medium size string 0<=L<=16777215 L+3 bytes
LONGTEXT large string 0<=L<=4294967295 L+4 bytes
ENUM Enumeration type, can only have one enumeration string value 0<=L<=65535 1 or 2 bytes, depending on the number of enumeration values ​​(maximum is 65535)
SET A collection of strings, string objects can have zero or more SET members 0<=L<=64 1, 2, 3, 4 or 8 bytes, depending on the number of set members (up to 64 members)

CHAR and VARCHAR types

Both CHAR and VARCHAR types can store relatively short strings.

type features length length range take up storage space
CHAR(M) Fixed length M 0<=M<=255 M bytes
VARCHAR(M) variable length M 0<=M<=65535 (actual length + 1/2) bytes

CHAR type:

  • CHAR(M) is a fixed-length character string, and the length of the character string is specified when it is defined. If not specified, it defaults to 1 character.

  • If the actual length of the data is smaller than the declared length of the CHAR type when saving, it will 右侧填充空格reach the specified length. When retrieving CHAR values, trailing spaces are removed.

  • When defining a CHAR type field, the declared field length is the number of bytes of storage space occupied by the CHAR type field.

CREATE TABLE test_char
(
	f1 CHAR,
	f2 CHAR(5)
);

DESC test_char;

INSERT INTO test_char(f1) VALUES('h');
#Data too long for column 'f1' at row 1
INSERT INTO test_char(f1) VALUES('he');
INSERT INTO test_char(f1) VALUES('你');

INSERT INTO test_char(f2) VALUES('C语言');
#Data too long for column 'f2' at row 1
INSERT INTO test_char(f2) VALUES('C语言Plus');

#测试在右侧填充的空格,会在查询的时候去掉的情况
SELECT CONCAT(f2,'***') FROM test_char;
#注意:自己插入的空格也会被去掉
INSERT INTO test_char(f2) VALUES('fk  ');

SELECT * FROM test_char;

VARCHAR type:

  • When VARCHAR(M) is defined, the length M must be specified, otherwise an error will be reported.

  • VARCHAR(M) means that M characters can be stored, and the actual space occupied is the actual length of the string plus 1 or 2.

  • VARCHAR Trailing spaces are preserved when saving and retrieving the value.

#错误,没有指定长度
CREATE TABLE test_varchar
(
	name VARCHAR
);

CREATE TABLE test_varchar
(
	name VARCHAR(5)
);

INSERT INTO test_varchar VALUES('星星'),('太阳月亮');
#Data too long forcolumn 'NAME' at row 1
INSERT INTO test_varchar VALUES('星星太阳月亮');

TEXT type

TEXT is used to save strings of text type, 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.

When inserting data into a TEXT type field, the system automatically stores it according to the actual length, and there is no need to define the length in advance.

type illustrate size (bytes)
TINYTEXT small text 0<=L<=255
TEXT plain text 0<=L<=65535
MEDIUMTEXT medium text 0<=L<=16777215
LONGTEXT long text 0<=L<=4294967295 (equivalent to 4GB characters)

TEXT text type can store relatively large text segments, and the search speed is slightly slower, so if the content is not particularly large, it is recommended to use CHAR and VARCHAR instead. There is also no need to add a default value for the TEXT type, and it is useless to add it.

ENUM type

The ENUM type is also called an enumeration type, and the value range of the ENUM type needs to be specified in the definition field. When setting field values, the ENUM type only allows a single value to be selected from members, and multiple values ​​cannot be selected at one time. If there are spaces in the created member, the trailing spaces will be automatically removed.

type length range take up storage space
ENUM 1<=L<=65535 1 or 2 bytes
  • ENUM values ​​are represented internally as integers, each enumeration value has an index value;
  • The member values ​​allowed by the list value are numbered from 1, which is the index number that MySQL stores, and the enumeration can have up to 65535 elements.

ENUM values ​​are sorted in column index order, with empty strings sorting before non-null strings, and NULL values ​​sorting before all other enumeration values.

Tip: ENUM columns always have a default value. If an ENUM column is declared as NULL, the NULL value is a valid value for the column, and the default value is NULL. If an ENUM column is declared NOT NULL, its default value is the first element of the list of allowed values.

  • View all values ​​of enum fields
SELECT
column_type
FROM
information_schema. COLUMNS
WHERE
#TABLE_SCHEMA = "test" AND
DATA_TYPE = 'enum'
AND table_name="test_enum"
AND column_name="ch";
  • test
CREATE TABLE test_enum
(
	gender ENUM('男','女','unknown')
);

INSERT INTO test_enum VALUES('男');
#Data truncated for column 'ch' at row 1
INSERT INTO test_enum VALUES('中');
#可以使用索引来插入枚举元素(注意:索引从1开始)
INSERT INTO test_enum VALUES(1),('2');

#当enum字段没有申明为NOT NULL时,插入NULL也是合法的
INSERT INTO test_enum VALUES(NULL);

SELECT * FROM test_enum;

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.

Range of members storage space used
1<=L<=8 1 byte
9<=L<=16 2 bytes
17<=L<=24 3 bytes
25<=L<=32 4 bytes
33<=L<=64 8 bytes
  • 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;

test

CREATE TABLE test_set
(
	hobby SET('吃饭','睡觉','写Bug')
);

INSERT INTO test_set VALUES('吃饭');

INSERT INTO test_set VALUES('吃饭,睡觉');

#重复插入时,会自动删除重复数据
INSERT INTO test_set VALUES('吃饭,睡觉,吃饭');
#插入的值在set中不存在时,会报错
INSERT INTO test_set VALUES('吃饭,睡觉,花钱');

SELECT * FROM test_set;

6 binary types

The binary string type in MySQL mainly stores some binary data, such as pictures, audio, video and other binary data.

Includes BIT, BINARY, VARBINARY, TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB.

The following table lists the binary data types in MySQL, and Mthe representation in parentheses can specify the length for it.

type name illustrate size (bytes)
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 length of the value assigned to a BIT(M) column is less than M bits, the value is left-padded with 0s. 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:

Column name BINARY(M) or 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 "a\0\0", when inserting ab, the actual stored content is "ab\0", regardless of the stored content Whether 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 four BLOB types: TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB, and they have different maximum lengths of values ​​they can hold, as shown in the table below.

type of data storage range
TINYBLOB Maximum length is 255 (28-1) bytes
BLOB Maximum length is 65535 (216-1) bytes
MEDIUMBLOB Maximum length is 16777215 (224-1) bytes
LUNG BLOB Maximum length is 4294967295 or 4GB (231-1) bytes

BLOB columns store binary strings (byte strings), and TEXT columns store non-decimal strings (character strings). BLOB columns are character sets, and sorting and comparisons are based on the numeric values ​​of the column value bytes; TEXT columns have a character set, and values ​​are sorted and compared according to the character set.

Summarize

In general, choosing an appropriate data type can improve database performance, save storage space, ensure data consistency and validity, and facilitate data processing and calculation. Therefore, when designing a database, it is very important to choose the appropriate data type. ~The next section brings the sharing of MySQL views

Guess you like

Origin blog.csdn.net/qq_72157449/article/details/132396010