正文开始!!!
数值越界测试:
mysql> insert into t1 values (127);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t1 values (-128);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t1 values (-1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t1 values (1);
Query OK, 1 row affected (0.01 sec)
mysql> insert into t1 values (128);
ERROR 1264 (22003): Out of range value for column 'num' at row 1
mysql> insert into t1 values (-129);
ERROR 1264 (22003): Out of range value for column 'num' at row 1
mysql在这里,如果插入的数据不符合要求,直接终止操作!
数据类型在mysql这里符合条件让你操作,不符合条件,不让你操作!—约束
可以保证数据库的数据是预期的!
mysql中数据类型本质就是一直约束!!!
说明:
mysql> create table t2(
-> num tinyint unsigned
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> insert into t2 values (0);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t2 values (-1);
ERROR 1264 (22003): Out of range value for column 'num' at row 1
mysql> insert into t2 values (255);
Query OK, 1 row affected (0.00 sec)
注意:尽量不适用unsigned,对于int类型可能存不下的数据,int unsigned同样可能存放不下.与其如此,还不如设计时,将int类型提升为bigint类型.
基本语法
bit[(M)] : 位字段类型,M表示每个值的位数,范围从1到64,如果M被忽略,默认为1.
举例:
mysql> create table t3(
-> id int,
-> a bit(8)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> desc t3;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| a | bit(8) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into t3 value (10,10);
Query OK, 1 row affected (0.00 sec)
mysql> select * from t3; ---发现很怪异的现象,a的数据10没有出现
+------+------+
| id | a |
+------+------+
| 10 | |
+------+------+
1 row in set (0.00 sec)
mysql> insert into t3 value (65,65);
Query OK, 1 row affected (0.01 sec)
mysql> select * from t3;
+------+------+
| id | a |
+------+------+
| 10 |
|
| 65 | A |
+------+------+
2 rows in set (0.00 sec)
mysql> create table t4(
-> name varchar(32),
-> sex bit(1)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> insert into t4 (name,sex) value ('张三',1);
Query OK, 1 row affected (0.01 sec)
mysql> insert into t4 (name,sex) value ('李四',0);
Query OK, 1 row affected (0.01 sec)
mysql> select * from t4;
+--------+------+
| name | sex |
+--------+------+
| 张三 | |
| 李四 | |
+--------+------+
2 rows in set (0.00 sec)
mysql> select * from t4 where sex=0;
+--------+------+
| name | sex |
+--------+------+
| 李四 | |
+--------+------+
1 row in set (0.00 sec)
mysql> select * from t4 where sex=1;
+--------+------+
| name | sex |
+--------+------+
| 张三 | |
+--------+------+
1 row in set (0.00 sec)
语法:
float[(m, d)] [unsigned] : M指定显示长度,d指定小数位数,占用空间4个字节
案例:
小数 : float(4,2)表示的范围是-99.99~99.99,MySQL在保存值时会进入四舍五入.
mysql> create table t5( id int, salary float(4,2) );
Query OK, 0 rows affected (0.01 sec)
mysql> desc t5;
+--------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| salary | float(4,2) | YES | | NULL | |
+--------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into t5 values (1,99.99);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t5 values (1,-99.99);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t5 values (1,99.999);
ERROR 1264 (22003): Out of range value for column 'salary' at row 1
mysql> insert into t5 values (1,999.99);
ERROR 1264 (22003): Out of range value for column 'salary' at row 1
mysql> insert into t5 values (1,1.01);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t5 values (1,99.993); #多的这一点被拿掉了
Query OK, 1 row affected (0.01 sec)
问题:当我们的float(4,2)如果是一个有符号的,则表示的范围是-99.99~99.99,如果float(6,3),那么范围是多少呢?
(-999.999~999.999)
案例:
mysql> create table t6(id int, salary float(4,2) unsigned);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t6 values (1,99.99);
Query OK, 1 row affected (0.01 sec)
mysql> insert into t6 values (1,0);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t6 values (1,-1);
ERROR 1264 (22003): Out of range value for column 'salary' at row 1
mysql> insert into t6 values (1,999.99);
ERROR 1264 (22003): Out of range value for column 'salary' at row 1
mysql> insert into t6 values (1,99.999);
ERROR 1264 (22003): Out of range value for column 'salary' at row 1
语法:
decimal(m, d) [unsigned] : 定点数m指定长度,d表示小数点的位数
mysql> create table tt8 ( id int, salary float(10,8), sa
decimal和float很像,但是有区别:float和decimal表示的精度不一样!!!
mysql> create table t7(
-> id int,
-> salary1 float(10,8),
-> salary2 decimal(10,8)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> desc t7;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| salary1 | float(10,8) | YES | | NULL | |
| salary2 | decimal(10,8) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> insert into t7 values (1,23.123456,23.123456);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t7 values (1,23.12345612,23.12345612);
Query OK, 1 row affected (0.00 sec)
mysql> select * from t7;
+------+-------------+-------------+
| id | salary1 | salary2 |
+------+-------------+-------------+
| 1 | 23.12345505 | 23.12345600 | #我们可以发现decimal的精度更准确,因此如果我们希望某个数据表示高精度,选择decimal
| 1 | 23.12345695 | 23.12345612 |
+------+-------------+-------------+
2 rows in set (0.00 sec)
说明:float表示的精度大约是7位.
建议:如果希望小数的精度高,推荐使用decimal.
语法:
char(L): 固定长度字符串,L是可以存储的长度,单位为字符,最大长度值可以为255
案例(char):
mysql> create table t8(
-> id int,
-> name char(2)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> desc t8;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(2) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into t8 values (1,'ab');
Query OK, 1 row affected (0.01 sec)
mysql> insert into t8 values (1,'罗斯');
Query OK, 1 row affected (0.01 sec)
mysql> insert into t8 values (1,'abc'); #长度超过3个字符,不能被插入
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> insert into t8 values (1,'詹姆斯'); #长度超过3个字符,不能被插入
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> select * from t8;
+------+--------+
| id | name |
+------+--------+
| 1 | ab |
| 1 | 罗斯 |
+------+--------+
2 rows in set (0.00 sec)
mysql中的字符,指的就是一个字符,可以一个abcd1234这样的,也可以是一个汉字.
char类型最多可以存放255个字符!!!
mysql> create table t9(
-> name char(255)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> create table t10(
-> name char(256)
-> );
ERROR 1074 (42000): Column length too big for column 'name' (max = 255); use BLOB or TEXT instead
语法:
varchar(L): 可变长度字符串,L表示字符长度,最大长度65535个字节
案例:
mysql> create table t11(
-> id int,
-> name varchar(6)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> desc t11;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(6) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into t11 values (1,'a');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t11 values (1,'hulu');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t11 values (1,'abcdef');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t11 values (1,'abcdefg');
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> select * from t11;
+------+--------+
| id | name |
+------+--------+
| 1 | a |
| 1 | hulu |
| 1 | abcdef |
+------+--------+
3 rows in set (0.00 sec)
说明:
关于varchar(len),len到底是多大,这个len值,和表的编码密切相关;
一个字符串长度有上限和它是变长的不冲突.
mysql> create table t12(
-> name varchar(21844)
-> )charset=utf8;
Query OK, 0 rows affected (0.01 sec)
mysql> create table t13(
-> 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
# 验证了utf8确实不能超过21844
常用的日期有如下三个:
date
:日期yyyy-mm-dd
占用三个字节.datetime
:时间日期格式yyyy-mm-dd HH:ii:ss
表示范围从1000到9999,占用八字节.timestamp
:时间戳,从1970年开始的yyyy-mm-dd HH:ii:ss
格式和datetime
完全一致,占用四字节.案例:
mysql> create table birthday(
-> t1 date,
-> t2 datetime,
-> t3 timestamp
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> desc birthday;
+-------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+-------------------+-----------------------------+
| t1 | date | YES | | NULL | |
| t2 | datetime | YES | | NULL | |
| t3 | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+-------------------+-----------------------------+
3 rows in set (0.00 sec)
mysql> insert into birthday (t1,t2) values ('2002-09-23','2023-04-12 10:00:00');
Query OK, 1 row affected (0.00 sec)
mysql> insert into birthday (t1,t2) values ('2002-09-23','2000-10-01 10:00:00');
Query OK, 1 row affected (0.00 sec)
mysql> select * from birthday;
+------------+---------------------+---------------------+
| t1 | t2 | t3 |
+------------+---------------------+---------------------+
| 2002-09-23 | 2023-04-12 10:00:00 | 2023-04-12 21:25:10 | # 添加数据时,时间戳自动补上当前时间
| 2002-09-23 | 2000-10-01 10:00:00 | 2023-04-12 21:25:35 |
+------------+---------------------+---------------------+
2 rows in set (0.00 sec)
mysql> update birthday set t1='1949-10-01';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from birthday;
+------------+---------------------+---------------------+
| t1 | t2 | t3 |
+------------+---------------------+---------------------+
| 1949-10-01 | 2023-04-12 10:00:00 | 2023-04-12 21:27:31 | # 更新数据,时间戳会更新成当前时间
| 1949-10-01 | 2000-10-01 10:00:00 | 2023-04-12 21:27:31 |
+------------+---------------------+---------------------+
语法:
enum:枚举,“单选”类型;
enum('选项1','选项2','选项3',...);
该设定只是提供了若干个选项的值,最终一个单元格中,实际只存储了其中一个值;而且处于效率考虑,这些值实际存储的是数字,因为这些选项的每个选项值依次对应如下数字:1,2,3,…最多65535个;当我们添加枚举值时,也可以添加对应的数字编号.
set:集合,“多选”类型;
set('选项值1','选项值2','选项值3', ...);
该设定只是提供了若干个选项值,最终在一个单元格中,设计可存储了其中任意多个值;而且出于效率考虑,这些值实际存储的是"数字",因为这些选项的每个选项值依次对应数字:1,2,4,8,16,32,…,最多64个.
说明:不建议在添加枚举值,集合值的时候采用数字的方式,因为不利于阅读.
案例:
mysql> create table votes(
-> name varchar(20),
-> gender enum('男','女'),
-> hobby set('唱','跳','rapper','篮球','足球')
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> desc votes;
+--------+---------------------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------------------------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| gender | enum('男','女') | YES | | NULL | |
| hobby | set('唱','跳','rapper','篮球','足球') | YES | | NULL | |
+--------+---------------------------------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> insert into votes values ('蔡徐坤','男','唱,跳,rapper');
Query OK, 1 row affected (0.00 sec)
mysql> insert into votes values ('张三','男','rapper');
Query OK, 1 row affected (0.00 sec)
mysql> insert into votes values ('李四','女','rapper,足球');
Query OK, 1 row affected (0.00 sec)
mysql> insert into votes values ('迪丽热巴','女','唱,跳');
Query OK, 1 row affected (0.00 sec)
mysql> insert into votes values ('乃万','女','唱,rapper');
Query OK, 1 row affected (0.01 sec)
mysql> select * from votes;
+--------------+--------+----------------+
| name | gender | hobby |
+--------------+--------+----------------+
| 蔡徐坤 | 男 | 唱,跳,rapper |
| 张三 | 男 | rapper |
| 李四 | 女 | rapper,足球 |
| 迪丽热巴 | 女 | 唱,跳 |
| 乃万 | 女 | 唱,rapper |
+--------------+--------+----------------+
5 rows in set (0.00 sec)
# 测试enum类型
mysql> insert into votes values ('罗斯',1,'篮球');
Query OK, 1 row affected (0.01 sec)
mysql> insert into votes values ('杨舒予',2,'篮球');
Query OK, 1 row affected (0.00 sec)
mysql> insert into votes values ('杨舒予',3,'篮球'); # 3就不能被插入
ERROR 1265 (01000): Data truncated for column 'gender' at row 1
mysql> select * from votes;
+--------------+--------+----------------+
| name | gender | hobby |
+--------------+--------+----------------+
| 蔡徐坤 | 男 | 唱,跳,rapper |
| 张三 | 男 | rapper |
| 李四 | 女 | rapper,足球 |
| 迪丽热巴 | 女 | 唱,跳 |
| 乃万 | 女 | 唱,rapper |
| 罗斯 | 男 | 篮球 |
| 杨舒予 | 女 | 篮球 |
+--------------+--------+----------------+
7 rows in set (0.00 sec)
# 测试set类型
mysql> insert into votes values ('赵六',1,1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into votes values ('赵六',1,2);
Query OK, 1 row affected (0.00 sec)
mysql> insert into votes values ('赵六',1,3);
Query OK, 1 row affected (0.01 sec)
mysql> insert into votes values ('赵六',1,4);
Query OK, 1 row affected (0.00 sec)
mysql> insert into votes values ('赵六',1,5);
Query OK, 1 row affected (0.00 sec)
mysql> insert into votes values ('赵六',1,8);
Query OK, 1 row affected (0.01 sec)
mysql> select * from votes;
+--------------+--------+----------------+
| name | gender | hobby |
+--------------+--------+----------------+
| 蔡徐坤 | 男 | 唱,跳,rapper |
| 张三 | 男 | rapper |
| 李四 | 女 | rapper,足球 |
| 迪丽热巴 | 女 | 唱,跳 |
| 乃万 | 女 | 唱,rapper |
| 罗斯 | 男 | 篮球 |
| 杨舒予 | 女 | 篮球 |
| 赵六 | 男 | 唱 |
| 赵六 | 男 | 跳 |
| 赵六 | 男 | 唱,跳 |
| 赵六 | 男 | rapper |
| 赵六 | 男 | 唱,rapper |
| 赵六 | 男 | 篮球 |
+--------------+--------+----------------+
13 rows in set (0.00 sec)
set类型中采用位图形式,比如set选项中对应00000,如果是1对应位图就是00001代表的就是唱
如果是3,对应位图就是00011,代表的就是唱,跳…
依次类推即可!!!
mysql> select * from votes where gender='男';
+-----------+--------+------------------------------+
| name | gender | hobby |
+-----------+--------+------------------------------+
| 蔡徐坤 | 男 | 唱,跳,rapper |
| 张三 | 男 | rapper |
| 罗斯 | 男 | 篮球 |
| 赵六 | 男 | 唱 |
| 赵六 | 男 | 跳 |
| 赵六 | 男 | 唱,跳 |
| 赵六 | 男 | rapper |
| 赵六 | 男 | 唱,rapper |
| 赵六 | 男 | 篮球 |
| 赵六 | 男 | 唱,跳,rapper,篮球,足球 |
+-----------+--------+------------------------------+
10 rows in set (0.00 sec)
mysql> select * from votes where gender='女';
+--------------+--------+---------------+
| name | gender | hobby |
+--------------+--------+---------------+
| 李四 | 女 | rapper,足球 |
| 迪丽热巴 | 女 | 唱,跳 |
| 乃万 | 女 | 唱,rapper |
| 杨舒予 | 女 | 篮球 |
+--------------+--------+---------------+
4 rows in set (0.00 sec)
mysql> select * from votes where gender=1;
+-----------+--------+------------------------------+
| name | gender | hobby |
+-----------+--------+------------------------------+
| 蔡徐坤 | 男 | 唱,跳,rapper |
| 张三 | 男 | rapper |
| 罗斯 | 男 | 篮球 |
| 赵六 | 男 | 唱 |
| 赵六 | 男 | 跳 |
| 赵六 | 男 | 唱,跳 |
| 赵六 | 男 | rapper |
| 赵六 | 男 | 唱,rapper |
| 赵六 | 男 | 篮球 |
| 赵六 | 男 | 唱,跳,rapper,篮球,足球 |
+-----------+--------+------------------------------+
10 rows in set (0.00 sec)
mysql> select * from votes where gender=2;
+--------------+--------+---------------+
| name | gender | hobby |
+--------------+--------+---------------+
| 李四 | 女 | rapper,足球 |
| 迪丽热巴 | 女 | 唱,跳 |
| 乃万 | 女 | 唱,rapper |
| 杨舒予 | 女 | 篮球 |
+--------------+--------+---------------+
4 rows in set (0.00 sec)
mysql> select * from votes;
+--------------+--------+------------------------------+
| name | gender | hobby |
+--------------+--------+------------------------------+
| 蔡徐坤 | 男 | 唱,跳,rapper |
| 张三 | 男 | rapper |
| 李四 | 女 | rapper,足球 |
| 迪丽热巴 | 女 | 唱,跳 |
| 乃万 | 女 | 唱,rapper |
| 罗斯 | 男 | 篮球 |
| 杨舒予 | 女 | 篮球 |
| 赵六 | 男 | 唱 |
| 赵六 | 男 | 跳 |
| 赵六 | 男 | 唱,跳 |
| 赵六 | 男 | rapper |
| 赵六 | 男 | 唱,rapper |
| 赵六 | 男 | 篮球 |
| 赵六 | 男 | 唱,跳,rapper,篮球,足球 |
+--------------+--------+------------------------------+
14 rows in set (0.00 sec)
mysql> select * from votes where hobby='rapper';
+--------+--------+--------+
| name | gender | hobby |
+--------+--------+--------+
| 张三 | 男 | rapper |
| 赵六 | 男 | rapper |
+--------+--------+--------+
2 rows in set (0.00 sec)
我们想找到爱好有rapper的,而不是只有rapper的该怎么办呢?
集合查询使用find_ in_ set
函数:
find_in_set(sub,str_list)
:如果 sub 在 str_list 中,则返回下标;如果不在,返回0;str_list 用逗号分隔的字符串。
mysql> select * from votes where find_in_set('rapper',hobby);
+-----------+--------+------------------------------+
| name | gender | hobby |
+-----------+--------+------------------------------+
| 蔡徐坤 | 男 | 唱,跳,rapper |
| 张三 | 男 | rapper |
| 李四 | 女 | rapper,足球 |
| 乃万 | 女 | 唱,rapper |
| 赵六 | 男 | rapper |
| 赵六 | 男 | 唱,rapper |
| 赵六 | 男 | 唱,跳,rapper,篮球,足球 |
+-----------+--------+------------------------------+
7 rows in set (0.00 sec)
(本章完!)