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.