mysql教程 sql enum数据类型插入,更新,删除操作
enum列总有1个默认值。如果未指定默认值,对于包含null的列,默认值为null;否则,第1个枚举值将被当作默认值。
如果在enum列中插入了不正确的值,或者,如果使用ignore将值强制插入了enum列,会将其设置为保留的枚举值0,对于字符串情形,将显示为空字符串。
如果在set列中插入了不正确值,该值将被忽略。例如,如果列能包含值“a”、“b”和“c”,并赋值“a,x,b,y”,结果为“a,b”。
假设表: xxx 中有一字段 folder 类型为enum('inbox','outbox','other')
保存
insert into `xxx` (`folder`) values ('inbox');
insert into `xxx` (`folder`) values ('outbox');
insert into `xxx` (`folder`) values ('other');
更新
update `xxx` set `folder` = 'inbox';
update `xxx` set `folder` = 'outbox';
update `xxx` set `folder` = 'other';
删除
delete from `xxx` where `folder` = 'inbox';
delete from `xxx` where `folder` = 'outbox';
delete from `xxx` where `folder` = 'other';
enum类型定义好以后 操作跟 字符串一样.只是mysql处理的时候是用整形来操作..比操作字串快很多而已
看个实例
create table employee_person (
id int unsigned not null primary key,
address varchar(60),
phone int,
email varchar(60),
birthday date,
sex enum('m', 'f'),
m_status enum('y','n'),
s_name varchar(40),
children int
);
insert into employee_person (id, address, phone, email, birthday, sex, m_status, s_name) values (1, '200, regina street', 7176666, '[email protected]', '1971-04-26', 'm', 'y', 'ane regina');
insert into employee_person (id, address, phone, email, birthday, sex, m_status, s_name, children) values (2, '1232 alberta road', 5553312, '[email protected]', '1968-03-02', 'm', 'y', 'jane van', 3);