数值类型
整数类型 | 字节 | 最小值 | 最大值 |
---|---|---|---|
tinyint | 1 | -128/0 | 127/255 |
mallint | 2 | ||
ediumint | 3 | ||
int/integer | 4 | ||
Bigint | 8 |
- 正数类型可以显示指定宽度,比如
int(5)
表示在当前数值小于5位时在前面填满宽度,一般与zerofill
配合使用,默认为int(11)。某列使用了zerofill
将会自动添加unsigned
属性。 auto_increment
:自增,一般从1开始,每次自增1(从当前最大的值开始)。对于任意想要使用auto_increment
的列应该定义为not null
,并定义为primary key
或unique
键。create table t1 (id int auto_increment not null primary key);
create table t2 (id int auto_increment not null, primary key(id));
create table t3 (id int auto_increment not null, unique(id));
浮点数类型 | 字节 | 最小值 | 最大值 |
---|---|---|---|
float | 4 | ||
double | 8 |
- 浮点数不写精度按照实际精度值显示。
定点数类型 | 字节 | 描述 |
---|---|---|
dec(M,D) / decimal(M,D) | M+2 | 最大值和Double一样,有效取值范围由M和D决定 |
- M,D成为精度和标度。表示一共显示M位数字(整数位和小数位),其中D位位于小数点后面。
- 定点数在MySQL内部以字符串形式存放,比浮点数更精确,适合用于货币等精度高的数据。
- 浮点数后面跟(M,D)是非标准用法,如果要进行数据库迁移,最好不要这么用。
- 默认值为(10,0),插入时四舍五入。
位类型 | 字节 | 最小值 | 最大值 |
---|---|---|---|
bit(M) | 1 ~ 8 | bit(1) | bit(64) |
- 查找时,直接select *会显示null,需要用
bin()
或者hex()
函数。select bin(id), hex(id) from t1;
参考:《深入浅出MySQL》