ENUM类型

表结构

Table: t1
Create Table: CREATE TABLE `t1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `en` enum('a','b','c','d') DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
insert into t1 (en) values (3);

执行结果:

+----+------+
| id | en   |
+----+------+
|  1 | c    |
+----+------+

enum类型的值是字母'a','b','c','d',但是mysql在存储enum类型时,存储的是编号1,2,3,4。

insert into t1 (en) values ('a');

执行结果:

+----+------+
| id | en   |
+----+------+
|  1 | c    |
|  2 | a    |
+----+------+

直接保存'a'或者保存a的编号,都行

insert into t1 (en) values ('e');

执行结果:

+----+------+
| id | en   |
+----+------+
|  1 | c    |
|  2 | a    |
|  3 |      |
+----+------+

保存不在enum里面的值时,在非严格模式下实际保存空字符串。

select * from t1 where en="";

执行结果:

+----+------+
| id | en   |
+----+------+
|  3 |      |
+----+------+

猜你喜欢

转载自www.cnblogs.com/bibiafa/p/9186499.html
今日推荐