MySQL基础(续)
1. mysql数据库操作
1.1 DML操作
DML操作包括增(INSERT)、删(DELETE)、改(UPDATE)、查(SELECT),均属针对表的操作。
1.1.1 INSERT语句
- DML操作之增操作insert
//进入数据库shuai;
mysql> use shuai;
Database changed
//一次插入一条数据
mysql> insert into student (id,name,age) value (1,'tom',20);
Query OK, 1 row affected (0.03 sec)
//查看一下插入数据
mysql> select * from student;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 20 |
+----+------+------+
1 row in set (0.00 sec)
//一次插入多条数据
mysql> insert into student (id,name,age) values (2,'curry',33),(3,'george',32),(4,'james',36),(5,'jordan',56),(6,'durant',33),(7,'ivring',null);
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | tom | 20 |
| 2 | curry | 33 |
| 3 | george | 32 |
| 4 | james | 36 |
| 5 | jordan | 56 |
| 6 | durant | 33 |
| 7 | ivring | NULL |
+----+--------+------+
7 rows in set (0.00 sec)
mysql>
1.1.2 SELECT语句
- 字段column表示法
表示符 | 代表 |
---|---|
* | 所有字段 |
as | 字段别名,如col1 AS alias1 当表名很长时用别名代替 |
- 条件判断语句WHERE
操作类型 | 常用操作符 |
---|---|
操作符 | >,<,>=,<=,=,!= BETWEEN column# AND column# LIKE:模糊匹配 RLIKE:基于正则表达式进行模式匹配 IS NOT NULL:非空 IS NULL:空 |
条件逻辑操作 | AND OR NOT |
- ORDER BY:排序,默认为升序(ASC)
ORDER BY语句 | 意义 |
---|---|
ORDER BY ‘column_name’ | 根据column_name进行升序排序 |
ORDER BY ‘column_name’ DESC | 根据column_name进行降序排序 |
ORDER BY ’column_name’ LIMIT 2 | 根据column_name进行升序排序 并只取前2个结果 |
ORDER BY ‘column_name’ LIMIT 1,2 | 根据column_name进行升序排序 并且略过第1个结果取后面的2个结果 |
- DML操作之查操作
//查看student表中所有内容
mysql> select * from student;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | tom | 20 |
| 2 | curry | 33 |
| 3 | george | 32 |
| 4 | james | 36 |
| 5 | jordan | 56 |
| 6 | durant | 33 |
| 7 | ivring | NULL |
+----+--------+------+
7 rows in set (0.00 sec)
//查看student表中name列内容
mysql> select name from student;
+--------+
| name |
+--------+
| tom |
| curry |
| george |
| james |
| jordan |
| durant |
| ivring |
+--------+
7 rows in set (0.00 sec)
//将student表根据年龄进行升序排序查看
mysql> select * from student order by age;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 7 | ivring | NULL |
| 1 | tom | 20 |
| 3 | george | 32 |
| 2 | curry | 33 |
| 6 | durant | 33 |
| 4 | james | 36 |
| 5 | jordan | 56 |
+----+--------+------+
7 rows in set (0.00 sec)
//根据年龄进行降序排列
mysql> select * from student order by age desc;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 5 | jordan | 56 |
| 4 | james | 36 |
| 2 | curry | 33 |
| 6 | durant | 33 |
| 3 | george | 32 |
| 1 | tom | 20 |
| 7 | ivring | NULL |
+----+--------+------+
7 rows in set (0.00 sec)
//根据年龄进行升序排序并只取前2各结果
mysql> select * from student order by age limit 2;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 7 | ivring | NULL |
| 1 | tom | 20 |
+----+--------+------+
2 rows in set (0.00 sec)
//根据年龄进行升序排序并且略过第1个结果取后面的2个结果
mysql> select * from student order by age limit 1,2;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | tom | 20 |
| 3 | george | 32 |
+----+--------+------+
2 rows in set (0.00 sec)
//查看student表中年龄大于等于32
mysql> select * from student where age >= 32;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 2 | curry | 33 |
| 3 | george | 32 |
| 4 | james | 36 |
| 5 | jordan | 56 |
| 6 | durant | 33 |
+----+--------+------+
5 rows in set (0.00 sec)
//查看表中年龄大于等于32并且名字为george
mysql> select * from student where age >= 32 and name= 'george'
-> ;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 3 | george | 32 |
+----+--------+------+
1 row in set (0.00 sec)
//查看表中年龄在32至36中间的
mysql> select * from student where age between 32 and 36;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 2 | curry | 33 |
| 3 | george | 32 |
| 4 | james | 36 |
| 6 | durant | 33 |
+----+--------+------+
4 rows in set (0.00 sec)
//查看表中年龄不为空值的
mysql> select * from student where age is not null;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | tom | 20 |
| 2 | curry | 33 |
| 3 | george | 32 |
| 4 | james | 36 |
| 5 | jordan | 56 |
| 6 | durant | 33 |
+----+--------+------+
6 rows in set (0.00 sec)
//查看表中年龄为空值的
mysql> select * from student where age is null;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 7 | ivring | NULL |
+----+--------+------+
1 row in set (0.00 sec)
mysql>
1.1.3 UPDATE语句
- DML操作之改操作
//查看所有内容
mysql> select * from student;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | tom | 20 |
| 2 | curry | 33 |
| 3 | george | 32 |
| 4 | james | 36 |
| 5 | jordan | 56 |
| 6 | durant | 33 |
| 7 | ivring | NULL |
+----+--------+------+
7 rows in set (0.00 sec)
//用update语句将ivring年龄从null改为31
mysql> update student set age = 31 where name = 'ivring';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
//查看ivring,age从null改为31
mysql> select * from student where name = 'ivring'
-> ;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 7 | ivring | 31 |
+----+--------+------+
1 row in set (0.00 sec)
mysql>
1.1.4 DELETE语句
- DML操作之删操作delete
//查看所有内容
mysql> select * from student;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | tom | 20 |
| 2 | curry | 33 |
| 3 | george | 32 |
| 4 | james | 36 |
| 5 | jordan | 56 |
| 6 | durant | 33 |
| 7 | ivring | 31 |
+----+--------+------+
7 rows in set (0.00 sec)
//删除id为1表记录
mysql> delete from student where id = 1;
Query OK, 1 row affected (0.00 sec)
//查看已被删除
mysql> select * from student;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 2 | curry | 33 |
| 3 | george | 32 |
| 4 | james | 36 |
| 5 | jordan | 56 |
| 6 | durant | 33 |
| 7 | ivring | 31 |
+----+--------+------+
6 rows in set (0.00 sec)
//删除整张表的内容
mysql> delete from student;
Query OK, 6 rows affected (0.01 sec)
mysql> select * from student;
Empty set (0.00 sec)
mysql> desc student;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql>
1.1.5 TRUNCATE语句
- truncate与delete的区别:
语句类型 | 特点 |
---|---|
delete | DELETE删除表内容时仅删除内容,但会保留表结构 DELETE语句每次删除一行,并在事务日志中为所删除的每行记录一项 可以通过回滚事务日志恢复数据 非常占用空间 |
truncate | 删除表中所有数据,且无法恢复 表结构、约束和索引等保持不变,新添加的行计数值重置为初始值 执行速度比DELETE快,且使用的系统和事务日志资源少 通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放 对于有外键约束引用的表,不能使用TRUNCATE TABLE删除数据 不能用于加入了索引视图的表 |
- DML操作之删操作truncate
//查看所有内容
mysql> select * from student;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 2 | curry | 33 |
| 3 | george | 32 |
| 4 | james | 36 |
| 5 | jordan | 56 |
| 6 | durant | 33 |
| 7 | ivring | NULL |
+----+--------+------+
6 rows in set (0.00 sec)
//删除student表
mysql> truncate student;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from student;
Empty set (0.00 sec)
mysql> desc student;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql>
1.2 DCL操作
1.2.1 创建授权GRANT
- 权限类型(priv_type)
权限类型 | 代表什么? |
---|---|
ALL | 所有权限 |
SELECT | 读取内容的权限 |
INSERT | 插入内容的权限 |
UPDATE | 更新内容的权限 |
DELETE | 删除内容的权限 |
- 指定要操作的对象db_name.table_name
表示方式 | 意义 |
---|---|
. | 所有库的所有表 |
db_name | 指定库的所有表 |
db_name.table_name | 指定库的指定表 |
WITH GRANT OPTION:被授权的用户可将自己的权限副本转赠给其他用户,说白点就是将自己的权限完全复制给另一个用户。不建议使用。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| shuai |
| sys |
+--------------------+
5 rows in set (0.00 sec)
//授权shuai用户在数据库本机上登录访问所有数据库
mysql> grant all on *.* to 'shuai'@'localhost' identified by 'LIshuai123!!!';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> grant all on *.* to 'shuai'@'127.0.0.1' identified by 'LIshuai123!!!';
Query OK, 0 rows affected, 1 warning (0.00 sec)
//授权shuai用户在192.168.183.133上远程登录访问shuai数据库
mysql> grant all on shuai.* to 'shuai'@'192.168.183.133' identified by 'LIshuai123!!!';
Query OK, 0 rows affected, 1 warning (0.00 sec)
//授权shuai用户在所有位置上远程登录访问shuai数据库
mysql> grant all on *.* to 'shuai'@'%' identified by 'LIshuai123!!!';
Query OK, 0 rows affected, 1 warning (0.00 sec)
1.2.2 查看授权
//查看当前登录用户的授权信息
mysql> show grants;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)
//查看指定用户shuai的授权信息
mysql> show grants for shuai;
+--------------------------------------------+
| Grants for shuai@% |
+--------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'shuai'@'%' |
+--------------------------------------------+
1 row in set (0.01 sec)
mysql> show grants for 'shuai'@'localhost';
+----------------------------------------------------+
| Grants for shuai@localhost |
+----------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'shuai'@'localhost' |
+----------------------------------------------------+
1 row in set (0.00 sec)
mysql> show grants for 'shuai'@'127.0.0.1';
+----------------------------------------------------+
| Grants for shuai@127.0.0.1 |
+----------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'shuai'@'127.0.0.1' |
+----------------------------------------------------+
1 row in set (0.00 sec)
1.2.3 取消授权REVOKE
注意:mysql服务进程启动时会读取mysql库中的所有授权表至内存中:
- GRANT或REVOKE等执行权限操作会保存于表中,mysql的服务进程会自动重读授权表,并更新至内存中
- 对于不能够或不能及时重读授权表的命令,可手动让mysql的服务进程重读授权表
mysql> revoke all on *.* from 'shuai'@'192.168.183.133';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
2. 实战案例
-
搭建mysql服务
//配置mysqlyum源 [root@localhost ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm URL transformed to HTTPS due to an HSTS policy --2022-07-26 16:29:10-- ······ mysql57-community-r 100%[===================>] 24.95K 22.9KB/s in 1.1s 2022-07-26 16:29:13 (22.9 KB/s) - 'mysql57-community-release-el7-10.noarch.rpm' saved [25548/25548] [root@localhost ~]# rpm -Uvh mysql57-community-release-el7-10.noarch.rpm warning: mysql57-community-release-el7-10.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Verifying... ################################# [100%] Preparing... ################################# [100%] Updating / installing... 1:mysql57-community-release-el7-10 ################################# [100%] //禁用mysql [root@localhost ~]# yum module disable mysql Failed to set locale, defaulting to C.UTF-8 MySQL Connectors Community 1.2 kB/s | 51 kB 00:43 ······ Is this ok [y/N]: y Complete! //安装mysql [root@localhost ~]# yum -y install mysql-community-server mysql-community-client mysql-community-common mysql-community-devel --nogpgcheck Failed to set locale, defaulting to C.UTF-8 ······ pkgconf-pkg-config-1.4.2-1.el8.x86_64 Complete! //设置开机自启 [root@localhost ~]# systemctl enable --now mysqld [root@localhost ~]# systemctl status mysqld ● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor pres> Active: active (running) since Tue 2022-07-26 16:39:34 CST; 8s ago ······ //确保端口监听 [root@localhost ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 80 *:3306 *:* //在日志文件中过滤临时密码 [root@localhost ~]# grep "password" /var/log/mysqld.log 2022-07-26T08:39:32.340208Z 1 [Note] A temporary password is generated for root@localhost: S8kt*-aJvt/D //登录mysql [root@localhost ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.39 Copyright (c) 2000, 2022, 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. mysql> //修改密码 mysql> set global validate_password_policy=0; Query OK, 0 rows affected (0.00 sec) mysql> set global validate_password_length=1; Query OK, 0 rows affected (0.00 sec) mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'mysql123!'; Query OK, 0 rows affected (0.00 sec) mysql> quit Bye //卸载yum源,避免自动升级 [root@localhost ~]# ls /etc/yum.repos.d/ ls.repo mysql-community-source.repo mysql-community.repo [root@localhost ~]# rpm -e mysql57-community-release [root@localhost ~]# ls /etc/yum.repos.d/ ls.repo [root@localhost ~]#
-
创建一个以你名字为名的数据库,并创建一张表student,该表包含三个字段(id,name,age),表结构如下:
mysql> desc student; +-------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(100) | NO | | NULL | | | age | tinyint(4) | YES | | NULL | | +-------+--------------+------+-----+---------+----------------+ 3 rows in set (0.01 sec)
mysql> create database lishuai; Query OK, 1 row affected (0.01 sec) mysql> use lishuai; Database changed mysql> create table student (id int not null,name varchar(100) not null,age tinyint,primary key (id)); Query OK, 0 rows affected (0.01 sec) mysql> desc student; +-------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(100) | NO | | NULL | | | age | tinyint(4) | YES | | NULL | | +-------+--------------+------+-----+---------+-------+ 3 rows in set (0.02 sec) mysql>
-
查看下该新建的表有无内容(用select语句)
mysql> select * from student; Empty set (0.00 sec) mysql>
-
往新建的student表中插入数据(用insert语句),结果应如下所示:
+----+-------------+------+ | id | name | age | +----+-------------+------+ | 1 | tom | 20 | | 2 | jerry | 23 | | 3 | wangqing | 25 | | 4 | sean | 28 | | 5 | zhangshan | 26 | | 6 | zhangshan | 20 | | 7 | lisi | NULL | | 8 | chenshuo | 10 | | 9 | wangwu | 3 | | 10 | qiuyi | 15 | | 11 | qiuxiaotian | 20 | +----+-------------+------+
mysql> insert into student (id,name,age) VALUES (1,'tom',20),(2,'jerry',23),(3,'wangqing',25),(4,'sean',28),(5,'zhangshan',26),(6,'zhangshan',20),(7,'lisi',NULL),(8,'chenshuo',10),(9,'wangwu',3),(10,'qiuyi',15),(11,'qiuxiaotian',20); Query OK, 11 rows affected (0.00 sec) Records: 11 Duplicates: 0 Warnings: 0 mysql> select * from student; +----+-------------+------+ | id | name | age | +----+-------------+------+ | 1 | tom | 20 | | 2 | jerry | 23 | | 3 | wangqing | 25 | | 4 | sean | 28 | | 5 | zhangshan | 26 | | 6 | zhangshan | 20 | | 7 | lisi | NULL | | 8 | chenshuo | 10 | | 9 | wangwu | 3 | | 10 | qiuyi | 15 | | 11 | qiuxiaotian | 20 | +----+-------------+------+ 11 rows in set (0.00 sec)
-
修改lisi的年龄为50
mysql> update student set age = 50 where name = 'lisi' -> ; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from student; +----+-------------+------+ | id | name | age | +----+-------------+------+ | 1 | tom | 20 | | 2 | jerry | 23 | | 3 | wangqing | 25 | | 4 | sean | 28 | | 5 | zhangshan | 26 | | 6 | zhangshan | 20 | | 7 | lisi | 50 | | 8 | chenshuo | 10 | | 9 | wangwu | 3 | | 10 | qiuyi | 15 | | 11 | qiuxiaotian | 20 | +----+-------------+------+ 11 rows in set (0.00 sec)
-
以age字段降序排序
mysql> select * from student order by age desc; +----+-------------+------+ | id | name | age | +----+-------------+------+ | 7 | lisi | 50 | | 4 | sean | 28 | | 5 | zhangshan | 26 | | 3 | wangqing | 25 | | 2 | jerry | 23 | | 1 | tom | 20 | | 6 | zhangshan | 20 | | 11 | qiuxiaotian | 20 | | 10 | qiuyi | 15 | | 8 | chenshuo | 10 | | 9 | wangwu | 3 | +----+-------------+------+ 11 rows in set (0.00 sec)
-
查询student表中年龄最小的3位同学跳过前2位
mysql> select * from student order by age limit 2,3; +----+-------------+------+ | id | name | age | +----+-------------+------+ | 10 | qiuyi | 15 | | 1 | tom | 20 | | 11 | qiuxiaotian | 20 | +----+-------------+------+ 3 rows in set (0.00 sec)
-
查询student表中年龄最大的4位同学
mysql> select * from student order by age desc limit 4; +----+-----------+------+ | id | name | age | +----+-----------+------+ | 7 | lisi | 50 | | 4 | sean | 28 | | 5 | zhangshan | 26 | | 3 | wangqing | 25 | +----+-----------+------+ 4 rows in set (0.00 sec)
-
查询student表中名字叫zhangshan的记录
mysql> select * from student where name = 'zhangshan'; +----+-----------+------+ | id | name | age | +----+-----------+------+ | 5 | zhangshan | 26 | | 6 | zhangshan | 20 | +----+-----------+------+ 2 rows in set (0.00 sec)
-
查询student表中名字叫zhangshan且年龄大于20岁的记录
mysql> select * from student where name = 'zhangshan' and age > 20; +----+-----------+------+ | id | name | age | +----+-----------+------+ | 5 | zhangshan | 26 | +----+-----------+------+ 1 row in set (0.00 sec)
-
查询student表中年龄在23到30之间的记录
mysql> select * from student where age between 23 and 30; +----+-----------+------+ | id | name | age | +----+-----------+------+ | 2 | jerry | 23 | | 3 | wangqing | 25 | | 4 | sean | 28 | | 5 | zhangshan | 26 | +----+-----------+------+ 4 rows in set (0.00 sec)
-
修改wangwu的年龄为100
mysql> update student set age = 100 where name = 'wangwu'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from student where name = 'wangwu'; +----+--------+------+ | id | name | age | +----+--------+------+ | 9 | wangwu | 100 | +----+--------+------+ 1 row in set (0.00 sec)
-
删除student中名字叫zhangshan且年龄小于等于20的记录
mysql> select * from student; +----+-------------+------+ | id | name | age | +----+-------------+------+ | 1 | tom | 20 | | 2 | jerry | 23 | | 3 | wangqing | 25 | | 4 | sean | 28 | | 5 | zhangshan | 26 | | 6 | zhangshan | 20 | | 7 | lisi | 50 | | 8 | chenshuo | 10 | | 9 | wangwu | 100 | | 10 | qiuyi | 15 | | 11 | qiuxiaotian | 20 | +----+-------------+------+ 11 rows in set (0.00 sec) mysql> delete from student where name = 'zhangshan' and age <= 20; Query OK, 1 row affected (0.00 sec) mysql> select * from student; +----+-------------+------+ | id | name | age | +----+-------------+------+ | 1 | tom | 20 | | 2 | jerry | 23 | | 3 | wangqing | 25 | | 4 | sean | 28 | | 5 | zhangshan | 26 | | 7 | lisi | 50 | | 8 | chenshuo | 10 | | 9 | wangwu | 100 | | 10 | qiuyi | 15 | | 11 | qiuxiaotian | 20 | +----+-------------+------+ 10 rows in set (0.00 sec)