MySQL Tutorial/Data Types/ENUM
Содержание
ENUM Type
The ENUM type is an enumerated list.
A column can only store one of the values that are declared in the given list.
An ENUM is a string object with a value chosen from a list of values that are enumerated at table creation time.
An enumeration value must be a quoted string literal.
mysql>
mysql> CREATE TABLE Test(
-> NY ENUM("Y","N") DEFAULT "N",
-> Size ENUM("S","M","L","XL","XXL"),
-> Color ENUM("Black","Red","White")
-> );
Query OK, 0 rows affected (0.02 sec)
mysql>
mysql> Insert into Test (NY, Size, Color) values ("Y","S","Black");
Query OK, 1 row affected (0.02 sec)
mysql>
mysql> select * from Test;
+------+------+-------+
| NY | Size | Color |
+------+------+-------+
| Y | S | Black |
+------+------+-------+
1 row in set (0.00 sec)
mysql>
mysql> drop table Test;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql>
You may have up to 65,535 items in your enumerated list.
An ENUM type must either contain a value from the list or NULL.
If you try to insert a value that is not in the list, a blank value will inserted.
ENUM with default value
mysql>
mysql> CREATE TABLE Employee (
-> ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
-> First_Name VARCHAR(20) NOT NULL,
-> Last_Name VARCHAR(30) NOT NULL,
-> Age INT,
-> Gender ENUM("M", "F") DEFAULT "F",
-> Favorite_Activity ENUM("Programming", "Eating","Biking", "Running", "None") DEFAULT "None"
-> );
Query OK, 0 rows affected (0.05 sec)
mysql>
mysql> desc Employee;
+-------------------+--------------------------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------------------------------------------------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| First_Name | varchar(20) | NO | | | |
| Last_Name | varchar(30) | NO | | | |
| Age | int(11) | YES | | NULL | |
| Gender | enum("M","F") | YES | | F | |
| Favorite_Activity | enum("Programming","Eating","Biking","Running","None") | YES | | None | |
+-------------------+--------------------------------------------------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)
mysql>
mysql> drop table Employee;
Query OK, 0 rows affected (0.00 sec)
mysql>
If you retrieve an ENUM value in a numeric context, the column value"s index is returned
SELECT enum_col+0 FROM tbl_name;
Inserting data to ENUM type column
mysql>
mysql>
mysql> CREATE TABLE Classes
-> (
-> ClassID SMALLINT NOT NULL PRIMARY KEY,
-> Dept CHAR(4) NOT NULL,
-> Level ENUM("Upper", "Lower") NOT NULL,
-> TotalStudents TINYINT UNSIGNED NOT NULL
-> );
Query OK, 0 rows affected (0.05 sec)
mysql>
mysql>
mysql> INSERT INTO Classes VALUES (1001, "ANTH", "Upper", 25),
-> (1002, "ANTH", "Upper", 25),
-> (1003, "MATH", "Upper", 18),
-> (1004, "ANTH", "Lower", 19),
-> (1005, "ENGL", "Upper", 28),
-> (1006, "MATH", "Lower", 23),
-> (1007, "ENGL", "Upper", 25),
-> (1008, "MATH", "Lower", 29),
-> (1009, "ANTH", "Upper", 25),
-> (1010, "ANTH", "Lower", 30),
-> (1011, "ENGL", "Lower", 26),
-> (1012, "MATH", "Lower", 22),
-> (1013, "ANTH", "Upper", 27),
-> (1014, "ANTH", "Upper", 21),
-> (1015, "ENGL", "Lower", 25),
-> (1016, "ENGL", "Upper", 32);
Query OK, 16 rows affected (0.01 sec)
Records: 16 Duplicates: 0 Warnings: 0
mysql>
mysql>
mysql> select * from Classes;
+---------+------+-------+---------------+
| ClassID | Dept | Level | TotalStudents |
+---------+------+-------+---------------+
| 1001 | ANTH | Upper | 25 |
| 1002 | ANTH | Upper | 25 |
| 1003 | MATH | Upper | 18 |
| 1004 | ANTH | Lower | 19 |
| 1005 | ENGL | Upper | 28 |
| 1006 | MATH | Lower | 23 |
| 1007 | ENGL | Upper | 25 |
| 1008 | MATH | Lower | 29 |
| 1009 | ANTH | Upper | 25 |
| 1010 | ANTH | Lower | 30 |
| 1011 | ENGL | Lower | 26 |
| 1012 | MATH | Lower | 22 |
| 1013 | ANTH | Upper | 27 |
| 1014 | ANTH | Upper | 21 |
| 1015 | ENGL | Lower | 25 |
| 1016 | ENGL | Upper | 32 |
+---------+------+-------+---------------+
16 rows in set (0.00 sec)
mysql>
mysql> drop table Classes;
Query OK, 0 rows affected (0.00 sec)
Possible value for a ENUM column
A column specified as ENUM("one", "two", "three") can have any of the values shown here
The index of each value is also shown:
Value Index NULL NULL "" 0 "one" 1 "two" 2 "three" 3
Use ENUM type column
mysql>
mysql> CREATE TABLE employee (
-> grp ENUM("A","B","C") NOT NULL,
-> id MEDIUMINT NOT NULL AUTO_INCREMENT,
-> name CHAR(30) NOT NULL,
-> PRIMARY KEY (grp,id)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql>
mysql> INSERT INTO employee (grp,name) VALUES ("A","A1"),
-> ("B","B1"),
-> ("C","C1"),
-> ("A","A2"),
-> ("B","B2"),
-> ("C","C2");
Query OK, 6 rows affected (0.01 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql>
mysql> SELECT * FROM employee ORDER BY grp,id;
+-----+----+------+
| grp | id | name |
+-----+----+------+
| A | 1 | A1 |
| A | 2 | A2 |
| B | 1 | B1 |
| B | 2 | B2 |
| C | 1 | C1 |
| C | 2 | C2 |
+-----+----+------+
6 rows in set (0.00 sec)
mysql>
mysql>
mysql> drop table employee;
Query OK, 0 rows affected (0.00 sec)
mysql>
Use numeric operation in ENUM type column
mysql>
mysql>
mysql> CREATE TABLE Test(
-> XY ENUM("Y","N") DEFAULT "N",
-> Size ENUM("S","M","L","XL","XXL"),
-> Color ENUM("Black","Red","White")
-> );
Query OK, 0 rows affected (0.03 sec)
mysql>
mysql> Insert into Test (XY, Size, Color) values ("Y","S","Black");
Query OK, 1 row affected (0.01 sec)
mysql>
mysql> select * from Test;
+------+------+-------+
| XY | Size | Color |
+------+------+-------+
| Y | S | Black |
+------+------+-------+
1 row in set (0.00 sec)
mysql>
mysql> select * from Test where size = 0;
Empty set (0.02 sec)
mysql>
mysql> drop table Test;
Query OK, 0 rows affected (0.00 sec)
mysql>