实验七 索引和视图的操作
【实验目的】:①学会使用update、delete、insert命令
②掌握视图的建立、修改、更新和删除。
③掌握索引的创建和使用。
【实验内容】:相关命令写在作业本上。
1、更新数据
(1)修改sc表中课程号为“3”的学生成绩。若成绩小于等于80分则提高10%,若大于80分则提高2分。(两条update命令完成)
update sc grade=grade*1.1 where cno='3' and grade<=80;
update sc set grade=grade*1.1 where cno='3' and grade<=80;
(2)把不及格的学生成绩全改为0。
update sc set grade=0 where grade<60;
(3)删除张娜老师的信息。
delete from teacher where tname='张娜';
(4)将选修了“数据结构”的学生的成绩降低10%。
update sc set grade=0.9*grade where cno in (select cno from course where cname='数据结构');
(5)插入一个新的学生记录(2011008,李兰, 1995-7-5,男, 20)。
insert into student values(NUll,'李兰','1995-7-5','男',20,'46478291@qq.com');
(6)插入一个新的学生记录(2011006,张丽娜, 1994-12-23,女,20)。主键冲突了应该如何修改呢?
insert into student values(2011006,'张丽娜','1994-5-11','女',20,'2282240015@qq.com');
replace into student values(2011006,'张丽娜','1998-5-6','女',20,'2282240015@qq.com');
(7)创建一个新表stu,包含三个字段:学号、姓名和系别。然后将student表中信息工程系的学生插入到新表stu中。
方法一:
create table stu(sno int primary key,sname varchar(5),deptno tinyint);
思考用其他方法实现呢????
方法二:
(8)在sc表中删除成绩最差的2条记录。
delete from sc order by grade ASC limit 2;
(9)在student表中删除刘茜同学信息,以及在sc表中其所有选课信息。
set foreign_key_checks=0;
delete student,sc from student inner join sc on student.sno=sc.sno where student.sname='刘茜';
(10)将张天同学的学号改为2012001,以及sc表的学号也要改为2012001。
update student inner join sc on student.sno=sc.sno set student.sno=2012001,sc.sno=2012001 where sname='张天';
2、视图的操作
(1)建立男学生的视图schoolboy,属性包括学号、姓名、性别和出生日期。
create view schoolboy(sno,sname,birthday,sex) As select sno,sname,birth,sex from student where sex=1 with check option;
(2)在视图schoolboy中查询2009年出生的学生学号、姓名、性别。
select sno,sname,sex from schoolboy where year(birth)=2009;
(3)在视图schoolboy中插入一个新的学生记录,其中学号为2011009,姓名为MING。
insert into schoolboy(sno,sname) values('2011009','ming');
(4)删除视图schoolboy中姓名为李兰的记录。判断是删除男生李兰还是女生李兰呢?
delete from schoolboy where sname='李兰';
3、索引的操作
(1)为student表的sname字段创建名为index_name,长度为4的普通索引。
create index index_name on student(sname(4));
(2)在student表中,为deptno和birth创建名为index_t的索引。deptno为升序,birth为降序。
create index index_t on student(deptno asc,birth desc);