mysql数据类型和常用函数

news2024/11/25 2:58:48

目录

1.整型

1.1参数signed和unsigned

1.2参数zerofill

1.3参数auto_increment

2.数字类型 

2.1floor()向下取整

2.2随机函数rand()

2.3重复函数repeat()

3.字符串类型

3.1length()查看字节长度,char_length()查看字符长度 

3.2字符集

3.2.1查看默认字符集

3.2.2查看已经创建的数据库的字符集

3.2.3修改已经创建的数据库的字符集

3.2.4修改mysql默认字符集

3.2.5两条转换表字符集的语句

3.2.6查看支持的字符集

3.3hex()函数

3.4cast()函数

3.5排序规则collation

3.6对于密码的加密,md5()函数

3.7upper()和lower()函数

3.8concat()和concat_ws()函数

3.9lpad()和rpad()函数

4.enum&set类型

5.时间类型

5.1日期函数

​编辑 5.1.1now()函数

5.1.2now()和sysdate()的区别

5.1.3date_add()和date_sub()函数

5.1.4date_format()函数

6.json类型

6.1json类型相关函数

6.1.1json_extract()函数

 6.1.2json_remove()函数


mysql的数据类型有:整型、数字类型、字符串类型、日期类型、json类型等主要类型

1.整型

1.1参数signed和unsigned

可以创建有符号(signed)和无符号(unsigned)的整型类型 

(root@localhost) [(none)]> use python;
Database changed
(root@localhost) [python]> create table z (a int unsigned,b tinyint signed);
Query OK, 0 rows affected (0.58 sec)

(root@localhost) [python]> insert into z values(1,1);
Query OK, 1 row affected (0.13 sec)

(root@localhost) [python]> insert into z values(-1,1);
ERROR 1264 (22003): Out of range value for column 'a' at row 1
(root@localhost) [python]> insert into z values(1,-1);
Query OK, 1 row affected (0.05 sec)

(root@localhost) [python]> select * from z;
+------+------+
| a    | b    |
+------+------+
|    1 |    1 |
|    1 |   -1 |
+------+------+
2 rows in set (0.00 sec)

1.2参数zerofill

查看创建表ddl有int(10)和tinyint(4),其中10和4代表显示的数值总长度,长度不够用0填充

(root@localhost) [python]> show create table z\G
*************************** 1. row ***************************
       Table: z
Create Table: CREATE TABLE `z` (
  `a` int(10) unsigned DEFAULT NULL,
  `b` tinyint(4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.04 sec)

(root@localhost) [python]> alter table z change column a a int unsigned zerofill;
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

(root@localhost) [python]> select * from z;
+------------+------+
| a          | b    |
+------------+------+
| 0000000001 |    1 |
| 0000000001 |   -1 |
+------------+------+
2 rows in set (0.00 sec)

(root@localhost) [python]> show create table z\G
*************************** 1. row ***************************
       Table: z
Create Table: CREATE TABLE `z` (
  `a` int(10) unsigned zerofill DEFAULT NULL,
  `b` tinyint(4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

可以修改显示的总长度

(root@localhost) [python]> alter table z change column a a int(4) unsigned zerofill;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

(root@localhost) [python]> select * from z;
+------+------+
| a    | b    |
+------+------+
| 0001 |    1 |
| 0001 |   -1 |
+------+------+
2 rows in set (0.00 sec)

(root@localhost) [python]> show create table z\G
*************************** 1. row ***************************
       Table: z
Create Table: CREATE TABLE `z` (
  `a` int(4) unsigned zerofill DEFAULT NULL,
  `b` tinyint(4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

可以正常插入比显示长度更大的值

(root@localhost) [python]> insert into z values(20000,2);
Query OK, 1 row affected (0.00 sec)

(root@localhost) [python]> select * from z;
+-------+------+
| a     | b    |
+-------+------+
|  0001 |    1 |
|  0001 |   -1 |
| 20000 |    2 |
+-------+------+
3 rows in set (0.00 sec)

1.3参数auto_increment

设置该参数,主键自动递增,select last_insert_id()查询上一次自增id值是多少,表数据量大主键自增字段建议用bigint类型

(root@localhost) [python]> truncate table z;
Query OK, 0 rows affected (0.13 sec)

(root@localhost) [python]> alter table z change column a a int auto_increment primary key;
Query OK, 0 rows affected (0.11 sec)
Records: 0  Duplicates: 0  Warnings: 0

(root@localhost) [python]> show create table z\G
*************************** 1. row ***************************
       Table: z
Create Table: CREATE TABLE `z` (
  `a` int(11) NOT NULL AUTO_INCREMENT,
  `b` tinyint(4) DEFAULT NULL,
  PRIMARY KEY (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

(root@localhost) [python]> insert into z values(null,10);
Query OK, 1 row affected (0.01 sec)

(root@localhost) [python]> insert into z values(null,20);
Query OK, 1 row affected (0.01 sec)

(root@localhost) [python]> insert into z(b) values(30);
Query OK, 1 row affected (0.05 sec)

(root@localhost) [python]> insert into z values(40);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
(root@localhost) [python]> select * from z;
+---+------+
| a | b    |
+---+------+
| 1 |   10 |
| 2 |   20 |
| 3 |   30 |
+---+------+
3 rows in set (0.00 sec)

(root@localhost) [python]> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
|                3 |
+------------------+
1 row in set (0.06 sec)

 自增字段值在不重启数据库的情况下不会回溯,只会越来越大

(root@localhost) [python]> delete from z where b=30;
Query OK, 1 row affected (0.06 sec)

(root@localhost) [python]> insert into z values(null,30);
Query OK, 1 row affected (0.04 sec)

(root@localhost) [python]> select * from z;
+---+------+
| a | b    |
+---+------+
| 1 |   10 |
| 2 |   20 |
| 4 |   30 |
+---+------+
3 rows in set (0.00 sec)

自增字段在删除select last_insert_id()的值后,重启数据库会回溯,查看ddl,AUTO_INCREMENT=7

(root@localhost) [python]> select * from z;
+---+------+
| a | b    |
+---+------+
| 1 |   10 |
| 2 |   20 |
| 5 |   40 |
| 6 |   30 |
+---+------+
4 rows in set (0.00 sec)

(root@localhost) [python]> show create table z\G
*************************** 1. row ***************************
       Table: z
Create Table: CREATE TABLE `z` (
  `a` int(11) NOT NULL AUTO_INCREMENT,
  `b` tinyint(4) DEFAULT NULL,
  PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

(root@localhost) [python]> delete from z where a=6;
Query OK, 1 row affected (0.01 sec)

(root@localhost) [python]> delete from z where a=5;
Query OK, 1 row affected (0.01 sec)

(root@localhost) [python]> select * from z;
+---+------+
| a | b    |
+---+------+
| 1 |   10 |
| 2 |   20 |
+---+------+
2 rows in set (0.00 sec)

删除a=6、a=5后重启数据库查看ddl,AUTO_INCREMENT=3,此时就回溯了,再次插入数据a=3、a=4就回来了,线上删除的值就又出现了,会乱掉,这个问题在mysql8.0得到解决

(root@localhost) [python]> show create table z\G
*************************** 1. row ***************************
       Table: z
Create Table: CREATE TABLE `z` (
  `a` int(11) NOT NULL AUTO_INCREMENT,
  `b` tinyint(4) DEFAULT NULL,
  PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

(root@localhost) [python]> select * from z;
+---+------+
| a | b    |
+---+------+
| 1 |   10 |
| 2 |   20 |
+---+------+
2 rows in set (0.00 sec)

(root@localhost) [python]> insert into z values(null,30);
Query OK, 1 row affected (0.00 sec)

(root@localhost) [python]> insert into z values(null,40);
Query OK, 1 row affected (0.01 sec)

(root@localhost) [python]> select * from z;
+---+------+
| a | b    |
+---+------+
| 1 |   10 |
| 2 |   20 |
| 3 |   30 |
| 4 |   40 |
+---+------+
4 rows in set (0.00 sec)

(root@localhost) [python]>

mysql8.0解决自增字段回溯问题,AUTO_INCREMENT值持久化

[root@localhost ~]# mysql -S/tmp/mysql.sock80 -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 8.0.35 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

(root@localhost) [(none)]> create database zs;
Query OK, 1 row affected (0.42 sec)

(root@localhost) [(none)]> use zs
Database changed
(root@localhost) [zs]> create table z (a bigint auto_increment primary key,b tinyint);
Query OK, 0 rows affected (1.36 sec)

(root@localhost) [zs]> show create table z\G;
*************************** 1. row ***************************
       Table: z
Create Table: CREATE TABLE `z` (
  `a` bigint NOT NULL AUTO_INCREMENT,
  `b` tinyint DEFAULT NULL,
  PRIMARY KEY (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.22 sec)

ERROR: 
No query specified

(root@localhost) [zs]> insert into z values(null,10);
Query OK, 1 row affected (0.08 sec)

(root@localhost) [zs]> insert into z values(null,20);
Query OK, 1 row affected (0.05 sec)

(root@localhost) [zs]> insert into z values(null,30);
Query OK, 1 row affected (0.05 sec)

(root@localhost) [zs]> insert into z values(null,40);
Query OK, 1 row affected (0.01 sec)

(root@localhost) [zs]> show create table z\G;
*************************** 1. row ***************************
       Table: z
Create Table: CREATE TABLE `z` (
  `a` bigint NOT NULL AUTO_INCREMENT,
  `b` tinyint DEFAULT NULL,
  PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)

ERROR: 
No query specified

(root@localhost) [zs]> select * from z;
+---+------+
| a | b    |
+---+------+
| 1 |   10 |
| 2 |   20 |
| 3 |   30 |
| 4 |   40 |
+---+------+
4 rows in set (0.01 sec)

(root@localhost) [zs]> delete from z where a=4;
Query OK, 1 row affected (0.06 sec)

(root@localhost) [zs]> delete from z where a=3;
Query OK, 1 row affected (0.05 sec)

(root@localhost) [zs]> select * from z;
+---+------+
| a | b    |
+---+------+
| 1 |   10 |
| 2 |   20 |
+---+------+
2 rows in set (0.00 sec)

(root@localhost) [zs]> show create table z\G;
*************************** 1. row ***************************
       Table: z
Create Table: CREATE TABLE `z` (
  `a` bigint NOT NULL AUTO_INCREMENT,
  `b` tinyint DEFAULT NULL,
  PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)

ERROR: 
No query specified

(root@localhost) [zs]> exit
Bye
[root@localhost ~]# mysqld_multi stop 80
[root@localhost ~]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld32 is not running
MySQL server from group: mysqld1 is not running
MySQL server from group: mysqld2 is not running
MySQL server from group: mysqld80 is not running
[root@localhost ~]# mysqld_multi start 80
[root@localhost ~]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld32 is not running
MySQL server from group: mysqld1 is not running
MySQL server from group: mysqld2 is not running
MySQL server from group: mysqld80 is running
[root@localhost ~]# mysql -S/tmp/mysql.sock80 -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.35 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

(root@localhost) [(none)]> use zs;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
(root@localhost) [zs]> show create table z\G;
*************************** 1. row ***************************
       Table: z
Create Table: CREATE TABLE `z` (
  `a` bigint NOT NULL AUTO_INCREMENT,
  `b` tinyint DEFAULT NULL,
  PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)

ERROR: 
No query specified

(root@localhost) [zs]> select * from z;
+---+------+
| a | b    |
+---+------+
| 1 |   10 |
| 2 |   20 |
+---+------+
2 rows in set (0.00 sec)

(root@localhost) [zs]> insert into z values(null,30);
Query OK, 1 row affected (0.01 sec)

(root@localhost) [zs]> insert into z values(null,40);
Query OK, 1 row affected (0.00 sec)

(root@localhost) [zs]> select * from z;
+---+------+
| a | b    |
+---+------+
| 1 |   10 |
| 2 |   20 |
| 5 |   30 |
| 6 |   40 |
+---+------+
4 rows in set (0.00 sec)

(root@localhost) [zs]>

int类型总结

  • 推荐不要使用UNSIGNED
  • 范围本质上没有大的改变
  • UNSIGNED可能会有溢出现象发生
  • 自增INT类型主键建议使用BIGINT

2.数字类型 

一般建议用decimal 

2.1floor()向下取整

(root@localhost) [(none)]> select floor(1.5);
+------------+
| floor(1.5) |
+------------+
|          1 |
+------------+
1 row in set (0.00 sec)

(root@localhost) [(none)]> select floor(1.9);
+------------+
| floor(1.9) |
+------------+
|          1 |
+------------+
1 row in set (0.00 sec)

(root@localhost) [(none)]> select floor(-1.9);
+-------------+
| floor(-1.9) |
+-------------+
|          -2 |
+-------------+
1 row in set (0.00 sec)

2.2随机函数rand()

(root@localhost) [(none)]> select rand() ;
+-------------------+
| rand()            |
+-------------------+
| 0.590530601880076 |
+-------------------+
1 row in set (0.00 sec)

(root@localhost) [(none)]> select rand() ;
+--------------------+
| rand()             |
+--------------------+
| 0.1034116913596277 |
+--------------------+
1 row in set (0.00 sec)

1~100随机整数

(root@localhost) [(none)]> select floor(1+rand()*99);
+--------------------+
| floor(1+rand()*99) |
+--------------------+
|                 74 |
+--------------------+
1 row in set (0.00 sec)

(root@localhost) [(none)]> select floor(1+rand()*99);
+--------------------+
| floor(1+rand()*99) |
+--------------------+
|                 42 |
+--------------------+
1 row in set (0.00 sec)

(root@localhost) [(none)]> select floor(1+rand()*99);
+--------------------+
| floor(1+rand()*99) |
+--------------------+
|                 85 |
+--------------------+
1 row in set (0.00 sec)

2.3重复函数repeat()

(root@localhost) [(none)]> select repeat(1,3);
+-------------+
| repeat(1,3) |
+-------------+
| 111         |
+-------------+
1 row in set (0.00 sec)

(root@localhost) [(none)]> select repeat(32,3);
+--------------+
| repeat(32,3) |
+--------------+
| 323232       |
+--------------+
1 row in set (0.00 sec)

(root@localhost) [(none)]> select repeat('b',5);
+---------------+
| repeat('b',5) |
+---------------+
| bbbbb         |
+---------------+
1 row in set (0.00 sec)

(root@localhost) [(none)]> select repeat('ba',4);
+----------------+
| repeat('ba',4) |
+----------------+
| babababa       |
+----------------+
1 row in set (0.00 sec)

字符串或数字变长

(root@localhost) [mysql]> select repeat('a',floor(1+rand()*99));
+-----------------------------------------------------------------------------------------------------+
| repeat('a',floor(1+rand()*99))                                                                      |
+-----------------------------------------------------------------------------------------------------+
| aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
+-----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

(root@localhost) [mysql]> select repeat('a',floor(1+rand()*99));
+--------------------------------------------+
| repeat('a',floor(1+rand()*99))             |
+--------------------------------------------+
| aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
+--------------------------------------------+
1 row in set (0.00 sec)

(root@localhost) [mysql]> select repeat('b1',floor(1+rand()*20));
+---------------------------------+
| repeat('b1',floor(1+rand()*20)) |
+---------------------------------+
| b1b1b1                          |
+---------------------------------+
1 row in set (0.00 sec)

(root@localhost) [mysql]> select repeat('b1',floor(1+rand()*20));
+---------------------------------+
| repeat('b1',floor(1+rand()*20)) |
+---------------------------------+
| b1b1b1b1b1b1b1b1b1              |
+---------------------------------+
1 row in set (0.00 sec)

3.字符串类型

char(n)和varchar(n)中的n代表字符,有字符集

(root@localhost) [python]> drop table z;
Query OK, 0 rows affected (0.72 sec)

(root@localhost) [python]> create table z (a char(1),b varchar(3),c binary(1),d varbinary(3));
Query OK, 0 rows affected (0.02 sec)

(root@localhost) [python]> insert into z values('a','bbb','c','ddd');
Query OK, 1 row affected (0.01 sec)

(root@localhost) [python]> select * from z;
+------+------+------+------+
| a    | b    | c    | d    |
+------+------+------+------+
| a    | bbb  | c    | ddd  |
+------+------+------+------+
1 row in set (0.00 sec)

(root@localhost) [python]> show create table z\G
*************************** 1. row ***************************
       Table: z
Create Table: CREATE TABLE `z` (
  `a` char(1) DEFAULT NULL,
  `b` varchar(3) DEFAULT NULL,
  `c` binary(1) DEFAULT NULL,
  `d` varbinary(3) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

(root@localhost) [python]> insert into z values('紫','bbc','x','ooo');
ERROR 1366 (HY000): Incorrect string value: '\xE7\xB4\xAB' for column 'a' at row 1
(root@localhost) [python]> alter table z convert to charset utf8mb4;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

(root@localhost) [python]> show create table z\G
*************************** 1. row ***************************
       Table: z
Create Table: CREATE TABLE `z` (
  `a` char(1) DEFAULT NULL,
  `b` varchar(3) DEFAULT NULL,
  `c` binary(1) DEFAULT NULL,
  `d` varbinary(3) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)

(root@localhost) [python]> insert into z values('紫','bbc','x','ooo');
Query OK, 1 row affected (0.00 sec)

(root@localhost) [python]> insert into z values('x','bbc','紫','ooo');
ERROR 1406 (22001): Data too long for column 'c' at row 1

3.1length()查看字节长度,char_length()查看字符长度 

(root@localhost) [python]> select length('紫'),char_length('紫');
+---------------+--------------------+
| length('紫')  | char_length('紫')  |
+---------------+--------------------+
|             3 |                  1 |
+---------------+--------------------+
1 row in set (0.00 sec)

3.2字符集

mysql中的字符集最小是在列级别上

3.2.1查看默认字符集

(root@localhost) [python]> show variables like '%character%';
+--------------------------+----------------------------------------------------------------+
| Variable_name            | Value                                                          |
+--------------------------+----------------------------------------------------------------+
| character_set_client     | utf8                                                           |
| character_set_connection | utf8                                                           |
| character_set_database   | latin1                                                         |
| character_set_filesystem | binary                                                         |
| character_set_results    | utf8                                                           |
| character_set_server     | utf8mb4                                                        |
| character_set_system     | utf8                                                           |
| character_sets_dir       | /usr/local/mysql-5.7.44-linux-glibc2.12-x86_64/share/charsets/ |
+--------------------------+----------------------------------------------------------------+

3.2.2查看已经创建的数据库的字符集

(root@localhost) [zs]> show create database python;
+----------+-------------------------------------------------------------------+
| Database | Create Database                                                   |
+----------+-------------------------------------------------------------------+
| python   | CREATE DATABASE `python` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-------------------------------------------------------------------+
1 row in set (0.00 sec)

3.2.3修改已经创建的数据库的字符集

(root@localhost) [python]> alter database python charset utf8mb4;
Query OK, 1 row affected (0.00 sec)

(root@localhost) [python]> show create database python;
+----------+--------------------------------------------------------------------+
| Database | Create Database                                                    |
+----------+--------------------------------------------------------------------+
| python   | CREATE DATABASE `python` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ |
+----------+--------------------------------------------------------------------+
1 row in set (0.00 sec)

3.2.4修改mysql默认字符集

在my.cnf中加入参数

[mysqld]

character_set_server=utf8mb4

3.2.5两条转换表字符集的语句

alter table t charset utf8mb4;与alter table t convert to charset(或者character set) utf8mb4;的不同,前者对已经存在的列不进行字符集转换,只对新增列转换,后者是对已经存在的列和新增列都转换,旧的字符集是新的字符集的子集,在生产上要注意转换字符集是要锁表的,要小心

(root@localhost) [python]> drop table t;
Query OK, 0 rows affected (0.01 sec)

(root@localhost) [python]> create table t(a varchar(10),b char(2)) charset gbk;
Query OK, 0 rows affected (0.04 sec)

(root@localhost) [python]> show create table t\G;
*************************** 1. row ***************************
       Table: t
Create Table: CREATE TABLE `t` (
  `a` varchar(10) DEFAULT NULL,
  `b` char(2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk
1 row in set (0.00 sec)

ERROR: 
No query specified

(root@localhost) [python]> alter table t charset utf8mb4;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

(root@localhost) [python]> show create table t\G;
*************************** 1. row ***************************
       Table: t
Create Table: CREATE TABLE `t` (
  `a` varchar(10) CHARACTER SET gbk DEFAULT NULL,
  `b` char(2) CHARACTER SET gbk DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)

ERROR: 
No query specified

(root@localhost) [python]> alter table t add column c varchar(11);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

(root@localhost) [python]> show create table t\G;
*************************** 1. row ***************************
       Table: t
Create Table: CREATE TABLE `t` (
  `a` varchar(10) CHARACTER SET gbk DEFAULT NULL,
  `b` char(2) CHARACTER SET gbk DEFAULT NULL,
  `c` varchar(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)

ERROR: 
No query specified

(root@localhost) [python]> alter table t convert to charset utf8mb4;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

(root@localhost) [python]> show create table t\G;
*************************** 1. row ***************************
       Table: t
Create Table: CREATE TABLE `t` (
  `a` varchar(10) DEFAULT NULL,
  `b` char(2) DEFAULT NULL,
  `c` varchar(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)

ERROR: 
No query specified

3.2.6查看支持的字符集

show charset,Maxlen表示最大的一个字符占用多少个字节

(root@localhost) [python]> show charset;
+----------+---------------------------------+---------------------+--------+
| Charset  | Description                     | Default collation   | Maxlen |
+----------+---------------------------------+---------------------+--------+
| big5     | Big5 Traditional Chinese        | big5_chinese_ci     |      2 |
| dec8     | DEC West European               | dec8_swedish_ci     |      1 |
| cp850    | DOS West European               | cp850_general_ci    |      1 |
| hp8      | HP West European                | hp8_english_ci      |      1 |
| koi8r    | KOI8-R Relcom Russian           | koi8r_general_ci    |      1 |
| latin1   | cp1252 West European            | latin1_swedish_ci   |      1 |
| latin2   | ISO 8859-2 Central European     | latin2_general_ci   |      1 |
| swe7     | 7bit Swedish                    | swe7_swedish_ci     |      1 |
| ascii    | US ASCII                        | ascii_general_ci    |      1 |
| ujis     | EUC-JP Japanese                 | ujis_japanese_ci    |      3 |
| sjis     | Shift-JIS Japanese              | sjis_japanese_ci    |      2 |
| hebrew   | ISO 8859-8 Hebrew               | hebrew_general_ci   |      1 |
| tis620   | TIS620 Thai                     | tis620_thai_ci      |      1 |
| euckr    | EUC-KR Korean                   | euckr_korean_ci     |      2 |
| koi8u    | KOI8-U Ukrainian                | koi8u_general_ci    |      1 |
| gb2312   | GB2312 Simplified Chinese       | gb2312_chinese_ci   |      2 |
| greek    | ISO 8859-7 Greek                | greek_general_ci    |      1 |
| cp1250   | Windows Central European        | cp1250_general_ci   |      1 |
| gbk      | GBK Simplified Chinese          | gbk_chinese_ci      |      2 |
| latin5   | ISO 8859-9 Turkish              | latin5_turkish_ci   |      1 |
| armscii8 | ARMSCII-8 Armenian              | armscii8_general_ci |      1 |
| utf8     | UTF-8 Unicode                   | utf8_general_ci     |      3 |
| ucs2     | UCS-2 Unicode                   | ucs2_general_ci     |      2 |
| cp866    | DOS Russian                     | cp866_general_ci    |      1 |
| keybcs2  | DOS Kamenicky Czech-Slovak      | keybcs2_general_ci  |      1 |
| macce    | Mac Central European            | macce_general_ci    |      1 |
| macroman | Mac West European               | macroman_general_ci |      1 |
| cp852    | DOS Central European            | cp852_general_ci    |      1 |
| latin7   | ISO 8859-13 Baltic              | latin7_general_ci   |      1 |
| utf8mb4  | UTF-8 Unicode                   | utf8mb4_general_ci  |      4 |
| cp1251   | Windows Cyrillic                | cp1251_general_ci   |      1 |
| utf16    | UTF-16 Unicode                  | utf16_general_ci    |      4 |
| utf16le  | UTF-16LE Unicode                | utf16le_general_ci  |      4 |
| cp1256   | Windows Arabic                  | cp1256_general_ci   |      1 |
| cp1257   | Windows Baltic                  | cp1257_general_ci   |      1 |
| utf32    | UTF-32 Unicode                  | utf32_general_ci    |      4 |
| binary   | Binary pseudo charset           | binary              |      1 |
| geostd8  | GEOSTD8 Georgian                | geostd8_general_ci  |      1 |
| cp932    | SJIS for Windows Japanese       | cp932_japanese_ci   |      2 |
| eucjpms  | UJIS for Windows Japanese       | eucjpms_japanese_ci |      3 |
| gb18030  | China National Standard GB18030 | gb18030_chinese_ci  |      4 |
+----------+---------------------------------+---------------------+--------+
41 rows in set (0.00 sec)

3.3hex()函数

查看字符的16进制

(root@localhost) [python]> select hex('a');
+----------+
| hex('a') |
+----------+
| 61       |
+----------+
1 row in set (0.00 sec)

(root@localhost) [python]> select hex('ab');
+-----------+
| hex('ab') |
+-----------+
| 6162      |
+-----------+
1 row in set (0.00 sec)

(root@localhost) [python]> select hex('abc');
+------------+
| hex('abc') |
+------------+
| 616263     |
+------------+
1 row in set (0.00 sec)

(root@localhost) [python]> select hex('紫');
+------------+
| hex('紫')  |
+------------+
| E7B4AB     |
+------------+
1 row in set (0.00 sec)

(root@localhost) [python]> select 0xE7B4AB;
+----------+
| 0xE7B4AB |
+----------+
| 紫       |
+----------+
1 row in set (0.00 sec)

(root@localhost) [python]> insert into t values('aaa',0xE7B4AB,0xE7B4AB);
Query OK, 1 row affected (0.00 sec)

(root@localhost) [python]> select * from t;
+------+------+------+
| a    | b    | c    |
+------+------+------+
| aaa  | 紫   | 紫   |
+------+------+------+
1 row in set (0.00 sec)

3.4cast()函数

CAST函数用于将某种数据类型的表达式显式转换为另一种数据类型。CAST()函数的参数是一个表达式,它包括用AS关键字分隔的源值和目标数据类型。

语法

CAST(value AS datatype)

datatype参数取值 

描述
DATE将value转换成'YYYY-MM-DD'格式
DATETIME将value转换成'YYYY-MM-DD HH:MM:SS'格式
TIME将value转换成'HH:MM:SS'格式
CHAR将value转换成CHAR(固定长度的字符串)格式
SIGNED将value转换成INT(有符号的整数)格式
UNSIGNED将value转换成INT(无符号的整数)格式
DECIMAL将value转换成FLOAT(浮点数)格式 
BINARY将value转换成二进制格式
(root@localhost) [python]> SELECT CAST('13.6' AS decimal(9,2));
+------------------------------+
| CAST('13.6' AS decimal(9,2)) |
+------------------------------+
|                        13.60 |
+------------------------------+
1 row in set (0.00 sec)

(root@localhost) [python]> select cast(123 as char(1));
+----------------------+
| cast(123 as char(1)) |
+----------------------+
| 1                    |
+----------------------+
1 row in set, 1 warning (0.00 sec)

(root@localhost) [python]> select cast(123 as char(10));
+-----------------------+
| cast(123 as char(10)) |
+-----------------------+
| 123                   |
+-----------------------+
1 row in set (0.00 sec)

(root@localhost) [python]> select cast('a' as char(10));
+-----------------------+
| cast('a' as char(10)) |
+-----------------------+
| a                     |
+-----------------------+
1 row in set (0.01 sec)

(root@localhost) [python]> select cast('a' as char(10) charset gbk);
+-----------------------------------+
| cast('a' as char(10) charset gbk) |
+-----------------------------------+
| a                                 |
+-----------------------------------+
1 row in set (0.00 sec)

(root@localhost) [python]> select hex(cast('a' as char(10) charset gbk));
+----------------------------------------+
| hex(cast('a' as char(10) charset gbk)) |
+----------------------------------------+
| 61                                     |
+----------------------------------------+
1 row in set (0.00 sec)

(root@localhost) [python]> select hex('a');
+----------+
| hex('a') |
+----------+
| 61       |
+----------+
1 row in set (0.00 sec)

(root@localhost) [python]> select hex(cast('紫' as char(10) charset gbk));
+------------------------------------------+
| hex(cast('紫' as char(10) charset gbk))  |
+------------------------------------------+
| D7CF                                     |
+------------------------------------------+
1 row in set (0.00 sec)

(root@localhost) [python]> select hex('紫');
+------------+
| hex('紫')  |
+------------+
| E7B4AB     |
+------------+
1 row in set (0.00 sec)

(root@localhost) [python]> select 0xD7CF;
+--------+
| 0xD7CF |
+--------+
| ؏       |
+--------+
1 row in set (0.00 sec)

(root@localhost) [python]> show create table z\G;
*************************** 1. row ***************************
       Table: z
Create Table: CREATE TABLE `z` (
  `a` char(1) DEFAULT NULL,
  `b` varchar(3) DEFAULT NULL,
  `c` binary(1) DEFAULT NULL,
  `d` varbinary(3) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)

ERROR: 
No query specified

(root@localhost) [python]> insert into z values('a',0xD7CF,'c',0xD7CF);
ERROR 1366 (HY000): Incorrect string value: '\xD7\xCF' for column 'b' at row 1
(root@localhost) [python]> insert into z values('a',0xE7B4AB,'c',0xD7CF);
Query OK, 1 row affected (0.00 sec)

(root@localhost) [python]> select * from z;
+------+------+------+------+
| a    | b    | c    | d    |
+------+------+------+------+
| a    | bbb  | c    | ddd  |
| 紫   | bbc  | x    | ooo  |
| a    | 紫   | c    | ؏     |
+------+------+------+------+
3 rows in set (0.00 sec)

从上面看出,varbinary类型不会检测对应的字符是否存在,只会去存16进制的这个值,varchar类型会去检测16进制的值对应的字符是否存在

3.5排序规则collation

(root@localhost) [(none)]> select 'a' = 'a';
+-----------+
| 'a' = 'a' |
+-----------+
|         1 |
+-----------+
1 row in set (0.00 sec)

(root@localhost) [(none)]> select 'a' = 'a    ';
+---------------+
| 'a' = 'a    ' |
+---------------+
|             1 |
+---------------+
1 row in set (0.00 sec)

(root@localhost) [(none)]> select 'a' = 'A';
+-----------+
| 'a' = 'A' |
+-----------+
|         1 |
+-----------+
1 row in set (0.01 sec)

(root@localhost) [(none)]> select 'a' = 'A    ';
+---------------+
| 'a' = 'A    ' |
+---------------+
|             1 |
+---------------+
1 row in set (0.00 sec)

(root@localhost) [python]> drop table t;
Query OK, 0 rows affected (0.02 sec)

(root@localhost) [python]> create table t (a char(1),unique key(a));
Query OK, 0 rows affected (0.02 sec)

(root@localhost) [python]> insert into t values('a');
Query OK, 1 row affected (0.00 sec)

(root@localhost) [python]> insert into t values('A');
ERROR 1062 (23000): Duplicate entry 'A' for key 'a'
(root@localhost) [python]> insert into t values('a    ');
ERROR 1062 (23000): Duplicate entry 'a' for key 'a'
(root@localhost) [python]> insert into t values('A    ');
ERROR 1062 (23000): Duplicate entry 'A' for key 'a'
(root@localhost) [python]> select * from t;
+------+
| a    |
+------+
| a    |
+------+
1 row in set (0.00 sec)

 show charset看到的有一列是Default collation,其中utf8mb4字符集对应的排序规则是utf8mb4_general_ci,ci(case insensitive)表示不区分大小写 

3.6对于密码的加密,md5()函数

(root@localhost) [python]> drop table t;
Query OK, 0 rows affected (0.01 sec)

(root@localhost) [python]> create table t(password varchar(128));
Query OK, 0 rows affected (0.01 sec)

(root@localhost) [python]> insert into t values('aaa');
Query OK, 1 row affected (0.00 sec)

(root@localhost) [python]> insert into t values('BBB');
Query OK, 1 row affected (0.01 sec)

(root@localhost) [python]> insert into t values(md5('aaa'));
Query OK, 1 row affected (0.01 sec)

(root@localhost) [python]> insert into t values(md5('BBB'));
Query OK, 1 row affected (0.00 sec)

(root@localhost) [python]> select * from t;
+----------------------------------+
| password                         |
+----------------------------------+
| aaa                              |
| BBB                              |
| 47bce5c74f589f4867dbd57e9ca9f808 |
| 2bb225f0ba9a58930757a868ed57d9a3 |
+----------------------------------+
4 rows in set (0.00 sec)

(root@localhost) [python]> insert into t values(md5(concat('aaa','zs')));
Query OK, 1 row affected (0.00 sec)

(root@localhost) [python]> insert into t values(md5(concat('BBB','zs')));
Query OK, 1 row affected (0.01 sec)

(root@localhost) [python]> select * from t;
+----------------------------------+
| password                         |
+----------------------------------+
| aaa                              |
| BBB                              |
| 47bce5c74f589f4867dbd57e9ca9f808 |
| 2bb225f0ba9a58930757a868ed57d9a3 |
| ebf93b07e04f07d5efea104226ce36f5 |
| 5812c79db401f78da7fd2aaf03f9ca97 |
+----------------------------------+
6 rows in set (0.00 sec)

密码通过加密存储,一般来说也不区分大小写

强制密码区分大小写

(root@localhost) [python]> create table y (password varchar(128) collate utf8mb4_bin,unique key(password));
Query OK, 0 rows affected (0.01 sec)

(root@localhost) [python]> insert into y values('a');
Query OK, 1 row affected (0.00 sec)

(root@localhost) [python]> insert into y values('A');
Query OK, 1 row affected (0.00 sec)

(root@localhost) [python]> select * from y;
+----------+
| password |
+----------+
| A        |
| a        |
+----------+
2 rows in set (0.00 sec)

3.7upper()和lower()函数

upper()字符串转换成大写,lower()字符串转换成小写

(root@localhost) [python]> select upper('abC');
+--------------+
| upper('abC') |
+--------------+
| ABC          |
+--------------+
1 row in set (0.01 sec)

(root@localhost) [python]> select lower('aBC');
+--------------+
| lower('aBC') |
+--------------+
| abc          |
+--------------+
1 row in set (0.01 sec)

3.8concat()和concat_ws()函数

concat()直接把字符串拼接起来,concat_ws()通过指定的符号把字符串拼接起来

(root@localhost) [python]> select concat('a','b','ccc',123);
+---------------------------+
| concat('a','b','ccc',123) |
+---------------------------+
| abccc123                  |
+---------------------------+
1 row in set (0.00 sec)

(root@localhost) [python]> select concat_ws('!','a','b','ccc',123);
+----------------------------------+
| concat_ws('!','a','b','ccc',123) |
+----------------------------------+
| a!b!ccc!123                      |
+----------------------------------+
1 row in set (0.00 sec)

3.9lpad()和rpad()函数

lpad()左填充,rpad()右填充

(root@localhost) [python]> select lpad('aaa',8,'!');
+-------------------+
| lpad('aaa',8,'!') |
+-------------------+
| !!!!!aaa          |
+-------------------+
1 row in set (0.00 sec)

(root@localhost) [python]> select rpad('aaa',8,'!');
+-------------------+
| rpad('aaa',8,'!') |
+-------------------+
| aaa!!!!!          |
+-------------------+
1 row in set (0.00 sec)

4.enum&set类型

枚举类型常用于性别等distinct的值不多的情况 

(root@localhost) [python]> drop table y;
Query OK, 0 rows affected (0.01 sec)

(root@localhost) [python]> create table y (sex enum('male','female'));
Query OK, 0 rows affected (0.01 sec)

(root@localhost) [python]> 
(root@localhost) [python]> insert into y values('male');
Query OK, 1 row affected (0.00 sec)

(root@localhost) [python]> insert into y values('male11');
ERROR 1265 (01000): Data truncated for column 'sex' at row 1
(root@localhost) [python]> insert into y values('female');
Query OK, 1 row affected (0.00 sec)

(root@localhost) [python]> insert into y values('zhs');
ERROR 1265 (01000): Data truncated for column 'sex' at row 1
(root@localhost) [python]> select * from y;
+--------+
| sex    |
+--------+
| male   |
| female |
+--------+
2 rows in set (0.01 sec)

5.时间类型

常用日期类型:datetime和timestamp 

5.1日期函数

 5.1.1now()函数

unix_timestamp(now())是1970-01-01 00:00:00到现在的秒数 

(root@localhost) [dbt3]> select now();
+---------------------+
| now()               |
+---------------------+
| 2024-03-21 13:21:05 |
+---------------------+
1 row in set (0.00 sec)

(root@localhost) [dbt3]> select unix_timestamp(now());
+-----------------------+
| unix_timestamp(now()) |
+-----------------------+
|            1710998483 |
+-----------------------+
1 row in set (0.00 sec)

(root@localhost) [dbt3]> select timestamp(now());
+---------------------+
| timestamp(now())    |
+---------------------+
| 2024-03-21 13:21:29 |
+---------------------+
1 row in set (0.00 sec)

(root@localhost) [dbt3]> create table z (a datetime,b timestamp);
Query OK, 0 rows affected (0.01 sec)

(root@localhost) [dbt3]> insert into z values(now(),now());
Query OK, 1 row affected (0.00 sec)

(root@localhost) [dbt3]> select * from z;
+---------------------+---------------------+
| a                   | b                   |
+---------------------+---------------------+
| 2024-03-21 13:26:50 | 2024-03-21 13:26:50 |
+---------------------+---------------------+
1 row in set (0.00 sec)

(root@localhost) [dbt3]> show variables like 'time_zone';
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| time_zone     | SYSTEM |
+---------------+--------+
1 row in set (0.00 sec)

(root@localhost) [dbt3]> set time_zone = '+0:00';
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [dbt3]> select * from z;
+---------------------+---------------------+
| a                   | b                   |
+---------------------+---------------------+
| 2024-03-21 13:26:50 | 2024-03-21 05:26:50 |
+---------------------+---------------------+
1 row in set (0.00 sec)

(root@localhost) [dbt3]> select now(10);
ERROR 1426 (42000): Too-big precision 10 specified for 'now'. Maximum is 6.
(root@localhost) [dbt3]> select now(6);
+----------------------------+
| now(6)                     |
+----------------------------+
| 2024-03-21 05:29:22.432329 |
+----------------------------+
1 row in set (0.00 sec)

(root@localhost) [dbt3]> select now(3);
+-------------------------+
| now(3)                  |
+-------------------------+
| 2024-03-21 05:29:36.677 |
+-------------------------+
1 row in set (0.00 sec)

5.1.2now()和sysdate()的区别

now()表示该条sql执行开始的时间,sysdate()表示执行到该函数的时间

(root@localhost) [dbt3]> select now(),sysdate(),sleep(4),now(),sysdate();
+---------------------+---------------------+----------+---------------------+---------------------+
| now()               | sysdate()           | sleep(4) | now()               | sysdate()           |
+---------------------+---------------------+----------+---------------------+---------------------+
| 2024-03-21 05:34:46 | 2024-03-21 05:34:46 |        0 | 2024-03-21 05:34:46 | 2024-03-21 05:34:50 |
+---------------------+---------------------+----------+---------------------+---------------------+
1 row in set (4.00 sec)

(root@localhost) [dbt3]> select now(6),sysdate(6);
+----------------------------+----------------------------+
| now(6)                     | sysdate(6)                 |
+----------------------------+----------------------------+
| 2024-03-21 05:35:58.930067 | 2024-03-21 05:35:58.930234 |
+----------------------------+----------------------------+
1 row in set (0.00 sec)

5.1.3date_add()和date_sub()函数

时间加减函数

(root@localhost) [dbt3]> set time_zone='+8:00';
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [dbt3]> select now();
+---------------------+
| now()               |
+---------------------+
| 2024-03-21 13:41:15 |
+---------------------+
1 row in set (0.00 sec)

(root@localhost) [dbt3]> select date_add(now(),interval 1 day);
+--------------------------------+
| date_add(now(),interval 1 day) |
+--------------------------------+
| 2024-03-22 13:42:18            |
+--------------------------------+
1 row in set (0.00 sec)

(root@localhost) [dbt3]> select date_add(now(),interval -2 day);
+---------------------------------+
| date_add(now(),interval -2 day) |
+---------------------------------+
| 2024-03-19 13:42:33             |
+---------------------------------+
1 row in set (0.00 sec)

(root@localhost) [dbt3]> select date_add(now(),interval -2 hour);
+----------------------------------+
| date_add(now(),interval -2 hour) |
+----------------------------------+
| 2024-03-21 11:43:06              |
+----------------------------------+
1 row in set (0.00 sec)

(root@localhost) [dbt3]> select date_add(now(),interval -2 minute);
+------------------------------------+
| date_add(now(),interval -2 minute) |
+------------------------------------+
| 2024-03-21 13:41:25                |
+------------------------------------+
1 row in set (0.00 sec)

(root@localhost) [dbt3]> select date_sub(now(),interval 2 day);
+--------------------------------+
| date_sub(now(),interval 2 day) |
+--------------------------------+
| 2024-03-19 14:45:05            |
+--------------------------------+
1 row in set (0.00 sec)

(root@localhost) [dbt3]> select date_sub(now(),interval -2 day);
+---------------------------------+
| date_sub(now(),interval -2 day) |
+---------------------------------+
| 2024-03-23 14:45:10             |
+---------------------------------+
1 row in set (0.00 sec)

(root@localhost) [dbt3]> select date_sub(now(),interval 4 hour);
+---------------------------------+
| date_sub(now(),interval 4 hour) |
+---------------------------------+
| 2024-03-21 10:45:42             |
+---------------------------------+
1 row in set (0.01 sec)

5.1.4date_format()函数

语法

DATE_FORMAT(date,format)

date 参数是合法的日期,format 规定日期/时间的输出格式。 可用的格式如下:

格式描述
%a缩写星期名
%b缩写月名
%c月,数值
%D带有英文前缀的月中的天
%d月的天,数值(00-31)
%e月的天,数值(0-31)
%f微秒
%H小时 (00-23)
%h小时 (01-12)
%I小时 (01-12)
%i分钟,数值(00-59)
%j年的天 (001-366)
%k小时 (0-23)
%l小时 (1-12)
%M月名
%m月,数值(00-12)
%pAM 或 PM
%r时间,12-小时(hh:mm:ss AM 或 PM)
%S秒(00-59)
%s秒(00-59)
%T时间, 24-小时 (hh:mm:ss)
%U周 (00-53) 星期日是一周的第一天
%u周 (00-53) 星期一是一周的第一天
%V周 (01-53) 星期日是一周的第一天,与 %X 使用
%v周 (01-53) 星期一是一周的第一天,与 %x 使用
%W星期名
%w周的天 (0=星期日, 6=星期六)
%X年,其中的星期日是周的第一天,4 位,与 %V 使用
%x年,其中的星期一是周的第一天,4 位,与 %v 使用
%Y年,4 位
%y年,2 位
(root@localhost) [dbt3]> select date_format(now(),'%Y/%m/%s %H.%i.%S');
+----------------------------------------+
| date_format(now(),'%Y/%m/%s %H.%i.%S') |
+----------------------------------------+
| 2024/03/25 13.49.25                    |
+----------------------------------------+
1 row in set (0.00 sec)

(root@localhost) [dbt3]> select date_format(now(),'%a');
+-------------------------+
| date_format(now(),'%a') |
+-------------------------+
| Thu                     |
+-------------------------+
1 row in set (0.00 sec)

(root@localhost) [dbt3]> select date_format(now(),'%b');
+-------------------------+
| date_format(now(),'%b') |
+-------------------------+
| Mar                     |
+-------------------------+
1 row in set (0.00 sec)

(root@localhost) [dbt3]> select date_format(now(),'%W');
+-------------------------+
| date_format(now(),'%W') |
+-------------------------+
| Thursday                |
+-------------------------+
1 row in set (0.00 sec)

6.json类型

 json类型跟python中的字典类似

(root@localhost) [dbt3]> create table userjson(uid bigint auto_increment primary key,data json);
Query OK, 0 rows affected (0.01 sec)

(root@localhost) [dbt3]> show create table userjson\G
*************************** 1. row ***************************
       Table: userjson
Create Table: CREATE TABLE `userjson` (
  `uid` bigint(20) NOT NULL AUTO_INCREMENT,
  `data` json DEFAULT NULL,
  PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)

(root@localhost) [dbt3]> INSERT INTO userjson(uid,data)VALUES (NULL,'{"name":"David","address": "Shanghai"}');
Query OK, 1 row affected (0.04 sec)

(root@localhost) [dbt3]> INSERT INTO userjson(uid,data)VALUES (NULL,'{"name":"Jim","passport":"E02318883"}');
Query OK, 1 row affected (0.00 sec)

(root@localhost) [dbt3]> select * from userjson;
+-----+------------------------------------------+
| uid | data                                     |
+-----+------------------------------------------+
|   1 | {"name": "David", "address": "Shanghai"} |
|   2 | {"name": "Jim", "passport": "E02318883"} |
+-----+------------------------------------------+
2 rows in set (0.00 sec)

6.1json类型相关函数

6.1.1json_extract()函数

(root@localhost) [dbt3]> select uid,json_extract(data,'$.name') from userjson;
+-----+-----------------------------+
| uid | json_extract(data,'$.name') |
+-----+-----------------------------+
|   1 | "David"                     |
|   2 | "Jim"                       |
+-----+-----------------------------+
2 rows in set (0.00 sec)

(root@localhost) [dbt3]> select uid,json_unquote(json_extract(data,'$.name')) from userjson;
+-----+-------------------------------------------+
| uid | json_unquote(json_extract(data,'$.name')) |
+-----+-------------------------------------------+
|   1 | David                                     |
|   2 | Jim                                       |
+-----+-------------------------------------------+
2 rows in set (0.00 sec)

(root@localhost) [dbt3]> select uid,data->'$.name' from userjson;
+-----+----------------+
| uid | data->'$.name' |
+-----+----------------+
|   1 | "David"        |
|   2 | "Jim"          |
+-----+----------------+
2 rows in set (0.00 sec)

(root@localhost) [dbt3]> select uid,data->>'$.name' from userjson;
+-----+-----------------+
| uid | data->>'$.name' |
+-----+-----------------+
|   1 | David           |
|   2 | Jim             |
+-----+-----------------+
2 rows in set (0.00 sec)

(root@localhost) [dbt3]> select uid,data->>'$.passport' from userjson;
+-----+---------------------+
| uid | data->>'$.passport' |
+-----+---------------------+
|   1 | NULL                |
|   2 | E02318883           |
+-----+---------------------+
2 rows in set (0.00 sec)

 6.1.2json_remove()函数

(root@localhost) [dbt3]> select * from userjson;
+-----+------------------------------------------+
| uid | data                                     |
+-----+------------------------------------------+
|   1 | {"name": "David", "address": "Shanghai"} |
|   2 | {"name": "Jim", "passport": "E02318883"} |
+-----+------------------------------------------+
2 rows in set (0.00 sec)

(root@localhost) [dbt3]> select uid,json_remove(data,'$.name') from userjson;
+-----+----------------------------+
| uid | json_remove(data,'$.name') |
+-----+----------------------------+
|   1 | {"address": "Shanghai"}    |
|   2 | {"passport": "E02318883"}  |
+-----+----------------------------+
2 rows in set (0.01 sec)

(root@localhost) [dbt3]> select uid,json_remove(data,'$.passport') from userjson;
+-----+------------------------------------------+
| uid | json_remove(data,'$.passport')           |
+-----+------------------------------------------+
|   1 | {"name": "David", "address": "Shanghai"} |
|   2 | {"name": "Jim"}                          |
+-----+------------------------------------------+
2 rows in set (0.00 sec)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1535822.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

工程信号的去噪和(分类、回归和时序)预测

🚀【信号去噪及预测论文代码指导】🚀 还为小论文没有思路烦恼么?本人专注于最前沿的信号处理与预测技术——基于信号模态分解的去噪算法和深度学习的信号(回归、时序和分类)预测算法,致力于为您提供最精确、…

MySql实战--深入浅出索引(下)

在开始这篇文章之前,我们先来看一下这个问题: 在下面这个表T中,如果我执行 select * from T where k between 3 and 5,需要执行几次树的搜索操作,会扫描多少行? 下面是这个表的初始化语句。 图1 InnoDB的索…

第 6 章 ROS-xacro练习(自学二刷笔记)

重要参考: 课程链接:https://www.bilibili.com/video/BV1Ci4y1L7ZZ 讲义链接:Introduction Autolabor-ROS机器人入门课程《ROS理论与实践》零基础教程 6.4.3 Xacro_完整使用流程示例 需求描述: 使用 Xacro 优化 URDF 版的小车底盘模型实现 结果演示: 1.编写 X…

ChatGPT已经掌控了全局:不仅写论文的在用ChatGPT,同行评审也在用ChatGPT!

大家好,我是木易,一个持续关注AI领域的互联网技术产品经理,国内Top2本科,美国Top10 CS研究生,MBA。我坚信AI是普通人变强的“外挂”,所以创建了“AI信息Gap”这个公众号,专注于分享AI全维度知识…

分库分表场景下多维查询解决方案(用户+商户)

在采用分库分表设计时,通过一个PartitionKey根据散列策略将数据分散到不同的库表中,从而有效降低海量数据下C端访问数据库的压力。这种方式可以缓解单一数据库的压力,提升了吞吐量,但同时也带来了新的问题。对于B端商户而言&#…

【Python爬虫】网络爬虫:信息获取与合规应用

这里写目录标题 前言网络爬虫的工作原理网络爬虫的应用领域网络爬虫的技术挑战网络爬虫的伦理问题结语福利 前言 网络爬虫,又称网络爬虫、网络蜘蛛、网络机器人等,是一种按照一定的规则自动地获取万维网信息的程序或者脚本。它可以根据一定的策略自动地浏…

linux查看usb是3.0还是2.0

1 作为device cat /sys/devices/platform/10320000.usb30drd/10320000.dwc3/udc/10320000.dwc3/current_speed 如下 high-speed usb2.0 super-speed usb3.0 2 作为host linux下使用以下命令查看 ,如果显示 速率为5G, 则为USB 3.0, USB2.0通常显示速率…

Day17|二叉树part04:110.平衡二叉树、257.二叉树的所有路径、404.左叶子之和、543: 二叉树的直径、124: 二叉树的最大路径和

之前的blog链接:https://blog.csdn.net/weixin_43303286/article/details/131982632?spm1001.2014.3001.5501 110.平衡二叉树 本题中,一棵高度平衡二叉树定义为:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。思路&#xff…

Matlab之已知2点绘制长度可定义的射线

目的:在笛卡尔坐标系中,已知两个点的位置,绘制过这两点的射线。同时射线的长度可以自定义。 一、函数的参数说明 输入参数: PointA:射线的起点; PointB:射线过的零一点; Length&…

AI PPT生成工具 V1.0.0

AI PPT是一款高效快速的PPT生成工具,能够一键生成符合相关主题的PPT文件,大大提高工作效率。生成的PPT内容专业、细致、实用。 软件特点 免费无广告,简单易用,快速高效,提高工作效率 一键生成相关主题的标题、大纲、…

【链表】Leetcode 138. 随机链表的复制【中等】

随机链表的复制 给你一个长度为 n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。 构造这个链表的 深拷贝。 深拷贝应该正好由 n 个 全新 节点组成,其中每个新节点的值都设为其对应的原节点…

Linux - 应用层HTTPS、传输层TCP/IP模型中典型协议解析

目录 应用层:自定制协议实例 HTTP协议首行头部空行正文http服务器的搭建 HTTPS协议 传输层UDP协议TCP协议 应用层: 应用层负责应用程序之间的沟通—程序员自己定义数据的组织格式 应用层协议:如何将多个数据对象组织成为一个二进制数据串进行…

代码签名证书被吊销的原因及其后果是什么?

代码签名证书是确保软件代码完整性和可信度的关键工具,然而,在某些情况下,此类证书可能会被撤销。这意味着证书颁发机构(CA)不再认可该证书的有效性,并宣布其失效。本文将解析导致代码签名证书撤销的原因、…

Bytebase 2.14.1 - 分支 (Branching) 功能支持 Oracle

🚀 新功能 分支 (Branching) 功能支持 Oracle。为 SQL 编辑器添加了项目选择器。 新增 SQL 审核规范: 禁止混合 DDL、DML 语句。禁止对同一张表进行不同类型的 DML 变更 (UPDATE,INSERT,DELETE)。 🔔 重大变更 工作空间设置中的「数据访问…

puppeteer使用示例云顶之弈官网

自己从0到1开发的,微信小程序【云顶宝藏】求求点个5星好评吧! 需求:拿到所有英雄的信息 思路:点击每个英雄,进入英雄详情页,拿信息,并返回,继续下一个英雄** 最终效果 本地环境 win…

【链表】Leetcode 148. 排序链表【中等】

排序链表 给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。 示例 1: 输入:head [4,2,1,3] 输出:[1,2,3,4] 解题思路 对链表进行升序排序,可以使用归并排序(Merge Sort)的…

vben admin上线后接口调用成功,控制台报promise错误

vben admin上线后接口调用成功,控制台报promise错误 问题原因 vben admin框架对返回的数据格式做了统一处理,不满足格式要求的接口返回直接抛异常。 解决方案 将返回参数校验关闭,重新打包部署。

windowsVMware虚拟机中扩展linux磁盘空间

1.虚拟磁盘扩容 VM中,关闭linux虚拟机,直接编辑虚拟机-硬盘-扩展磁盘容量 2.通过Gparted工具进行LINUX系统磁盘分区 未分区挂载前可以看到/挂载点下空间为20G: 通过虚拟机-快照-拍摄快照,操作前可拍摄快照(便于恢复之前…

Elasticsearch数据写入、检索流程及底层原理全方位解析

码到三十五 : 个人主页 心中有诗画,指尖舞代码,目光览世界,步履越千山,人间尽值得 ! 目录 ✍🏻序言✍🏻1️⃣✍🏻es的架构简介1. 分布式架构2. 索引与搜索3. 数据写入与持久化4. 缓…

酷开科技以内容技术服务和数字营销服务为核心,自主研发酷开系统

家庭场景的需求,才是大屏电视的目的。屏幕越大得到的画幕越大,消费者也就看的越清楚,从而获得更好的观看体验,尤其是家里有老人孩子的,为了得到更好的视觉效果,使得消费者对于大屏的需求也在增加。酷开系统…