一、数据类型的分类

二、数值类型

1.tinyint类型
mysql> create table tt1(num tinyint);
Query OK, 0 rows affected (0.08 sec)
mysql> insert into tt1 values(1);
Query OK, 1 row affected (0.02 sec)
mysql> insert into tt1 values(128);
ERROR 1264 (22003): Out of range value for column 'num' at row 1
说明:
- 在MySQL中,整型是可以指定是有符号的和无符号的,默认是有符号的。
- 可以通过unsigned来说明某个字段是无符号的
mysql> alter table tt1 add column unnum tinyint unsigned after num;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> update tt1 set unnum=128 where num=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from tt1;
+------+-------+
| num | unnum |
+------+-------+
| 1 | 128 |
+------+-------+
1 row in set (0.00 sec)
mysql> insert into tt1 value(1,256);
ERROR 1264 (22003): Out of range value for column 'unnum' at row 1
mysql> insert into tt1 value(1,255);
Query OK, 1 row affected (0.02 sec)
2.bit 类型
基本语法
bit(M) : 位字段类型。M表示每个值的位数,范围从1到64。如果M被忽略,默认为1。
例子
mysql> insert into tt2 value(1,255);
Query OK, 1 row affected (0.01 sec)mysql> insert into tt2 value(1,256);
ERROR 1406 (22001): Data too long for column 'a' at row 1
mysql> select * from tt2;
3.小数类型
基本语法
float(m, d) unsigned : M指定显示长度,d指定小数位数,占用空间4个字节
例子:
小数:float(4,2)表示的范围是-99.99-99.99 ,MySQL在保存值的时候会四舍五入。
mysql> create table tt3(id int,salary float(4,2));
Query OK, 0 rows affected, 1 warning (0.08 sec)
mysql> insert into tt3 value(1,-99.99);
Query OK, 1 row affected (0.02 sec)
mysql> insert into tt3 value(1,99.99);
Query OK, 1 row affected (0.02 sec)
mysql> insert into tt3 value(1,99.994);
Query OK, 1 row affected (0.02 sec)
mysql> insert into tt3 value(1,99.995);
ERROR 1264 (22003): Out of range value for column 'salary' at row 1
mysql> select * from tt3;
+------+--------+
| id | salary |
+------+--------+
| 1 | -99.99 |
| 1 | 99.99 |
| 1 | 99.99 |
+------+--------+
3 rows in set (0.00 sec)
案例:
如果定义的是float(4,2) unsigned 这时,因为把它指定为无符号的数,范围是0~99.99
mysql> alter table tt3 add column salary2 float(4,2) unsigned after salary;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 2
mysql> insert into tt3 values(2,99,99);
Query OK, 1 row affected (0.03 sec)
mysql> insert into tt3 values(2,99,996);
ERROR 1264 (22003): Out of range value for column 'salary2' at row 1
mysql> insert into tt3 values(2,-0.01);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
4.decimal
语法
decimal(m, d) unsigned : 定点数m指定长度,d表示小数点的位数
decimal和float的区别主要在于decimal的精度比float高
mysql> create table tt4(f float(10,8),d decimal(10,8));
Query OK, 0 rows affected, 1 warning (0.09 sec)
mysql> insert into tt4 value(2.123456789,2.123456789);
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> select * from tt4;
+------------+------------+
| f | d |
+------------+------------+
| 2.12345672 | 2.12345679 |
+------------+------------+
1 row in set (0.00 sec)
mysql>
三、字符串类型
1.char
语法
char(L): 固定长度字符串,L是可以存储的长度,单位为字符,最大长度值可以为255
字符可以是字母,也可以是汉字,但是不能超过L个,L最多为255
案例:
mysql> create table tt5(name char(5));
Query OK, 0 rows affected (0.09 sec)mysql> insert into tt5 value('牛逼六六六');
Query OK, 1 row affected (0.02 sec)
mysql> insert into tt5 value('12345');
Query OK, 1 row affected (0.02 sec)
mysql> insert into tt5 value('123456');
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> select * from tt5;
+-----------------+
| name |
+-----------------+
| 牛逼六六六 |
| 12345 |
+-----------------+
2 rows in set (0.00 sec)
2.varchar
语法:
varchar(L): 可变长度字符串,L表示字符长度,最大长度65535个字节
案例:
mysql> create table tt6(name varchar(6));
Query OK, 0 rows affected (0.21 sec)
mysql> insert into tt6 value('hello');
Query OK, 1 row affected (0.02 sec)
mysql> insert into tt6 value('牛逼六六六六');
Query OK, 1 row affected (0.03 sec)
mysql> insert into tt6 value('牛逼六六六六七');
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> insert into tt6 value('hello00');
ERROR 1406 (22001): Data too long for column 'name' at row 1mysql> select * from tt6;
+--------------------+
| name |
+--------------------+
| hello |
| 牛逼六六六六 |
+--------------------+
2 rows in set (0.00 sec)
说明:
- 关于varchar(len),len到底多大,这个len的值,和表的编码密切相关
- varchar长度可以指定为0-65535之间的值,但是有1-2个字节用于记录数据大小,所以说有效的字节数是65533
- 当我们的表的编码是utf8时,varchar(n)参数n的最大值为65533/3=21844(utf8中一个字符占3个字节)如果我们表的编码是gbk,varchar(n)的参数n的最大值为65533/2=32766(gbk编码中一个字符占2个字节)
mysql> create table tt7(name varchar(21844)) charset=utf8;
Query OK, 0 rows affected, 1 warning (0.08 sec)
mysql> create table tt8(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 tt8(name varchar(32766)) charset=gbk;
Query OK, 0 rows affected (0.07 sec)
mysql> create table tt9(name varchar(32767)) charset=gbk;
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
3.char和varchar的对比

如何选择呢?
- 如果数据长度确定,用定长(char)如:身份证,手机号
- 如果长度有变化,用变长(varchar)如:名字,地址 但要确保最长的地址你存进去
- 定长的磁盘空间比较浪费,但是效率高
- 变长的磁盘空间比较节省,但是效率低
4.日期和时间类型
常见的日期有如下三个:
- date:日期'yyyy-mm-dd',占用三字节
- datetime:时间日期格式'yyyy-mm-dd HH:ii::ss'表示范围从1000到9999,占用8个字节
- timestamp:时间戳,从1970年开始的'yyyy-mm-dd HH:ii::ss'格式和datetime完全一样,占用四个字节
例子:
mysql> create table tt9(t1 date,t2 datetime,t3 timestamp);
Query OK, 0 rows affected (0.12 sec)
mysql> insert into tt9(t1,t2) value('1997-7-1','2006-7-22 12:11:11');
Query OK, 1 row affected (0.02 sec)
mysql> select * from tt9;
+------------+---------------------+------+
| t1 | t2 | t3 |
+------------+---------------------+------+
| 1997-07-01 | 2006-07-22 12:11:11 | NULL |
+------------+---------------------+------+
1 row in set (0.00 sec)
mysql> alter table tt9 modify t3 timestamp default current_timestamp;Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from tt9;
+------------+---------------------+------+
| t1 | t2 | t3 |
+------------+---------------------+------+
| 1997-07-01 | 2006-07-22 12:11:11 | NULL |
+------------+---------------------+------+
1 row in set (0.00 sec)
mysql> insert into tt9(t1,t2) value('1997-7-1','2006-7-22 12:11:11');
Query OK, 1 row affected (0.01 sec)
mysql> select * from tt9;
+------------+---------------------+---------------------+
| t1 | t2 | t3 |
+------------+---------------------+---------------------+
| 1997-07-01 | 2006-07-22 12:11:11 | NULL |
| 1997-07-01 | 2006-07-22 12:11:11 | 2026-10-18 19:02:55 |
+------------+---------------------+---------------------+
2 rows in set (0.00 sec)
5.enum和set
语法:
enum:枚举,"单选"类型;
enum('选项1','选项2','选项3');
该设定只是提供了若干个选项的值,最终一个单元格里,实际只存储了一个值,而且出于效率考虑,这些值时间存储的是数字,因为这些选项的每个选项值依次对应如下数字:1,2,3最多65535个;当我们添加枚举值,也可以添加编号
mysql> drop table tt10;
Query OK, 0 rows affected (0.06 sec)
mysql> create table tt10(gender enum('男','女'));
Query OK, 0 rows affected (0.08 sec)
mysql> insert into tt10 value(0);
ERROR 1265 (01000): Data truncated for column 'gender' at row 1
mysql> insert into tt10 value(1);
Query OK, 1 row affected (0.01 sec)
mysql> insert into tt10 value(2);
Query OK, 1 row affected (0.02 sec)
mysql> insert into tt10 value('男');
Query OK, 1 row affected (0.02 sec)
mysql> select * from tt10;
+-------+
| gender|
+-------+
| 男 |
| 女 |
| 男 |
+-------+
3 rows in set (0.00 sec)
语法:
set:集合,"多选类型";
set('选项值1','选项值2','选项值3',......);
该设定只是提供了若干个选项的值,最终一个单元格中,设计可存储了其中任意多个值;而处于效率考虑,这些值实际存储的是"数字",因为这些选项的每个选项值依次对应如下数字:1,2,4,8,16......最多64个
说明:不建议在添加枚举值,集合值时采用数字的方式,因为不利于阅读
mysql> create table tt11(hobby set('跑','跳','滑','走'));
Query OK, 0 rows affected (0.10 sec)
mysql> insert into tt11 value('跑,跳');
Query OK, 1 row affected (0.01 sec)
mysql> insert into tt11 value('跑');
Query OK, 1 row affected (0.03 sec)
mysql> insert into tt11 value('跑,滑,走');
Query OK, 1 row affected (0.01 sec)
mysql> insert into tt11 value(10);
Query OK, 1 row affected (0.03 sec)
mysql> select * from tt11;
+-------------+
| hobby |
+-------------+
| 跑,跳 |
| 跑 |
| 跑,滑,走 |
| 跳,走 |
+-------------+
4 rows in set (0.00 sec)
mysql> select * from tt11 where hobby='跑';
+-------+
| hobby |
+-------+
| 跑 |
+-------+
1 row in set (0.00 sec)
不能查询出所有爱好为跑的人
集合查询使用find_in_set函数:
find_in_set(sub,str_list) :如果 sub 在 str_list 中,则返回下标;如果不在,返回0;
str_list 用逗号分隔的字符串。
mysql> select * from tt11 where find_in_set('跑',hobby);
+-------------+
| hobby |
+-------------+
| 跑,跳 |
| 跑 |
| 跑,滑,走 |
+-------------+
3 rows in set (0.02 sec)