MySQL学习笔记3:列类型

简介

就像数据框概念一样,每个行是一个示例或者样本,每个列是一个参数或者变量(除去id列),同一列之中,数据类型是一样的,要么是字符串,要么是整数型,然而列与列之间在数据存储时对于磁盘空间的需求是不一样的,有的对存储空间要求高,有的要求低,出于利用效率和使用效率的考虑,在建表的时候,需要对字段进行声明。
anyway,
存储同样的数据,不同的列类型,所占据的空间和效率是不一样的…这是建表前要声明类型的意义。

MySQL三大列类型

  1. 数值型:
    1.1整型:tinyint、smallint、int、bigint
    1.2小数型:float、decimal
  2. 字符串型:char、varchar、text、blob
  3. 日期时间类型:date、time、datetime、timestamp、year

1.1 整型 注:对于int型,占据的字节越多,存储的范围越大。
int系列声明时的参数:(M) unsigned zerofill

EXAMPLE: 练习tinyint的参数并验证字节与范围的关系。

create table team (
sname varchar(20) not null default '',
age tinyint not null default 0
)engine myisam charset utf8;

为其插入数据,age到底是-128-127还是0-255呢?

mysql> insert into team
    -> (age)
    -> values
-> (255);
ERROR 1264 (22003): Out of range value for column 'age' at row 1

所以,tinyint默认为有符号的。是-128-127。

EXAMPLE: 修改表,加一个学分列。

mysql> alter table team add socre tinyint unsigned not null default 0;

测试学分列的赋值范围是否被影响。

mysql> insert into team
    -> values
    -> ('小明',15,-15);
ERROR 1264 (22003): Out of range value for column 'socre' at row 1

mysql> insert into team
    -> values
    -> ('小明',15,15);
Query OK, 1 row affected (0.14 sec)

所以,经过更改,tinyint修改为了无符号。0-255。
因此, 列类型加unsigned表示其为无符号型。

整数型的M参数和zerofill参数

注:M必须和zerofill配合才有意义。

EXAMPLE:
给学员增加学号列。 1:学号不为负2:学号位数一般相同 如,00013;01255
即使不够位数,用0填充。

mysql> alter table team add snum smallint(5) zerofill not null default 0;
Query OK, 2 rows affected (0.30 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from team;
+-------+-----+-------+------+-------+
| sname | age | socre | age1 | snum  |
+-------+-----+-------+------+-------+
| 小明  |  15 |    15 |    0 | 00000 |
| 张三  |   0 |     0 |   15 | 00000 |
+-------+-----+-------+------+-------+

mysql> insert into team
    -> (sname,snum)
    -> values
    -> ('朝五洋',225);
Query OK, 1 row affected (0.02 sec)

mysql> select * from team;
+--------+-----+-------+------+-------+
| sname  | age | socre | age1 | snum  |
+--------+-----+-------+------+-------+
| 小明   |  15 |    15 |    0 | 00000 |
| 张三   |   0 |     0 |   15 | 00000 |
| 朝五洋 |   0 |     0 |    0 | 00225 |
+--------+-----+-------+------+-------+

从上面的例子可以看出来,snum列统一补到五位。这是因为前面对于smallint型的M参数设的是5。

总结:M参数表示补零宽度,和zerofill配合使用才有意义。且,zerofill则同时必为unsigned类型。

1.2 小数型
M叫“标度”–>代表“总位数”;
D是“精度”,代表小数位。
因此,float(6,2) 的range是 -9999.99–9999.99。

EXAMPLE:建立一个薪水表

mysql> create table salary(
    -> sname varchar(20) not null default '',
    -> salary float(8,2) not null default 0
    -> )engine myisam charset utf8;
Query OK, 0 rows affected, 1 warning (0.17 sec)

mysql> desc salary;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| sname  | varchar(20) | NO   |     |         |       |
| salary | float(8,2)  | NO   |     | 0.00    |       |
+--------+-------------+------+-----+---------+-------+

float型也可以申明为unsigned:

mysql> alter table salary add bonus float(8,2) unsigned not null default 0;
Query OK, 1 row affected (0.21 sec)

Q:浮点与定点的区别:
A:定点是把整数部分与小数部分分开存储,比float精确。

EXAMPLE:比较decimal型与float型。

mysql> create table bank(
    -> id int not null default 0,
    -> account1 float(9,2) not null default 0,
    -> account2 decimal(9,2) not null default 0
-> )engine myisam charset utf8;
mysql>  insert into bank
    -> values
    -> (1,3240155.16,3240155.16);
Query OK, 1 row affected (0.10 sec)

mysql> select * from bank;
+----+------------+------------+
| id | account1   | account2   |
+----+------------+------------+
|  1 |   20155.66 |   20155.66 |
|  1 | 3240155.25 | 3240155.16 |
+----+------------+------------+
2 rows in set (0.00 sec)

可以看到,float有时会损失精度,所以在对精度有较高要求的时候,使用decimal型。

2.字符串型
char和varchar分别称为定长和变长类型。

char(N):
查找行记录时,如果都是定长,完全可以通过行数与行的长度计算出来文件指针的偏移量。因为每个变量占的磁盘宽度是一样的。
且,对于定长N,无论够不够指定N长,实际都占N个长度,并且用空格在末尾补齐N个长度。

Varchar(N):

3.日期时间类型

date型
注:date的范围是1000-01-01到9999-12-31

mysql> create table celebirties(
    -> cele varchar(20) not null default '',
    -> birth date not null default '1000-01-01'
-> )engine myisam charset utf8;

time类型

mysql> alter table celebirties add login time not null default '00:00';
Query OK, 0 rows affected (0.36 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc celebirties;
+-------+-------------+------+-----+------------+-------+
| Field | Type        | Null | Key | Default    | Extra |
+-------+-------------+------+-----+------------+-------+
| cele  | varchar(20) | NO   |     |            |       |
| birth | date        | NO   |     | 1000-01-01 |       |
| login | time        | NO   |     | 00:00:00   |       |
+-------+-------------+------+-----+------------+-------+

mysql> insert into celebirties
    -> values
    -> ('钟馗','2015-11-15','11:15');
Query OK, 1 row affected (0.12 sec)

mysql> select * from celebirties;
+------+------------+----------+
| cele | birth      | login    |
+------+------------+----------+
| 钟馗 | 2015-11-15 | 11:15:00 |
+------+------------+----------+

datetime类型
datetime类型是date型和time型的组合。

mysql> alter table celebirties add test2 datetime not null default '1000-10-10 00:00:00';
Query OK, 1 row affected (0.60 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into celebirties
    -> (cele,test2)
    -> values
    -> ('孙红雷','1995-11-25 11:55:55');
Query OK, 1 row affected (0.04 sec)

mysql> select * from celebirties;
+--------+------------+----------+---------------------+---------------------+
| cele   | birth      | login    | test1               | test2               |
+--------+------------+----------+---------------------+---------------------+
| 钟馗   | 2015-11-15 | 11:15:00 | 2019-03-31 16:51:02 | 1000-10-10 00:00:00 |
| 孙红雷 | 1000-01-01 | 00:00:00 | 2019-04-02 00:20:40 | 1995-11-25 11:55:55 |
+--------+------------+----------+---------------------+---------------------+

timestamp类型
timestamp类型有专有的自动更新特性。

year类型
注:year类型只占1个字节。注意year类型的range。从1901-2155,加上0000这个特殊值。

mysql> create table things(
    -> thing varchar(20) not null default '',
    -> yr year not null default '0000'
    -> )engine myisam charset utf8;

mysql> insert into things
    -> values
    -> ('百团大战','1943');
Query OK, 1 row affected (0.14 sec)

mysql> select * from things;
+----------+------+
| thing    | yr   |
+----------+------+
| 百团大战 | 1943 |
+----------+------+

year类型还可以简写为2位,但是极不推荐这种写法,因为
[00-69]+2000
[70-99]+1900
即,填2位表示1970-2069。

小结

数值型:
在这里插入图片描述
字符型:
在这里插入图片描述
日期时间型:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值