mysql数值型

  • 5种整型 tinyint、smallint、mediumint、int和bigint,主要区别就是取值范围不同,还可以在类 型前添加一个限制词unsigned,不允许添加负数
  • 3种浮点型:不能精确存放float和double,可以精确存放decimal和numeric
类型名称说明
tinyint1B,取值范围-128到127
smallint2B,取值范围为-32768到32767
mediumint3B,取值范围为-8388608到8388607
int或者 Integer4B,取值范围为2e9
bigint8B,取值范围为9e18
float(n,m)4B,单精度浮点型,取值范围3.4e38,其中n为总位宽,m为小数位数。要求 n>=m,如果不定义nm则默认提供7-8位的有效数据
double8B,双精度浮点型,取值范围1.7e308,总位数不超过15位
decimal和 numeric采用定点存放浮点数,具体字节数取决于定义时设置的参数。如果不设置位宽,则 默认(10,0),总位宽最大65,小数位数最大30

 unsigned设置列为无符号型,只能存放大于等于0的数据,没有负数。当使用无符号类型时取值范围由 于没有负数部分,从而导致上限扩大一倍

create table t3(id int unsigned);
mysql> insert into t3 value(-10); -- 不允许存储负数
ERROR 1264 (22003): Out of range value for column 'id' at row 1
mysql> insert into t3 value(255); -- 存储数据的上线扩大一倍
Query OK, 1 row affected (0.01 sec)

 可以在类型名后添加括号,其中包含一个正整数,例如int(5),这里的含义并不是要求只能存放5位长度 的整数;含义是当进行查询时自动使用空格填充到5个长,如果真实数据长度大于5,则按实际输出  

mysql> create table t4(id int(2));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t4 values(99999);
Query OK, 1 row affected (0.01 sec)
mysql> insert into t4 values(9);
Query OK, 1 row affected (0.01 sec)
mysql> select * from t4;
+-------+
|    id |
+-------+
| 99999 |
|     9 |
+-------+
2 rows in set (0.00 sec)

 decimal和numeric作为字符串存储浮点数,可以实现浮点数的精确存储,并不是float和double种使用 二进制浮点数存储。使用方法numeric(总位宽,小数位数),小数位数必须小于等于总位宽;小数位数最 大值30,总位宽最大值为65,注意可能存储的数据会超出范围,其中的符号位和小数点不占位宽,

mysql> create table t5(id numeric(5,3));
Query OK, 0 rows affected (0.03 sec)
mysql> insert into t5 values(99.999);
Query OK, 1 row affected (0.01 sec)
mysql> insert into t5 values(-99.999); -- 位宽统计中符号位不算
Query OK, 1 row affected (0.01 sec)
mysql> insert into t5 values(100); -- 整数超出范围报错
ERROR 1264 (22003): Out of range value for column 'id' at row 1
mysql> insert into t5 values(9.1235); -- 小数位数多余则自动四舍五入
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into t5 values(9.1234);
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> select * from t5;
+---------+
|      id |
+---------+
|  99.999 |
| -99.999 |
|   9.124 |
|   9.123 |
+---------+
4 rows in set (0.00 sec)

 int和numeric

  •         int类型不能保存小数位,存储小数时会自动进行四舍五入
mysql> create table t6(id int);
Query OK, 0 rows affected (0.03 sec)
mysql> insert into t6 values(99.99);
Query OK, 1 row affected (0.01 sec)
mysql> select * from t6;
+------+
|   id |
+------+
|  100 |
+------+
1 row in set (0.00 sec)
  •  numeric可以保存小数位,如果小数位数为0,可以模拟得到int类型的存储效果。采用的实际存储 方式为字符串。查询效率远低于int
mysql> create table t7(id numeric);
Query OK, 0 rows affected (0.03 sec)
mysql> desc t7;
+-------+---------------+------+-----+---------+-------+
| Field |   Type        | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
|    id | decimal(10,0) | YES  |     | NULL    |       |
+-------+---------------+------+-----+---------+-------+
1 row in set (0.00 sec)

数值列的扩展属性

  • auto_increment一般用于主键【非键列不能使用】,可以实现该列的自动生成连续整数值
mysql> create table t8(id int auto_increment,name varchar(20));
ERROR 1075 (42000): Incorrect table definition; there can be only one
auto column and it must be defined as a key
mysql> create table t8(id int primary key auto_increment,name
varchar(20));
Query OK, 0 rows affected (0.03 sec)
mysql> insert into t8 values(null,'yan1');-- 插入数据时不指定插入的值或者设置
插入值为null则会自动添加一个自增值
Query OK, 1 row affected (0.01 sec)
mysql> insert into t8 values(99,'yan1'); -- 如果插入数据时不指定对应的值,则
自增长max(id)+1;如果指定对应的值,则自增效果失效
Query OK, 1 row affected (0.01 sec)
mysql> insert into t8(name) values('yan1');-- 如果不向指定列插入数据,则
auto_increment生效
Query OK, 1 row affected (0.01 sec)
mysql> select * from t8;
+-----+------+
|  id | name |
+-----+------+
|   1 | yan1 |
|  99 | yan1 |
| 100 | yan1 |
+-----+------+
3 rows in set (0.00 sec)
  •  unsigned禁用负值
  • zerofill如果查询显示时,实际数据小于指定位宽,则自动添加0值
mysql> create table t9(id int(5) zerofill);
Query OK, 0 rows affected (0.03 sec)
mysql> insert into t9 values(12);
Query OK, 1 row affected (0.01 sec)
mysql> select * from t9;
+-------+
|    id |
+-------+
| 00012 |
+-------+
1 row in set (0.00 sec)
mysql> insert into t9 values(123456);
Query OK, 1 row affected (0.01 sec)
mysql> select * from t9;
+--------+
|     id |
+--------+
|  00012 |
| 123456 |
+--------+
2 rows in set (0.00 sec)
  •  default用于设置默认值

default只有在不插入数据时生效,如果插入数据,即使插入null值,仍旧不生效

mysql> create table t10(id int default 0,name varchar(20));
Query OK, 0 rows affected (0.03 sec)
mysql> insert into t10 values(11,'yan1');-- 指定对应的数据,则default无效
Query OK, 1 row affected (0.01 sec)
mysql> insert into t10 values(null,'yan1'); -- 指定对应的数据,则default无
效,即使设置的值为null
Query OK, 1 row affected (0.01 sec)
mysql> insert into t10(name) values('yan1'); -- 只有不针对这个列进行数据插入
时default才能生效
Query OK, 1 row affected (0.00 sec)
mysql> select * from t10;
+------+------+
|   id | name |
+------+------+
|   11 | yan1 |
| NULL | yan1 |
|    0 | yan1 |
+------+------+
3 rows in set (0.00 sec)

 null是什么意思

null的意思为未知的数据,它既不是空字符串,不是任何一个具体的值;不能把任何值与一个null 值进行比较。因为null的意思是不确定的值。真正的null值比较必须使用特殊的运算符is null或者is not null

mysql> select null=null; -- =表示等值比较
+-----------+
| null=null |
+-----------+
|      NULL |
+-----------+
1 row in set (0.00 sec)
mysql> select null!=null; -- 比较不相等
+------------+
| null!=null |
+------------+
|       NULL |
+------------+
1 row in set (0.00 sec)
mysql> select null is null;
+--------------+
| null is null |
+--------------+
|            1 |
+--------------+
1 row in set (0.00 sec)
mysql> select null is not null;
+------------------+
| null is not null |
+------------------+
|                0 |
+------------------+
1 row in set (0.00 sec)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值