mysql中枚舉類型之enum詳解

來源:互聯網
上載者:User

對於255至65535個成員,需要2個位元組儲存。最多允許有65535個成員。
先通過sql語句建立一個具有枚舉類型的資料表。

 代碼如下

create table user_sex( sex enum('M','F'));
 

再往表中寫入幾條測試資料:


insert into user_sex values('M'),('s'),('2'),('f');

 


在表中查看寫入的結果時,發現變成了這樣M,M,M,F(注意每個字元為一條記錄)。由結果可知enum類型是忽略大小寫,'f'被自動轉化成了'F',對寫入了不在指定範圍內的值時,會強制寫入枚舉裡的第一個值(M)。另外,enum類型只從填集合中選取單個值,而不能取多個值。

ENUM測試總結

n  枚舉類型ENUM

a).資料庫表mysqlops_enum結構
執行資料庫表mysqlops_enum建立的SQL語句:

假設表: 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類型定義好以後 操作跟 字串一樣


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.