mysql练习
Posted 落阳٩
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql练习相关的知识,希望对你有一定的参考价值。
导入hellodb.sql生成数据库
#准备环境
[root@centos8 ~]# mysql < hellodb_innodb.sql
[root@centos8 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \\g.
Your MySQL connection id is 10
Server version: 8.0.26 Source distribution
Copyright (c) 2000, 2021, 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> show databases; #查看数据库
+--------------------+
| Database |
+--------------------+
| hellodb |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use hellodb; #进入hellodb数据库
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
mysql> show tables; #查看hellodb中的表
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.00 sec)
(1)在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄#######
mysql> select name,age from students where Age>25 and Gender=M;
+--------------+-----+
| name | age |
+--------------+-----+
| Xie Yanke | 53 |
| Ding Dian | 32 |
| Yu Yutong | 26 |
| Shi Qing | 46 |
| Tian Boguang | 33 |
| Xu Xian | 27 |
| Sun Dasheng | 100 |
+--------------+-----+
7 rows in set (0.00 sec)
(2) 以ClassID为分组依据,显示每组的平均年龄#######
#一旦分组 group by ,select语句后只跟分组的字段,聚合函数
mysql> select classid,avg(age) from students group by classid;
+---------+----------+
| classid | avg(age) |
+---------+----------+
| 2 | 36.0000 |
| 1 | 20.5000 |
| 4 | 24.7500 |
| 3 | 20.2500 |
| 5 | 46.0000 |
| 7 | 19.6667 |
| 6 | 20.7500 |
| NULL | 63.5000 |
+---------+----------+
8 rows in set (0.00 sec)
(3) 显示第2题中平均年龄大于30的分组及平均年龄#######
##HAVING: 对分组聚合运算后的结果指定过滤条件
mysql> select classid,avg(age) avg from students group by classid having avg>30;
+---------+---------+
| classid | avg |
+---------+---------+
| 2 | 36.0000 |
| 5 | 46.0000 |
| NULL | 63.5000 |
+---------+---------+
3 rows in set (0.00 sec)
##(4) 显示以L开头的名字的同学的信息
mysql> select * from students where name like L%;
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
+-------+-------------+-----+--------+---------+-----------+
3 rows in set (0.00 sec)
数据库授权magedu用户,允许192.168.1.0/24网段可以连接mysql
#mysql在8.0之后取消了常见用户同时授权的操作
mysql> create user magedu@192.168.1.% identified by 123456;
Query OK, 0 rows affected (0.04 sec)
mysql> select user,host from mysql.user;
+------------------+-------------+
| user | host |
+------------------+-------------+
| magedu | 192.168.1.% |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-------------+
5 rows in set (0.00 sec)
mysql> grant all on *.* to magedu@192.168.1.%; #授权账号
Query OK, 0 rows affected (0.01 sec)
以上是关于mysql练习的主要内容,如果未能解决你的问题,请参考以下文章