mysql 列类型

列类型ide

整型 tinyint,smallint,mediumint,int,bigint  (可选参数unsigned ,(M,zerofill,结合使用才有意义))编码

浮点型 float(可选参数D,M) decimal(可选参数D,M)ci

字符串型 char(M) varchar(M) text-文本类型资源

日期时间类型 date,time,datatime,year字符串


给表追加一个列,列类型为无符号tinyintit

alter table [vg] add [id] tinyint unsigned not null default 0;table

 

表中可存入255的idclass

insert into vgtest

(sname,id)date

values

('test',255);


#分析M参数

alter table vg add age1 tinyint(1) not null default 0;


insert into vg class (sname,age1) values ('M1',3);

insert into vg (sname,age1) values ('agin M',99);


#这时候M是没有意义的

select * from vg;

+--------+-----+-------+------+------+

| sname  | age | socre | test | age1 |

+--------+-----+-------+------+------+

| 刘备   |  28 |     0 |    0 |    0 |

| 张飞   |   0 |    -1 |    0 |    0 |

| test   |   0 |     0 |  255 |    0 |

| M1     |   0 |     0 |    0 |    3 |

| agin M |   0 |     0 |    0 |   99 |

+--------+-----+-------+------+------+


#M必须和zerofill配合才有意义


增长列

alter table vg add snum smallint(5) zerofill not null default 0;


insert into vg (sname,snum) values ('吕布',1);

insert into vg (sname,snum) values ('廖化',15);


select * from vg;

+--------+-----+-------+------+------+-------+

| sname  | age | socre | test | age1 | snum  |

+--------+-----+-------+------+------+-------+

| 刘备   |  28 |     0 |    0 |    0 | 00000 |

| 张飞   |   0 |    -1 |    0 |    0 | 00000 |

| test   |   0 |     0 |  255 |    0 | 00000 |

| M1     |   0 |     0 |    0 |    3 | 00000 |

| agin M |   0 |     0 |    0 |   99 | 00000 |

| 吕布   |   0 |     0 |    0 |    0 | 00001 |

| 廖化   |   0 |     0 |    0 |    0 | 00015 |

+--------+-----+-------+------+------+-------+


#比较上一个显示,能看出M和zerofill结合的意义

#总结:M表示补0的宽度,和zerofill结合使用才有意义


char(M) 定义以后每一个列的存储长度是固定的,M个固定编码(utf8,gbk)长度  (查询速度比较快,形成必定资源浪费)

类比于:无论远近,投币一元的公交,对短途形成浪费


varchar(M) 长度能够是(0--<M) 根据具体存入的字符个数决定(会加入一个头记录记录字符的长度,用于查找)(查询速度稍慢,资源利用率必定程度会比较高)

类比于:分段收费,须要增长一个售票员记录远近收费

相关文章
相关标签/搜索