数据库
首页 > 数据库> > 山东大学 2020级数据库系统 实验四

山东大学 2020级数据库系统 实验四

作者:互联网

What’s more

山东大学 2020级数据库系统 实验一
山东大学 2020级数据库系统 实验二
山东大学 2020级数据库系统 实验三
山东大学 2020级数据库系统 实验四
山东大学 2020级数据库系统 实验五
山东大学 2020级数据库系统 实验六
山东大学 2020级数据库系统 实验七
山东大学 2020级数据库系统 实验八、九

写在前面

做数据库实验一定要静得下心来,才能发现其中的错误然后进行改正。同时,如果发现 SQL 语句总是报错,“一定是你错了,只是不知道错在哪里!”

其次,SQL 语句中较为复杂的点博主都进行了注释,希望大家一定要看懂思路后自己写一遍,而不是盲目的 Ctrl+C,Ctrl+V,切记切记!!

实验四

实验四主要考察的内容如下:
对于 alter 语句的掌握程度以及是否能够使用它来对表中的属性进行操作;

对于 update … set … where 子句的使用;

对字符串的处理以及删除字符串中相应的字符;

alter table test4_01
	add sum_score int
update test4_01 S
set sum_score =
	(select sum(score)
	from pub.student_course T
	where S.sid = T.sid
	group by sid)
alter table test4_02
	add avg_score numeric(3, 1)
update test4_02 S
set avg_score = 
	(select round(avg(score), 1)
	from pub.student_course T
	where S.sid = T.sid
	group by sid)
alter table test4_03
	add sum_credit int
update test4_03 t0
set sum_credit = 
	(select sum(credit)
	from pub.course natural join 
		(select sid, cid, max(score) max_score		-- 得到每个学生每门课的最高分
		from pub.student_course
		group by sid, cid) t1
	where t0.sid = sid
	and t1.max_score >= 60				-- 利用最高分来进行判断
	group by sid)
create table test4_04 as
select *
from pub.student_41
update test4_04 t0
set dname =
	(select did
	from pub.department
	where dname = t0.dname)
where t0.dname in
	(select dname
	from pub.department)

需要注意的是:对于院系编号地更新时,现在是从两个表中寻找对应的院系编号而不是一个表了(当时卡了好久/(ㄒoㄒ)/~~),这两个表可以通过 union 来连接。(由于是前面问题的综合,因此代码较长)

create table test4_05 as
select *
from pub.student_41
alter test4_05
	add sum_score int
-----------------分开哦-----------------------
alter test4_05
	add avg_score numeric(3, 1)
-----------------分开哦-----------------------
alter test4_05
	add sum_credit int
-----------------分开哦-----------------------
alter test4_05
	add did varchar(2)
update test4_05 t0
set
sum_score = 
	(select sum(score)
	from pub.student_course t1
	where t0.sid = t1.sid
	group by sid), 
avg_score =
	(select round(avg(score), 1)
	from pub.student_course t2
	where t0.sid = t2.sid
	group by sid),
sum_credit = 
	(select sum(credit)
	from pub.course natural join 
		(select sid, cid, max(score) max_score
		from pub.student_course
		group by sid, cid) t1
	where t0.sid = sid
	and t1.max_score >= 60
	group by sid),
did = 
case
	when dname in
	(	(select dname
		from pub.department)
		union
		(select dname
		from pub.department_41)	)
	then
	(select did
	from 
	(	(select dname, did
		from pub.department)
		union
		(select dname, did
		from pub.department_41)	)
	where dname = t0.dname)
else '00'
end
create table test4_06 as
select *
from pub.student_42
update test4_06
set name = replace(name, ' ', '')
create table test4_07 as
select *
from pub.student_42
update test4_07
set sex = 
case
	when sex like '%男%' then '男'		-- %表示省略一个或多个字符
	when sex like '%女%' then '女'
	else sex
End
create table test4_08 as
select *
from pub.student_42
update test4_08
set class = replace(class, '级', '')
create table test4_09 as
select *
from pub.student_42
update test4_09
set age = 2012 - extract(year from birthday)
where age is null
create table test4_10 as
select *
from pub.student_42
update test4_10
set name = replace(name, ' ', ''),
dname = replace(dname, ' ', ''), 
sex = 
case
	when sex like '%男%' then '男'
	when sex like '%女%' then '女'
	else sex
end,
class = replace(class, '级%', ''),
age =
case 
	when age is null then 2012 - extract(year from birthday)
	else age
end

再次强调:一定是看懂思路之后自己实践哈~~
有问题还请斧正!

请添加图片描述

标签:test4,pub,score,2020,student,sid,数据库系统,山东大学,select
来源: https://blog.csdn.net/qq_53413759/article/details/120467158