整形
tinyInt:1 字节
有符号:-128~127
无符号:0~255
samllInt:2 字节
mediumInt:3 字节
int:4 字节
bigInt:8 字节
使用 unsigned 声明没有符号:
有些数据不需要负值,Mysql 默认的是带符号的,将数据类型声明为无符号,就多了一个 bit ,可以表示更多的数据。
create table stu(
name varchar(10),
age int unsigned
);
显示宽度:
通过数据的显示宽度,可以达到显示长度统一的效果。
格式:类型(M) zerofill
M 代表显示的最小宽度,利用关键字 zerofill 填充 0
create table test02 (
num int(3) zerofill
);
insert into test02 values(1);
insert into test02 values(12);
insert into test02 values(123);
insert into test02 values(1234);
select * from test02;
↑ 可以看到 ,显示宽度与插入数据的大小无关,只是在显示时,把小于长度的填充 0 而已。
布尔类型:
在 MySQL 中是没有布尔类型的,可以使用 tinyInt 来代表。
1 True
0 False
小数类型
小数在计算机中的存储:
分别存储的是 精度 和 次幂,例如:
123.456 转换后 0.123456 * 10^3 实际存储的是 123456 和 3
存储的值不一定精确,取决于当前的操作系统和 CPU
浮点数类型:不能保证数据的准确性
① 单精度:float 4 个字节
② 双精度:double 8 个字节
定义方式:
type (m,n) m 代表的是总位数,n 代表的是 小数位。
create table if not exists test01 (
num float(10,5) zerofill
);
定点数 decimal:不会出现精度丢失
decimal (m,n) m 代表总位数,n 代表小数位
create table test02 (
num decimal(10,5) zerofill
);
insert into test02 values(123.123);
insert into test02 values(1234567890.123); 提示错误,超出范围
insert into test02 values(123.1234567); 小数超出范围会四舍五入
insert into test02 values(123456.123); 整数超出范围会报错
insert into test02 values(0.123123E3); 支持科学计数法
时间类型
日期类型:dateTime
格式:
dateTime YYYY-MM-DD HH:MM:SS
create table test01 (
my_time datetime
);
insert into test01 values(now()); 当前时间
insert into test01 values('2015-06-06 12:12:12'); 分隔符可以任意指定
insert into test01 values('2015:06:06 12:12:12'); 分隔符可以任意指定
时间戳类型:timestamp
使用方法和 datetime 大致相同
create table test02 (
my_time timestamp
);
insert into test02 values(now()); 当前时间
insert into test02 values('2015-06-06 12:12:12'); 分隔符可以任意指定
insert into test02 values('2015:06:06 12:12:12'); 分隔符可以任意指定
时间戳类型 timestamp 和日期类型 datetime 的区别?
① 时间戳(4 字节) 表示的时间范围比 日期类型(8 字节)的要小。
② 时间戳 可以根据时区的改变而变化,而 日期类型不会变化。
date 类型:
格式: YYYY-MM-DD
create table test03 (testTime date);
insert into test03 values('2015-1-1');
time 类型:
格式:HH:MM:SS
① 可以代表一天中的时间
create table test04 (
my_time time
);
insert into test04 values('11:22:46');
insert into test04 values('66:16:16'); 正确,代表时间间隔。H 可以超出 24,但是 M S 不可以超出 60
insert into test04 values('3 11:22:46'); 正确,3 代表天数。转化为了小时,3*24
year 类型:
代表年: 1901~2155