not null -在 enum 类型中,默认允许 null 值。为了禁止 null 值,我们需要在描述 enum 列时使用 not null 属性。null - null 属性是 default null 的同义词。 null 的索引值为 null。default - default 属性会导致 enum 数据类型在未指定值时具有默认值。换句话说,我们可以说 insert 语句不必包含该字段的值,因为如果不包含则将插入 default 后面的值。 default 表达式中不允许使用函数。对于 enum 数据类型,default 值包括 null 和空字符串 ('')。示例 以下示例将展示这些属性与 enum 数据类型的使用。
mysql> set session sql_mode = '';query ok, 0 rows affected (0.00 sec)mysql> create table enumtesting(e_value enum('a','1')not null default '1',id tinyint not null);query ok, 0 rows affected (0.23 sec)mysql>insert intoenumtesting(e_value,id)values('1','1'),('',2),(null,3),('abc',4);query ok, 4 rows affected, 3 warnings (0.09 sec)records: 4 duplicates: 0 warnings: 3mysql> show warnings;+---------+------+----------------------------------------------------+| level | code | message |+---------+------+----------------------------------------------------+| warning | 1265 | data truncated for column 'e_value' at row 2 || warning | 1048 | column 'e_value' cannot be null || warning | 1265 | data truncated for column 'e_value' at row 4 |+---------+------+----------------------------------------------------+3 rows in set (0.00 sec)mysql> select * from enumtesting;+----+---------+| e_value | id |+---------+----+| 1 | 1 || | 2 || | 3 || | 4 |+---------+----+4 rows in set (0.00 sec)mysql> insert into enumtesting(id) values(5);query ok, 1 row affected (0.11 sec)mysql> select * from enumtesting;+---------+----+| e_value | id |+---------+----+| 1 | 1 || | 2 || | 3 || | 4 || 1 | 5 |+---------+----+5 rows in set (0.00 sec)mysql> select e_value, e_value+0 as enum_index, id from enumtetsing;+---------+------------+----+| e_value | enum_index | id |+---------+------------+----+| 1 | 2 | 1|| | 0 | 2|| | 0 | 3|| | 0 | 4|| 1 | 2 | 5|+---------+------------+----+5 rows in set (0.00 sec)
以上就是mysql enum 数据类型有哪些不同的属性?的详细内容。