| Level | Code | Message |
+---------+------+------------------------------------------------+
| Warning | 1265 | Data truncated for column 'Work_City' at row 1 |
+---------+------+------------------------------------------------+
1 row in set (0.00 sec)
小结:
集合类型SET字段即使没有定义空格字符串作为元素,也会默认成为其中一个组成元素;
集合类型SET字段不允许为NULL时,向其写入NULL值会报错,导致SQL执行失败;
集合类型SET字段不允许为NULL且无显式申明默认值时,未集合类型字段给出值的INSERT操作,会出现警告信息,提示字段值阶段,并且用空字符串值替代,SQL语句执行成功;
集合类型SET字段的值域列表中有空字符串元素时,决断用空字符串值替代的记录,与显式写入空字符串值的序列编号不同,前者序号为0,后者序号为值域列表中真实的顺序;
向集合类型SET字段写入一个值域列表中,不存在的值,会发生字段值截断,并且用空格字符串替代,SQL语句执行成功;
集合类型SET字段值域列表中任意元素的组合,只要用逗号分隔,就是合法的值;
集合类型SET字段值域列表中任意元素的组合时,若部分元素的值没有用逗号分隔,或者部分不是值域列表中元素值或组合,则会把非法的部分截断掉,并且给出警告信息,SQL语句执行成功;
c). 查询数据库表mysqlops_SET的数据
root@localhost : test 03:18:31> select * from mysqlops_SET;
+----+-----------------+----------------------------------------------------+
| ID | Work_Option | Work_City |
+----+-----------------+----------------------------------------------------+
| 1 | QA | shanghai |
| 2 | NA | |
| 3 | | hangzhou |
| 4 | | |
| 5 | DBA | shanghai |
| 6 | DBA | shanghai |
| 7 | DBA,SA | shanghai,beijing |
| 8 | DBA,SA,NA | shanghai,beijing,hangzhou |
| 9 | DBA,SA,NA | shanghai,beijing,hangzhou,shenzhen,guangzhou,other |
| 10 | Coding Engineer | |
| 11 | Coding Engineer | shanghai |
| 12 | | shanghai |
| 13 | | hangzhou |
| 20 | DBA,SA,NA | shanghai,beijing |
+----+-----------------+----------------------------------------------------+
14 rows in set (0.00 sec)
root@localhost : test 03:07:02> SELECT * FROM mysqlops_set WHERE Work_Option=0;
+----+-------------+-----------+
| ID | Work_Option | Work_City |
+----+-------------+-----------+
| 3 | | hangzhou |
| 4 | | |
+----+-------------+-----------+
2 rows in set (0.00 sec)
root@localhost : test 03:07:42> SELECT * FROM mysqlops_set WHERE Work_Option=1;
Empty set (0.00 sec)
root@localhost : test 03:07:44> SELECT * FROM mysqlops_set WHERE Work_Option=2;
+----+-------------+-----------+
| ID | Work_Option | Work_City |
+----+-------------+-----------+
| 5 | DBA | shanghai |
| 6 | DBA | shanghai |
+----+-------------+-----------+
2 rows in set (0.00 sec)
root@localhost : test 03:08:09> SELECT * FROM mysqlops_set WHERE Work_Option=6;
+----+-------------+------------------+
| ID | Work_Option | Work_City |
+----+-------------+------------------+
| 7 | DBA,SA | shanghai,beijing |
+----+-------------+------------------+
1 row in set (0.00 sec)
root@localhost : test 03:10:04> SELECT * FROM mysqlops_set WHERE Work_City=0;
+----+-------------+-----------+
| ID | Work_Option | Work_City |
+----+-------------+-----------+
| 2 | NA | |
| 4 | | |
+----+-------------+-----------+
2 rows in set (0.00 sec)
root@localhost : test 03:10:18> SELECT * FROM mysqlops_set WHERE Work_City=1;
+----+-------------+-----------+
| ID | Work_Option | Work_City |
+----+-------------+-----------+
| 1 | QA | shanghai |
| 5 | DBA | shanghai |
| 6 | DBA | shanghai |
+----+-------------+-----------+
3 rows in set (0.00 sec)
root@localhost : test 03:10:20> SELECT * FROM mysqlops_set WHERE Work_City=2;
Empty set (0.00 sec)
root@localhost : test 03:10:22> SELECT * FROM mysqlops_set WHERE Work_City=3;
+----+-------------+------------------+
| ID | Work_Option | Work_City |
+----+-------------+------------------+
| 7 | DBA,SA | shanghai,beijing |
+----+-------------+------------------+
1 row in set (0.00 sec)
root@localhost : test 03:10:24> SELECT * FROM mysqlops_set WHERE Work_City=4;
原文转自:http://blogread.cn/it/article/5114