|
MySQL数据库提供针对字符串存储的一种特殊数据类型:集合类型SET,这种数据类型可以给予我们更多提高性能、降低存储容量和降低程序代码理解的技巧。
集合类型 SET
a).数据库表mysqlops_set结构
执行创建数据库表mysqlops_set的语句:
- <div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">root@localhost : test 05:06:13> CREATE TABLE Mysqlops_SET(ID INT NOT NULL AUTO_INCREMENT,</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">-> Work_Option SET(’’,‘DBA’,‘SA’,‘Coding Engineer’,‘JavaScript’,‘NA’,‘QA’,‘other’) NOT NULL,</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">-> Work_City SET(‘shanghai’,‘beijing’,‘hangzhou’,‘shenzhen’,‘guangzhou’,‘other’) NOT NULL DEFAULT ‘shanghai’,</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">-> PRIMARY KEY(ID)</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">-> )ENGINE=InnoDB CHARACTER SET ‘utf8’ COLLATE ‘utf8_general_ci’;</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">Query OK, 0 rows affected (0.00 sec)</span></font></div>
复制代码
查阅数据库中创建的mysqlops_set表的结构定义信息:
- <div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">root@localhost : test 04:33:38> SHOW CREATE TABLE Mysqlops_set\G</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">*************************** 1. row ***************************</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">Table: Mysqlops_set</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">Create Table: CREATE TABLE Mysqlops_set (</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">ID int(11) NOT NULL AUTO_INCREMENT,</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">Work_Option set(’’,‘DBA’,‘SA’,‘Coding Engineer’,‘JavaScript’,‘NA’,‘QA’,‘other’) NOT NULL,</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">Work_City set(‘shanghai’,‘beijing’,‘hangzhou’,‘shenzhen’,‘guangzhou’,‘other’) NOT NULL DEFAULT ‘shanghai’,</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">PRIMARY KEY (ID)</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">1 row in set (0.00 sec)</span></font></div>
复制代码
小结:
对于集合类型字段定义,MySQL没有做任何强制性转换或修改。
b). 写入不同类型的测试数据
- <div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">root@localhost : test 05:06:19> INSERT INTO Mysqlops_SET(ID,Work_Option,Work_City) VALUES(1,‘QA’,‘shanghai’);</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">Query OK, 1 row affected (0.00 sec)</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">
- </span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">root@localhost : test 05:06:26> INSERT INTO Mysqlops_SET(ID,Work_Option,Work_City) VALUES(2,‘NA’,’’);</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">Query OK, 1 row affected (0.00 sec)</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">
- </span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">root@localhost : test 05:06:33> INSERT INTO Mysqlops_SET(ID,Work_Option,Work_City) VALUES(3,‘Other’,NULL);</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">ERROR 1048 (23000): Column ‘Work_City’ cannot be null</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">
- </span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">root@localhost : test 05:06:47> INSERT INTO Mysqlops_SET(ID,Work_Option,Work_City) VALUES(3,’’,‘hangzhou’);</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">Query OK, 1 row affected (0.00 sec)</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">
- </span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">root@localhost : test 05:06:55> INSERT INTO Mysqlops_SET(ID,Work_City) VALUES(4,‘ningbo’);</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">Query OK, 1 row affected, 2 warnings (0.00 sec)</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">
- </span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">root@localhost : test 05:07:09> SHOW WARNINGS;</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">±--------±-----±-------------------------------------------------+</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">| Level | Code | Message |</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">±--------±-----±-------------------------------------------------+</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">| Warning | 1364 | Field ‘Work_Option’ doesn’t have a default value |</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">| Warning | 1265 | Data truncated for column ‘Work_City’ at row 1 |</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">±--------±-----±-------------------------------------------------+</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">2 rows in set (0.00 sec)</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">
- </span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">root@localhost : test 05:07:19> INSERT INTO Mysqlops_SET(ID,Work_Option) VALUES(5,‘DBA’);</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">Query OK, 1 row affected (0.00 sec)</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">
- </span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">root@localhost : test 03:06:01> INSERT INTO Mysqlops_SET(ID,Work_Option,Work_City) VALUES(6,‘DBA’,‘shanghai’);</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">Query OK, 1 row affected (0.00 sec)</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">
- </span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">root@localhost : test 03:06:10> INSERT INTO Mysqlops_SET(ID,Work_Option,Work_City) VALUES(7,‘DBA,SA’,‘shanghai,beijing’);</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">Query OK, 1 row affected (0.00 sec)</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">
- </span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">root@localhost : test 03:06:18> INSERT INTO Mysqlops_SET(ID,Work_Option,Work_City) VALUES(8,‘DBA,SA,NA’,‘shanghai,beijing,hangzhou’);</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">Query OK, 1 row affected (0.00 sec)</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">
- </span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">root@localhost : test 03:06:12> INSERT INTO Mysqlops_SET(ID,Work_Option,Work_City) VALUES(9,‘DBA,SA,NA’,‘shanghai,beijing,hangzhou,shenzhen,guangzhou,other’);</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">Query OK, 1 row affected (0.00 sec)</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">
- </span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">root@localhost : test 03:18:25> INSERT INTO Mysqlops_SET(ID,Work_Option,Work_City) VALUES(20,‘DBA,SA,NA’,‘shanghai,beijing,hangzhou!shenzhen!guangzhou!other’);</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">Query OK, 1 row affected, 1 warning (0.00 sec)</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">
- </span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">root@localhost : test 03:18:27> show warnings;</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">±--------±-----±-----------------------------------------------+</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">| Level | Code | Message |</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">±--------±-----±-----------------------------------------------+</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">| Warning | 1265 | Data truncated for column ‘Work_City’ at row 1 |</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">±--------±-----±-----------------------------------------------+</span></font></div><div><font color="#4d4d4d" face="Microsoft YaHei, SF Pro Display, Roboto, Noto, Arial, PingFang SC, sans-serif"><span style="font-size: 16px; font-variant-ligatures: common-ligatures;">1 row in set (0.00 sec)</span></font></div>
复制代码
小结:
I.集合类型SET字段即使没有定义空格字符串作为元素,也会默认成为其中一个组成元素;
II.集合类型SET字段不允许为NULL时,向其写入NULL值会报错,导致SQL执行失败;
III.集合类型SET字段不允许为NULL且无显式申明默认值时,未集合类型字段给出值的INSERT操作,会出现警告信息,提示字段值阶段,并且用空字符串值替代,SQL语句执行成功;
IV.集合类型SET字段的值域列表中有空字符串元素时,决断用空字符串值替代的记录,与显式写入空字符串值的序列编号不同,前者序号为0,后者序号为值域列表中真实的顺序;
V.向集合类型SET字段写入一个值域列表中,不存在的值,会发生字段值截断,并且用空格字符串替代,SQL语句执行成功;
VI.集合类型SET字段值域列表中任意元素的组合,只要用逗号分隔,就是合法的值;
VII.集合类型SET字段值域列表中任意元素的组合时,若部分元素的值没有用逗号分隔,或者部分不是值域列表中元素值或组合,则会把非法的部分截断掉,并且给出警告信息,SQL语句执行成功;
|
|