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