Data types in MySQL enum and set

Source: Internet
Author: User
Tags string format

String data type Set,enum in MySQL

1. Enum

A single-choice string data type that is suitable for storing "radio values" in the form interface.

When you set an enum, you need to give a "fixed number of options" and store only one of the values.

To set the format of an enum:

Enum ("Option 1", "Option 2", "option 3",...) ;

In fact, the enum option will correspond to a number, followed by 1,2,3,4,5 ..., with a maximum of 65,535 options

When used, you can use the string format of the option, or you can use the corresponding number.

2. Set

Multi-select string data type, suitable for storing the "multi-select value" of the form interface.

When setting the set, it is also necessary to give a "fixed number of options", storing several of these values.

To set the format of the set:

Set ("Option 1", "Option 2", "option 3",...)

Similarly, each option value for set corresponds to a number, followed by 1,2,4,8,16 ..., with a maximum of 64 options

When used, you can use the SET option's string itself (multiple options are separated by commas), or you can use multiple options for the sum of numbers (for example: 1+2+4=7)

The code is as follows:

/* CREATE TABLE */
Mysql> Create Tableenum_set_table ( -IdintAuto_incrementPrimary Key, -Gender enum ('M','F'), -HobbySet('Music','movie','Swimming','Footbal') - ); Query OK,0Rows Affected (0.01sec)
/* An enum value, a set value, both using the string format of the option */mysql> Insert intoEnum_set_table (Id,gender,hobby)Values(NULL,'M','Music'); Query OK,1Row affected (0.01sec)
/* An enum value, multiple set values, both using the string format of the option */mysql> Insert intoEnum_set_table (Id,gender,hobby)Values(NULL,'F','Music,movie,footbal'); Query OK,1Row affected (0.00sec)
/* An enum value, a set value, both using the number format of the option * /MySQL> Insert intoEnum_set_table (Id,gender,hobby)Values(NULL,1,1); Query OK,1Row affected (0.00sec)
/* An enum value, multiple set values, both using the number format of the option, where the value of enum 2<=> ' F ', 15=1+2+4+8 <=> ' Music,movie,swimming,footbal ' */mysql> Insert intoEnum_set_table (Id,gender,hobby)Values(NULL,2, the); Query OK,1Row affected (0.00sec)
/* An enum value, multiple set values, enum value using the option string format, set value using the number format of the option, 7=1+2+4 <=> ' music,movie,swimming ' */mysql> Insert intoEnum_set_table (Id,gender,hobby)Values(NULL,'F',7); Query OK,1Row affected (0.01sec)
/* Query result */mysql> Select * fromenum_set_table;+----+--------+------------------------------+|Id|Gender|Hobby|+----+--------+------------------------------+| 1 |M|Music|| 2 |F|Music,movie,footbal|| 3 |M|Music|| 4 |F|Music,movie,swimming,footbal|| 5 |F|Music,movie,swimming|+----+--------+------------------------------+5Rowsinch Set(0.00Sec

Data types in MySQL enum and set

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.