mysql数据类型学习

整型
整型用来存放整数,根据可以存放的数字大小又分为TINYINT,SMALLINT,MEDIUMINT,INT,BIGINT,每种类型又分为无符号(UNSIGNED)和有符号,
创建一个表,并插入数据,int后面括号里面的数字并不是限制了可存放的数字的大小,而是在查询结果输出的时候会改变输出的格式,方便查看,并且在设置了zerofill的时候才会生效

create table `int_table` (`int1` int,`int2` int(3),`int3` int(3) zerofill)engine=MYISAM;
insert into int_table values (1,1,1),(2,2,2),(8,8,8),(11,11,11);

结果

+------+------+------+
| int1 | int2 | int3 |
+------+------+------+
|    1 |    1 |  001 |
|    2 |    2 |  002 |
|    8 |    8 |  008 |
|   11 |   11 |  011 |
+------+------+------+

浮点型
用来存放小数,根据数据范围的大小可分为FLOAT、DOUBLE 和 DECIMAL,和整型一样 UNSIGNED 和 ZEROFILL 也可以作用于浮点型
FLOAT 数值类型用于表示单精度浮点数值,而 DOUBLE 数值类型用于表示双精度浮点数值。
与整数一样,这些类型也带有附加参数:一个显示宽度指示器和一个小数点指示器。比如语句 FLOAT(7,3) 规定显示的值不会超过 7 位数字,小数点后面带有 3 位数字。
对于小数点后面的位数超过允许范围的值,MySQL 会自动将它四舍五入为最接近它的值,再插入它

create table `float_t` (`f1` float,`f2` float(5,2),`f3` float(7,2) zerofill)engine=MYISAM;
insert into `float_t` values (1.1,1.1,1.1),(2.222,2.222,2.222),(5.555,5.555,5.555),(1.16666,1.16666,1.166666);
select * from float_t;
+---------+------+---------+
| f1      | f2   | f3      |
+---------+------+---------+
|     1.1 | 1.10 | 0001.10 |
|   2.222 | 2.22 | 0002.22 |
|   5.555 | 5.55 | 0005.55 |
| 1.16666 | 1.17 | 0001.17 |
+---------+------+---------+

日期类型
year 类型
插入数据时,注意带上引号的数据有什么不同

create table `year_t` (`y1` year(4),`y2` year(2));
insert into year_t values (16,16),(2016,2016),(0,0),('0','0');
select * from year_t;
+------+------+
| y1   | y2   |
+------+------+
| 2016 | 2016 |
| 2016 | 2016 |
| 0000 | 0000 |
| 2000 | 2000 |
+------+------+

time 类型
空格前的数字代表天数

create table `time_t` (`t1` time);
insert into `time_t` values ('01:01:01'),(020202),('030303'),(0404),('0505'),('6 06:06'),('7 07');
select * from time_t;
+-----------+
| t1        |
+-----------+
| 01:01:01  |
| 02:02:02  |
| 03:03:03  |
| 00:04:04  |
| 00:05:05  |
| 150:06:00 |
| 175:00:00 |
+-----------+

date类型

create table `date_t` (`d1` date);
insert into `date_t` values ('2016-08-08'),('16-09-09');
select * from `date_t`;
+------------+
| d1         |
+------------+
| 2016-08-08 |
| 2016-09-09 |
+------------+

datetime 类型

create table `datatime_t` (`d1` datetime);
insert into `datatime_t` values ('2016-01-01'),('2016-02-02 02:02:02'),('16-03-03 03:03:03'),('16*04*04 04:04:04');
select * from `datatime_t`;
+---------------------+
| d1                  |
+---------------------+
| 2016-01-01 00:00:00 |
| 2016-02-02 02:02:02 |
| 2016-03-03 03:03:03 |
| 2016-04-04 04:04:04 |
+---------------------+

timestamp 类型
会随着系统时区的变化而变化

create table `timestamp_t` (`t1` timestamp);
insert into `timestamp_t` values (now());
show variables like 'time_zone';
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| time_zone     | SYSTEM |
+---------------+--------+
select * from `timestamp_t`;
+---------------------+
| t1                  |
+---------------------+
| 2016-09-21 15:21:33 |
+---------------------+
set time_zone='+10:00';
show variables like 'time_zone';
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| time_zone     | +10:00 |
+---------------+--------+
select * from `timestamp_t`;
+---------------------+
| t1                  |
+---------------------+
| 2016-09-21 17:21:33 |
+---------------------+

字符串类型
字符串里面类型很多,就记录下其中的几个
char : 固定长度
varchar : 可变长度
从以下的sql看区别

create table `char_t` (`c1` char(6),`c2` varchar(6));
insert into char_t values ('abc   ','abc   ');
select *,concat(c1,'!'),concat(c2,'!'),length(c1),length(c2) from char_t;
+------+--------+----------------+----------------+------------+------------+
| c1   | c2     | concat(c1,'!') | concat(c2,'!') | length(c1) | length(c2) |
+------+--------+----------------+----------------+------------+------------+
| abc  | abc    | abc!           | abc   !        |          3 |          6 |
+------+--------+----------------+----------------+------------+------------+

标签: mysql

添加新评论