为一个中学的考试建立一个初步的数据库管理系统 其中初步设计包含三张表,请按下列具体要求建表、输入数据、并查询结果
具体设计要求:
建一数据库名为:StuQa (create database StuQa)
其中包括三张表
学生基本信息表 表名:StuInfo
StuId char(10) 主键-- 学号
FName varchar(50) 非空 -- 姓名
StuGen char(2) 非空-- 性别
StuAdds varchar(50)非空--地址
StuEmail varchar(50) check 约束 --Email
课程表:Course
CourseId char(10) 主键 --课程号
CourseName varchar(50) not null 课程名
学生成绩表:Score
ScoreId int 标识列 (起始1,增量1)主键 --成绩记录号
CourseId char(10) 课程号外键
StuId char(10) 学生号外键
Score int not null –分数
课程表内容:
课程号 课程名
F001 语文
F002 数学
F003 英语
F004 历史
学生表内容:
学号 姓名 性别 地址 Email
M001 张赛 男 上海 [email protected]
M002 王强 男 上海 [email protected]
M003 王五 男 上海 [email protected]
M004 李四 男 上海 [email protected]
M005 阳阳 男 上海 [email protected]
M006 虎子 男 上海 [email protected]
M007 夏雪 女 北京 [email protected]
M008 璐璐 女 深圳 [email protected]
M009 姗姗 女 广州 [email protected]
M010 香奈儿 女 重庆 [email protected]
成绩表内容:
课程号 学号 分数
F001 M001 78
F002 M001 67
F003 M001 89
F004 M001 76
F001 M002 89
F002 M002 67
F003 M002 84
F004 M002 96
F001 M003 70
F002 M003 87
F003 M003 92
F004 M003 56
F001 M004 80
F002 M004 78
F003 M004 97
F004 M004 66
F001 M006 88
F002 M006 55
F003 M006 86
F004 M006 79
F002 M007 77
F003 M008 65
F004 M007 48
F004 M009 75
F002 M009 88
create table StuInfo1(
StuId char(10) primary key,
FName varchar(50) not null,
StuGen char(2) not null,
StuAdds varchar(50) not null,
StuEmail varchar(50) check(StuEmail like '_%@_%._%')
)
create table Course1(
CourseId char(10) primary key,
CourseName varchar(50) not null
)
create table Score1(
ScoreId int identity(1,1) primary key,
CourseId char(10) foreign key references Course1(CourseId),
StuId char(10) foreign key references StuInfo1(StuId),
Score int not null
)
insert into Course1
select 'F001','语文' union
select 'F002','数学' union
select 'F003','英语' union
select 'F004','历史'
insert into StuInfo1
select 'M001','张赛','男','上海','[email protected]' union
select 'M002','王强','男','上海','[email protected]' union
select 'M003','王五','男','上海','[email protected]' union
select 'M004','李四','男','上海','[email protected]' union
select 'M005','阳阳','男','上海','[email protected]' union
select 'M006','虎子','男','上海','[email protected]' union
select 'M007','夏雪','女','北京','[email protected]' union
select 'M008','璐璐','女','深圳','[email protected]' union
select 'M009','姗姗','女','广州','[email protected]' union
select 'M010','香奈儿','女','重庆','[email protected]'
insert into Score1(CourseId,StuId,Score)
select 'F001','M001',78 union
select 'F002','M001',67 union
select 'F003','M001',89 union
select 'F004','M001',76 union
select 'F001','M002',89 union
select 'F002','M002',67 union
select 'F003','M002',84 union
select 'F004','M002',96 union
select 'F001','M003',70 union
select 'F002','M003',87 union
select 'F003','M003',92 union
select 'F004','M003',56 union
select 'F001','M004',80 union
select 'F002','M004',78 union
select 'F003','M004',97 union
select 'F004','M004',66 union
select 'F001','M006',88 union
select 'F002','M006',55 union
select 'F003','M006',86 union
select 'F004','M006',79 union
select 'F002','M007',77 union
select 'F003','M008',65 union
select 'F004','M007',48 union
select 'F004','M009',75 union
select 'F002','M009',88
--1.查询出所有学生的总数
select count(*) '学生总数' from stuinfo1
--2.查询出所有参加考试的学生学号
select distinct(stuid) from score1
--3.向学生表插入三条新记录(M011王宝宝 男 湖南 [email protected] 、M012李删删 女 四川 [email protected]、M013赛飞飞 女 浙江
[email protected])
insert into stuinfo1
select 'M011','王宝宝','男','湖南','[email protected]' union
select 'M012','李删删','女','四川','[email protected]' union
select 'M013','赛飞飞','女','浙江','[email protected]'
--4.将‘王强’的数学成绩更改为76分
update score1
set score = 76
where stuid = (select stuid
from stuinfo1
where fname = '王强')
and
courseid =(select courseid
from course1
where coursename = '数学')
update score1
set score = 90
from stuinfo1 A
inner join score1 B on A.stuid = B.stuid
inner join course1 C on B.courseid = C.courseid
where fname = '王强' and coursename = '数学'
--5.查询出每一门课程参加考试的人数---要求结果集显示课程名
select A.coursename, count(B.scoreid) '人数'
from course1 A
left join score1 B on A.courseid = B.courseid
group by A.courseid,A.coursename
--6.查询出没有参加考试的同学信息
select *
from stuinfo1 A
left join score1 B on A.stuid = B.stuid
where B.score is null or B.score = ''
--7.查询出参加考试学生的总数
select count(distinct(stuid)) '总数' from score1
select count(distinct(A. StuId) ) '参加考试学生的总数'
from StuInfo1 A
inner join Score1 B on B.StuId =A.StuId
--8.查询出所有上海的学生
select * from stuinfo1 where stuadds = '上海'
--9.查询所有北京、上海、广州的学生
select * from stuinfo1 where stuadds in ('北京','上海','广州')
--10.查询平均成绩在70分以上的学生信息
select A.stuid,A.fname, avg(B.score)
from stuinfo1 A
inner join score1 B on A.stuid = B.stuid
group by A.stuid,A.fname
having avg(B.score) > 70
--11.查询每门课程的平均分、总分
select A.coursename, avg(B.score) '平均分', sum(B.score) '总分'
from course1 A
left join score1 B on A.courseid = B.courseid
group by A.courseid, A.coursename
--12.查询每个同学的总分、平均分
select A.stuid,A.fname, avg(B.score) '平均分', sum(B.score) '总分'
from stuinfo1 A
left join score1 B on A.stuid = B.stuid
group by A.stuid,A.fname
select * from course1
select * from stuinfo1
select * from score1