Ⅰ MySQL之存储引擎
【一】什么是存储引擎
- 日常生活中文件格式有很多,并且针对不同的文件格式会有对应不同的存储方式和处理机制
- 针对不同的数据应该有对应的不同的处理机制
存储引擎就是不同的处理机制
【二】MySQL四种主要的存储引擎
【1】Innodb
- 是MySQL5.5版本之后的默认存储引擎
- 支持事务/行锁/外键
【2】myisam
- 是MySQL5.5版本之前的默认存储引擎
- 速度比innodb快,但是数据安全性较弱
【3】memory
- 内存引擎
- 数据全部存放在内存中
- 存储速度快,但是断电数据丢失
【4】blackhole
- 黑洞引擎
- 往里面存储任何数据都会被清空
【三】不同的存储引擎存储数据的异同点
【1】查看所有的数据库存储引擎
- 语法
show engines;
- 示例
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.15 sec)
- 利用不同的引擎创建表 ,向表中插入数据比较每一种引擎的特点
【2】建表
(1)建表语句
- 语法
create table t1(id int) engine=innodb;
create table t2(id int) engine=myisam;
create table t3(id int) engine=blackhole;
create table t4(id int) engine=memory;
- 示例
# 先选创建的库
mysql> use study001;
Database changed
mysql> create table t1(id int) engine=innodb;
Query OK, 0 rows affected (0.53 sec)
mysql> create table t2(id int) engine=myisam;
Query OK, 0 rows affected (0.04 sec)
mysql> create table t3(id int) engine=blackhole;
Query OK, 0 rows affected (0.04 sec)
mysql> create table t4(id int) engine=memory;
Query OK, 0 rows affected (0.11 sec)
mysql> show tables; # 查看库中所有的表
+--------------------+
| Tables_in_study001 |
+--------------------+
| t1 |
| t2 |
| t3 |
| t4 |
| user |
+--------------------+
5 rows in set (0.00 sec)
mysql> drop table user; # 删除库中user的表
Query OK, 0 rows affected (0.27 sec)
mysql> show tables;
+--------------------+
| Tables_in_study001 |
+--------------------+
| t1 |
| t2 |
| t3 |
| t4 |
+--------------------+
4 rows in set (0.00 sec)
(2)innodb引擎
Innodb:默认的引擎
- frm : 存储的表结构
- ibd : 存储表的数据
- 特点:存储数据持久化存储退出也存在
- 语法
# 存储数据语法
insert into t1 values(15);
# 查看数据语法
select * from t1;
- 示例
mysql> insert into t1 values(15);
Query OK, 1 row affected (0.30 sec)
# 未退出时查看后显示的数据
mysql> select * from t1;
+------+
| id |
+------+
| 15 |
+------+
1 row in set (0.10 sec)
# 退出重新登录查看后显示的数据
mysql> select * from t1;
+------+
| id |
+------+
| 15 |
+------+
1 row in set (0.00 sec)
(3)myisam引擎
myisam:数据具有索引,读取数据快
- frm : 存储的表结构
- MYD : 表的数据
- MYI : 表的索引结构,类似于书的目录,基于目录查找数据的速度会很快
- 特点:存储数据持久化存储,退出也存在
- 语法
# 存储数据语法
insert into t2 values(21);
# 查看数据语法
select * from t2;
- 示例
mysql> insert into t2 values(21);
Query OK, 1 row affected (0.08 sec)
# 未退出时查看后显示的数据
mysql> select * from t2;
+------+
| id |
+------+
| 21 |
+------+
1 row in set (0.00 sec)
# 退出重新登录查看后显示的数据
mysql> select * from t2;
+------+
| id |
+------+
| 21 |
+------+
1 row in set (0.00 sec)
(4)blackhole引擎
blackhole:黑洞引擎,存什么数据都会消失
- frm :存储的表结构
- 特点:存储数据不会有任何反应,不会存储任何数据
- 语法
# 存储数据语法
insert into t3 values(33);
# 查看数据语法
select * from t3;
- 示例
mysql> insert into t3 values(33);
Query OK, 1 row affected (0.00 sec)
# 未退出时查看后显示的数据
mysql> select * from t3;
Empty set (0.00 sec)
# 退出重新登录查看后显示的数据
mysql> select * from t3;
Empty set (0.00 sec)
(5)memory引擎
memory:数据缓存到内存
- frm:存储的表结构
- 特点就是基于内存存储,只要服务重启,所有数据丢失
- 语法
# 存储数据语法
insert into t4 values(40);
# 查看数据语法
select * from t4;
- 示例
mysql> insert into t4 values(40);
Query OK, 1 row affected (0.00 sec)
# 未退出时查看后显示的数据
mysql> select * from t4;
+------+
| id |
+------+
| 40 |
+------+
1 row in set (0.00 sec)
# 退出重新登录查看后显示的数据
mysql> select * from t4;
Empty set (0.00 sec)
Ⅱ 约束条件(null)
【一】完整的建表语句
【1】最后一行不能加逗号
- 最后一个字段不要加 , !
create table 表名(
字段名1 类型(宽度) 约束条件,
字段名2 类型(宽度) 约束条件,
字段名3 类型(宽度) 约束条件 # 最后一个字段这里不要加 , !
);
- 字段名 和 类型 是必须有的参数
- 同一张表中字段名不能重复
- 宽度和约束条件是可选的(可写可不写)
- 约束条件可以写多个
【二】创建一张表
- 错误语法
create table t5(
id
);
# 错误结果
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 ')' at line 3
- 正确语法
create table t5(
id int
);
# 正确结果
mysql> create table t5(
-> id int
-> );
Query OK, 0 rows affected (0.29 sec)
mysql> desc t5; # 查看表的详细信息
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
# 上面的 int(11) 这个 11 就是当前字段的宽度
# 有的字段是带有默认宽度的
- 有的mysql版本是默认开启长度限制的,超过最大长度会报错
- 有的版本是不开启,如果超出最大长度则会按照最大长度截取数据
# 超过最大长度会报错
mysql> insert into t5 values(12345678912);
ERROR 1264 (22003): Out of range value for column 'id' at row 1
mysql> insert into t5 values(1234567891);
Query OK, 1 row affected (0.04 sec)
mysql> select * from t5;
+------------+
| id |
+------------+
| 1234567891 |
+------------+
1 row in set (0.00 sec)
【三】约束条件 null / not null
【1】创建一个含有null / not null的表
- 语法
create table t6(
id int not null,
name int null
);
- 示例
mysql> create table t6(
-> id int not null,
-> name int null
-> );
Query OK, 0 rows affected (0.30 sec)
mysql> desc t6;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
(1)插入数据
- 语法
insert into t6 values(1,1);
- 示例
mysql> insert into t6 values(1,1);
Query OK, 1 row affected (0.12 sec)
mysql> select * from t6;
+----+------+
| id | name |
+----+------+
| 1 | 1 |
+----+------+
1 row in set (0.00 sec)
【2】 null
只给id插入数据 不给name插入数据看null是否可以不插入数据
- 默认允许插入 空
- 语法
insert into t6(id) values(1);
- 示例
mysql> insert into t6(id) values(1);
Query OK, 1 row affected (0.05 sec)
mysql> select * from t6;
+----+------+
| id | name |
+----+------+
| 1 | 1 |
| 1 | NULL |
+----+------+
2 rows in set (0.00 sec)
【3】not null
只给name插入数据 不给id插入数据看not null是否可以不插入数据
- id字段的 null 是 No 不能为空
- 语法
insert into t6(name) values(1);
- 示例
mysql> insert into t6(name) values(1);
ERROR 1364 (HY000): Field 'id' doesn't have a default value
【4】宽度和约束条件之间的联系
- 宽度是用来限制数据的存储
- 约束条件是在宽度的基础上增加的额外的约束
Ⅲ 严格模式
- 控制MySQL里面存储数据的一些限制规则
- 控制超出长度不提示,控制超出长度提示报错
【一】如何查看严格模式
- 语法
show variables like "%mode"; # %匹配任意个字符
show variables like "_mode"; # _只能匹配单个字符
- 示例
mysql> show variables like "%mode";
+--------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| block_encryption_mode | aes-128-ecb |
| gtid_mode | OFF |
| innodb_autoinc_lock_mode | 1 |
| innodb_strict_mode | ON |
| offline_mode | OFF |
| pseudo_slave_mode | OFF |
| rbr_exec_mode | STRICT |
| slave_exec_mode | STRICT |
| sql_mode | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+--------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
9 rows in set, 1 warning (0.00 sec)
- sql_mode(严格模式)
【二】模糊匹配/查询like "%mode"
- 关键字 :
- like "%mode"匹配任意多个字符
- like "_mode"只能匹配单个字符
【三】修改严格模式
- 只在当前窗口有效
# 临时修改 : 退出客户端重新连接,修改后严格模式还是原来的
set session sql_mode = 'STRICT_TRANS_TABLES'
- 全局有效
# 永久修改 : 退出客户端重新连接,严格模式修改会一直生效
set global sql_mode = 'STRICT_TRANS_TABLES'
- 语法(严格模式)
set global sql_mode = 'STRICT_TRANS_TABLES'
- 修改完成后,重启服务端,即可生效
Ⅳ 基本字段类型之整型
【一】引入
- 整型默认情况下都是带有符号的
- 针对整型()内的符号到底是什么用?
- 针对整型,括号内的宽度是用来限制存储数据的范围的
特例:只有整型括号内的数字不是表示限制位数而是显示长度
整数类型 | 字节 | 无符号数的取值范围 | 有符号数的取值范围 |
---|---|---|---|
TINYINT | 1 | 0~255 | -128~127 |
SMALLINT | 2 | 0~65535 | -32768~32767 |
MEDIUMINT | 3 | 0~16777215 | -8388608~8388607 |
INT | 4 | 0~4294967295 | -2147483648~2147483647 |
BIGINT | 8 | 0~18446744073709551615 | -9223372036854774808~9223372036854774807 |
【二】作用
- 存储年龄、ID、等级、号码等
【三】以TINYINT存储数据为例
- 是否有符号
- 默认情况下是带符号的
- 超出最大范围会如何
- 超出限制只能存最大接受值
【1】TINYINT存储数据超出最大范围会如何
- 创建表
- 语法
create table t9(id tinyint);
- 示例
mysql> create table t9(id tinyint);
Query OK, 0 rows affected (0.30 sec)
mysql> desc t9;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | tinyint(4) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
1 row in set (0.00 sec)
- 表中已经有了相应的数据格式
- TINYINT 有符号最大限制 -128~127
(1)插入数据
- 语法
insert into t9 values(-128);
insert into t9 values(-129);
insert into t9 values(127);
insert into t9 values(128);
- 示例
mysql> insert into t9 values(-128);
Query OK, 1 row affected (0.12 sec)
mysql> insert into t9 values(-129);
ERROR 1264 (22003): Out of range value for column 'id' at row 1
mysql> insert into t9 values(127);
Query OK, 1 row affected (0.03 sec)
mysql> insert into t9 values(128);
ERROR 1264 (22003): Out of range value for column 'id' at row 1
mysql> select * from t9;
+------+
| id |
+------+
| -128 |
| 127 |
+------+
2 rows in set (0.00 sec)
【四】约束条件之unsigned(无符号)
- 创建表(约束条件:tinyint unsigned)
- 语法
create table t10(id tinyint unsigned);
- 示例
mysql> create table t10(id tinyint unsigned);
Query OK, 0 rows affected (0.36 sec)
mysql> desc t10;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| id | tinyint(3) unsigned | YES | | NULL | |
+-------+---------------------+------+-----+---------+-------+
1 row in set (0.00 sec)
【1】插入数据
- 语法
insert into t10 values(-128);
insert into t10 values(-129);
insert into t10 values(127);
insert into t10 values(128);
- 示例
mysql> insert into t10 values(-128);
ERROR 1264 (22003): Out of range value for column 'id' at row 1
mysql> insert into t10 values(-129);
ERROR 1264 (22003): Out of range value for column 'id' at row 1
mysql> insert into t10 values(127);
Query OK, 1 row affected (0.12 sec)
mysql> insert into t10 values(128);
Query OK, 1 row affected (0.12 sec)
mysql> select * from t10;
+------+
| id |
+------+
| 127 |
| 128 |
+------+
2 rows in set (0.00 sec)
mysql> insert into t10 values(128888);
ERROR 1264 (22003): Out of range value for column 'id' at row 1
【2】有无约束符号意思
TINYINT:范围从 -128 到 127(或有符号)或从 0 到 255(或无符号)
SMALLINT:范围从 -32,768 到 32,767(或有符号)或从 0 到 65,535(或无符号)
MEDIUMINT:范围从 -8,388,608 到 8,388,607(或有符号)或从 0 到 16,777,215(或无符号)
INT 或 INTEGER:范围从 -2,147,483,648 到 2,147,483,647(或有符号)或从 0 到 4,294,967,295(或无符号)
BIGINT:范围从 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807(或有符号)或从 0 到 18,446,744,073,709,551,615(或无符号)
【五】字段类型的宽度
- 字段类型后面跟着的宽度只是显示的宽度
- 插入的长度是按照,当前字段的最大长度进行限制
- 针对整型,括号内的宽度是用来限制存储数据的范围的
- 语法
create table t11(
id int(8)
);
- 示例
mysql> create table t11(
-> id int(8)
-> );
Query OK, 0 rows affected (0.31 sec)
mysql> desc t11;
+-------+--------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------+------+-----+---------+-------+
| id | int(8) | YES | | NULL | |
+-------+--------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> insert into t11 values(123456789);
Query OK, 1 row affected (0.04 sec)
mysql> select * from t11;
+-----------+
| id |
+-----------+
| 123456789 |
+-----------+
1 row in set (0.00 sec)
- 限制了 8 存储了 9 位
特例:
只有整型括号内的数字不是表示限制位数而是显示长度
id int(8)
如果数字没有超出 8 位 ,那么默认用 0 填充至 8 位
如果数字超出 8 位 ,那么 有几位存几位(但是还是要遵循最大范围)
【六】约束条件之 zerofill
无符号,用 0 填充至 8 位
- 语法
create table t13(id int(8) unsigned zerofill);
- 示例
mysql> create table t13(id int(8) unsigned zerofill);
Query OK, 0 rows affected (0.32 sec)
mysql> desc t13;
+-------+--------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------------------+------+-----+---------+-------+
| id | int(8) unsigned zerofill | YES | | NULL | |
+-------+--------------------------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> insert into t13 values(1);
Query OK, 1 row affected (0.07 sec)
mysql> insert into t13 values(123456789);
Query OK, 1 row affected (0.04 sec)
mysql> select * from t13;
+-----------+
| id |
+-----------+
| 00000001 |
| 123456789 |
+-----------+
2 rows in set (0.00 sec)
- 数字不够 8 位 用 0 填充
- 针对整型字段,无需指定宽度,因为它默认的宽度已经足够展示所有数据了
Ⅴ 字段类型之浮点型
【一】浮点型
数据类型 | 字节数 | 取值范围 |
---|---|---|
FLOAT | 4 | -2^128~2 ^128,即-3.40E+38~+3.40E+38 |
DOUBLE | 8 | -2^1024~ 2^1024,即-1.79E+308~1.79E+308 |
DECIMAL | 设置位数和精度。 | 65 ~ 30 |
【二】存储限制
float(255,30)
# 总共255位 , 小数部分占 30 位
double(255,30)
# 总共255位 , 小数部分占 30 位
decimal(65,30)
# 总共65位 , 小数部分占 30 位
【三】精确度验证
【1】float(255,30)
- 语法
create table t15(id float(255,30));
insert into t15 values(1.1111111111111111111111);
- 示例
mysql> create table t15(id float(255,30));
Query OK, 0 rows affected (0.29 sec)
mysql> desc t15;
+-------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| id | float(255,30) | YES | | NULL | |
+-------+---------------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> insert into t15 values(1.1111111111111111111111);
Query OK, 1 row affected (0.16 sec)
mysql> select * from t15;
+----------------------------------+
| id |
+----------------------------------+
| 1.111111164093017600000000000000 |
+----------------------------------+
1 row in set (0.00 sec)
# float 类型 从第8为开始模糊
【2】double(255,30)
- 语法
create table t16(id double(255,30));
insert into t16 values(1.1111111111111111111111);
- 示例
mysql> create table t16(id double(255,30));
Query OK, 0 rows affected (0.37 sec)
mysql> desc t16;
+-------+----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------------+------+-----+---------+-------+
| id | double(255,30) | YES | | NULL | |
+-------+----------------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> insert into t16 values(1.1111111111111111111111);
Query OK, 1 row affected (0.12 sec)
mysql> select * from t16;
+----------------------------------+
| id |
+----------------------------------+
| 1.111111111111111200000000000000 |
+----------------------------------+
1 row in set (0.00 sec)
# double 类型 从第16为开始模糊
【3】decimal(65,30)
- 语法
create table t17(id decimal(65,30));
insert into t17 values(1.1111111111111111111111);
- 示例
mysql> create table t17(id decimal(65,30));
Query OK, 0 rows affected (0.32 sec)
mysql> desc t17;
+-------+----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------------+------+-----+---------+-------+
| id | decimal(65,30) | YES | | NULL | |
+-------+----------------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> insert into t17 values(1.1111111111111111111111);
Query OK, 1 row affected (0.13 sec)
mysql> select * from t17;
+----------------------------------+
| id |
+----------------------------------+
| 1.111111111111111111111100000000 |
+----------------------------------+
1 row in set (0.00 sec)
# decimal 类型 从23为开始模糊
【4】结论
-- float 类型 从第8为开始模糊
-- double 类型 从第16为开始模糊
-- decimal 类型 从23为开始模糊
--- 精确度 decimal > double > float
Ⅵ 字段类型之字符串类型
【一】字符串类型
【1】char
- 定长字符串
char(4)
#(超过四个字符直接报错,不够默认用空格补全 )
【2】varchar
- 不定长字段
varchar(4)
#(超过四个字符直接报错,不够有几个存几个)
【3】char 与 varchar 的比较
(1)创建表
- 语法
create table t18(name char(4));
create table t19(name varchar(4));
- 示例
mysql> create table t18(name char(4));
Query OK, 0 rows affected (0.41 sec)
mysql> create table t19(name varchar(4));
Query OK, 0 rows affected (0.33 sec)
mysql> desc t18;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| name | char(4) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> desc t19;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| name | varchar(4) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
1 row in set (0.00 sec)
(2)插入数据
- 语法
insert into t18 values('a');
insert into t19 values('a');
- 示例
mysql> insert into t18 values('a');
Query OK, 1 row affected (0.04 sec)
mysql> insert into t19 values('a');
Query OK, 1 row affected (0.06 sec)
mysql> select * from t18;
+------+
| name |
+------+
| a |
+------+
1 row in set (0.00 sec)
mysql> select * from t19;
+------+
| name |
+------+
| a |
+------+
1 row in set (0.00 sec)
(3)插入数据(超出字符范围)
mysql> insert into t18 values('abcde');
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> insert into t19 values('abcde');
ERROR 1406 (22001): Data too long for column 'name' at row 1
(4)优缺点对比
1.char
- 缺点:浪费空间
- 优点:存储简单
- 直接按照固定的字符存储数据即可
2.varchar
- 优点:节省空间
- 缺点:存储麻烦
- 存的时候,在真正数据的前面加报头(表示数据真正大小)
- 取的时候,需要先读取报头,才能读取真实的数据
以前用 char 现在 varchar 使用较多
【4】查看长度char_length
char_length :统计字段长度
- 语法
select char_length(name) from t18;
- 示例
mysql> select * from t18;
+------+
| name |
+------+
| a |
| abcd |
+------+
2 rows in set (0.00 sec)
mysql> select char_length(name) from t18;
+-------------------+
| char_length(name) |
+-------------------+
| 1 |
| 4 |
+-------------------+
2 rows in set (0.12 sec)
- char 在硬盘上存的绝对是真正的数据,带有空格
- 但是在现实的时候MySQL会自动将多余的空格删除
【5】取消默认去除空格
再次修改 sql_mode 让MySQL不做自动剔除操作
- 查看命令
show variables like "%mode";
mysql> show variables like "%mode";
+--------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| block_encryption_mode | aes-128-ecb |
| gtid_mode | OFF |
| innodb_autoinc_lock_mode | 1 |
| innodb_strict_mode | ON |
| offline_mode | OFF |
| pseudo_slave_mode | OFF |
| rbr_exec_mode | STRICT |
| slave_exec_mode | STRICT |
| sql_mode | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+--------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
9 rows in set, 1 warning (0.14 sec)
- 修改命令
要在原有的基础上进行替换,所以要带有原有的属性
加了一个严格模式PAD_CHAR_TO_FULL_LENGTH
# 在修改严格模式的时候去除或者增加单个 (带上原来的)
set global sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,PAD_CHAR_TO_FULL_LENGTH';
# 或者是直接覆盖掉
set session sql_mode = "PAD_CHAR_TO_FULL_LENGTH";
- 示例
mysql> set session sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,PAD_CHAR_TO_FULL_LENGTH';
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t18 values('a');
Query OK, 1 row affected (0.05 sec)
mysql> select * from t18;
+------+
| name |
+------+
| a |
| abcd |
| a |
+------+
3 rows in set (0.00 sec)
mysql> select char_length(name) from t18;
+-------------------+
| char_length(name) |
+-------------------+
| 4 |
| 4 |
| 4 |
+-------------------+
3 rows in set (0.00 sec)
mysql> insert into t19 values('a');
Query OK, 1 row affected (0.05 sec)
mysql> select * from t19;
+------+
| name |
+------+
| a |
| ab |
| abc |
| a |
+------+
4 rows in set (0.00 sec)
mysql> select char_length(name) from t19;
+-------------------+
| char_length(name) |
+-------------------+
| 1 |
| 2 |
| 3 |
| 1 |
+-------------------+
4 rows in set (0.00 sec)
Ⅶ 字段类型之日期时间类型
【一】日期时间类型
date : 年月日
datetime : 年月日时分秒
time : 时分秒
year : 年份
【二】示例
- 创建表
- 语法
create table student(
id int,
name varchar(16),
born_year year,
birth date,
study_time time,
reg_time datetime
);
- 示例
mysql> create table student(
-> id int,
-> name varchar(16),
-> born_year year,
-> birth date,
-> study_time time,
-> reg_time datetime
-> );
Query OK, 0 rows affected (0.38 sec)
mysql> desc student;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(16) | YES | | NULL | |
| born_year | year(4) | YES | | NULL | |
| birth | date | YES | | NULL | |
| study_time | time | YES | | NULL | |
| reg_time | datetime | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
【三】插入数据
- 语法
insert into student values(
1,
'silence',
'2024',
'2024-5-31',
'11:11:11',
'2023-6-30 11:11:11'
);
- 示例
mysql> insert into student values(
-> 1,
-> 'silence',
-> '2024',
-> '2024-5-31',
-> '11:11:11',
-> '2023-6-30 11:11:11'
-> );
Query OK, 1 row affected (0.13 sec)
mysql> select * from student;
+------+---------+-----------+------------+------------+---------------------+
| id | name | born_year | birth | study_time | reg_time |
+------+---------+-----------+------------+------------+---------------------+
| 1 | silence | 2024 | 2024-05-31 | 11:11:11 | 2023-06-30 11:11:11 |
+------+---------+-----------+------------+------------+---------------------+
1 row in set (0.00 sec)
- 数据错误会报错
insert into student values(
1,
'silence',
'128',
'2024-5-31',
'11:11:11',
'2023-6-30 11:11:11'
);
-- ERROR 1264 (22003): Out of range value for column 'born_year' at row 1
Ⅷ 字段类型之枚举和集合类型
【一】枚举和集合
-- 枚举:多选一 用枚举类型给多个备选项,但是你只能选一个 enum
-- 集合:多选多 用集合类型给多个备选项,但是你能选多个 set
【二】枚举类型 enum
枚举类型存储数据只能从候选项中选取一个才行
- 语法
create table user(
id int,
name char(16),
gender enum('male','female','others')
);
- 示例
mysql> create table user(
-> id int,
-> name char(16),
-> gender enum('male','female','others')
-> );
Query OK, 0 rows affected (0.25 sec)
mysql> desc user;
+--------+--------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(16) | YES | | NULL | |
| gender | enum('male','female','others') | YES | | NULL | |
+--------+--------------------------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
【1】插入数据
- 语法
# 正常插入
insert into user(id,name,gender) values(117,'silence','male');
# 异常插入
insert into user(id,name,gender) values(117,'silence','male,female');
- 示例
# 正常结果
mysql> insert into user(id,name,gender) values(117,'silence','male');
Query OK, 1 row affected (0.12 sec)
mysql> select * from user;
+------+------------------+--------+
| id | name | gender |
+------+------------------+--------+
| 117 | silence | male |
+------+------------------+--------+
1 row in set (0.00 sec)
# 异常结果
mysql> insert into user(id,name,gender) values(117,'silence','male,female');
ERROR 1265 (01000): Data truncated for column 'gender' at row 1
# 枚举类型存储数据只能从候选项中选取一个才行 这是两个'male,female'
【三】集合类型 set
- 语法
create table teacher(
id int,
name varchar(16),
gender enum('male','female','others'),
hobby set('read books','listen music','play games')
);
- 示例
mysql> create table teacher(
-> id int,
-> name varchar(16),
-> gender enum('male','female','others'),
-> hobby set('read books','listen music','play games')
-> );
Query OK, 0 rows affected (0.46 sec)
mysql> desc teacher;
+--------+-----------------------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-----------------------------------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(16) | YES | | NULL | |
| gender | enum('male','female','others') | YES | | NULL | |
| hobby | set('read books','listen music','play games') | YES | | NULL | |
+--------+-----------------------------------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
【1】插入数据
- 语法
insert into teacher values(
1,
'silence',
'male',
'read books'
);
insert into teacher values(
2,
'mark',
'female',
'read books,listen music'
);
- 示例
mysql> insert into teacher values(
-> 1,
-> 'silence',
-> 'male',
-> 'read books'
-> );
Query OK, 1 row affected (0.13 sec)
mysql> select * from teacher;
+------+---------+--------+------------+
| id | name | gender | hobby |
+------+---------+--------+------------+
| 1 | silence | male | read books |
+------+---------+--------+------------+
1 row in set (0.00 sec)
mysql> insert into teacher values(
-> 2,
-> 'mark',
-> 'female',
-> 'read books,listen music'
-> );
Query OK, 1 row affected (0.05 sec)
mysql> select * from teacher;
+------+---------+--------+-------------------------+
| id | name | gender | hobby |
+------+---------+--------+-------------------------+
| 1 | silence | male | read books |
| 2 | mark | female | read books,listen music |
+------+---------+--------+-------------------------+
2 rows in set (0.00 sec)
Ⅸ 总结
【一】字段类型
-- 【一】数字类型
-- 【1】整数类型
-- tinyint int bigint ...
-- 【2】浮点数类型
-- float double decimal
-- 【二】字符串类型
-- 开启严格模式
-- char 自动补全长度
-- varchar 不会自动补全
-- 【三】时间日期类型
-- date 日期 年月日
-- time 时间 时分秒
-- datetime 年月日时分秒
-- year 年份
-- 【四】枚举和集合
-- 枚举 enum 在多个备选项中只能选一个
-- 集合 set 在多个备选项中能选多个 多个之间用,合开
【二】约束条件
-- unsigned 无符号
-- null可以为空
-- not null 不能为空
-- zerofill 默认长度用0填充
-- unsigned约束条件用于指定某个字段只能包含非负数(正整数或零)。
-- 当将字段定义为unsigned时,它会限制该字段的取值范围为大于等于零的整数。
【三】严格模式
-- 【一】查看所有的严格模式
show variables like "%mode";
-- % 模糊查询任意长度
-- _ 模糊查询一个长度
-- 【二】修改严格模式
-- 【1】全局永久性修改
set global sql_mode = "";
-- 【2】局部临时性修改
set session sql_mode = "";
标签:set,MySQL,sec,mysql,类型,values,null,id,row
From: https://www.cnblogs.com/zyb123/p/18218766