MySQL-数据库基本操作

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介: MySQL-数据库基本操作

系统数据库

• information_schema(虚拟库)
    ○ 用户表信息、列信息、权限信息、字符信息等
• performance_schema
    ○ 主要存储数据库服务器的性能参数
• mysql(授权库)
    ○ 主要存储系统用户的权限信息
• sys(优化库)
    ○ 主要存储数据库服务器的性能参数
• bgx(业务库)
    ○ 主要存放业务所需要的库和表

一、数据库的连接方式

使用mysql -u root -p可以连接数据库, 但这只是本地连接数据库的方式, 在生产很多情况下都是连接网络中某一个主机上的数据库

-P //指定连接远程数据库端口
-h //指定连接远程数据库地址
-u //指定连接远程数据库账户
-p //指定连接远程数据库密码
[root@liza ~]# mysql -h192.168.112.160 -p3306 -uroot -p
mysql: [Warning] Using a password on the command line interface can be insecure.
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.32 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
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.
mysql> 

二、数据库的基本操作

1、查看数据库版本

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.32    |
+-----------+
1 row in set (0.00 sec)

2、创建数据库

mysql> create database bgx_edu default character set utf8;
Query OK, 1 row affected (0.00 sec)
数据库名称严格区分大小写
数据库名称必须是唯一
数据库名称不允许使用数字
数据库名称不能使用关键字命名create select

3、查看当前的库内容

mysql> SHOW DATABASES;            ---执行命令不区分大小写
+--------------------+
| Database           |
+--------------------+
| information_schema |
| bgx_edu            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

4、删除数据库

mysql> drop database bgx_edu;
Query OK, 0 rows affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
删除库下的表
mysql> drop table Bgx_edu.t1;

5、查询某个库的表

mysql> use bgx_edu;
Database changed
mysql> show tables;
+-------------------+
| Tables_in_bgx_edu |
+-------------------+
| t1                |
+-------------------+
1 row in set (0.00 sec)

6、查看某张表的建表语句

mysql> show create table mysql.slow_log\G
*************************** 1. row ***************************
       Table: slow_log
Create Table: CREATE TABLE `slow_log` (
  `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
  `user_host` mediumtext NOT NULL,
  `query_time` time(6) NOT NULL,
  `lock_time` time(6) NOT NULL,
  `rows_sent` int(11) NOT NULL,
  `rows_examined` int(11) NOT NULL,
  `db` varchar(512) NOT NULL,
  `last_insert_id` int(11) NOT NULL,
  `insert_id` int(11) NOT NULL,
  `server_id` int(10) unsigned NOT NULL,
  `sql_text` mediumblob NOT NULL,
  `thread_id` bigint(21) unsigned NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log'
1 row in set (0.00 sec)

三、数据库增删查改

在MySQL管理软件中, 可以通过SQL语句中的DML语言来实现数据的操作, 包括如下:

  1. INSERT数据插入
  2. UPDATE数据更新
  3. DELETE数据删除

1、准备操作环境数据表

创建数据文件

mysql> create database bgx default character set utf8;
Query OK, 1 row affected (0.00 sec)
mysql> use bgx;
Database changed
mysql> create table t1 (
    -> id int,
    -> name varchar(10),
    -> sex enum('man','woman'),
    -> age int
    -> );
Query OK, 0 rows affected (0.00 sec)

查看表字段

mysql> desc t1;
+-------+---------------------+------+-----+---------+-------+
| Field | Type                | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| id    | int(11)             | YES  |     | NULL    |       |
| name  | varchar(10)         | YES  |     | NULL    |       |
| sex   | enum('man','woman') | YES  |     | NULL    |       |
| age   | int(11)             | YES  |     | NULL    |       |
+-------+---------------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

2、插入数据INSERT语句

方法一:插入完整数据, 顺序插入: INSERT INTO 表名(字段1,字段2,字段n) VALUES (值1,值2,值 n);

mysql> insert into t1 (id,name,sex,age) values ("1","bgx","man","18");
Query OK, 1 row affected (0.01 sec)

方法二:插入完整数据, 推荐方式: INSERT INTO 表名 VALUES (值1,值2,值n);

mysql> insert into t1 values("2","bgx2","woman","10");
Query OK, 1 row affected (0.00 sec)

方法三:指定字段插入:INSERT INTO 表名(字段2,字段3…) VALUES (值 2,值3…);

mysql> insert into t1 values
    -> ("3","bgx4","man","18"),
    -> ("4","bgx5","man","18"),
    -> ("5","bgx6","woman","26");
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

查看

mysql> select * from t1;
+------+------+-------+------+
| id   | name | sex   | age  |
+------+------+-------+------+
|    1 | bgx  | man   |   18 |
|    2 | bgx2 | woman |   10 |
| NULL | bgx3 | man   |   20 |
|    3 | bgx4 | man   |   18 |
|    4 | bgx5 | man   |   18 |
|    5 | bgx6 | woman |   26 |
+------+------+-------+------+
6 rows in set (0.00 sec)

3、更新数据UPDATE语句

语法: 更新 表 设置 字段1=值1, 字段2=值2, WHERE 条件;

  1. 查看需要修改的表的字段 desc
  2. 查询对用的字段 select
  3. 更新对应的表字段 update
  4. 添加对应的where条件,精准修改
示例1: 将t1表中, name字段等于bgx1的改为update_bgx
mysql> update t1 set name="update_bgx" where name="bgx";
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from t1;
+------+------------+-------+------+
| id   | name       | sex   | age  |
+------+------------+-------+------+
|    1 | update_bgx | man   |   18 |
|    2 | bgx2       | woman |   10 |
| NULL | bgx3       | man   |   20 |
|    3 | bgx4       | man   |   18 |
|    4 | bgx5       | man   |   18 |
|    5 | bgx6       | woman |   26 |
+------+------------+-------+------+
6 rows in set (0.00 sec)
示例2: 修改密码示例, 查看表字段内容
mysql> select user,host,authentication_string from mysql.user;
+---------------+-----------+-------------------------------------------+
| user          | host      | authentication_string                     |
+---------------+-----------+-------------------------------------------+
| root          | %         | *5F2035C0E578A25296968F8A3F33B8D9EFE1C8F0 |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys     | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+---------------+-----------+-------------------------------------------+
3 rows in set (0.00 sec)

更新字段

mysql> update mysql.user set
    -> authentication_string=password("Bgx123.com")
    -> where user='root' and host='localhost';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
4、删除数据DELETE

语法: DELETE FROM 表名 WHERE CONITION;

删除字段包含update_bgx

mysql> delete from t1 where name="update_bgx";
Query OK, 1 row affected (0.00 sec)
mysql> select * from t1;
+------+------+-------+------+
| id   | name | sex   | age  |
+------+------+-------+------+
|    2 | bgx2 | woman |   10 |
| NULL | bgx3 | man   |   20 |
|    3 | bgx4 | man   |   18 |
|    4 | bgx5 | man   |   18 |
|    5 | bgx6 | woman |   26 |
+------+------+-------+------+
5 rows in set (0.00 sec)

清空表数据

mysql> truncate t1;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from t1;
Empty set (0.00 sec)

四、数据库查询语句

单表查询

在学习查询前, 需要定义好对应数据进行查询

编号 id int
姓名 name varchar(30)
性别 sex enum
日期 time date
职位 post varchar(50)
描述 job varchar(100)
薪水 salary double(15,2)
部门编号 dep_id int

创建表

mysql> create table bgx.t2 (
    -> id int primary key AUTO_INCREMENT not null,
    -> name varchar(30) not null,
    -> sex enum('man','woman') default 'man' not null,
    -> time date not null,
    -> post varchar(50) not null,
    -> job varchar(100),
    -> salary double(15,2) not null,
    -> office int,
    -> dep_id int
    -> );
Query OK, 0 rows affected (0.00 sec)

插入数据

mysql> insert into bgx.t2(name,sex,time,post,job,salary,office,dep_id) values 
('jack','man','20180202','instructor','teach',5000,501,100), 
('tom','man','20180203','instructor','teach',5500,501,100),
('robin','man','20180202','instructor','teach',8000,501,100),
('alice','woman','20180202','instructor','teach',7200,501,100), 
('bgx','man','20180202','hr','hrcc',600,502,101),
('harry','man','20180202','hr', NULL,6000,502,101),
('trf','woman','20180206','sale','salecc',20000,503,102),
('test','woman','20180205','sale','salecc',2200,503,102),
('dog','man','20180205','sale', NULL,2200,503,102),
('alex','man','20180205','sale','',2200,503,102);
Query OK, 10 rows affected (0.00 sec)
Records: 10  Duplicates: 0  Warnings: 0

1、简单查询

查看表字段与表信息

mysql> desc t2;
+--------+---------------------+------+-----+---------+----------------+
| Field  | Type                | Null | Key | Default | Extra          |
+--------+---------------------+------+-----+---------+----------------+
| id     | int(11)             | NO   | PRI | NULL    | auto_increment |
| name   | varchar(30)         | NO   |     | NULL    |                |
| sex    | enum('man','woman') | NO   |     | man     |                |
| time   | date                | NO   |     | NULL    |                |
| post   | varchar(50)         | NO   |     | NULL    |                |
| job    | varchar(100)        | YES  |     | NULL    |                |
| salary | double(15,2)        | NO   |     | NULL    |                |
| office | int(11)             | YES  |     | NULL    |                |
| dep_id | int(11)             | YES  |     | NULL    |                |
+--------+---------------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)
查询所有数据
mysql> select * from t2;
+----+-------+-------+------------+------------+--------+----------+--------+--------+
| id | name  | sex   | time       | post       | job    | salary   | office | dep_id |
+----+-------+-------+------------+------------+--------+----------+--------+--------+
| 11 | jack  | man   | 2018-02-02 | instructor | teach  |  5000.00 |    501 |    100 |
| 12 | tom   | man   | 2018-02-03 | instructor | teach  |  5500.00 |    501 |    100 |
| 13 | robin | man   | 2018-02-02 | instructor | teach  |  8000.00 |    501 |    100 |
| 14 | alice | woman | 2018-02-02 | instructor | teach  |  7200.00 |    501 |    100 |
| 15 | bgx   | man   | 2018-02-02 | hr         | hrcc   |   600.00 |    502 |    101 |
| 16 | harry | man   | 2018-02-02 | hr         | NULL   |  6000.00 |    502 |    101 |
| 17 | trf   | woman | 2018-02-06 | sale       | salecc | 20000.00 |    503 |    102 |
| 18 | test  | woman | 2018-02-05 | sale       | salecc |  2200.00 |    503 |    102 |
| 19 | dog   | man   | 2018-02-05 | sale       | NULL   |  2200.00 |    503 |    102 |
| 20 | alex  | man   | 2018-02-05 | sale       |        |  2200.00 |    503 |    102 |
+----+-------+-------+------------+------------+--------+----------+--------+--------+
10 rows in set (0.00 sec)
指定字段查询
mysql> select name,salary,dep_id from t2;
+-------+----------+--------+
| name  | salary   | dep_id |
+-------+----------+--------+
| jack  |  5000.00 |    100 |
| tom   |  5500.00 |    100 |
| robin |  8000.00 |    100 |
| alice |  7200.00 |    100 |
| bgx   |   600.00 |    101 |
| harry |  6000.00 |    101 |
| trf   | 20000.00 |    102 |
| test  |  2200.00 |    102 |
| dog   |  2200.00 |    102 |
| alex  |  2200.00 |    102 |
+-------+----------+--------+
10 rows in set (0.00 sec)
避免重复查询字段distinct
mysql> select post from t2;
+------------+
| post       |
+------------+
| instructor |
| instructor |
| instructor |
| instructor |
| hr         |
| hr         |
| sale       |
| sale       |
| sale       |
| sale       |
+------------+
10 rows in set (0.00 sec)
mysql> select distinct post from t2;
+------------+
| post       |
+------------+
| instructor |
| hr         |
| sale       |
+------------+
3 rows in set (0.00 sec)
通过四则运算查询, 计算每个人的年薪(14个月)
mysql> select name,salary,salary*14 from t2;
+-------+----------+-----------+
| name  | salary   | salary*14 |
+-------+----------+-----------+
| jack  |  5000.00 |  70000.00 |
| tom   |  5500.00 |  77000.00 |
| robin |  8000.00 | 112000.00 |
| alice |  7200.00 | 100800.00 |
| bgx   |   600.00 |   8400.00 |
| harry |  6000.00 |  84000.00 |
| trf   | 20000.00 | 280000.00 |
| test  |  2200.00 |  30800.00 |
| dog   |  2200.00 |  30800.00 |
| alex  |  2200.00 |  30800.00 |
+-------+----------+-----------+
10 rows in set (0.00 sec)
mysql> select name,salary,salary*14 AS Annual_salary from t2;
+-------+----------+---------------+
| name  | salary   | Annual_salary |
+-------+----------+---------------+
| jack  |  5000.00 |      70000.00 |
| tom   |  5500.00 |      77000.00 |
| robin |  8000.00 |     112000.00 |
| alice |  7200.00 |     100800.00 |
| bgx   |   600.00 |       8400.00 |
| harry |  6000.00 |      84000.00 |
| trf   | 20000.00 |     280000.00 |
| test  |  2200.00 |      30800.00 |
| dog   |  2200.00 |      30800.00 |
| alex  |  2200.00 |      30800.00 |
+-------+----------+---------------+
10 rows in set (0.00 sec)
定义显示格式 CONCAT() 函数用于连接字符串
mysql> select concat(name,'annual salary:',salary*14) from t2;
+-----------------------------------------+
| concat(name,'annual salary:',salary*14) |
+-----------------------------------------+
| jackannual salary:70000.00              |
| tomannual salary:77000.00               |
| robinannual salary:112000.00            |
| aliceannual salary:100800.00            |
| bgxannual salary:8400.00                |
| harryannual salary:84000.00             |
| trfannual salary:280000.00              |
| testannual salary:30800.00              |
| dogannual salary:30800.00               |
| alexannual salary:30800.00              |
+-----------------------------------------+
10 rows in set (0.00 sec)

2、单条件查询

单条件查询
多条件查询
关键字 BETWEEN AND
关键字 IS NULL
关键字 IN 集合查询
关键字 LIKE 模糊查询
单条件查询
mysql> select name,post from t2 where post='hr';
+-------+------+
| name  | post |
+-------+------+
| bgx   | hr   |
| harry | hr   |
+-------+------+
2 rows in set (0.00 sec)
多条件查询
mysql> select name,post,salary from t2 where post = 'hr' and salary > 5000;
+-------+------+---------+
| name  | post | salary  |
+-------+------+---------+
| harry | hr   | 6000.00 |
+-------+------+---------+
1 row in set (0.00 sec)
查找薪资范围在8000-2000,使用BETWEEN区间
mysql> select name,salary from t2 where salary between 8000 and 20000;
+-------+----------+
| name  | salary   |
+-------+----------+
| robin |  8000.00 |
| trf   | 20000.00 |
+-------+----------+
2 rows in set (0.00 sec)
查找部门为Null
没有部门的员工
mysql> select name,job from t2 where job is null;
+-------+------+
| name  | job  |
+-------+------+
| harry | NULL |
| dog   | NULL |
+-------+------+
2 rows in set (0.01 sec)
有部门的员工
mysql> select name,job from t2 where job is not null;
+-------+--------+
| name  | job    |
+-------+--------+
| jack  | teach  |
| tom   | teach  |
| robin | teach  |
| alice | teach  |
| bgx   | hrcc   |
| trf   | salecc |
| test  | salecc |
| alex  |        |
+-------+--------+
8 rows in set (0.00 sec)
查看部门为空的员工
mysql> select name,job from t2 where job = '';
+------+------+
| name | job  |
+------+------+
| alex |      |
+------+------+
1 row in set (0.00 sec)
集合查询
mysql> select name,salary from t2 where salary=4000 OR salary=5000 OR salary=8000;
+-------+---------+
| name  | salary  |
+-------+---------+
| jack  | 5000.00 |
| robin | 8000.00 |
+-------+---------+
2 rows in set (0.00 sec)
mysql> select name,salary from t2 where salary in(4000,5000,8000);
+-------+---------+
| name  | salary  |
+-------+---------+
| jack  | 5000.00 |
| robin | 8000.00 |
+-------+---------+
2 rows in set (0.00 sec)
模糊查询like, 通配符%
mysql> select * from t2 where name like 'al%';
+----+-------+-------+------------+------------+-------+---------+--------+--------+
| id | name  | sex   | time       | post       | job   | salary  | office | dep_id |
+----+-------+-------+------------+------------+-------+---------+--------+--------+
| 14 | alice | woman | 2018-02-02 | instructor | teach | 7200.00 |    501 |    100 |
| 20 | alex  | man   | 2018-02-05 | sale       |       | 2200.00 |    503 |    102 |
+----+-------+-------+------------+------------+-------+---------+--------+--------+
2 rows in set (0.00 sec)
mysql> select * from t2 where name like 'al__';
+----+------+-----+------------+------+------+---------+--------+--------+
| id | name | sex | time       | post | job  | salary  | office | dep_id |
+----+------+-----+------------+------+------+---------+--------+--------+
| 20 | alex | man | 2018-02-05 | sale |      | 2200.00 |    503 |    102 |
+----+------+-----+------------+------+------+---------+--------+--------+
1 row in set (0.00 sec)

3、查询排序

  1. 单列排序
  2. 多列排序
按单列排序, 按薪水从低到高排序, 默认ASC
mysql> select * from t2 ORDER BY salary ASC
    -> ;
+----+-------+-------+------------+------------+--------+----------+--------+--------+
| id | name  | sex   | time       | post       | job    | salary   | office | dep_id |
+----+-------+-------+------------+------------+--------+----------+--------+--------+
| 15 | bgx   | man   | 2018-02-02 | hr         | hrcc   |   600.00 |    502 |    101 |
| 18 | test  | woman | 2018-02-05 | sale       | salecc |  2200.00 |    503 |    102 |
| 19 | dog   | man   | 2018-02-05 | sale       | NULL   |  2200.00 |    503 |    102 |
| 20 | alex  | man   | 2018-02-05 | sale       |        |  2200.00 |    503 |    102 |
| 11 | jack  | man   | 2018-02-02 | instructor | teach  |  5000.00 |    501 |    100 |
| 12 | tom   | man   | 2018-02-03 | instructor | teach  |  5500.00 |    501 |    100 |
| 16 | harry | man   | 2018-02-02 | hr         | NULL   |  6000.00 |    502 |    101 |
| 14 | alice | woman | 2018-02-02 | instructor | teach  |  7200.00 |    501 |    100 |
| 13 | robin | man   | 2018-02-02 | instructor | teach  |  8000.00 |    501 |    100 |
| 17 | trf   | woman | 2018-02-06 | sale       | salecc | 20000.00 |    503 |    102 |
+----+-------+-------+------------+------------+--------+----------+--------+--------+
10 rows in set (0.00 sec)
按单列排序, 薪水从低往高排序, DESC倒序
mysql> select * from t2 ORDER BY salary DESC;
+----+-------+-------+------------+------------+--------+----------+--------+--------+
| id | name  | sex   | time       | post       | job    | salary   | office | dep_id |
+----+-------+-------+------------+------------+--------+----------+--------+--------+
| 17 | trf   | woman | 2018-02-06 | sale       | salecc | 20000.00 |    503 |    102 |
| 13 | robin | man   | 2018-02-02 | instructor | teach  |  8000.00 |    501 |    100 |
| 14 | alice | woman | 2018-02-02 | instructor | teach  |  7200.00 |    501 |    100 |
| 16 | harry | man   | 2018-02-02 | hr         | NULL   |  6000.00 |    502 |    101 |
| 12 | tom   | man   | 2018-02-03 | instructor | teach  |  5500.00 |    501 |    100 |
| 11 | jack  | man   | 2018-02-02 | instructor | teach  |  5000.00 |    501 |    100 |
| 18 | test  | woman | 2018-02-05 | sale       | salecc |  2200.00 |    503 |    102 |
| 19 | dog   | man   | 2018-02-05 | sale       | NULL   |  2200.00 |    503 |    102 |
| 20 | alex  | man   | 2018-02-05 | sale       |        |  2200.00 |    503 |    102 |
| 15 | bgx   | man   | 2018-02-02 | hr         | hrcc   |   600.00 |    502 |    101 |
+----+-------+-------+------------+------------+--------+----------+--------+--------+
10 rows in set (0.00 sec)
多列排序, 先按入职时间,再按薪水排序
mysql> select * from t2 ORDER BY time DESC,salary ASC;
+----+-------+-------+------------+------------+--------+----------+--------+--------+
| id | name  | sex   | time       | post       | job    | salary   | office | dep_id |
+----+-------+-------+------------+------------+--------+----------+--------+--------+
| 17 | trf   | woman | 2018-02-06 | sale       | salecc | 20000.00 |    503 |    102 |
| 18 | test  | woman | 2018-02-05 | sale       | salecc |  2200.00 |    503 |    102 |
| 19 | dog   | man   | 2018-02-05 | sale       | NULL   |  2200.00 |    503 |    102 |
| 20 | alex  | man   | 2018-02-05 | sale       |        |  2200.00 |    503 |    102 |
| 12 | tom   | man   | 2018-02-03 | instructor | teach  |  5500.00 |    501 |    100 |
| 15 | bgx   | man   | 2018-02-02 | hr         | hrcc   |   600.00 |    502 |    101 |
| 11 | jack  | man   | 2018-02-02 | instructor | teach  |  5000.00 |    501 |    100 |
| 16 | harry | man   | 2018-02-02 | hr         | NULL   |  6000.00 |    502 |    101 |
| 14 | alice | woman | 2018-02-02 | instructor | teach  |  7200.00 |    501 |    100 |
| 13 | robin | man   | 2018-02-02 | instructor | teach  |  8000.00 |    501 |    100 |
+----+-------+-------+------------+------------+--------+----------+--------+--------+
10 rows in set (0.00 sec)
多列排序, 先按职位, 再按薪水排序
mysql> select * from t2 ORDER BY post,salary DESC;
+----+-------+-------+------------+------------+--------+----------+--------+--------+
| id | name  | sex   | time       | post       | job    | salary   | office | dep_id |
+----+-------+-------+------------+------------+--------+----------+--------+--------+
| 16 | harry | man   | 2018-02-02 | hr         | NULL   |  6000.00 |    502 |    101 |
| 15 | bgx   | man   | 2018-02-02 | hr         | hrcc   |   600.00 |    502 |    101 |
| 13 | robin | man   | 2018-02-02 | instructor | teach  |  8000.00 |    501 |    100 |
| 14 | alice | woman | 2018-02-02 | instructor | teach  |  7200.00 |    501 |    100 |
| 12 | tom   | man   | 2018-02-03 | instructor | teach  |  5500.00 |    501 |    100 |
| 11 | jack  | man   | 2018-02-02 | instructor | teach  |  5000.00 |    501 |    100 |
| 17 | trf   | woman | 2018-02-06 | sale       | salecc | 20000.00 |    503 |    102 |
| 18 | test  | woman | 2018-02-05 | sale       | salecc |  2200.00 |    503 |    102 |
| 19 | dog   | man   | 2018-02-05 | sale       | NULL   |  2200.00 |    503 |    102 |
| 20 | alex  | man   | 2018-02-05 | sale       |        |  2200.00 |    503 |    102 |
+----+-------+-------+------------+------------+--------+----------+--------+--------+
10 rows in set (0.00 sec)

4、限制查询的记录数

查询薪资最高前5名同事, 默认初始位置为0
mysql> select * from t2 ORDER BY salary DESC limit 5;
+----+-------+-------+------------+------------+--------+----------+--------+--------+
| id | name  | sex   | time       | post       | job    | salary   | office | dep_id |
+----+-------+-------+------------+------------+--------+----------+--------+--------+
| 17 | trf   | woman | 2018-02-06 | sale       | salecc | 20000.00 |    503 |    102 |
| 13 | robin | man   | 2018-02-02 | instructor | teach  |  8000.00 |    501 |    100 |
| 14 | alice | woman | 2018-02-02 | instructor | teach  |  7200.00 |    501 |    100 |
| 16 | harry | man   | 2018-02-02 | hr         | NULL   |  6000.00 |    502 |    101 |
| 12 | tom   | man   | 2018-02-03 | instructor | teach  |  5500.00 |    501 |    100 |
+----+-------+-------+------------+------------+--------+----------+--------+--------+
5 rows in set (0.00 sec)
从第4条开始, 并显示5条数据
mysql> select * from t2 ORDER BY salary DESC limit 3,5;
+----+-------+-------+------------+------------+--------+---------+--------+--------+
| id | name  | sex   | time       | post       | job    | salary  | office | dep_id |
+----+-------+-------+------------+------------+--------+---------+--------+--------+
| 16 | harry | man   | 2018-02-02 | hr         | NULL   | 6000.00 |    502 |    101 |
| 12 | tom   | man   | 2018-02-03 | instructor | teach  | 5500.00 |    501 |    100 |
| 11 | jack  | man   | 2018-02-02 | instructor | teach  | 5000.00 |    501 |    100 |
| 20 | alex  | man   | 2018-02-05 | sale       |        | 2200.00 |    503 |    102 |
| 18 | test  | woman | 2018-02-05 | sale       | salecc | 2200.00 |    503 |    102 |
+----+-------+-------+------------+------------+--------+---------+--------+--------+
5 rows in set (0.00 sec)

5、使用集合函数查询

统计当前表总共多少条数据
mysql> select count(*) from t2;
+----------+
| count(*) |
+----------+
|       10 |
+----------+
1 row in set (0.00 sec)
统计dep_id为101有多少条数据
mysql> select count(*) from t2 where dep_id=101;
+----------+
| count(*) |
+----------+
|        2 |
+----------+
1 row in set (0.00 sec)
薪水最高
mysql> select MAX(salary) from t2;
+-------------+
| MAX(salary) |
+-------------+
|    20000.00 |
+-------------+
1 row in set (0.00 sec)
薪水最低
mysql> select min(salary) from t2;
+-------------+
| min(salary) |
+-------------+
|      600.00 |
+-------------+
1 row in set (0.00 sec)
平均少薪水
mysql> select avg(salary) from t2;
+-------------+
| avg(salary) |
+-------------+
| 5890.000000 |
+-------------+
1 row in set (0.00 sec)
总计发了多少薪水
mysql> select sum(salary) from t2;
+-------------+
| sum(salary) |
+-------------+
|    58900.00 |
+-------------+
1 row in set (0.00 sec)
hr部门发放多少薪水
mysql> select sum(salary) from t2 where post='hr';
+-------------+
| sum(salary) |
+-------------+
|     6600.00 |
+-------------+
1 row in set (0.00 sec)
哪个部门哪个人薪水最高
mysql> select * from t2 where salary=(select max(salary) from t2);
+----+------+-------+------------+------+--------+----------+--------+--------+
| id | name | sex   | time       | post | job    | salary   | office | dep_id |
+----+------+-------+------------+------+--------+----------+--------+--------+
| 17 | trf  | woman | 2018-02-06 | sale | salecc | 20000.00 |    503 |    102 |
+----+------+-------+------------+------+--------+----------+--------+--------+
1 row in set (0.00 sec)

6、分组查询

GROUP BY 和 GROUP_CONCAT()函数一起使用
mysql> select post,GROUP_CONCAT(name) from t2 GROUP BY post;
+------------+----------------------+
| post       | GROUP_CONCAT(name)   |
+------------+----------------------+
| hr         | bgx,harry            |
| instructor | jack,tom,robin,alice |
| sale       | trf,test,dog,alex    |
+------------+----------------------+
3 rows in set (0.00 sec)
GROUP BY 和集合函数一起使用
mysql> select post,sum(salary) from t2 GROUP BY post;
+------------+-------------+
| post       | sum(salary) |
+------------+-------------+
| hr         |     6600.00 |
| instructor |    25700.00 |
| sale       |    26600.00 |
+------------+-------------+
3 rows in set (0.00 sec)

7、使用正则表达式查询

对字符串匹配方式

  1. WHERE name = ‘trf’;
  2. WHERE name LIKE ‘ha%’;
  3. WHERE name REGEXP ‘gx$’;
mysql> select * from t2 where name REGEXP '^ali';
+----+-------+-------+------------+------------+-------+---------+--------+--------+
| id | name  | sex   | time       | post       | job   | salary  | office | dep_id |
+----+-------+-------+------------+------------+-------+---------+--------+--------+
| 14 | alice | woman | 2018-02-02 | instructor | teach | 7200.00 |    501 |    100 |
+----+-------+-------+------------+------------+-------+---------+--------+--------+
1 row in set (0.00 sec)
mysql> select * from t2 where name REGEXP 'gx$';
+----+------+-----+------------+------+------+--------+--------+--------+
| id | name | sex | time       | post | job  | salary | office | dep_id |
+----+------+-----+------------+------+------+--------+--------+--------+
| 15 | bgx  | man | 2018-02-02 | hr   | hrcc | 600.00 |    502 |    101 |
+----+------+-----+------------+------+------+--------+--------+--------+
1 row in set (0.00 sec)

多表查询

  1. 多表连接查询
  2. 复合条件连接查询
  3. 子查询
  4. 准备2张数据表
准备表1
mysql> create table bgx.t3 ( 
id int auto_increment primary key not null,
name varchar(50),
age int,
dep_id int
);
为表1插入数据
mysql> insert into t3 (name,age,dep_id) values
    -> ('bgx',18,200),
    -> ('tom',26,201),
    -> ('jack',30,201),
    -> ('alice',24,202),
    -> ('robin',40,'200'),
    -> ('natasha',28,204);
Query OK, 6 rows affected (0.01 sec)
Records: 6  Duplicates: 0  Warnings: 0
mysql> select * from t3;
+----+---------+------+--------+
| id | name    | age  | dep_id |
+----+---------+------+--------+
|  1 | bgx     |   18 |    200 |
|  2 | tom     |   26 |    201 |
|  3 | jack    |   30 |    201 |
|  4 | alice   |   24 |    202 |
|  5 | robin   |   40 |    200 |
|  6 | natasha |   28 |    204 |
+----+---------+------+--------+
6 rows in set (0.00 sec)
准备表2
mysql> create table t4(
    -> dep_id int, 
    -> dept_name varchar(100)
    -> );
Query OK, 0 rows affected (0.01 sec)
为表2插入数据
mysql> insert into t4 values
    -> (200,'hr'),
    -> (201,'it'),
    -> (202,'xs'),
    -> (203,'cw');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0
mysql> select * from t4;
+--------+-----------+
| dep_id | dept_name |
+--------+-----------+
|    200 | hr        |
|    201 | it        |
|    202 | xs        |
|    203 | cw        |
+--------+-----------+
4 rows in set (0.00 sec)

1、交叉连接, 不使用任何匹配条件

mysql> select t3.name,t3.age,t3.dep_id,t4.dept_name from t3,t4;
+---------+------+--------+-----------+
| name    | age  | dep_id | dept_name |
+---------+------+--------+-----------+
| bgx     |   18 |    200 | hr        |
| bgx     |   18 |    200 | it        |
| bgx     |   18 |    200 | xs        |
| bgx     |   18 |    200 | cw        |
| tom     |   26 |    201 | hr        |
| tom     |   26 |    201 | it        |
| tom     |   26 |    201 | xs        |
| tom     |   26 |    201 | cw        |
| jack    |   30 |    201 | hr        |
| jack    |   30 |    201 | it        |
| jack    |   30 |    201 | xs        |
| jack    |   30 |    201 | cw        |
| alice   |   24 |    202 | hr        |
| alice   |   24 |    202 | it        |
| alice   |   24 |    202 | xs        |
| alice   |   24 |    202 | cw        |
| robin   |   40 |    200 | hr        |
| robin   |   40 |    200 | it        |
| robin   |   40 |    200 | xs        |
| robin   |   40 |    200 | cw        |
| natasha |   28 |    204 | hr        |
| natasha |   28 |    204 | it        |
| natasha |   28 |    204 | xs        |
| natasha |   28 |    204 | cw        |
+---------+------+--------+-----------+
24 rows in set (0.01 sec)

2、内连接, 只连接匹配的行

只找出有部门的员工, (部门表中没有natasha所在的部门)

mysql> select t3.id,t3.name,t3.age,t4.dep_id,t4.dept_name from t3,t4 
    -> where t3.dep_id=t4.dep_id;
+----+-------+------+--------+-----------+
| id | name  | age  | dep_id | dept_name |
+----+-------+------+--------+-----------+
|  1 | bgx   |   18 |    200 | hr        |
|  2 | tom   |   26 |    201 | it        |
|  3 | jack  |   30 |    201 | it        |
|  4 | alice |   24 |    202 | xs        |
|  5 | robin |   40 |    200 | hr        |
+----+-------+------+--------+-----------+
5 rows in set (0.00 sec)

3、外连接

SELECT 字段列表 FROM 表1 LEFT|RIGHT JOIN 表2 ON 表1.字段 = 表2.字段;

左连接
mysql> select id,name,t4.dept_name from t3 left join t4 on t3.dep_id = t4.dep_id;
+----+---------+-----------+
| id | name    | dept_name |
+----+---------+-----------+
|  1 | bgx     | hr        |
|  5 | robin   | hr        |
|  2 | tom     | it        |
|  3 | jack    | it        |
|  4 | alice   | xs        |
|  6 | natasha | NULL      |
+----+---------+-----------+
6 rows in set (0.00 sec)
右连接
mysql> select id,name,t4.dept_name from t3 right join t4 on t3.dep_id = t4.dep_id;
+------+-------+-----------+
| id   | name  | dept_name |
+------+-------+-----------+
|    1 | bgx   | hr        |
|    2 | tom   | it        |
|    3 | jack  | it        |
|    4 | alice | xs        |
|    5 | robin | hr        |
| NULL | NULL  | cw        |
+------+-------+-----------+
6 rows in set (0.00 sec)

4、符合条件连接查询

以内连接的方式查询 t3和t4表, 找出公司所有部门中年龄大于25岁的员工
mysql> select t3.id,t3.name,t3.age,t4.dept_name 
    -> from t3,t4 
    -> where t3.dep_id = t4.dep_id 
    -> and age >25;
+----+-------+------+-----------+
| id | name  | age  | dept_name |
+----+-------+------+-----------+
|  5 | robin |   40 | hr        |
|  2 | tom   |   26 | it        |
|  3 | jack  |   30 | it        |
+----+-------+------+-----------+
3 rows in set (0.01 sec)
以内连接的方式查询 t3和t4表,并且以age字段降序显示
mysql> select t3.id,t3.name,t3.age,t4.dept_name from t3,t4 where t3.dep_id = t4.dep_id
    -> ORDER BY age DESC;
+----+-------+------+-----------+
| id | name  | age  | dept_name |
+----+-------+------+-----------+
|  5 | robin |   40 | hr        |
|  3 | jack  |   30 | it        |
|  2 | tom   |   26 | it        |
|  4 | alice |   24 | xs        |
|  1 | bgx   |   18 | hr        |
+----+-------+------+-----------+
5 rows in set (0.00 sec)

子查询

  1. 子查询是将一个查询语句嵌套在另一个查询语句中。
  2. 内层查询语句的查询结果,可以为外层查询语句提供查询条件。
  3. 子查询中可以包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等关键字还可以包含比较运算符:=、 !=、> 、<等
1、带 IN 关键字的子查询

查询t3表,但dept_id必须在t4表中出现过

mysql> select * from t3 where dep_id IN (select dep_id from t4);
+----+-------+------+--------+
| id | name  | age  | dep_id |
+----+-------+------+--------+
|  1 | bgx   |   18 |    200 |
|  2 | tom   |   26 |    201 |
|  3 | jack  |   30 |    201 |
|  4 | alice |   24 |    202 |
|  5 | robin |   40 |    200 |
+----+-------+------+--------+
5 rows in set (0.00 sec)
2、代表运算符子查询

查询年龄大于等于 25 岁员工所在部门(查询老龄化的部门)

mysql> select dep_id,dept_name from t4
    -> where dep_id IN
    -> (select DISTINCT dep_id from t3 where age >=25);
+--------+-----------+
| dep_id | dept_name |
+--------+-----------+
|    201 | it        |
|    200 | hr        |
+--------+-----------+
2 rows in set (0.00 sec)
  1. 子查询 EXISTS 关字键字表示存在。在使用 EXISTS关键字时,内层查询语句不返回查询的记录,而是返回一个真假值。
  2. Ture 或 False,当返回 Ture 时,外层查询语句将进行查询;当返回值为 False 时,外层查询语 句不进行查询
  3. t4 表中存在 dep_id=203,Ture
mysql> select * from t3
    -> where EXISTS (select * from t4 where dep_id=203);
+----+---------+------+--------+
| id | name    | age  | dep_id |
+----+---------+------+--------+
|  1 | bgx     |   18 |    200 |
|  2 | tom     |   26 |    201 |
|  3 | jack    |   30 |    201 |
|  4 | alice   |   24 |    202 |
|  5 | robin   |   40 |    200 |
|  6 | natasha |   28 |    204 |
+----+---------+------+--------+
6 rows in set (0.00 sec)
相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1天前
|
关系型数据库 MySQL Linux
在 CentOS 7 中通过编译源码方式安装 MySQL 数据库的详细步骤,包括准备工作、下载源码、编译安装、配置 MySQL 服务、登录设置等。
本文介绍了在 CentOS 7 中通过编译源码方式安装 MySQL 数据库的详细步骤,包括准备工作、下载源码、编译安装、配置 MySQL 服务、登录设置等。同时,文章还对比了编译源码安装与使用 RPM 包安装的优缺点,帮助读者根据需求选择最合适的方法。通过具体案例,展示了编译源码安装的灵活性和定制性。
10 2
|
4天前
|
存储 关系型数据库 MySQL
MySQL vs. PostgreSQL:选择适合你的开源数据库
在众多开源数据库中,MySQL和PostgreSQL无疑是最受欢迎的两个。它们都有着强大的功能、广泛的社区支持和丰富的生态系统。然而,它们在设计理念、性能特点、功能特性等方面存在着显著的差异。本文将从这三个方面对MySQL和PostgreSQL进行比较,以帮助您选择更适合您需求的开源数据库。
18 4
|
8天前
|
存储 监控 关系型数据库
MySQL并发控制与管理:优化数据库性能的关键
【10月更文挑战第17天】MySQL并发控制与管理:优化数据库性能的关键
29 0
|
8天前
|
存储 SQL 关系型数据库
MySQL Workbench支持哪些数据库引擎
【10月更文挑战第17天】MySQL Workbench支持哪些数据库引擎
8 0
|
18天前
|
存储 SQL 关系型数据库
Mysql学习笔记(二):数据库命令行代码总结
这篇文章是关于MySQL数据库命令行操作的总结,包括登录、退出、查看时间与版本、数据库和数据表的基本操作(如创建、删除、查看)、数据的增删改查等。它还涉及了如何通过SQL语句进行条件查询、模糊查询、范围查询和限制查询,以及如何进行表结构的修改。这些内容对于初学者来说非常实用,是学习MySQL数据库管理的基础。
74 6
|
15天前
|
存储 关系型数据库 MySQL
Mysql(4)—数据库索引
数据库索引是用于提高数据检索效率的数据结构,类似于书籍中的索引。它允许用户快速找到数据,而无需扫描整个表。MySQL中的索引可以显著提升查询速度,使数据库操作更加高效。索引的发展经历了从无索引、简单索引到B-树、哈希索引、位图索引、全文索引等多个阶段。
50 3
Mysql(4)—数据库索引
|
18天前
|
SQL Ubuntu 关系型数据库
Mysql学习笔记(一):数据库详细介绍以及Navicat简单使用
本文为MySQL学习笔记,介绍了数据库的基本概念,包括行、列、主键等,并解释了C/S和B/S架构以及SQL语言的分类。接着,指导如何在Windows和Ubuntu系统上安装MySQL,并提供了启动、停止和重启服务的命令。文章还涵盖了Navicat的使用,包括安装、登录和新建表格等步骤。最后,介绍了MySQL中的数据类型和字段约束,如主键、外键、非空和唯一等。
57 3
Mysql学习笔记(一):数据库详细介绍以及Navicat简单使用
|
22天前
|
缓存 算法 关系型数据库
Mysql(3)—数据库相关概念及工作原理
数据库是一个以某种有组织的方式存储的数据集合。它通常包括一个或多个不同的主题领域或用途的数据表。
42 5
Mysql(3)—数据库相关概念及工作原理
|
9天前
|
存储 关系型数据库 MySQL
如何在MySQL中创建数据库?
【10月更文挑战第16天】如何在MySQL中创建数据库?
|
13天前
|
SQL Oracle 关系型数据库
安装最新 MySQL 8.0 数据库(教学用)
安装最新 MySQL 8.0 数据库(教学用)
74 4