数据库操作--关联删除数据、查找数据、创建视图、创建存储过程

任务点:

• 向每个表插入3条测试数据;

• 查询出出生年份在2002年-2003年的所有学生信息;

• 创建视图查询选修了“大数据”的学生姓名、平时成绩、期末成绩、总评成绩;

• 删除学号为“S22001”的学生的所有成绩;

• 创建存储过程,统计每人的平均总评成绩。


首先创建三个数据表:T_student_info、T_course_info、T_student_scores。

-- 学生信息表
create table T_student_info(
student_no char(6) not null comment'学号',
student_name char(20) not null comment'姓名',
sex	 char(2) not null comment'性别',
birth datetime not null comment	'出生年月',
enter_date int not null comment	'入学年份',
address varchar(50) not null comment	'家庭住址',
constraint pk_student_no primary key (student_no)
);

-- 课程信息表
create table T_course_info(
course_no char(8) not null comment	'课程编号',
course_name char(50) not null comment	'课程名',
credit int not null comment	'学分',
class_hour	int not null comment'学时',
constraint pk_course_no primary key (course_no)
);

-- 成绩信息表
create table T_student_scores(
course_no char(8) not null comment'课程编号',
student_no char(6) not null comment'学号',
ordinary_scores float not null comment'平时成绩',
end_scores float not null comment'期末成绩',
total_scores float not null comment'总评成绩',
primary key(course_no,student_no),
constraint fk_course_no foreign key (course_no) references T_course_info(course_no),
constraint fk_student_no foreign key (student_no) references T_student_info(student_no)
);

•向每个表插入3条测试数据(样本数据包含下面题目中使用的数据)

insert into T_student_info values
('202010','小鞠','女','2002-04-23',2020,'湖南长沙劳动东路99号'),
('202011','刘德华','男','2002-10-23',2020,'湖南长沙劳动东路168号'),
('202012','郭启东','男','2005-03-09',2021,'湖南长沙劳动东路03号'),
('202013','张国荣','男','2002-06-19',2020,'湖南长沙劳动东路19号');


insert into T_course_info VALUES
('#C04','Java程序设计',1,100),
('#C01','大数据',1,100),
('#C02','C语言',1,120),
('#C03','C++',1,110);


insert into T_student_scores values 
('#C02','202011',85.6,90.3,98.5),
('#C04','202010',98.6,99.3,98.5),
('#C01','202011',89.6,94.3,92.5),
('#C02','202012',93.6,96.3,95.5),
('#C03','202013',96.6,98.3,97.5);

• 查询出出生年份在2002年-2003年的所有学生信息;

select *|查询字段 from 表名 [where 条件]

between 条件 and 条件   ---(在什么和什么之间)

select * from T_student_info
where year(birth) between 2002 and 2003;

 • 创建视图查询选修了“大数据”的学生姓名、平时成绩、期末成绩、总评成绩;

create view T_s1 as 
select a.student_name 学生姓名,b.ordinary_scores	平时成绩,b.end_scores 期末成绩,b.total_scores 总评成绩
from T_student_info a,T_student_scores b
where a.student_no=b.student_no and b.course_no=(
select course_no from T_course_info
where course_name='大数据');

select * from T_s1;

 • 删除学号为“S22001”的学生的所有成绩;

删除表中的数据:

---- delete from 表名 where 条件

delete from T_student_scores where student_no='202012';

select * from T_student_scores;

• 创建存储过程,统计每人的平均总评成绩。

create procedure cc()
begin
	select a.student_name 学生姓名,avg(b.total_scores) 平均总评成绩
	from T_student_info a,T_student_scores b
	where a.student_no=b.student_no group by a.student_no;
end

call cc();


完成啦,希望有帮助到大家,有疑问或问题也可在评论区留言~ 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值