首页 > 试题广场 >

按照要求用SQL语句创建下面三张表:

[问答题]

按照要求用SQL语句创建下面三张表:

. 学生表:表名——Student

属性:Sno 字符型,最大7个字符表示学生学号

Sname 字符型,最大8个字符表示学生姓名

Sage 整型表示学生年龄

Ssex 字符型,最大2个字符 表示学生性别

Sdate 日期时间型表示学生入学时间

Sdept 字符型,长度为20 表示所在系

约束:主码——Sno 非空属性——Sname

默认入学时间——‘2000-09-01


. 课程表:表名:Course

属性:Cno 字符型,最大4个字符表示课程编号

Cname 字符型,最大10个字符表示课程名

Cteacher 字符型,最大8个字符表示教师姓名

Coffice 字符型,最大20个字符表示办公室

约束:主码——Cno 非空属性——Cname


. 选课表:表名——SC

属性:Sno 字符型,最大7个字符表示学生学号

Cno 字符型,最大4个字符 表示课程号

Grade 整型表示成绩

约束:主码——SnoCno SnoCno均为外码)


针对上面的三个基本表做如下的练习:

.向基本表Student中插入学生元组(”0201901””ZHAO”1820020831NULL

.把WANG同学的选课记录全部删除

.查询在C117办公室的老师所授课程的课程号和课程名

.查询平均成绩在75分以上的学生学号和其平均成绩

.查询其他系比信息系年龄最小的学生还小的学生姓名、年龄

.查询所有缺考的学生的学号、姓名和系别

.查询选修了“C01 课程的学生总人数和最高分

.查询在2000-8-31日入学的男学生的学号和姓名

.查询与刘晨选修的一门课程相同的学生姓名

.查询与QIAN老师在同一个办公室的其它老师的姓名

.查询至少选修LIU老师所授课程中一门课程的女学生姓名

.查询哪些课程没有人选修

.查询选修了课程‘C02’且成绩高于此课程的平均成绩的学生的学号和成绩

.求平均分最高的学生姓名

. 显示20岁学生的基本信息和选课信息(课程名和分数),若没有选课,也要将基本信息显示出来

. 建立计算机系选修了课程‘c01 的学生的视图

.建立视图(SGrade),包含每个学生的学号(Sno),选课门数(Count_Cno),平均分(Avg_Grade

(21). 利用上述视图进行查询:列出平均分大于80分的学生的学号及其选课门数

(22). 查询选修了3门以上课程的学生学号

(23). 查询以DB开头且倒数第三个字母是i的课程的课程号、课程名


(1)
Create table student
(
Sno char(7) primary key,
Sname char(8) not null,
Sage int  check(sage>10 and sage<60),
Ssex char(2) check(ssex=’男’ or ssex=’女’),
Sdate datetime default ‘2000-9-1’,
Sdept char(20)
)
(2)
Create table Course(
Cno char(4) primary key,
Cname char(10) not null,
Cteacher char(8),
Coffice varchar(20)
)
(3)
Create table sc(
Sno char(7) ,
Cno char(4),
Primary key(sno,cno),
Grade int check(grade>=0 and grade<=100) ,
Foreign key(sno) references student(sno),
Foreign key(cno) references course(cno)
)
(4)
Insert into Student(”0201901”,”ZHAO”,18,”女”,2002-08-31,NULL)
(5)
Delete from sc where sno in(select sno from student where sname=’ WANG’)
(6)
Select cno,cname from course where coffice=’ C117’
(7)
Select avg(grade),sno from sc group by sno having avg(grade)>75
(8)
Select sname,sage from student where sdept!=’ 信息系’ and sage<all(select sage from student where sdept=’ 信息系’)
(9)
Select student.sno,sname,sdept from student join sc on student.sno=sc.sno where grade=0
(10)
Select count(sno),max(grade) from sno=’ C01’
(11)
Select sno,sname where sdate=’ 2000-8-31’ and ssex=’ 男’
(12)
Select sname from student join sc on student.sno=sc.sno join course on sc.cno=course.cno where sname=’ 刘晨’)
(13)
Select cteacher from course where cteacher!=’ QIAN’ and coffice=(select coffice from course where cteacher=’ QIAN’ )
(14)
Select sname from student join sc on student.sno=sc.sno where ssex=’ 女’ and Cno in (Select cno from course where cteacher=’ LIU’)
(15)
Select * from course where not exists(select * from sc where sc.cno=course.cno)
(16)
Select sno,grade from sc where cno=’c02’and grade>(select avg(grade) from sc where cno=’c02’)
(17)
Select sname from student where sno in(select sno from sc group by sno having avg(grade)>all(select avg(grade) from sc group by sno))
(18)
Select student.*,cname,grade from student left join sc on student.sno=sc.sno join course on sc.cno=course.cno where sage=20
(19)
Create view shitu
AS
Select Sno,Sname,Sage,Ssex,Sdate,Sdept from student join sc on student.sno=sc.sno where sdept=’计算机系’and cno=’c01’
(20)
Create view SGrade(Sno, Count_Cno, Avg_Grade)
AS
Select sno,count(cno),avg(grade) from sc
Group by sno
(21)
Select sno, Count_Cno from SGrade where Avg_Grade>80
(22)
Select sno from sc group by sno having count(cno)>3
(23)
Select cno,cname from course where cname like ‘DB%i_ _’
发表于 2017-05-14 22:01:27 回复(0)