MySQL数据库数据类型之集合类型SET测试总结

发表于:2013-12-14来源:IT博客大学习作者:Eugene点击数: 标签:MySQL
MySQL数据库数据类型之集合类型SET测试总结。MySQL数据库提供针对字符串存储的一种特殊数据类型:集合类型SET,这种数据类型可以给予我们更多提高性能、降低存储容量和降低程序代码理解的技巧,前面介绍了首先介绍了四种数据类型的特性总结,其后又分别介绍了布尔类型BO

  【导读】

  MySQL数据库提供针对字符串存储的一种特殊数据类型:集合类型SET,这种数据类型可以给予我们更多提高性能、降低存储容量和降低程序代码理解的技巧,前面介绍了首先介绍了四种数据类型的特性总结,其后又分别介绍了布尔类型BOOL或称布尔类型BOOLEAN、枚举类型ENUM,本文我们详细介绍集合类型set测试过程与总结,加深对mysql数据库集合类型set的理解记忆。

  n 集合类型 SET

  a).数据库表mysqlops_set结构

  执行创建数据库表mysqlops_set的语句:

  root@localhost : test 05:06:13> CREATE TABLE Mysqlops_SET(ID INT NOT NULL AUTO_INCREMENT,

  -> Work_Option SET('','DBA','SA','Coding Engineer','JavaScript','NA','QA','other') NOT NULL,

  -> Work_City SET('shanghai','beijing','hangzhou','shenzhen','guangzhou','other') NOT NULL DEFAULT 'shanghai',

  -> PRIMARY KEY(ID)

  -> )ENGINE=InnoDB CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';

  Query OK, 0 rows affected (0.00 sec)

  查阅数据库中创建的mysqlops_set表的结构定义信息:

  root@localhost : test 04:33:38> SHOW CREATE TABLE Mysqlops_set\\G

  *************************** 1. row ***************************

  Table: Mysqlops_set

  Create Table: CREATE TABLE `Mysqlops_set` (

  `ID` int(11) NOT NULL AUTO_INCREMENT,

  `Work_Option` set('','DBA','SA','Coding Engineer','JavaScript','NA','QA','other') NOT NULL,

  `Work_City` set('shanghai','beijing','hangzhou','shenzhen','guangzhou','other') NOT NULL DEFAULT 'shanghai',

  PRIMARY KEY (`ID`)

  ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8

  1 row in set (0.00 sec)

  小结:

  对于集合类型字段定义,MySQL没有做任何强制性转换或修改。

  b). 写入不同类型的测试数据

  root@localhost : test 05:06:19> INSERT INTO Mysqlops_SET(ID,Work_Option,Work_City) VALUES(1,'QA','shanghai');

  Query OK, 1 row affected (0.00 sec)

  root@localhost : test 05:06:26> INSERT INTO Mysqlops_SET(ID,Work_Option,Work_City) VALUES(2,'NA','');

  Query OK, 1 row affected (0.00 sec)

  root@localhost : test 05:06:33> INSERT INTO Mysqlops_SET(ID,Work_Option,Work_City) VALUES(3,'Other',NULL);

  ERROR 1048 (23000): Column 'Work_City' cannot be null

  root@localhost : test 05:06:47> INSERT INTO Mysqlops_SET(ID,Work_Option,Work_City) VALUES(3,'','hangzhou');

  Query OK, 1 row affected (0.00 sec)

  root@localhost : test 05:06:55> INSERT INTO Mysqlops_SET(ID,Work_City) VALUES(4,'ningbo');

  Query OK, 1 row affected, 2 warnings (0.00 sec)

  root@localhost : test 05:07:09> SHOW WARNINGS;

  +---------+------+--------------------------------------------------+

  | Level | Code | Message |

  +---------+------+--------------------------------------------------+

  | Warning | 1364 | Field 'Work_Option' doesn't have a default value |

  | Warning | 1265 | Data truncated for column 'Work_City' at row 1 |

  +---------+------+--------------------------------------------------+

  2 rows in set (0.00 sec)

  root@localhost : test 05:07:19> INSERT INTO Mysqlops_SET(ID,Work_Option) VALUES(5,'DBA');

  Query OK, 1 row affected (0.00 sec)

  root@localhost : test 03:06:01> INSERT INTO Mysqlops_SET(ID,Work_Option,Work_City) VALUES(6,'DBA','shanghai');

  Query OK, 1 row affected (0.00 sec)

  root@localhost : test 03:06:10> INSERT INTO Mysqlops_SET(ID,Work_Option,Work_City) VALUES(7,'DBA,SA','shanghai,beijing');

  Query OK, 1 row affected (0.00 sec)

  root@localhost : test 03:06:18> INSERT INTO Mysqlops_SET(ID,Work_Option,Work_City) VALUES(8,'DBA,SA,NA','shanghai,beijing,hangzhou');

  Query OK, 1 row affected (0.00 sec)

  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');

  Query OK, 1 row affected (0.00 sec)

  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');

  Query OK, 1 row affected, 1 warning (0.00 sec)

  root@localhost : test 03:18:27> show warnings;

  +---------+------+------------------------------------------------+

原文转自:http://blogread.cn/it/article/5114