MySQL enumeration usage

Enumeration: enum , will implement all possible outcomes are designed, in fact, the stored data must be defined a good data.

Enumeration of use

Definitions: enum (list of elements that may arise); // as enum ( 'male', 'female')

Use: storage of data, data storage only defined above

Meaning that:

1, likelihood values ​​defined!

2, fast speed faster than a normal string!

The reason is that the use of an integer enumeration managed, it is possible to manage 2 bytes!

Each value is an integer identifier, beginning from the first option to 1, incremented by one!

When an integer form of management, faster than the string!

A total of 2 bytes, 0-65535, so you can have 65,535 options are available! ,

- Create an enumeration table

create table my_enum(

gender enum ( 'male', 'female', 'confidential')

)charset utf8;

 

One of the functions: Specification data format, wherein the data only in a predetermined data

Bis role: to save storage space (usually an alias enumeration: single box), enumeration value is actually stored, rather than the string itself

In mysql, the system also automatically convert the format, but substantially the same as PHP (especially string-to-digital)

 

- insert data

-- valid data

insert into my_enum values ​​( 'M'), ( 'Confidential'); 

- Insert element of the enumeration values

insert into my_enum values (1),(2);

In the following principle

- erroneous data

insert into my_enum values ​​( 'male'); - Error: This element No

 

Data storage is proven field values: +0 removed the data can determine the original data stored in the end is a string or numeric, string if the end result is always 0, otherwise other values

- The results of the field is taken out for operation +0

select gender + 0,gender from my_enum;

Find out the actual enumeration of the elements of the law: elements appear in the order, from start number 1

 

Enumeration principle : during the enumeration data specifications when (definition of the time), the system will automatically create a corresponding relationship between numbers and enumeration element (relationship into logs); then when the data insertion is performed, the system automatically characters are converted into a corresponding digital memory, and when the data extraction is performed, the system automatically converted into a digital string display.

Because I enumerate the actual value is stored, so you can directly insert values

 

Note: Standing mysql point of view, as much as possible with the set!

Php mysql standing but operating angle, with as little as possible! (Poor compatibility)

 

--------------------- 
Disclaimer: This article is CSDN blogger "State of art small" in the original article, follow the CC 4.0 by-sa copyright agreement, reproduced attach the original source link and this statement.
Original link: https: //blog.csdn.net/qq_41725214/article/details/79771842

 

Guess you like

Origin www.cnblogs.com/jiayu123/p/11333809.html