Enumeration in the MySQL Field

Source: Internet
Author: User

I believe everyone has some knowledge about the MySQL field. The following describes the enumeration in the MySQL field. I hope it will be helpful for you to learn about the MySQL field.

Enumeration in the MySQL field:

Mysql> create table meiju (f1 enum ('1', '2', '3', '4', '5', '6 '));
Query OK, 0 rows affected (0.92 sec)

Mysql> desc meiju;
+ ------- + ------------------------------- + ------ + ----- + --------- + ------- +
| Field | Type | Null | Key | Default | Extra |
+ ------- + ------------------------------- + ------ + ----- + --------- + ------- +
| F1 | enum ('1', '2', '3', '4', '5', '6') | YES | NULL |
+ ------- + ------------------------------- + ------ + ----- + --------- + ------- +

Mysql> insert into meiju values (2 );

Mysql> insert into meiju values ("2 ");

Mysql> insert into meiju values ("6 ");

1 2 3 4 5

The content cannot be inserted without serial numbers.

Mysql> insert into meiju values (8 );
ERROR 1265 (01000): Data truncated for column 'f1' at row 1
Mysql> insert into meiju values ("8 ");
ERROR 1265 (01000): Data truncated for column 'f1' at row 1

Mysql> select * from meiju;
+ ------ +
| F1 |
+ ------ +
| 1 |
| 2 |
| 2 |
| 6 |
+ ------ +

Change table

Mysql> alter table meiju modify f1 enum ("a", "B", "c", "d", "e", "f ");
Query OK, 4 rows affected (0.39 sec)
Records: 4 Duplicates: 0 Warnings: 0

The record can be automatically matched with the new field value when the serial number (amazing)

Mysql> select * from meiju;
+ ------ +
| F1 |
+ ------ +
| A |
| B |
| B |
| F |
+ ------ +

One record can only have one enumerated value, unlike set.

Mysql> insert into meiju values (7 );
ERROR 1265 (01000): Data truncated for column 'f1' at row 1

Note that the value should be enclosed in quotation marks. Otherwise, it should be processed as a serial number.

When the number with quotation marks does not meet the enumeration requirements, it is not appropriate to use it as the serial number.

For example, insert into meiju values ("7 ");

7. If 7th values are not included in the enumerated values, if no seven values exist, an error is returned.

Differences between set and enumeration

1. The set can have 64 values, and 65535 can be enumerated.

2. The serial number of the set is 1 2 4 8 16 enumeration is 1 2 3 4 5 6

3. A set of Field Values can have several values.

+ ------- +
| F1 |
+ ------- +
| F, m |
+ ------- +

But only one enumeration can be
 

Add and delete fields in MySQL

MySQL Field Type

Mysql foreign Key Usage Analysis

How MySQL defines foreign keys

Seven considerations for converting MySQL to Oracle

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.