enum类型占用几个字节?

netxuning 2009-12-16 05:20:23
enum('男', '女');
...全文
1659 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
kazake 2012-08-08
  • 打赏
  • 举报
回复
说的不完全正确,在一张表中enum类型应该是占用1个或2个字节,取决于此表中enum类型的个数,enum类型字段占用的总字节数应该等于enum类型数量+1个字节。
ACMAIN_CHM 2009-12-16
  • 打赏
  • 举报
回复
除了固定的字段会占用空间,另外还有一些其它记录用的信息需要存储。

The expected row length for dynamic-sized rows is calculated using the following expression:

3
+ (number of columns + 7) / 8
+ (number of char columns)
+ (packed size of numeric columns)
+ (length of strings)
+ (number of NULL columns + 7) / 8

ACMAIN_CHM 2009-12-16
  • 打赏
  • 举报
回复
mysql> create table tbl(gender enum('男', '女')) engine=myisam;
Query OK, 0 rows affected (0.06 sec)

mysql> insert into tbl(gender) values('男'),( '男'), ('女'),( '女');
Query OK, 4 rows affected (0.05 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql> show table status like 'tbl'\G
*************************** 1. row ***************************
Name: tbl
Engine: MyISAM
Version: 10
Row_format: Fixed
Rows: 4
Avg_row_length: 7
Data_length: 28
Max_data_length: 1970324836974591
Index_length: 1024
Data_free: 0
Auto_increment: NULL
Create_time: 2009-12-16 19:40:28
Update_time: 2009-12-16 19:40:32
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)

mysql> create table tbl1(gender SMALLINT) engine=myisam;
Query OK, 0 rows affected (0.06 sec)

mysql> insert into tbl1 values(1),(2),(3),(4);
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql> show table status like 'tbl1'\G
*************************** 1. row ***************************
Name: tbl1
Engine: MyISAM
Version: 10
Row_format: Fixed
Rows: 4
Avg_row_length: 7
Data_length: 28
Max_data_length: 1970324836974591
Index_length: 1024
Data_free: 0
Auto_increment: NULL
Create_time: 2009-12-16 19:42:31
Update_time: 2009-12-16 19:42:34
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)

mysql>
netxuning 2009-12-16
  • 打赏
  • 举报
回复
做了个实验,好像是7个字节:

create table(gender enum('男', '女'));
insert into tbl(gender) values('男', '男', '女', '女');
mysql> SHOW TABLE STATUS LIKE 'tbl'\G
*************************** 1. row ***************************
Name: tbl
Engine: MyISAM
Version: 10
Row_format: Fixed
Rows: 4
Avg_row_length: 7
Data_length: 28
Max_data_length: 1970324836974591
Index_length: 1024
Data_free: 0
Auto_increment: NULL
Create_time: 2009-12-16 17:09:48
Update_time: 2009-12-16 17:13:29
Check_time: NULL
Collation: gb2312_chinese_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)


Rows: 4
Avg_row_length: 7
ACMAIN_CHM 2009-12-16
  • 打赏
  • 举报
回复
2 Bytes

57,063

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧