MySql基础:数据类型

1. 数据类型的整体分类

        说明:在mysql表中建立属性列都是 列名称在前 类型在后。

2. 整数数据类型

        说明:在MySQL中,整型可以指定是有符号的和无符号的,默认是有符号的。可以通过UNSIGNED来说明某个字段是无符号的

类型字节最小值最大值
(带符号的/无符号的)(带符号的/无符号的)
TINYINT1-128127
UNSIGNED0255
SMALLNT2-3276832767
UNSIGNED065535
MEDIUMINT3-83886088388607
UNSIGNED01677215
INT4-21474836482147483647
UNSIGNED04294967295
BIGINT8-92233720368547758089223372036854775807
UNSIGNED018446744073709551615

 2.1 TINYINT 类型

        说明:尽量不使用unsigned,对于int类型可能存放不下的数据,int unsigned同样可能存放不 下,与其如此,还不如设计时,将int类型提升为bigint类型。

有符号越界测试案例:
mysql> create table test1(num tinyint);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into test1 values(-128);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test1 values(127);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test1 values(128);
ERROR 1264 (22003): Out of range value for column 'num' at row 1

         说明:如果我们向MySQL特定的类型中插入不合法的数据,MYSQL一般都是直接拦截不让我们对应操作!反之如果我们有数据已经插入到MySQL中,那么数据一定是合法的。简而言之,这也是对数据类型的一种约束!

无符号测试案例:
mysql> insert into test2 values(0);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test2 values(255);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test2 values(256);
ERROR 1264 (22003): Out of range value for column 'num' at row 1

2.2 bit 类型 

语法: bit[(M)] : 位字段类型。M表示每个值的位数,范围从1到64。如果M被忽略,默认为1

一个字节插入测试:
mysql> insert test1 values(1,1);
Query OK, 1 row affected (0.01 sec)

mysql> insert test1 values(1,0);
Query OK, 1 row affected (0.01 sec)

mysql> insert test1 values(1,2);
ERROR 1406 (22001): Data too long for column 'num' at row 1

        说明:当bit的位数位1时只能插入0/1,插入其他则报错 。

测试:
mysql> select *from test1;
+------+------------+
| id   | num        |
+------+------------+
|    1 | 0x01       |
|    1 | 0x00       |
+------+------------+
2 rows in set (0.00 sec)

       

2.3 小数类型

 2.3.1 float

语法:float[(m, d)] [unsigned] : M指定显示长度,d指定小数位数,占用空间4个字节

         说明:假如float的指定长度为4,那么表示的范围则为-99.99~99.99,同时MYSQL会在保存时四舍五入。

测试案例:
mysql> create table test1 (id int ,salary float(4,2));
Query OK, 0 rows affected, 1 warning (0.02 sec)

mysql> insert test1 values(1,99.99);
Query OK, 1 row affected (0.01 sec)

mysql> insert test1 values(2,0);
Query OK, 1 row affected (0.01 sec)

mysql> insert test1 values(3,-1);
Query OK, 1 row affected (0.01 sec)

mysql> select *from test1;
+------+--------+
| id   | salary |
+------+--------+
|    1 |  99.99 |
|    2 |   0.00 |
|    3 |  -1.00 |
+------+--------+
3 rows in set (0.00 sec)

        说明:虽然MYSQL会检查数据的类型,如果超出范围则会报错,但对于float类型,会自动的进行四舍五入后再查看时候超出范围。

        说明:如果定义的float(4,2)是unsigned时,范围则为0-99.99.

无符号测试:
mysql> insert test2 values(1,99.99);
Query OK, 1 row affected (0.00 sec)

mysql> insert test2 values(2,0);
Query OK, 1 row affected (0.01 sec)

mysql> insert test2 values(3,-1);
ERROR 1264 (22003): Out of range value for column 'salary' at row 1
mysql> insert test2 values(4,-0.1);
ERROR 1264 (22003): Out of range value for column 'salary' at row 1

2.3.2 decimal

        说明:float表示的精度大约为7位,所以7位以后则会产生精度丢失。而decimal的精度则远大于float,所以如果插入的数据需要高精度则可以使用decimal。至于decimal的使用法和float大致相同。

语法:decimal(m, d) [unsigned] : 定点数m指定长度,d表示小数点的位数

测试:
mysql> desc test4;
+-------+---------------+------+-----+---------+-------+
| Field | Type          | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| num1  | float(10,8)   | YES  |     | NULL    |       |
| num2  | decimal(10,8) | YES  |     | NULL    |       |
+-------+---------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert test4 values(20.12345678,20.12345678);
Query OK, 1 row affected (0.00 sec)

mysql> select *from test4;
+-------------+-------------+
| num1        | num2        |
+-------------+-------------+
| 20.12345695 | 20.12345678 |
+-------------+-------------+
1 row in set (0.00 sec)

        说明:如上代码所示,同样插入相同的数据,float类型会自动调整,而decimal类型则会保持原来的精度。

2.4 字符串类型

2.4.1 char

语法:char(L): 固定长度字符串,L是可以存储的长度,单位为字符(和以往所认为的一个字符等同于一个字节不一样,在MySQL中一个字符可以是一个数字、一个汉字、字母等),最大长度值可以为255

测试:
mysql> create table test1(id int ,name char(2));
Query OK, 0 rows affected (0.03 sec)

mysql> desc test1;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int     | YES  |     | NULL    |       |
| name  | char(2) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert test1 values(1,'a');
Query OK, 1 row affected (0.01 sec)

mysql> insert test1 values(2,'ab');
Query OK, 1 row affected (0.01 sec)

mysql> insert test1 values(3,'abc');
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> insert test1 values(3,'中国');
Query OK, 1 row affected (0.01 sec)

mysql> insert test1 values(4,'去上学');
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> select *from test1;
+------+--------+
| id   | name   |
+------+--------+
|    1 | a      |
|    2 | ab     |
|    3 | 中国   |
+------+--------+
3 rows in set (0.00 sec)

         说明:char(2) 表示可以存放两个字符,可以是字母或汉字,但是不能超过2个, 最多只能是255。

测试:
mysql> create table test2(id int ,name char(255));
Query OK, 0 rows affected (0.02 sec)

mysql> desc test2;
+-------+-----------+------+-----+---------+-------+
| Field | Type      | Null | Key | Default | Extra |
+-------+-----------+------+-----+---------+-------+
| id    | int       | YES  |     | NULL    |       |
| name  | char(255) | YES  |     | NULL    |       |
+-------+-----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> create table test3 (id int ,name char(256));
ERROR 1074 (42000): Column length too big for column 'name' (max = 255); use BLOB or TEXT instead

2.4.3 varchar

        说明:char和varchar最大的区别就是,varchar是用多少给多少,而char在最开始就已经确定。对于char和varchar的上限都是一样确定的。

语法:varchar(L): 可变长度字符串,L表示字符长度,最大长度65535个字节

测试:
mysql> create table test1 (id int ,name varchar(6));
Query OK, 0 rows affected (0.03 sec)

mysql> insert test1 values(1,'abcdef');
Query OK, 1 row affected (0.01 sec)

mysql> insert test1 values(1,'123456');
Query OK, 1 row affected (0.00 sec)

mysql> insert test1 values(1,'我不想去上学');
Query OK, 1 row affected (0.00 sec)

mysql> select *from test1;
+------+--------------------+
| id   | name               |
+------+--------------------+
|    1 | abcdef             |
|    1 | 123456             |
|    1 | 我不想去上学       |
+------+--------------------+
3 rows in set (0.00 sec)

        说明:关于varchar(len),len到底是多大,这个len值,和表的编码密切相关: varchar长度可以指定为0到65535之间的值,但是有1 - 3 个字节用于记录数据大小,所以说有效字 节数是65532。 当我们的表的编码是utf8时,varchar(n)的参数n最大值是65532/3=21844[因为utf中,一个字符占 用3个字节],如果编码是gbk,varchar(n)的参数n最大是65532/2=32766(因为gbk中,一个字符 占用2字节)。

测试:
mysql> create table test2 (name varchar(21845))charset=utf8;
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs
mysql> create table test (name varchar(21844))charset=utf8
    -> ;
Query OK, 0 rows affected, 1 warning (0.03 sec)

那么如何选择定长或变长字符串?

        如果数据确定长度都一样,就使用定长(char),比如:身份证,手机号,md5

        如果数据长度有变化,就使用变长(varchar), 比如:名字,地址,但是你要保证最长的能存的进去。

        定长的磁盘空间比较浪费,但是效率高。

        变长的磁盘空间比较节省,但是效率低。

        定长的意义是,直接开辟好对应的空间 变长的意义是,在不超过自定义范围的情况下,用多少,开辟多少。

2. 5 日期和时间类型

        说明:常见的日期和时间类型有以下三个

        date :日期 'yyyy-mm-dd' ,占用三字节

        datetime 时间日期格式 'yyyy-mm-dd HH:ii:ss' 表示范围从 1000 到 9999 ,占用八字节        

         timestamp :时间戳,自动记录插入或者修改数据时最新的时间日期格式和datetime一致,占用四个字节。

时间戳列可以有四张组合定义,其含义分别为:

        1.当字段定义为timestamp,该字段在插入和更新时都不会自动设置为当前时间。

        2. 当字段定义为timestamp DEFAULT CURRENT_TIMESTAMP,该字段仅在插入且未指定值时被赋予当前时间,再更新时且未指定值时不做修改。

        3. 当字段定义为timestamp ON UPDATE CURRENT_TIMESTAMP,该字段在插入且未指定值时被赋值为"0000-00-00 00:00:00",在更新且未指定值时更新为当前时间。

        4. 当字段定义为timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,该字段在插入或更新时未指定值,则被赋值为当前时间。

         说明:当字段定义为timestamp,该字段在插入和更新时都不会自动设置为当前时间。

测试:
mysql> creat table test (t1 date,t2 datetime,t3 timestamp);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'creat table test (t1 date,t2 datetime,t3 timestamp)' at line 1
mysql> create table test (t1 date,t2 datetime,t3 timestamp);
Query OK, 0 rows affected (0.02 sec)

mysql> insert test(t1,t2)values('1992-02-20','1992-02-20');
Query OK, 1 row affected (0.01 sec)

mysql> insert test(t1,t2)values('1992-02-20','1992-02-21');
Query OK, 1 row affected (0.01 sec)

mysql> select*from test;
+------------+---------------------+------+
| t1         | t2                  | t3   |
+------------+---------------------+------+
| 1992-02-20 | 1992-02-20 00:00:00 | NULL |
| 1992-02-20 | 1992-02-21 00:00:00 | NULL |
+------------+---------------------+------+
2 rows in set (0.00 sec)

        说明: 当字段定义为timestamp DEFAULT CURRENT_TIMESTAMP,该字段仅在插入且未指定值时被赋予当前时间,再更新时且未指定值时不做修改。

测试:
mysql> create table test1 (d1 int ,t1 timestamp default current_timestamp);
Query OK, 0 rows affected (0.02 sec)

mysql> desc test1;
+-------+-----------+------+-----+-------------------+-------------------+
| Field | Type      | Null | Key | Default           | Extra             |
+-------+-----------+------+-----+-------------------+-------------------+
| d1    | int       | YES  |     | NULL              |                   |
| t1    | timestamp | YES  |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED |
+-------+-----------+------+-----+-------------------+-------------------+
2 rows in set (0.01 sec)
mysql> insert test1(d1)values(1);
Query OK, 1 row affected (0.00 sec)

mysql> select *from test1;
+------+---------------------+
| d1   | t1                  |
+------+---------------------+
|    1 | 2024-10-09 14:46:19 |
+------+---------------------+
1 row in set (0.01 sec)

mysql> update test1 d1=2 where d1=1;
mysql> update test1 set d1=2 where d1=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select*from test1;
+------+---------------------+
| d1   | t1                  |
+------+---------------------+
|    2 | 2024-10-09 14:46:19 |
+------+---------------------+
1 row in set (0.00 sec)

        说明:当字段定义为timestamp ON UPDATE CURRENT_TIMESTAMP,该字段在插入且未指定值时被赋值为"0000-00-00 00:00:00",在更新且未指定值时更新为当前时间。

测试:
mysql> create table test2(id int ,t1 timestamp on update current_timestamp);
Query OK, 0 rows affected (0.02 sec)

mysql> insert test2 (id)values(1);
Query OK, 1 row affected (0.01 sec)

mysql> select*from test2;
+------+------+
| id   | t1   |
+------+------+
|    1 | NULL |
+------+------+
1 row in set (0.01 sec)

mysql> update test2 set id=2 where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select  *from test2;
+------+---------------------+
| id   | t1                  |
+------+---------------------+
|    2 | 2024-10-09 14:52:20 |
+------+---------------------+
1 row in set (0.00 sec)

        说明:当字段定义为timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,该字段在插入或更新时未指定值,则被赋值为当前时间。

测试:
mysql> create table test3(id int ,t1 timestamp default current_timestamp on update current_timestamp);
Query OK, 0 rows affected (0.02 sec)

mysql> insert test3 (id)values(1);
Query OK, 1 row affected (0.01 sec)

mysql> select *from test3;
+------+---------------------+
| id   | t1                  |
+------+---------------------+
|    1 | 2024-10-09 14:56:37 |
+------+---------------------+
1 row in set (0.01 sec)

mysql> update test3 set id=2 where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select *from test3;
+------+---------------------+
| id   | t1                  |
+------+---------------------+
|    2 | 2024-10-09 14:57:00 |
+------+---------------------+
1 row in set (0.00 sec)

2.6 enum和set类型

        说明:enum类型设定只是提供了若干个选项的值,最终一个单元格中,实际只存储了其中一个值;而且出于效率考虑,这些值实际存储的是“数字”,因为这些选项的每个选项值依次对应如下数字:1,2,3,....最多65535个;当我们添加枚举值时,也可以添加对应的数字编号。

语法:enum:枚举,“单选”类型;

        enum('选项1','选项2','选项3',...);

测试:
mysql> create table test1(id int ,sex enum('男','女'));
Query OK, 0 rows affected (0.02 sec)

mysql> desc test1;
+-------+-------------------+------+-----+---------+-------+
| Field | Type              | Null | Key | Default | Extra |
+-------+-------------------+------+-----+---------+-------+
| id    | int               | YES  |     | NULL    |       |
| sex   | enum('男','女')   | YES  |     | NULL    |       |
+-------+-------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert test1 values(1,'男');
Query OK, 1 row affected (0.00 sec)

mysql> insert test1 values(2,'女');
Query OK, 1 row affected (0.00 sec)

mysql> insert test1 values(3,1);
Query OK, 1 row affected (0.00 sec)

mysql> insert test1 values(4,2);
Query OK, 1 row affected (0.01 sec)

mysql> select *from test1;
+------+------+
| id   | sex  |
+------+------+
|    1 | 男   |
|    2 | 女   |
|    3 | 男   |
|    4 | 女   |
+------+------+
4 rows in set (0.00 sec)

         说明:set设定只是提供了若干个选项的值,最终一个单元格中,设计可存储了其中任意多个值;而且出于效率考虑,这些值实际存储的是“数字”,因为这些选项的每个选项值依次对应如下数字:1,2,4,8,16,32,....最多64个。

语法:set:集合,“多选”类型;

        set('选项值1','选项值2','选项值3', ...);

测试:
mysql> create table test2 (id int ,hobby set('game','video','sports'));
Query OK, 0 rows affected (0.02 sec)

mysql> desc test2;
+-------+------------------------------+------+-----+---------+-------+
| Field | Type                         | Null | Key | Default | Extra |
+-------+------------------------------+------+-----+---------+-------+
| id    | int                          | YES  |     | NULL    |       |
| hobby | set('game','video','sports') | YES  |     | NULL    |       |
+-------+------------------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert test2 values(1,'game');
Query OK, 1 row affected (0.00 sec)

mysql> insert test2 values(3,'game,video,sports');
Query OK, 1 row affected (0.01 sec)

mysql> insert test2 values(4,1);
Query OK, 1 row affected (0.00 sec)

mysql> insert test2 values(5,2);
Query OK, 1 row affected (0.00 sec)

mysql> insert test2 values(6,3);
Query OK, 1 row affected (0.00 sec)

mysql> select *from test2;
+------+-------------------+
| id   | hobby             |
+------+-------------------+
|    1 | game              |
|    2 | game,video        |
|    3 | game,video,sports |
|    4 | game              |
|    5 | video             |
|    6 | game,video        |
+------+-------------------+
6 rows in set (0.00 sec)

 补充:

        说明:当使用枚举类型后,有些数据列会有多个选项值,那么在查找的时候如何找到所有对应的数据。

语法:find_in_set(sub,str_list) :如果 sub 在 str_list 中,则返回下标;如果不在,返回0; str_list 用逗号分隔的字符串。

测试:
mysql> select *from test2;
+------+-------------------+
| id   | hobby             |
+------+-------------------+
|    1 | game              |
|    2 | game,video        |
|    3 | game,video,sports |
|    4 | game              |
|    5 | video             |
|    6 | game,video        |
+------+-------------------+
6 rows in set (0.00 sec)

mysql> select *from test2 where find_in_set('video',hobby);
+------+-------------------+
| id   | hobby             |
+------+-------------------+
|    2 | game,video        |
|    3 | game,video,sports |
|    5 | video             |
|    6 | game,video        |
+------+-------------------+
4 rows in set (0.00 sec)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值