数据库---单表查询

一、单表查询 库表student.report,有3个字段, 姓名、 学科、 成绩, 记录如下, 根据要求完成SQL语句

NameSubjectResult
李白Math95
杜甫English83
李商隐Math79
白居易Math98
李清照English85
王维Math74

1、查询姓李的同学的个数

2、查询表中数学成绩大于80的前2名同学的名字, 并按分数从大到小的顺序排列

二、用户授权

1.MySQL如何对用户smart授权访问,密码为123456。

2.授权用户tom可以在网络中的192.168.4.254主机登录,仅对对userdb库下的user表有查看记录、更新name字段的权限 , 登录密码userweb888。

三、备份恢复 现在有一个MySQL数据库,库名test,要求使用mysqldump对数据库进行备份。

1、创建数据库

mysql> create database student;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| student |
| sys |
+--------------------+

2、插入表数据

mysql> use student
Database changed
mysql> create table `report`(
 -> `sname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
 -> `subject` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
 -> `result` int(2) NOT NULL);
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO `report` VALUES ('李白','Math',95);
mysql> INSERT INTO `report` VALUES ('杜甫','English',83);
mysql> INSERT INTO `report` VALUES ('李商隐','Math',79);
mysql> INSERT INTO `report` VALUES ('白居易','Math',98);
mysql> INSERT INTO `report` VALUES ('李清照','English',85);
mysql> INSERT INTO `report` VALUES ('王维','Math',74);
mysql> select * from report;
+-----------+---------+--------+
| sname | subject | result |
+-----------+---------+--------+
| 李白 | Math | 95 |
| 白居易 | Math | 98 |
| 杜甫 | English | 83 |
| 李商隐 | Math | 79 |
| 王维 | Math | 74 |
| 李清照 | English | 85 |
+-----------+---------+--------+
6 rows in set (0.00 sec)

3、查询姓李的同学的个数

mysql> select count(*) 老李头个数 from report where sname like "李%";
+-----------------+
| 老李头个数 |
+-----------------+
| 3 |
+-----------------+
1 row in set (0.00 sec)

4、查询表中数学成绩大于80的前2名同学的名字, 并按分数从大到小的顺序排列

mysql> select sname 姓名 ,result 成绩 from report where result > 80 order by result desc limit 2;
+-----------+--------+
| 姓名 | 成绩 |
+-----------+--------+
| 白居易 | 98 |
| 李白 | 95 |
+-----------+--------+
2 rows in set (0.00 sec)

5、MySQL如何对用户smart授权访问,密码为123456。

mysql> grant all on *.* to smart@'%' identified by'123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>

6、授权用户tom可以在网络中的192.168.4.254主机登录,仅对对userdb库下的user表有查看记录、更新name字段的权限 , 登录密码userweb888。 (需要单独创建数据库)

mysql> grant select,update(name) on userdb.user to tom@'192.168.4.254' identified by'userweb888';
Query OK, 0 rows affected, 1 warning (0.00 sec)
作者:ChAnAn原文地址:https://www.cnblogs.com/sre-chan/p/17238265.html

%s 个评论

要回复文章请先登录注册