1.导入hellodb.sql生成数据库
导入hellodb数据库:
[root@centosmin7 ~]# mysql -p < hellodb_innodb.sql
[root@centosmin7 ~]# mysql -p
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| hellodb |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.01 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| hellodb |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.01 sec)
MariaDB [(none)]> use hellodb;
Database changed
MariaDB [hellodb]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.00 sec)
(1) 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
MariaDB [hellodb]> desc 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.01 sec)
MariaDB [hellodb]> 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为分组依据,显示每组的平均年龄
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)
(3) 显示第2题中平均年龄大于30的分组及平均年龄
MariaDB [hellodb]> SELECT ClassID,avg(age) as age FROM students GROUP BY ClassID HAVING age > 30;
+---------+---------+
| ClassID | age |
+---------+---------+
| NULL | 63.5000 |
| 2 | 36.0000 |
| 5 | 46.0000 |
+---------+---------+
3 rows in set (0.00 sec)
(4) 显示以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)
2.数据库授权magedu用户,允许192.168.177.0/24网段可以连接mysql
MariaDB [(none)]> use hellodb
Database changed
MariaDB [hellodb]> CREATE user magedu@'192.168.177.%' identified by 'stone';
Query OK, 0 rows affected (0.00 sec)
MariaDB [hellodb]> SELECT user,host,password FROM mysql.user WHERE user='magedu';
+--------+---------------+-------------------------------------------+
| user | host | password |
+--------+---------------+-------------------------------------------+
| magedu | 192.168.177.% | *BBE37FCE8F92DCEABE3FFE8C290F7A15F12F9DCF |
+--------+---------------+-------------------------------------------+
1 row in set (0.00 sec)
MariaDB [hellodb]> GRANT all on *.* to magedu@'192.168.177.%' identified by 'stone';
Query OK, 0 rows affected (0.00 sec)
MariaDB [hellodb]> SHOW GRANTS FOR magedu@'192.168.177.%';
+----------------------------------------------------------------------------------------------------------------------------+
| Grants for magedu@192.168.177.% |
+----------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'magedu'@'192.168.177.%' IDENTIFIED BY PASSWORD '*BBE37FCE8F92DCEABE3FFE8C290F7A15F12F9DCF' |
+----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
修改账户权限:
MariaDB [hellodb]> use mysql
Database changed
MariaDB [mysql]> UPDATE user set host='192.168.177.%' WHERE user='magedu';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
MariaDB [mysql]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> SELECT user,host,password FROM user WHERE user='magedu';
+--------+---------------+-------------------------------------------+
| user | host | password |
+--------+---------------+-------------------------------------------+
| magedu | 192.168.177.% | *BBE37FCE8F92DCEABE3FFE8C290F7A15F12F9DCF |
+--------+---------------+-------------------------------------------+
1 row in set (0.00 sec)
3.总结mysql常见的存储引擎以及特点。
https://www.jianshu.com/p/cf4e34fb999f
网友评论