MySQL使用ENUM和SET型別

2021-09-24 21:06:10 字數 734 閱讀 8498

使用enum和set型別

概述定義資料庫列時,可以使用enum(enumeration,列舉)和set(集合)型別。通過它們,可以變通的實現check約束

它們兩者的區別是:使用enum,只能選乙個值;使用set,可以選多個值

enum和set中的值都必須是字串型別

enum列舉

列舉只能是字串列,後邊沒有資料型別

示例:create table t5 (id int,name varchar(10),*** enum(『f』,『m』)); ##設定***列不是f就是m

在內部儲存enum值時,mysql給enum中的每個值乙個順序號碼。第乙個值的順序號碼是1,第二個值的順序號碼是2,以此類推。當排序或比較enum的時候,使用這些順序號碼進行

set型別

跟enum大體相似,比較少用

示例:create table teams_new(

teamno integer not null,

playerno integer not null,

division set(『first』,『second』,『third』,『fourth』)

);insert into teams_new values(1,27,『first』);

insert into teams_new values(2,27,『first,third』);

insert into teams_new values(4,27,null);

mysql中的enum和set型別

mysql中的enum和set其實都是string型別的而且只能在指定的集合裡取值,不同的是set可以取多個值,enum只能取乙個 create table 20121101 t id int 11 not null auto increment,name varchar 20 not null,c...

mysql中的enum和set型別

mysql中的enum和set其實都是string型別的而且只能在指定的集合裡取值,不同的是set可以取多個值,enum只能取乙個 create table 20121101 t id int 11 not null auto increment,name varchar 20 not null,c...

MySQl 資料型別 ENUM 和 SET

enum,set 為 mysql 中的字串資料型別,相較於char,varchar 這類隨意插入任意字元的字串型別,enum,set 為只能在指定的集合裡取值的字串資料型別。1.單選字串資料型別 2.設定enum的時候,需要給定 固定的幾個選項 儲存的時候就只儲存其中的乙個值 欄位名 enum 選項...