Versions available for this page: CUBRID 9.0.0 |
ENUM type column is handled as a number and considered as an index number value, which corresponds to the ENUM type when the compared value is CHAR/VARCHAR in the query.
<enum_type>
: ENUM '(' <char_string_literal_list> ')'
<char_string_literal_list>
: <char_string_literal_list> ',' CHAR_STRING
| CHAR_STRING
The following example shows the definition of the ENUM column.
CREATE TABLE tbl (
color ENUM('red', 'yellow', 'blue')
);
The color column can have one of following values:
|
Value |
Index Number |
|---|---|
|
NULL |
NULL |
|
'red' |
1 |
|
'yellow' |
2 |
|
'blue' |
3 |
The following example shows the insertion of a value into the ENUM column.
INSERT into tbl values ('yellow'), ('red'), (2), ('blue');
The following example shows the SELECT statement that retrieves the ENUM column where the value has been inserted in the above.
SELECT color FROM tbl;
color
======================
yellow
red
yellow
blue
SELECT color FROM tbl ORDER BY color ASC;
color
======================
red
yellow
yellow
blue
SELECT color FROM tbl ORDER BY cast(color as char) ASC;
color
======================
blue
red
yellow
yellow
SELECT CONCAT(enum_col, 'color') FROM tbl_name;
CONCAT(color, '_color')
======================
yellow_color
red_color
yellow_color
blue_color
SELECT color + 0 FROM tb;
color + 0
======================
2
1
2
3
-- will use the ENUM index value because it is compared with a number
SELECT color FROM tbl WHERE color <= 1;
color
======================
red
-- will use the ENUM char literal value because it is compared with a CHAR type
SELECT color FROM tbl WHERE color <= 'red';
color
======================
red
blue
CREATE TABLE tb2 (nums enum('0', '1', '2'));
INSERT INTO tb2 (nums) VALUES(1),('1'),('3');
SELECT * FROM tb2;
nums
======================
0
1
2
SELECT color FROM tb ORDER BY cast(color as char) ASC;
|
Type |
Value (Index Number/String) |
|---|---|
|
*SHORT |
Index Number |
|
*INTEGER |
Index Number |
|
*BIGINT |
Index Number |
|
*FLOAT |
Index Number |
|
*DOUBLE |
Index Number |
|
*NUMERIC |
Index Number |
|
*MONETARY |
Index Number |
|
*TIME |
String |
|
*DATE |
String |
|
*DATETIME |
String |
|
*TIMESTAMP |
String |
|
*CHAR |
String |
|
*VARCHAR |
String |
|
BIT |
String |
|
VARBIT |
String |
SELECT * FROM tb WHERE color=0;
|
Type 1 |
Type 2 |
Result Type |
|---|---|---|
|
SHORT |
ENUM |
SHORT |
|
INTEGER |
ENUM |
INTEGER |
|
BIGINT |
ENUM |
BIGINT |
|
FLOAT |
ENUM |
FLOAT |
|
DOUBLE |
ENUM |
DOUBLE |
|
NUMERIC |
ENUM |
NUMERIC |
|
MONETARY |
ENUM |
MONETARY |
|
TIME |
ENUM |
TIME |
|
DATE |
ENUM |
DATE |
|
DATETIME |
ENUM |
DATETIME |
|
TIMESTAMP |
ENUM |
TIMESTAMP |
|
CHAR |
ENUM |
CHAR |
|
VARCHAR |
ENUM |
VARCHAR |
The ENUM type is not specially mapped to various drivers such as JDBC and CCI. Therefore, application developers can use the STRING type as they have used. The following example shows the JDBC application.
Statement stmt = connection.createStatement(¡°SELECT color FROM tbl¡±);
ResultSet rs = stmt.executeQuery();
while(rs.next()){
System.out.println(rs.getString());
}
The following example shows the CCI application.
req_id = cci_prepare (conn, ¡°SELECT color FROM tbl¡±, 0, &err);
error = cci_execute (req_id, 0, 0, &err);
if (error < CCI_ER_NO_ERROR)
{
/* handle error */
}
error = cci_cursor (req_id, 1, CCI_CURSOR_CURRENT, &err);
if (error < CCI_ER_NO_ERROR)
{
/* handle error */
}
error = cci_fetch (req_id, &err);
if (error < CCI_ER_NO_ERROR)
{
/* handle error */
}
cci_get_data (req, idx, CCI_A_TYPE_STR, &data, 1);
CREATE TABLE tb (
color ENUM('red', CONCAT('light ','gray'), 'blue')
);