1、 导入hellodb.sql生成数据库
- 导入hellodb.sql生成数据库
[root@centos7 ~]# ll hellodb_innodb.sql
-rw-r--r-- 1 root root 7786 Dec 4 20:24 hellodb_innodb.sql
[root@centos7 ~]# mysql < hellodb_innodb.sql
- 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄;
MariaDB [hellodb]> SELECT Name,Age FROM students WHERE Age > 25 ;
+--------------+-----+
| 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)
- 以ClassID为分组依据,显示每组的平均年龄;
MariaDB [hellodb]> SELECT ClassID,AVG(Age) FROM students GROUP 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)
- 显示第2题中平均年龄大于30的分组及平均年龄;
MariaDB [hellodb]> SELECT ClassID,AVG(Age) FROM students GROUP BY ClassID HAVING AVG(Age) > 30;
+---------+----------+
| ClassID | AVG(Age) |
+---------+----------+
| NULL | 63.5000 |
| 2 | 36.0000 |
| 5 | 46.0000 |
+---------+----------+
3 rows in set (0.00 sec)
- 显示以L开头的名字的同学的信息;
MariaDB [hellodb]> 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
MariaDB [(none)]> GRANT ALL ON . to magedu@'192.168.0.%' identified by 'magedu';
Query OK, 0 rows affected (0.00 sec)
另一台主机192.168.0.105上测试,可以看到可以连接
[root@centos7 ~]# mysql -umagedu -pmagedu -h192.168.0.104
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具