数值范围测试:
mysql> create table tt1(num tinyint);
Query OK, 0 rows affected (0.18 sec)
mysql> insert into tt1 values(1);
Query OK, 1 row affected (0.03 sec)
mysql> insert into tt1 values(127);
Query OK, 1 row affected (0.04 sec)
mysql> insert into tt1 values(-128);
Query OK, 1 row affected (0.03 sec)
mysql> insert into tt1 values(128); -- 越界插入,报错。
ERROR 1264 (22003): Out of range value for column 'num' at row 1
mysql> insert into tt1 values(-129); -- 越界插入,报错。
ERROR 1264 (22003): Out of range value for column 'num' at row 1
mysql> select * from tt1;
+------+
| num |
+------+
| 1 |
| 127 |
| -128 |
+------+
3 rows in set (0.00 sec)
mysql> insert into tt2 values(-1); -- 越界插入,报错。
ERROR 1264 (22003): Out of range value for column 'num' at row 1
mysql> insert into tt2 values(0);
Query OK, 1 row affected (0.02 sec)
mysql> insert into tt2 values(255);
Query OK, 1 row affected (0.03 sec)
mysql> insert into tt2 values(256); -- 越界插入,报错。
ERROR 1264 (22003): Out of range value for column 'num' at row 1
基本语法:
bit[(M)] : 位字段类型。M表示每个值的位数,范围从1到64。如果M被忽略,默认为1。
举例:
mysql> create table tt3(id int, a bit(8));
Query OK, 0 rows affected (0.20 sec)
mysql> insert into tt3 values(10, 10);
Query OK, 1 row affected (0.04 sec)
mysql> select * from tt3;
+------+------+
| id | a |
+------+------+
| 10 |
|
+------+------+
1 row in set (0.02 sec)
语法:
float[(m, d)] [unsigned] : M指定显示长度,d指定小数位数,占用空间4个字节
案例:
小数:float(4,2)表示的范围是-99.99 ~ 99.99,MySQL在保存值时会进行四舍五入(当然最后结果不能超过范围,不然报错)。
mysql> create table tt4(id int, salary float(4, 2));
Query OK, 0 rows affected (0.17 sec)
mysql> insert into tt4 values(1, -99.99);
Query OK, 1 row affected (0.03 sec)
mysql> insert into tt4 values(2, -99.994);
Query OK, 1 row affected (0.04 sec)
mysql> insert into tt4 values(3, -99.995); -- 越界插入,报错。
ERROR 1264 (22003): Out of range value for column 'salary' at row 1
mysql> insert into tt4 values(4, 99.99);
Query OK, 1 row affected (0.03 sec)
mysql> insert into tt4 values(5, 99.994);
Query OK, 1 row affected (0.03 sec)
mysql> insert into tt4 values(6, 99.996); -- 越界插入,报错。
ERROR 1264 (22003): Out of range value for column 'salary' at row 1
mysql> select * from tt4;
+------+--------+
| id | salary |
+------+--------+
| 1 | -99.99 |
| 2 | -99.99 |
| 4 | 99.99 |
| 5 | 99.99 |
+------+--------+
4 rows in set (0.00 sec)
案例:
如果定义的是float(4,2) unsigned 这时,因为把它指定为无符号的数,范围是 0 ~ 99.99。
mysql> create table tt5(id int, salary float(4, 2) unsigned);
Query OK, 0 rows affected (0.25 sec)
mysql> insert into tt5 values(1, 99.99);
Query OK, 1 row affected (0.04 sec)
mysql> insert into tt5 values(2, 99.994);
Query OK, 1 row affected (0.03 sec)
mysql> insert into tt5 values(3, 99.997); -- 越界插入,报错。
ERROR 1264 (22003): Out of range value for column 'salary' at row 1
mysql> insert into tt5 values(4, 0);
Query OK, 1 row affected (0.04 sec)
mysql> insert into tt5 values(5, -0.001); -- 越界插入,报错。
ERROR 1264 (22003): Out of range value for column 'salary' at row 1
mysql> select * from tt5;
+------+--------+
| id | salary |
+------+--------+
| 1 | 99.99 |
| 2 | 99.99 |
| 4 | 0.00 |
+------+--------+
3 rows in set (0.00 sec)
语法:
decimal(m, d) [unsigned] : 定点数m指定长度,d表示小数点的位数
案例:
mysql> create table tt6(salary1 float(10, 8), salary2 decimal(10, 8));
Query OK, 0 rows affected (0.22 sec)
mysql> insert into tt6 values (22.12345612, 22.12345612);
Query OK, 1 row affected (0.04 sec)
mysql> select * from tt6;
+-------------+-------------+
| salary1 | salary2 |
+-------------+-------------+
| 22.12345695 | 22.12345612 |
+-------------+-------------+
1 row in set (0.00 sec)
语法:
char(L): 固定长度字符串,L是可以存储的长度,单位为字符,最大长度值可以为255
注意:char(2) 表示可以存放两个字符,可以是字母或汉字,但是不能超过2个, 最多只能是255;根c/c++语言中char不同哈~
案例:
mysql> create table tt7(id int, name char(2));
Query OK, 0 rows affected (0.21 sec)
mysql> insert into tt7 values(101, '中国');
Query OK, 1 row affected (0.02 sec)
mysql> insert into tt7 values(102, 'ab');
Query OK, 1 row affected (0.02 sec)
mysql> select * from tt7;
+------+--------+
| id | name |
+------+--------+
| 101 | 中国 |
| 102 | ab |
+------+--------+
2 rows in set (0.01 sec)
语法:
varchar(L): 可变长度字符串,L表示字符长度,最大长度65535个字节
varchar长度可以指定为0到65535之间的值,但是有1 - 3 个字节用于记录数据大小,所以说有效字节数是65532。
常用的日期有如下三个:
案例:
// 创建表
mysql> create table birthday(y date, y_m datetime, y_m_d timestamp);
Query OK, 0 rows affected (0.16 sec)
// 插入数据
mysql> insert into birthday(y, y_m) values('2023-4-9', '2023-4-9 21:33:30');
Query OK, 1 row affected (0.04 sec)
mysql> select * from birthday;
+------------+---------------------+---------------------+
| y | y_m | y_m_d |
+------------+---------------------+---------------------+
| 2023-04-09 | 2023-04-09 21:33:30 | 2023-04-09 21:34:08 | --添加数据时,时间戳自动补上当前时间。
+------------+---------------------+---------------------+
1 row in set (0.00 sec)
// 更新数据
mysql> update birthday set y_m='2023-4-9 21:35:10';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from birthday;
+------------+---------------------+---------------------+
| y | y_m | y_m_d |
+------------+---------------------+---------------------+
| 2023-04-09 | 2023-04-09 21:35:10 | 2023-04-09 21:36:32 | --更新数据时,时间戳自动更新到当前时间。
+------------+---------------------+---------------------+
1 row in set (0.00 sec)
语法:
enum:枚举,“单选”类型;
enum('选项1','选项2','选项3',...);
这里的枚举就像生活中的问卷调查中的单项选择。
set:集合,“多选”类型;
set('选项值1','选项值2','选项值3', ...);
这里的集合就像生活中的问卷调查中的多项选择。
例子:
mysql> create table votes(
-> username varchar(30),
-> hobby set('登山','游泳','篮球','武术'),
-> gender enum('男','女'));
Query OK, 0 rows affected (0.24 sec)
mysql> insert into votes values('乔丹', '篮球', '男');
Query OK, 1 row affected (0.05 sec)
mysql> insert into votes values('雷锋', 10, 1);
Query OK, 1 row affected (0.03 sec)
mysql> insert into votes values('花木兰', '武术', 2);
Query OK, 1 row affected (0.04 sec)
mysql> select * from votes;
+-----------+---------------+--------+
| username | hobby | gender |
+-----------+---------------+--------+
| 乔丹 | 篮球 | 男 |
| 雷锋 | 游泳,武术 | 男 |
| 花木兰 | 武术 | 女 |
+-----------+---------------+--------+
3 rows in set (0.00 sec)
集合查询使用find_ in_ set函数:
查找喜欢武术的
mysql> select * from votes where find_in_set('武术', hobby);
+-----------+---------------+--------+
| username | hobby | gender |
+-----------+---------------+--------+
| 雷锋 | 游泳,武术 | 男 |
| 花木兰 | 武术 | 女 |
+-----------+---------------+--------+