数据库建表查询

本文介绍了如何在数据库中创建学生、教师、课程和成绩表,并填充相关数据。同时,提供了多个SQL查询示例,包括查询学生信息、教师单位、课程成绩等,以及涉及分组、聚合、子查询和联接操作的复杂查询,展示了数据库管理和数据分析的基础操作。
摘要由CSDN通过智能技术生成


use stumanage;

#建立学生表
create table student
(
sno varchar(20) not null primary key,
sname varchar(20) not null,
ssex varchar(20) not null,
sbirthday datetime,
class varchar(20)
)

#建立教师表
create table teacher
(
tno varchar(20) not null primary key,
tname varchar(20) not null,
tsex varchar(20) not null,
tbirthday datetime,
prof varchar(20),
depart varchar(20) not null

);
#建立课程表course
create table course
(
cno varchar(20) not null primary key,
cname varchar(20) not null,
tno varchar(20) not null,
foreign key(tno) references teacher(tno)

);

#建立成绩表
create table score
(
sno varchar(20) not null primary key,
foreign key(sno) references student(sno),
cno varchar(20) not null,
foreign key(cno) references course(cno),
degree decimal

);



#添加学生信息
insert into student values('108','曾华','男','1977-09-01','95033');
insert into student values('105','匡明','男','1975-10-02','95031');
insert into student values('107','王丽','女','1976-01-23','95033');
insert into student values('101','李军','男','1976-02-20','95033');
insert into student values('109','王芳','女','1975-02-10','95031');
insert into student values('103','陆君','男','1974-06-03','95031');

#添加教师表
insert into teacher values('804','李诚','男','1958-12-02','副教授','计算机系');
insert into teacher values('856','张旭','男','1969-03-12','讲师','电子工程系');
insert into teacher values('825','王萍','女','1972-05-05','助教','计算机系');
insert into teacher values('831','刘冰','女','1977-08-14','助教','电子工程系');


#添加课程表
insert into course values('3-105','计算机导论','825');
insert into course values('3-245','操作系统','804');
insert into course values('6-166','数字电路','856');
insert into course values('9-888','高等数学','831');

#添加成绩表
INSERT INTO `score` VALUES ('101', '3-105', '76');
INSERT INTO `score` VALUES ('101', '6-166', '68');
INSERT INTO `score` VALUES ('103', '3-105', '92');
INSERT INTO `score` VALUES ('103', '3-245', '86');
INSERT INTO `score` VALUES ('103', '6-166', '85');
INSERT INTO `score` VALUES ('105', '3-105', '91');
INSERT INTO `score` VALUES ('105', '3-245', '75');
INSERT INTO `score` VALUES ('105', '6-166', '79');
INSERT INTO `score` VALUES ('107', '3-105', '66');
INSERT INTO `score` VALUES ('107', '3-245', '78');
INSERT INTO `score` VALUES ('107', '6-166', '77');
INSERT INTO `score` VALUES ('109', '3-105', '76');
INSERT INTO `score` VALUES ('109', '3-245', '68');
INSERT INTO `score` VALUES ('109', '6-166', '81');

#习题

1、查询Student表中的所有记录的Sname、Ssex和Class列。
select Sname,Ssex,Class from student

2、查询教师所有的单位即不重复的Depart列。
select DISTINCT depart from teacher

3、查询Student表的所有记录。
select * from student

4、查询Score表中成绩在60到80之间的所有记录。
select * from score where degree BETWEEN 60 and 80

5、查询Score表中成绩为85,86或88的记录。
select * from score where degree='85'or degree='75' or degree='80'

select * from score where degree in('85','75','80')

6、查询Student表中“95031”班或性别为“女”的同学记录。
select * from student where class='95031' and ssex='女'

7、以Class降序查询Student表的所有记录。
select * from student ORDER BY class desc

8、以Cno升序、Degree降序查询Score表的所有记录。
select * from score ORDER BY cno asc,degree desc

9、查询“95031”班的学生人数。
select COUNT(*) as '人数' from student where class='95031'

*10、查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
select sno,cno,degree from score where degree =(select MAX(degree) from score)

select * from score ORDER BY degree desc limit 0,1

select s.sname,c.cname,sc.degree from student s LEFT JOIN score sc
on s.sno=sc.sno
LEFT JOIN course c
on sc.cno=c.cno
ORDER BY sc.degree desc LIMIT 0,1

select s.sname,c.cname,sc.degree from student s,score sc,course c
from s.sno=sc.sno and on sc.cno=c.cno
 sc.degree desc LIMIT 0,1
11、查询每门课的平均成绩。
select cno,AVG(degree) as '平均分' from score GROUP BY cno

select c.cname,c.cno,AVG(degree) as '平均分'
from course c LEFT JOIN score sc on c.cno=sc.cno
GROUP BY c.cno,c.cname

*12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
-- select cno,AVG(degree) as '平均分' from score
-- where cno in(select cno from course where cno like '3%')

select cno,AVG(degree) as '平均分' from score
GROUP BY cno
HAVING cno like '3%' and COUNT(*)>=5

13、查询分数大于70,小于90的Sno列。
select sno,degree from score where degree>70 and degree<90

14、查询所有学生的Sname、Cno和Degree列。
select s.sname,c.cno,sc.degree
from student s,course c, score sc
where s.sno=sc.sno
and sc.cno=c.cno
ORDER BY s.sname


15、查询所有学生的Sno、Cname和Degree列。
select s.sno,c.cname,sc.degree
from student s,course c, score sc
where s.sno=sc.sno
and sc.cno=c.cno
ORDER BY s.sno

16、查询所有学生的Sname、Cname和Degree列。
select s.sname,c.cname,sc.degree
from student s,course c,score sc
where s.sno=sc.sno
and sc.cno=c.cno
ORDER BY s.sname

17、查询“95033”班学生的平均分。
select AVG(degree) as '平均分' from score where sno
in(select sno from student where class in
(select class from student where class='95033'))

select AVG(degree) as '平均分' from score sc
where sno in(select sno from student where class='95033')

*18、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
select * from student where sno in
(select sno from score where cno='3-105'
and degree>(select MAX(degree) from score where sno='109' and cno='3-105'))

select * from student s LEFT JOIN score sc
on s.sno=sc.sno
where sc.cno='3-105'
and sc.degree>(select MAX(degree) from score where sno='109' and cno='3-105')


**19、选了多门课程并且是这个课程下不是最高分的
select * from score a where a.sno
in(select sno from score  GROUP BY sno HAVING COUNT(*)>1)
and a.degree<(select max(degree) from score b where a.cno=b.cno)

*20、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
select s.sname,sc.degree from student s LEFT JOIN score sc
on s.sno=sc.sno
where sc.degree<(select degree from score where sno='109' and cno='3-105')

*21、查询和学号为108、101的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
select sno,sname,sbirthday from student
where YEAR(sbirthday) in(select YEAR(sbirthday) from student where sno in('108','101'))

22、查询“张旭“教师任课的学生成绩。
select * from score where cno
in(select cno from course where tno=(select tno from teacher where tname='张旭'))

select sc.sno,sc.degree from score sc LEFT JOIN course c
on sc.cno = c.cno LEFT JOIN teacher t
on c.tno=t.tno
where t.tname='张旭'

select s.sname,b.myscore from student s RIGHT JOIN(
select sc.sno as 'snum',sc.degree as 'myscore' from score sc LEFT JOIN course c
on sc.cno = c.cno LEFT JOIN teacher t
on c.tno=t.tno
where t.tname='张旭'
) b on s.sno=b.snum
**23、查询选修某课程的同学人数多于5人的教师姓名。
select t.tname,c.cname from teacher t LEFT JOIN course c
on t.tno=c.cno
where c.cno in(
select cno from score GROUP BY cno HAVING COUNT(*)>=5)

24、查询95033班和95031班全体学生的记录。
select * from student where class in('95033','95031')

25、查询存在有85分以上成绩的课程Cno.
select cno from course where cno in(select cno from score where degree>85 )

*26、查询出“计算机系“教师所教课程的成绩表。
select sc.sno,c.cname,sc.degree,t.depart
from  score sc LEFT JOIN course c on sc.cno=c.cno
LEFT JOIN teacher t on c.tno=t.tno
where t.depart='计算机系'

select sc.sno,c.cname,sc.degree,t.depart
from  score sc LEFT JOIN course c on sc.cno=c.cno
LEFT JOIN teacher t on c.tno=t.tno
where t.tno in (select tno from teacher where t.depart='计算机系')

27、查询“计算 机系”与“电子工程系“不同职称的教师的Tname和Prof。
select tname,prof from teacher where depart='计算机系'
and prof not in(select prof from teacher where depart='电子工程系')
UNION
select tname,prof from teacher where depart='电子工程系'
and prof not in(select prof from teacher where depart='计算机系')
*28、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。any:代表括号中任意一个成绩就可以
select cno,sno,degree from score
where cno='3-105' and degree>(select MIN(degree) from score where cno='3-245') ORDER BY degree desc

select cno,sno,degree from score
where cno='3-105' and degree>any(select degree from score where cno='3-245') ORDER BY degree desc

*29、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. all:代表括号中的所有成绩
select cno,sno,degree from score
where cno='3-105' and degree>(select MAX(degree) from score where cno='3-245')

select cno,sno,degree from score
where cno='3-105' and degree>all(select degree from score where cno='3-245')

30、查询所有教师和同学的name、sex和birthday.
select s.sname as '姓名',s.ssex as '性别' ,s.sbirthday as '生日' from student s
union ALL
select t.tname,t.tsex,t.tbirthday from teacher t

31、查询所有“女”教师和“女”同学的name、sex和birthday.
select s.sname as '姓名',s.ssex as '性别',s.sbirthday as '生日' from student s where s.ssex='女'
union ALL
select t.tname,t.tsex,t.tbirthday from teacher t where t.tsex='女'

**32、查询成绩比该课程平均成绩低的同学的成绩表。
select * from score sc
where sc.degree<(select AVG(degree)
from score scc where sc.cno=scc.cno)

select * from score a
where degree <(select avg(degree) from score b where a.cno=b.cno)

33、查询所有任课教师的Tname和Depart.
select t.tname,c.cname from teacher t,course c
where t.tno=c.tno and c.cno in(
select DISTINCT(cno) from score)

34、查询所有未讲课的教师的Tname和Depart.
select t.tname,c.cname from teacher t,course c
where t.tno=c.tno and c.cno not in(
select DISTINCT(cno) from score)

*35、查询至少有2名男生的班号。
select class from student
where class in
(select class from student where ssex='男' GROUP BY class HAVING COUNT(*)>=2)

36、查询Student表中不姓“王”的同学记录。
select * from student where sname not like '王%'

37、查询Student表中每个学生的姓名和年龄。
select sname,(YEAR(NOW())-year(sbirthday)) as '年龄' from student

select sname,(YEAR(CURRENT_TIMESTAMP()))-year(sbirthday)) as '年龄' from student

38、查询Student表中最大和最小的Sbirthday日期值。
select MAX(sbirthday) as '年龄最大',MIN(sbirthday) as '年龄最小'  from student

39、以班号和年龄从大到小的顺序查询Student表中的全部记录。
select * from student ORDER BY class desc, sbirthday DESC

40、查询“男”教师及其所上的课程。
select c.cno,c.cname ,t.tname from teacher t RIGHT JOIN course c on c.tno=t.tno
where tsex='男'

select t.tname,c.cname from teacher t LEFT JOIN course c on t.tno=c.tno
where tsex='男'

41、查询最高分同学的Sno、Cno和Degree列。
select sno,cno,degree from score
where degree in(select MAX(degree) from score)

42、查询和“李军”同性别的所有同学的Sname.
select * from student
where ssex=(select ssex from student where sname='李军')

43、查询和“李军”同性别并同班的同学Sname.
select sname from student
where ssex = (select ssex from student where sname='李军')
and class in(select class from student where sname='李军' )

44、查询所有选修“计算机导论”课程的“男”同学的成绩表。

select * from score where  cno
=(select cno from course where cname='计算机导论' )
and sno in( select sno from student where ssex='男')
 
select * from course c LEFT JOIN score sc
on c.cno=sc.cno
where c.cname='计算机导论'
and sc.sno in(select sno from student where ssex='男')

45.视图的创建
语法:create view 视图名 as select 语句

create view myView as
select s.sname,c.cname,sc.degree
from student s LEFT JOIN score sc
on s.sno=sc.sno LEFT JOIN course c
on c.cno=sc.cno

select * from myView


46.删除视图
drop view myView

47.修改视图
alter view 视图名 as 新的select语句

alter view myView as
select s.sno, s.sname,c.cname,sc.degree
from student s LEFT JOIN score sc
on s.sno=sc.sno LEFT JOIN course c
on c.cno=sc.cno
48.查询视图中的数据
语法:select    col1,col2….. From 视图名  where  condition

select sname,cname,degree
from myView
where sno='101'

49.视图的特点
1:在完成视图的创立之后,就可以像使用基本表一样来使用视图
2:在创建视图时,并非所有的select子查询都可用,如:compute和compute by,order by[除非与top一起连用] 
3:但在查询时,依然都可以用在创建时禁用的select子查询
4:在视图创建时,必须为没有标题列指定标题

50.什么是视图
视图(view):从一个或几个基本表中根据用户需要而做成一个虚表
  1:视图是虚表,它在存储时只存储视图的定义,而没有存储对应的数据
  2:视图只在刚刚打开的一瞬间,通过定义从基表中搜集数据,并展现给用户

51.视图与查询的区别
1:存储上的区别:视图存储为数据库设计的一部分,而查询则不是.
2:更新限制的要求不一样
3:排序结果:通过sql语句,可以对一个表进行排序,而视图则不行

52.视图的优点
1:能分割数据,简化观点。可以通过select和where来定义视图,从而可以分割数据基表中某些对于用户不关心的数据,使用户把注意力集中到所关心的数据列.进一步简化浏览数据工作
2:为数据提供一定的逻辑独立性。 如果为某一个基表定义一个视图,即使以后基本表的内容的发生改变了也不会影响“视图定义”所得到的数据
3:提供自动的安全保护功能。 视图能像基本表一样授予或撤消访问许可权
4:视图可以间接对表进行更新,因此视图的更新就是表的更新



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值