Introduction to set and enum in mysql

Source: Internet
Author: User

Set and enum data types are all string objects.Set can contain up to 64 elements, and any element in the set can be obtained. However, enum can only obtain one Wood Element in the set, which can contain a maximum of 65536 elements. That is to say, set is a multiple choice, and enum is a single choice.
Here we will compare the similarities and differences between them:
Copy codeThe Code is as follows:
Mysql> create table db_set (
-> Set1 set ('x', 'y', 'z') not null,
-> Enum1 enum ('one', 'two', 'three ') not null );
Query OK, 0 rows affected (0.06 sec)
Mysql> desc db_set;
+ ------- + --------------------------- + ------ + ----- + --------- + ------- +
| Field | Type | Null | Key | Default | Extra |
+ ------- + --------------------------- + ------ + ----- + --------- + ------- +
| Set1 | set ('x', 'y', 'z') | NO | NULL |
| Enum1 | enum ('one', 'two', 'three ') | NO | NULL |
+ ------- + --------------------------- + ------ + ----- + --------- + ------- +
Mysql> insert into db_set values (1, 3), (1, 4), (4, 1 );
Query OK, 3 rows affected, 1 warning (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 1
Mysql> select * from db_set;
+ ------ + ------- +
| Set1 | enum1 |
+ ------ + ------- +
| X | three |
| X |
| Z | one |
+ ------ + ------- +
3 rows in set (0.01 sec)
 

Here we can see their output results. I was puzzled and later I learned:
In the set type, if the value is beyond the range it can represent, it is added in binary format:
Set Element
Decimal
Binary

'X'
1
0001

'Y'
2
0010

'Z'
4
0100

If the enum type is out of the valid range, a null value is attached:
Enum Element
Index

Null
Null

''
0

'One'
1

'Two'
2

'Three'
3

Now everyone understands.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.