1、 操作系统:window10
2、 MySQL 5.7
insert into student values
('2005010','赵清江',18,'男','Cs'),
('2005011','张丽萍',19,'女','Ch'),
('2005012','陈景欢',20,'男','Is'),
('2005013','陈婷婷',16,'女','Ph'),
('2005014','李军',16,'女','Ph');
insert into sc values
('2005010','1',87),
('2005010','2',null),
('2005010','3',80),
('2005010','4',87),
('2005010','6',85),
('2005011','1',52),
('2005011','2',47),
('2005011','3',53),
('2005011','5',45),
('2005012','1',84),
('2005012','3',null),
('2005012','4',67),
('2005012','5',81);
insert into sc
select sno,'7',null
from student
where sdept='Is';
2.2在表student中检索每门课程均不及格的学生学号、姓名、年龄、性别及所在系信息,并把检索到的信息存入新表ts中:
create table ts(
sno char(7) primary key,
sname char(10),
sage int,
ssex char(4),
sdept char(2)
)
insert into ts
select *
from student
where sno in(select sno from sc
group by sno having max(grade)<60);
2.3将学号为2005001的学生姓名改为“刘华”,年龄增加1岁:
update student set
sname='刘华', sage=sage+1
where sno='2005001';
2.4把选修了“数据库系统”课程而成绩不及格的学生的成绩全部改为空值:
SET SQL_SAFE_UPDATES = 0;
update sc set grade=null
where cno in(select cno from course where cname='数据库系统') and grade<60;
SET SQL_SAFE_UPDATES = 1;
注:SET SQL_SAFE_UPDATES = 0;语句关闭了安全模式,使得更新数据时的where语句中不包含primary key的列也可以进行更新。SQL_SAFE_UPDATES的值默认为1,即保持安全模式打开,安全模式是为了防止在更新数据时忘了加where语句对要更新的数据进行限制而一次修改了全部数据。一般修改结束后要记得重新打开安全模式。
2.5 学生王林在3号课程考试作弊,该课程成绩改为空值:
为了使查询有结果,我们对表sc的内容进行修改:
update sc
set grade=null
where cno='3' and sno in(select sno from student where sname='王林');
set sql_safe_updates=0;
update sc
set grade=grade*1.05
where grade<(select avg_grade from(select avg(grade) as avg_grade from sc) as t) and sno in(select sno from student where ssex='女');
set sql_safe_updates=1;
2.7 在基本表sc中修改课程号为“2”号课程的成绩,成绩小于等于80分时降低2%,成绩大于80分时降低1%:
update sc set grade=grade*0.98 where grade<=80 and cno='2';
update sc set grade=grade*0.99 where grade>80 and cno='2';
2.8 把“钱横”的选课记录全部删去:
set sql_safe_updates=0;
delete from sc
where sno in (select sno from student where sname='钱横');
set sql_safe_updates=1;