08, MySQL- string

String

1、Char

Fixed-length character : After the specified length, the system will allocate space for storing the specified data

The basic syntax:

char (L), L represents the number of characters (letters as Chinese and English), L the length of 0 to 255

2、Varchar

Variable-length character : after a specified length, the system calculates the length of the data actually stored, assign the appropriate length (the data does not exceed the length)

The basic syntax:

Varchar (L), L represents the number of characters, L is the length of the theoretical value bits 0 to 65535

Because varchar length data to be recorded (data length according to the system automatically allocate space), so that after each data generation varchar, the system will increase 1-2 bytes of overhead data behind: it is used to save space occupied by the data length

If the data itself is less than 127 characters: an overhead byte; if more than 127, on the overhead bytes of two

(1) Char and varchar comparison data storage (utf8, a character will occupy 3 bytes)

Storing data

Char(2)

Varchar(2)

Char share of bytes

Varchar of Bytes

A

A

A

2 * 3 = 6

1 * 3 + 1 = 4

FROM

FROM

FROM

2 * 3 = 6

2 * 3 + 1 = 7

The difference (2) Char and the varchar

1, char will use the specified space, it varchar based on data given space

2, char data query efficiency is higher than varchar: varchar is calculated by the number of records required behind

 

Description:

If determined that the data must be accounted for a specified length, then the use of char type;

If you are unsure how much data there is in the end, then use the varchar type;

If the data is longer than 255 characters, whether or not a fixed length, will use the text, using no char and varchar

 

3、Text

Text types: mysql provides two types of essentially text

Text: storing plain text characters

Blob: storing binary text (pictures, documents) are not normally use blob to store the file itself, usually using a link pointing to the corresponding file itself.

(1) Text : The system provides four text

(2) TINYTEXT : The system uses one byte to store, the actual data can be stored as follows: 1 + 2 ^ 8

(. 3) the Text : saving two bytes actually stored as: 2 + 16 ^ 2

(. 4) MEDIUMTEXT : using three bytes of storage, real storage is: 2 ^ 24 + 3

(. 5) LONGTEXT : stored using four bytes, actual storage of: 2 ^ 32 + 4

note:

(1) corresponding to the text in the selected memory when not deliberately to choose the type of text, the system will automatically select an appropriate type based on the data length of the text stored.

(2) when the selected character is stored, if the data exceeds 255 characters, then select the stored text

 

4、Enum

Enumerated type: Before data is inserted, to set up some items, these items may eventually result data appear.

If the data to determine a field of only a few values: such as gender, male and female, confidentiality, the system can be specified when setting field of the current field can hold only a fixed number of values: We'll use enumeration 

The basic syntax:

enum (a data value, the data value 2, ...)

The system provides one or two bytes to store data enumerated: selects the actual storage space specifically recited enum value calculation: if the data value within the list 255, then a byte is enough, if more than 255 but less than 65535, the system uses two bytes saved.

 

Create a table ①

 

② insert data: valid data, corresponding to the field value must be set to the table when the determined value

 

③ Data Error: enum functional specification data, to ensure that the data must be inserted into the set range, other types can not

 

Enumeration enum ④ storage principle: in fact the value of the field is not really stored string, but the string corresponding index: when the system is set enumeration type, each element will enumerate the definition of an index, the index rules from the beginning

Enum (1 => 'M', 2 => 'F', 3 => 'Confidential')

特性:在mysql中系统是自动进行类型转换的:如果数据碰到“+、-、*、/”系统就会自动将数据转换成 数值,而普通字符串转换成数值为0

范例:查看enum元素的值

Select 字段名 + 0 from 表名;

 

⑤ 既然实际enum字段存储的结果是数值:那么在进行数据插入的时候,就可以使用对应的数值来进行。

 

枚举的意义:

(1) 规范数据本身,限定只能插入规定的数据项

(2) 节省存储空间

5、Set

集合:是一种将多个数据选项可以同时保存的数据类型,本质是将指定的项按照对应的二进制位来进行控制:1表示该选项被选中,0表示该选项没有被选中。

基本语法:

set(‘值1’,’值2’,’值3’…)

系统为set提供了多个字节进行保存,但是系统会自动计算来选择具体的存储单元

1个字节:set只能有8个选项

2个字节:set只能有16个选项

3个字节:set只能表示24个选项

8个字节:set可以表示64个选项

Set和enum一样,最终存储到数据字段中的依然是数字而不是真实的字符串

① 创建表

 

② 插入数据:可以插入多个数据,就是在数据插入的字符串中,使用对应的逗号“,”将选项进行隔开

 

③ 数据选项所在的数据与数据插入的顺序无关:最终都会变成选项对应的顺序

 

④ 分析数据存储的方式

(1) 系统将对应的数据选项(设计)按照顺序进行编排:从第一个开始进行占位,每一个都对应一个二进制位。

 

(2) 数据在存储的时候,如果被选中,那么对应的位的值就为1,否则为0

 

(3) 系统在进行存储的时候会自动将得到的最终的二进制颠倒过来,然后再进行转换成十进制存储

 

⑤ 查看数据:按照自动转换成数值来查看

 

⑥ 既然是数值,那么就可以插入数值来代替实际插入数据

 

注意:数字插入的前提是对应的二进制位上都有对应的数据项

Set集合的意义: 规范数据、节省存储空间

Enum:单选框

Set:复选框

Guess you like

Origin www.cnblogs.com/CSAH/p/11361541.html