随笔 - 18  文章 - 0 评论 - 2 阅读 - 1732
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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)

posted on   ray0712  阅读(37)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示