Mysql数据类型

2020-12-14  本文已影响0人  爱折腾的傻小子

整数类型

有符号
create table demo1(
  c1 tinyint
); 
insert into demo1 values((-pow(2,7)), (pow(2,7)-1);
select * from demo1;
/*
+------+
| c1 |
+------+
| -128 |
| 127 |
+------+
2 rows in set (0.00 sec)
*/
类型(n)
CREATE TABLE test3 (
 `a` int,
 `b` int(5),
 `c` int(5) unsigned,
 `d` int(5) zerofill,  -- 默认设置为 d int(5) unsigned zerofill 无符号类型
 `e` int(5) unsigned zerofill,
 `f` int zerofill,
 `g` int unsigned zerofill
 );
insert into test3 values 
  (1,1,1,1,1,1,1),
  (11,11,11,11,11,11,11),
  (12345,12345,12345,12345,12345,12345,12345);
select * from test3;
/*
+-------+-------+-------+-------+-------+------------+------------+
| a | b | c | d | e | f | g |
+-------+-------+-------+-------+-------+------------+------------+
| 1 | 1 | 1 | 00001 | 00001 | 0000000001 | 0000000001 |
| 11 | 11 | 11 | 00011 | 00011 | 0000000011 | 0000000011 |
| 12345 | 12345 | 12345 | 12345 | 12345 | 0000012345 | 0000012345 |
+-------+-------+-------+-------+-------+------------+------------+
*/
show create table test3;
/*
| Table | Create Table 
| test3 | CREATE TABLE `test3` (
 `a` int(11) DEFAULT NULL,
 `b` int(5) DEFAULT NULL,
 `c` int(5) unsigned DEFAULT NULL,
 `d` int(5) unsigned zerofill DEFAULT NULL,
 `e` int(5) unsigned zerofill DEFAULT NULL,
 `f` int(10) unsigned zerofill DEFAULT NULL,
 `g` int(10) unsigned zerofill DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
*/

浮点数类型

create table test5(
  a float(5,2),
  b double(5,2),
  c decimal(5,2)
);
-- 插入测试数据
 insert into test5 values 
  (1,1,1),
  (2.1,2.1,2.1),
  (3.123,3.123,3.123),
  (4.125,4.125,4.125),
  (5.115,5.115,5.115),
  (6.126,6.126,6.126),
  (7.116,7.116,7.116),
  (8.1151,8.1151,8.1151),
  (9.1251,9.1251,9.1251),
  (10.11501,10.11501,10.11501),
  (11.12501,11.12501,11.12501);
-- 查询
select * from test5;
/*
+-------+-------+-------+
| a | b | c |
+-------+-------+-------+
| 1.00 | 1.00 | 1.00 |
| 2.10 | 2.10 | 2.10 |
| 3.12 | 3.12 | 3.12 |
| 4.12 | 4.12 | 4.13 |
| 5.12 | 5.12 | 5.12 |
| 6.13 | 6.13 | 6.13 |
| 7.12 | 7.12 | 7.12 |
| 8.12 | 8.12 | 8.12 |
| 9.13 | 9.13 | 9.13 |
| 10.12 | 10.12 | 10.12 |
| 11.13 | 11.13 | 11.13 |
+-------+-------+-------+
*/
create table test6(
  a float,
  b double,
  c decimal
);
-- 插入数据
insert into test6 values 
  (1,1,1),
  (1.234,1.234,1.4),
  (1.234,0.01,1.5);
-- 查询数据
select * from test6;
/*
+-------+-------+------+
| a | b | c |
+-------+-------+------+
| 1 | 1 | 1 |
| 1.234 | 1.234 | 1 |
| 1.234 | 0.01 | 2 |
+-------+-------+------+
*/
 select sum(a),sum(b),sum(c) from test5;
/*
+--------+--------+--------+
| sum(a) | sum(b) | sum(c) |
+--------+--------+--------+
| 67.21 | 67.21 | 67.22 |
+--------+--------+--------+
*/
select sum(a),sum(b),sum(c) from test6;
/*
+--------------------+--------------------+--------+
| sum(a) | sum(b) | sum(c) |
+--------------------+--------------------+--------+
| 3.4679999351501465 | 2.2439999999999998 | 4 |
+--------------------+--------------------+--------+
*/

字符串类型


日期


总结


上一篇 下一篇

猜你喜欢

热点阅读