Note/大学笔记/数据库系统原理/练习题/第三次上机.md

7.1 KiB
Raw Blame History

一、上机内容

  1. 使用SQL语句ALTER TABLE分别删除studentsdb数据库的student_info表、grade表、curriculum表的主键索引。
alter table student_info drop primary key;
alter table grade drop primary key;
alter table curriculum drop primary key;
  1. 使用SQL语句为curriculum表的课程编号创建唯一性索引命名为cno_idx。
create unique index cno_idx on curriculum(课程编号);
  1. 使用SQL语句为grade表的“分数”字段创建一个普通索引命名为grade_idx。
create index grade_idx on grade(分数);
  1. 使用SQL语句为grade表的“学号”和“课程编号”字段创建一个复合唯一索引命名为grade_sid_cid_idx。
create index grade_sid_cid_idx on grade(学号,课程编号);
  1. 查看grade表上的索引信息。
show index from grade;
  1. 使用SQL语句删除索引grade_idx。再次查看grade表上的索引信息。
drop index grade_idx on grade;
show index from grade;
  1. 使用SQL语句CREATE VIEW建立一个名为v_stu_c的视图显示学生的学号、姓名、所学课程的课程编号并利用视图查询学号为0003的学生情况。
create view v_stu_c as select s.学号,姓名,课程编号 from student_info s,grade g where s.学号=g.学号;
select * from v_stu_c where 学号 = '0003';

8.基于student_info表、curriculum表和grade表建立一个名为v_stu_g的视图视图包括所有学生的学号、姓名、课程名称、分数。使用视图v_stu_g查询学号为0001的学生的课程平均分。

create view v_stu_g as select s.学号,姓名,课程名称,分数 from student_info s,grade g,curriculum c where s.学号=g.学号 and g.课程编号=c.课程编号;
select avg(分数) 平均分 from v_stu_g where 学号='0001';

9.使用SQL语句修改视图v_stu_g显示学生的学号、姓名、性别。

alter view v_stu_g as select 学号,姓名,性别 from student_info;
select 学号,姓名,性别 from v_stu_g;

10.利用视图v_stu_g为student_info表添加一行数据学号为0010、姓名为陈婷婷、性别为女。

insert into v_stu_g (学号,姓名,性别) values ('0010','陈婷婷','女');

11.利用视图v_stu_g删除学号为0010的学生记录。

delete from v_stu_g where 学号='0010';

12.利用视图v_stu_g修改姓名为张青平的学生的高等数学的分数为87。

update grade set 分数=87 where 学号=(select 学号 from v_stu_g where 姓名='张青平') and 课程编号=(select 课程编号 from curriculum where 课程名称='高等数学');

13.使用SQL语句删除视图v_stu_c和v_stu_g。

drop view v_stu_c,v_stu_g;
  1. 在本地主机创建用户账号st_01密码为123456。
create user st_01@localhost identified by'123456';
  1. 查看MySQL下所有用户账号列表。
use mysql;
select * from user;
  1. 修改用户账号st_01的密码为111111。
set password for st_01@localhost='111111';
  1. 使用studentsdb数据库中的student_info表。

1授予用户账号st_01查询表的权限。

grant select on table studentsdb.student_info to st_01@localhost;

2授予用户账号st_01更新家庭住址列的权限。

grant update(家庭住址) on table studentsdb.student_info to st_01@localhost;

3授予用户账号st_01修改表结构的权限。

grant alter on table studentsdb.student_info to st_01@localhost;
  1. 使用studentsdb数据库。

1授予用户账号st_01在studentsdb数据库上创建表、删除表、查询数据、插入数据的权限。

grant create,select,insert,drop on studentsdb.* to st_01@localhost;

2以用户账号st_01连接MySQL服务器创建新表st_copy与表student_info完全相同。

exit
mysql -ust_01 -p
create table studentsdb.st_copy select * from studentsdb.student_info;

3以用户账号st_01连接MySQL服务器删除表st_copy。

drop table studentsdb.st_copy;
  1. 撤消用户账号st_01在studentsdb数据库上创建表、删除表、查询数据、插入数据的权限。
revoke create,select,insert,drop on studentsdb.* from st_01@localhost;
  1. 撤消用户账号st_01所有权限.
revoke all privileges,grant option from st_01@localhoat;
错误

22 使用studentsdb数据库中的student_info表。

1创建本地机角色student。

create role 'student'@'localhost';

2授予角色student查询student_info表的权限。

grant select on table studentsdb.student_info to 'student'@'localhost';

3创建本地机用户账号st_02密码为123。

create user st_02@localhost identified by '123';

4授予用户账号st_02角色student的权限。

grant 'student'@'localhost' to st_02@localhost;
set global activate_all_roles_on_login = on;

5以用户账号st_02连接MySQL服务器查看student_info表信息。

exit
mysql -ust_02 -p
select * from studentsdb.student_info;

6撤消用户账号st_02角色student的权限。

revoke all privileges ,grant option from 'student'@'localhost';

7删除角色student。

drop role 'student'@'localhost';

23.删除用户账号st_01、st_02。

drop user st_01@localhost,st_02@localhost;

二、实验思考

1.建立索引的目的。什么情况下不适于在表上建立索引。

目的:

1.加快表之间的连接 2.加快数据的检索速度 3.通过唯一性索引可确保数据的唯一性 4.减少分组和排序时间 5.使用优化隐藏器提高系统性能

以下情况不适合在表上创建索引:

1.数据量小的表 2.大量重复数据的列 3.当修改性能远大于检索性能 4.查询过程中很少使用或参考的列

2.能否在视图上建立索引。

mysql的视图不能创建视图

3.想通过视图修改表中数据,视图应具备哪些条件。

1.select语句在选择列表中没有聚合函数也不包含top,group by,union或distinct子句 2.select语句的选择列表中没有派生列 3.select语句中的from子句至少引用一个表 4.insert,update和delete语句在引用可更新视图之前也必须如上述条件指定的那样满足某些限制条件

4.视图的作用。

1.视图隐藏了底层的表结构,简化了数据访问操作 2.视图提供了一个统一访问数据的接口 3.加强了安全性,使用户只能看到视图所显示的数据 4.视图还可以被嵌套,一个视图中可以嵌套另一个视图

5.用户账号、角色和权限之间的关系是什么?没有角色能给用户授予权限吗?

关系:

1.角色对应一定的权限,角色的权限可以被赋予和收回(有些特定的不能改) 2.用户可以通过被赋予角色来得到权限,也可以直接被赋予权限。

没有角色不能给用户授权权限

6.角色在用户账号连接服务器后自动被激活的设置方法。