1.INT
MySQL支持5中整数类型,每个整数类型又可以分为有符号整数和无符号整数。
类型 |
字节数 |
TINYINT |
1 |
SMALLINT |
2 |
MEDIUMINT |
3 |
INT |
4 |
BIGINT |
8 |
1.1.2. DECIMAL(NUMERIC)
MySQL使用DECIMAL表示定点数,NUMERIC是DECIMAL的一个别名。
DECIMAL(5,2)表示这个定点数类型的整数和小数部分总共5位,其中3位是整数,2位是小数。
DECIMAL的总位数最多65位,超出65则报错。
mysql> create table n3 ( id numeric(66) primary key);
ERROR 1426 (42000): Too-big precision 66 specified for ‘id‘. Maximum is 65.
创建一个包含62位整数加3位小数的定点数类型的列的数据表。
mysql> create table n2 ( id numeric(65,3) primary key);
Query OK, 0 rows affected (0.02 sec)
mysql> show create table n2;
+-------+----------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------+
| n2 | CREATE TABLE `n2` (
`id` decimal(65,3) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
在DECIMAL(65,3)类型中,小数部分超出3位时则自动截取为3位小数。
mysql> insert into n2 values (12345678901234567890123456789012345678901234567890123456789012.345);
Query OK, 1 row affected (0.05 sec)
mysql> insert into n2 values (12345678901234567890123456789012345678901234567890123456789012.12345);
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> insert into n2 values (12345678901234567890123456789012345678901234567890123456789012);
Query OK, 1 row affected (0.01 sec)
mysql> select * from n2;
+--------------------------------------------------------------------+
| id |
+--------------------------------------------------------------------+
| 12345678901234567890123456789012345678901234567890123456789012.000 |
| 12345678901234567890123456789012345678901234567890123456789012.123 |
| 12345678901234567890123456789012345678901234567890123456789012.345 |
+--------------------------------------------------------------------+
3 rows in set (0.00 sec)
在DECIMAL(65,3)类型中,整数部分不允许超过62位。
mysql> insert into n2 values (12345678901234567890123456789012345678901234567890123456789012345);
ERROR 1264 (22003): Out of range value for column ‘id‘ at row 1
在使用DECIMAL类型时,如果不指定精度,则默认为DECIMAL(10,0)。
mysql> create table n7 ( id decimal primary key);
Query OK, 0 rows affected (0.03 sec)
mysql> show create table n7;
+-------+----------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------+
| n7 | CREATE TABLE `n7` (
`id` decimal(10,0) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
定点数的运算:
mysql> select cast( 123456789012345678901234567890.1234567890 as decimal(40,10) ) + cast(1.1111111111 as decimal(40,10) );
+-------------------------------------------------------------------------------------------------------------+
| cast( 123456789012345678901234567890.1234567890 as decimal(40,10) ) + cast(1.1111111111 as decimal(40,10) ) |
+-------------------------------------------------------------------------------------------------------------+
| 123456789012345678901234567891.2345679001 |
+-------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
2.FLOAT/DOUBLE
MySQL提供了浮点数FLOAT和DOUBLE类型,其中FLOAT占用4个字节,DOUBLE占用8个字节。
FLOAT(5,2)表示有3位整数加上2位小数的浮点数。
浮点数的查询不能简单的相等比较:
mysql> create table n8 ( id float(10,3) primary key);
Query OK, 0 rows affected (0.03 sec)
mysql> show create table n8;
+-------+--------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------+
| n8 | CREATE TABLE `n8` (
`id` float(10,3) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> insert into n8 values ( 1234567.123);
Query OK, 1 row affected (0.01 sec)
mysql> select * from n8;
+-------------+
| id |
+-------------+
| 1234567.125 |
+-------------+
1 row in set (0.00 sec)
mysql> select * from n8 where id=1234567.123;
Empty set (0.00 sec)
mysql> select * from n8 where id=1234567.125;
+-------------+
| id |
+-------------+
| 1234567.125 |
+-------------+
1 row in set (0.00 sec)
mysql> select * from n8 where id=1234567.126;
Empty set (0.00 sec)
上述查询中,插入的原始数据是1234567.123,而查询到的实际是1234567.125。