数据库学习13 - 第五章作业

本文分享了作者在学习数据库时遇到的T-SQL难题,主要涉及第五章的第2题和第6题,承认自己在这方面的不足,并表示会加强练习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章目录

第2题 .

在这里插入图片描述

标准SQL(1)
create procedure divide_rank
as
	declare score_90_100 int,
			score_80_90 int,
			score_70_80 int,
			score_60_70 int,
			score_0_60 int,
			score int;
			
	select score_90_100=count(*) 
	from SC,Course 
	where SC.Cno=Course.Cno and Grade>=90 and Grade<=100 and Cname='离散数学' ;
	
	select score_80_90 =count(*) 
	from SC,Course 
	where SC.Cno=Course.Cno and Grade>=80 and Grade<90 and Cname='离散数学' ;
	
	select score_70_80 =count(*) 
	from SC,Course 
	where SC.Cno=Course.Cno and Grade>=70 and Grade<80 and Cname='离散数学' ;
	
	select score_60_70 =count(*) 
	from SC,Course 
	where SC.Cno=Course.Cno and Grade>=60 and Grade<70 and Cname='离散数学' ;
	
	select score_0_60  =count(*) 
	from SC,Course 
	where SC.Cno=Course.Cno and Grade>=0 and Grade<60 and Cname='离散数学' ;
	
	select score_90_100 as '[90,100]',
		   score_80_90  as '[80,90)',
		   score_70_80  as '[70,80)',
		   score_60_70  as '[60,70)',
		   score_0_60   as '[0,60)';

(2)
标准SQLcreate procedure AVG_Cscore(course_name char(10))
as
	select avg(Grade) 
	from SC,Course 
	where SC.Cno=Course.Cno and Course.Cname=course_name

T-SQLcreate procedure AVG_Cscore
	@course_name char(10)
as
	select avg(Grade) 
	from SC,Course 
	where SC.Cno=Course.Cno and Course.Cname=@course_name;
	
(3)
alter table SC add Grade_level char(1);
go
create procedure score_to_level()
as
	update SC set Grade_level='A' where Grade>=90 and Grade<=100;
	update SC set Grade_level='B' where Grade>=80 and Grade<90;
	update SC set Grade_level='C' where Grade>=70 and Grade<80;
	update SC set Grade_level='D' where Grade>=60 and Grade<70;
	update SC set Grade_level='E' where Grade>=0 and Grade<60;

第6题 .

在这里插入图片描述

部门表:

create table Dept
(Deptno char(9),
 Dename char(10),
 Demanager char(10),
 Detel char(11),
 constraint  DeptKey primary key (Deptno), //定义主码
);

职工表:

create table Staff
(Snum char(9),
 Sname char(10),
 Sage char(4) check (Sage<=60),			  //职工年龄不超过60岁
 Swork char(10),
 Sal int,
 Deptno char(9),
 constraint StaffKey primary key (Snum),  //定义主码
 constraint StaKey foreign key(Deptno) references Dept(Deptno)
 										 //定义参照完整性
);

说实话,T-SQL还真不怎么会用,以至于例题有的都做不出来,还要靠同学的表验证,之后要更加强这方面的练习。
…………………………………………………………………………….

在这里插入图片描述
以上就是文章全部内容,感谢阅读。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不想秃头少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值