一、概述

sql作为结构化查询语言,在日常的系统中应用广泛,对于这个做几个小练习。

二、练习内容

1、导入练习环境hellodb

image.png

2、练习一:在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄

select Name,Age from students where Age > 25;

image.png

3、练习二:以ClassID为分组依据,显示每组的平均年龄

 select avg( Age),ClassID from students group by ClassID;

image.png

4、练习三: 显示练习二中平均年龄大于30的分组及平均年龄

select ClassID,avg(Age) from students group by ClassID having avg(Age) > 30;

image.png

5、练习四:显示以L开头的名字的同学的信息

 select * from students where Name like 'L%';

image.png

6、练习五:数据库授权magedu用户,允许192.168.1.0/24网段可以连接mysql

create user 'magedu'@'192.168.1.0/24';

image.png 确定增加了用户 image.png

给用户授权所有数据库的所有表

GRANT ALL PRIVILEGES on *.* to 'magedu'@'192.168.1.0/24' identified by 'magedu';
flush PRIVILEGES;

image.png image.png

三、总结

通过以上练习,对基础操作有了基本的理解。