1、 導(dǎo)入hellodb.sql生成數(shù)據(jù)庫
[root@centos8 workspace]# mysql -uroot -pddq123 < /root/workspace/hellodb_innodb.sql
[root@centos8 workspace]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.33 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 |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use hellodb;
mysql> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.00 sec)
(1) 在students表中油额,查詢年齡大于25歲泞坦,且為男性的同學(xué)的名字和年齡
mysql> describe students;
+-----------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+----------------+
| StuID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| Name | varchar(50) | NO | | NULL | |
| Age | tinyint(3) unsigned | NO | | NULL | |
| Gender | enum('F','M') | NO | | NULL | |
| ClassID | tinyint(3) unsigned | YES | | NULL | |
| TeacherID | int(10) unsigned | YES | | NULL | |
+-----------+---------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
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為分組依據(jù),顯示每組的平均年齡
mysql> select ClassID,avg(Age) from students group by ClassID order by ClassID;
+---------+----------+
| ClassID | avg(Age) |
+---------+----------+
| NULL | 63.5000 |
| 1 | 20.5000 |
| 2 | 36.0000 |
| 3 | 20.2500 |
| 4 | 24.7500 |
| 5 | 46.0000 |
| 6 | 20.7500 |
| 7 | 19.6667 |
+---------+----------+
8 rows in set (0.00 sec)
(3) 顯示第2題中平均年齡大于30的分組及平均年齡
mysql> select ClassID,avg(Age) from students group by ClassID having avg(Age)>30 order by ClassID;
+---------+----------+
| ClassID | avg(Age) |
+---------+----------+
| NULL | 63.5000 |
| 2 | 36.0000 |
| 5 | 46.0000 |
+---------+----------+
3 rows in set (0.00 sec)
(4) 顯示以L開頭的名字的同學(xué)的信息
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)
2愕贡、數(shù)據(jù)庫授權(quán)magedu用戶草雕,允許192.1.0/24網(wǎng)段可以連接mysql
mysql> create user 'magedu'@'192.168.1.0/24' identified by 'magedu123';
Query OK, 0 rows affected (0.00 sec)
#在192.168.1.0/24網(wǎng)段上登錄mysql服務(wù)器
[root@centos8 ~]# ip address show ens3 |grep inet
inet 192.168.1.8/24 brd 192.168.1.255 scope global noprefixroute ens3
inet6 fe80::42b0:4ef8:a735:7ae/64 scope link noprefixroute
[root@centos8 ~]# mysql -umagedu -p -h192.168.0.8
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.33 Source distribution
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>