MySQL数据类型
字段类型
字符串类型
varchar类型
可变长度字符串,必须设定其长度,最大长度 理论值 65535 实际 65533,但考虑到存储字符编码,也会有进一步减少,比如
存储中文gbk,最多 65533/2
中utf8------>65533/3
例1 《php中文网中留言板》中title设置
title varchar(255) NOT NULL,
例2 京东商品属性对应表
attr_value varchar(255) not null default '' comment '属性值',
例3 京东商品相册表
img_url varchar(50) not null default '' comment '图片URL',
thumb_url varchar(50) not null default '' comment '缩略图URL',
img_desc varchar(50) not null default '' comment '图片描述',
例4 用户表
email varchar(50) not null default '' comment '电子邮箱',
例5 用户收货地址表
consignee varchar(60) not null default '' comment '收货人姓名',
例6 电子邮箱
email varchar(50) not null default '' comment '电子邮箱',
char类型
定长字符串:使用时通常需要设定其长度,如果不设定,默认为1,最大理论长度 255个
可见的明确的固定长度的字符
比如 手机号 中国邮政编码 身份证号码 银行卡号
例1
create table tab_char(
postcode char(6),
name varchar(10)
);
例2 用户表
password char(32) not null default '' comment '用户密码,md5加密',
enum类型
单选项字符串数据类型。它适合于存储表单界面中的“单选项值”,
它设定的时候,是需要给定“固定的几个选项”,然后存储的时候,就只存储其中一个值;
形式如下:
enum ("选项1","选项2","选项3",.......);
实际内部
这些字符串选项值对应的是数字值:1,2,3,4,5.............最多65535个选项
例1 商品分类表中是否上架
is_on_sale enum('是','否') not null default '是' comment '是否上架',
set 类型
多选项字符串数据类型。它非常适合于存储表单界面的“多选项值”
它设定的时候,也需要给定“固定的几个选项”,然后存储的时候,就可以存储其中若干个值
形式如下:
set("选项1","选项2")
create table enum_set(
id int auto_increment primary key,
sex enum('男‘,'女'),
fav set('篮球','排球','足球','中国足球','台球')
);
例2 insert into eum_set(id,sex,fav) values(null,'男','篮球');
例3 insert into eum_set(id,sex,fav) values(null,1,1);
例4 insert into eum_set(id,sex,fav) values(null,"男","篮球,排球,足球");
等价于 insert into eum_set(id,sex,fav) values(null,"男",19);
篮球:1
排球:2
台球:16
查看结果
text类型
长文本字符类型,不占据表格中数据限制
一个表格最多存储:65535
其他同类字符类型 smalltext tinytext longtext
例1 商品描述
goods_desc longtext comment '商品描述',
其他
binary 二进制字符串类型
varbinary 二进制字符串类型
blob 二进制数字类型适应于图片,其他文件
时间类型
datetime类型
时间日期
date类型
日期
创建后台管理员表:
time类型
时间
year类型
年份
在应用中,时间日期类型,在我们自己给定的数据情形下,需要使用”单引号引起“
timestamp
时间戳”一个时间的数据值,
类比js:getTime();
意思:秒数
得到的结果:数字
重要作用:自动获得时间戳---相当于now()
演示时间日期类型字段使用
create table tab_time(
dt datetime,
d2 date,
t2 time,
y year,
ts timestamp
);
insert into tab_time(dt,d2,t2,y) values('2015-7-8 10:12:30','2015/7/8','13:14:15','2014');
例1 添加当前时间
insert into tab_time(dt,d2,t2,y) values(now(),now(),now(),now());
select*from tab_time;
添加时间
addtime datetime not null comment '添加时间',