mysql 列类型
2016-01-06 11:59
645 查看
列类型
整型 tinyint,smallint,mediumint,int,bigint (可选参数unsigned ,(M,zerofill,结合使用才有意义))
浮点型 float(可选参数D,M) decimal(可选参数D,M)
字符串型 char(M) varchar(M) text-文本类型
日期时间类型 date,time,datatime,year
给表追加一个列,列类型为无符号tinyint
alter table [vg] add [id] tinyint unsigned not null default 0;
表中可存入255的id
insert into vg
(sname,id)
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) 根据具体存入的字符个数决定(会加入一个头记录记录字符的长度,用于查找)(查询速度稍慢,资源利用率一定程度会比较高)
类比于:分段收费,需要增加一个售票员记录远近收费
本文出自 “代码易” 博客,请务必保留此出处http://codeyi.blog.51cto.com/11082384/1732011
整型 tinyint,smallint,mediumint,int,bigint (可选参数unsigned ,(M,zerofill,结合使用才有意义))
浮点型 float(可选参数D,M) decimal(可选参数D,M)
字符串型 char(M) varchar(M) text-文本类型
日期时间类型 date,time,datatime,year
给表追加一个列,列类型为无符号tinyint
alter table [vg] add [id] tinyint unsigned not null default 0;
表中可存入255的id
insert into vg
(sname,id)
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) 根据具体存入的字符个数决定(会加入一个头记录记录字符的长度,用于查找)(查询速度稍慢,资源利用率一定程度会比较高)
类比于:分段收费,需要增加一个售票员记录远近收费
本文出自 “代码易” 博客,请务必保留此出处http://codeyi.blog.51cto.com/11082384/1732011
相关文章推荐
- mysql出现错误“ Every derived table must have its own alias”
- mysql 日期和时间格式转换实现语句
- mysql 表的增删改
- MYSQL数据库导入SQL文件出现乱码
- mysql 基本操作
- MySQL 之 from_unixtime()
- 编译安装MYSQL5.6.26--【动手实验】
- mysql压缩包安装后修改初始密码
- MySQL数据库三种常用存储引擎特性对比
- 你真的了解 MySQL 数据库的运行状况吗?
- MySQL存储过程调试工具-dbForge Studio for MySQL
- 你真的了解 MySQL 数据库的运行状况吗?
- 你真的了解 MySQL 数据库的运行状况吗?
- 你真的了解 MySQL 数据库的运行状况吗?
- MySQL做为手动开启事务用法
- mysqldump常用参数
- MySQL解决[Err] 1206 - The total number of locks exceeds the lock table size问题
- MYSQL 查询数据
- MySQL中添加唯一约束和联合唯一约束
- MAMP添加mysql的环境变量