11.3.6 The SET TypeSET类型

A SET is a string object that can have zero or more values, each of which must be chosen from a list of permitted values specified when the table is created. SET是可以有零个或多个值的字符串对象,每个值必须从创建表时指定的允许值列表中选择。SET column values that consist of multiple set members are specified with members separated by commas (,). 由多个集合成员组成的SET列值由逗号(,)分隔的成员指定。A consequence of this is that SET member values should not themselves contain commas.这样做的结果是SET成员值本身不应包含逗号。

For example, a column specified as SET('one', 'two') NOT NULL can have any of these values:例如,一个指定为SET('one', 'two') NOT NULL的列可以具有这些值中的任一种:

''
'one'
'two'
'one,two'

A SET column can have a maximum of 64 distinct members.一个SET列最多可以有64个不同的成员。

Duplicate values in the definition cause a warning, or an error if strict SQL mode is enabled.如果启用了严格SQL模式,定义中的重复值将导致警告或错误。

Trailing spaces are automatically deleted from SET member values in the table definition when a table is created.创建表时,将自动从表定义中的SET成员值中删除尾随空格。

See String Type Storage Requirements for storage requirements for the SET type.有关SET类型的存储要求,请参阅字符串类型存储要求

See Section 11.3.1, “String Data Type Syntax” for SET type syntax and length limits.有关SET类型语法和长度限制,请参阅第11.3.1节,“字符串数据类型语法”

When retrieved, values stored in a SET column are displayed using the lettercase that was used in the column definition. 检索时,存储在SET列中的值将使用列定义中使用的大小写显示。Note that SET columns can be assigned a character set and collation. 请注意,可以为SET列指定字符集和排序规则。For binary or case-sensitive collations, lettercase is taken into account when assigning values to the column.对于二进制或区分大小写的排序规则,在为列赋值时会考虑大小写。

MySQL stores SET values numerically, with the low-order bit of the stored value corresponding to the first set member. MySQL以数字形式存储SET值,存储值的低位对应于第一个集合成员。If you retrieve a SET value in a numeric context, the value retrieved has bits set corresponding to the set members that make up the column value. 如果在数字上下文中检索SET值,则检索到的值具有与构成列值的集成员相对应的位集。For example, you can retrieve numeric values from a SET column like this:例如,可以从如下所示的SET列中检索数值:

mysql> SELECT set_col+0 FROM tbl_name;

If a number is stored into a SET column, the bits that are set in the binary representation of the number determine the set members in the column value. 如果一个数字被存储到一个SET列中,则该数字的二进制表示形式中设置的位决定了列值中的集合成员。For a column specified as SET('a','b','c','d'), the members have the following decimal and binary values.对于指定为SET('a','b','c','d')的列,成员具有以下十进制和二进制值。

SET Member成员Decimal Value十进制值Binary Value二进制值
'a'10001
'b'20010
'c'40100
'd'81000

If you assign a value of 9 to this column, that is 1001 in binary, so the first and fourth SET value members 'a' and 'd' are selected and the resulting value is 'a,d'.如果将值9赋给该列,则二进制形式为1001,因此选择第一个和第四个集值成员'a''d',得到的值为'a,d'

For a value containing more than one SET element, it does not matter what order the elements are listed in when you insert the value. 对于包含多个SET元素的值,插入值时元素的排列顺序无关紧要。It also does not matter how many times a given element is listed in the value. 给定元素在值中列出多少次也无关紧要。When the value is retrieved later, each element in the value appears once, with elements listed according to the order in which they were specified at table creation time. 稍后检索该值时,该值中的每个元素都会出现一次,并根据在表创建时指定的顺序列出元素。Suppose that a column is specified as SET('a','b','c','d'):假定列被指定为SET('a','b','c','d')

mysql> CREATE TABLE myset (col SET('a', 'b', 'c', 'd'));

If you insert the values 'a,d', 'd,a', 'a,d,d', 'a,d,a', and 'd,a,d':如果插入值'a,d''d,a''a,d,d''a,d,a''d,a,d'

mysql> INSERT INTO myset (col) VALUES 
-> ('a,d'), ('d,a'), ('a,d,a'), ('a,d,d'), ('d,a,d');
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

Then all these values appear as 'a,d' when retrieved:检索时,所有这些值都显示为'a,d'

mysql> SELECT col FROM myset;
+------+
| col  |
+------+
| a,d  |
| a,d  |
| a,d  |
| a,d  |
| a,d  |
+------+
5 rows in set (0.04 sec)

If you set a SET column to an unsupported value, the value is ignored and a warning is issued:如果将SET列设置为不受支持的值,则会忽略该值并发出警告:

mysql> INSERT INTO myset (col) VALUES ('a,d,d,s');
Query OK, 1 row affected, 1 warning (0.03 sec)

mysql> SHOW WARNINGS;
+---------+------+------------------------------------------+
| Level   | Code | Message                                  |
+---------+------+------------------------------------------+
| Warning | 1265 | Data truncated for column 'col' at row 1 |
+---------+------+------------------------------------------+
1 row in set (0.04 sec)

mysql> SELECT col FROM myset;
+------+
| col  |
+------+
| a,d  |
| a,d  |
| a,d  |
| a,d  |
| a,d  |
| a,d  |
+------+
6 rows in set (0.01 sec)

If strict SQL mode is enabled, attempts to insert invalid SET values result in an error.如果启用了严格SQL模式,则尝试插入无效的SET值将导致错误。

SET values are sorted numerically. SET值按数字排序。NULL values sort before non-NULL SET values.NULL值排在非空SET值之前。

Functions such as SUM() or AVG() that expect a numeric argument cast the argument to a number if necessary. 如果需要,SUM()AVG()等需要数字参数的函数会将参数转换为数字。For SET values, the cast operation causes the numeric value to be used.对于SET值,强制转换操作将使用数值。

Normally, you search for SET values using the FIND_IN_SET() function or the LIKE operator:通常,可以使用FIND_IN_SET()函数或LIKE运算符搜索集合值:

mysql> SELECT * FROM tbl_name WHERE FIND_IN_SET('value',set_col)>0;
mysql> SELECT * FROM tbl_name WHERE set_col LIKE '%value%';

The first statement finds rows where set_col contains the value set member. 第一条语句查找set_col包含value集成员的行。The second is similar, but not the same: It finds rows where set_col contains value anywhere, even as a substring of another set member.第二种方法类似,但不相同:它可以在任意位置查找set_col包含值的行,甚至作为另一个集成员的子字符串。

The following statements also are permitted:也允许以下声明:

mysql> SELECT * FROM tbl_name WHERE set_col & 1;
mysql> SELECT * FROM tbl_name WHERE set_col = 'val1,val2';

The first of these statements looks for values containing the first set member. 第一个语句查找包含第一个集合成员的值。The second looks for an exact match. 第二种是寻找完全匹配的。Be careful with comparisons of the second type. 小心第二种类型的比较。Comparing set values to 'val1,val2' returns different results than comparing values to 'val2,val1'. 将集值与'val1,val2'进行比较会返回与将值与'val2,val1'进行比较不同的结果。You should specify the values in the same order they are listed in the column definition.您应该按照列定义中列出的顺序指定值。

To determine all possible values for a SET column, use SHOW COLUMNS FROM tbl_name LIKE set_col and parse the SET definition in the Type column of the output.要确定SET列的所有可能值,请使用SHOW COLUMNS FROM tbl_name LIKE set_col并在输出的Type列中解析SET定义。

In the C API, SET values are returned as strings. 在C API中,SET值作为字符串返回。For information about using result set metadata to distinguish them from other strings, see C API Basic Data Structures.有关使用结果集元数据将其与其他字符串区分开来的信息,请参见C API基本数据结构