| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
SET Type A SET is a string object that can have zero or more values, each of
which must be chosen from a list of allowed values specified when the table
is created. SET column values that consist of multiple set members
are specified with members separated by commas (`,'). A consequence of
this is that SET member values cannot themselves contain commas.
For example, a column specified as SET('one', 'two') NOT NULL can have
any of these values:
'' 'one' 'two' 'one,two' |
A SET can have a maximum of 64 different members.
Starting from 3.23.51, trailing spaces are automatically deleted from
SET values when the table is created.
MySQL stores SET values numerically, with the low-order bit
of the stored value corresponding to the first set member. 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. For example,
you can retrieve numeric values from a SET column like this:
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. For a column specified as
SET('a','b','c','d'), the members have the following decimal and
binary values:
| Decimal Value | Binary Value |
| | |
| | |
| | |
| | |
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'.
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. 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 will appear
once, with elements listed according to the order in which they were
specified at table creation time. If a column is specified as
SET('a','b','c','d'), then 'a,d', 'd,a', and
'd,a,a,d,d' all will appear as 'a,d' when retrieved.
If you set a SET column to an unsupported value, the value will
be ignored.
SET values are sorted numerically. NULL values sort before
non-NULL SET values.
Normally, you perform a SELECT on a SET column using
the FIND_IN_SET() function or the LIKE operator:
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. 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.
The following statements also are legal:
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' will
return different results than comparing values to 'val2,val1'.
You should specify the values in the same order they are listed in the
column definition.
If you want 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 second column of the output.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on July, 26 2004 using texi2html 1.70.