关于表的主外键关系练习 师生 分数表

--创建三个表

--表一:学生表 student
--学号:code int (主键)从1开始
--姓名:name varchar(50)
--性别:sex char(10)
--班级:banji char(10)
--语文教师编号:yujiao int
--数学教师编号:yujiao int
--英语教师编号:yujiao int


--表二:教师表 teacher
--教师名字:name
--教师编号:code int (主键) 从1001开始
--负责课程:lesson char(10)(语文、数学、英语)
--年龄:age int
--生日:birth datetime


--表三:分数表 score
--学号:code int (学生学号的外键)
--语文分数:yufen decimal(18,2)
--数学分数:shufen decimal(18,2)
--英语分数:yingfen decimal(18,2)

create database chengjidan
go
use chengjidan
go

create table jiaoshibiao--教师表
(
   tcode int primary key identity(1001,1), --教师编号,设置为主键,从1001开始,每次增长1
   tname char(10),
   kecheng char(10),--负责课程
   age int, --年龄
   birth datetime, --生日
)
create table studentbiao--学生表
(
   xcode int primary key identity(1,1),--学生编号,设为主键,从一开始每次增加1,identity自增加,在添加数据时不用写
   name varchar(50),--学生名字
   sex char(10),--性别
   banji char(10),--班级
   yujiao int,--语文教师编号,教师表中教师编号的外键
   shujiao int,--数学教师编号,教师表中教师编号的外键
   yingjiao int,--英语教师编号,教师表中教师编号的外键
)
create table fenshubiao--分数表
(
   xuehao int,--学生编号,学生表中学号的外键
   yufen decimal(18,2),--语文分数
   shufen decimal(18,2),--数学分数
   yingfen decimal(18,2),--英语分数
)
--根据主外键先建立好表之间的关系再向三个表中添加数据
--教师表 insert into jiaoshibiao values('孔一','语文',35,'1981-03-15')--因为教师表中编号设置了identity自增长,所以此处不用添加编号 insert into jiaoshibiao values('孔二','数学',36,'1980-04-08') insert into jiaoshibiao values('孔三','英语',34,'1982-05-01') insert into jiaoshibiao values('孔四','语文',33,'1983-03-15') insert into jiaoshibiao values('孔五','数学',32,'1984-04-08') insert into jiaoshibiao values('孔六','英语',31,'1985-05-01') insert into jiaoshibiao values('孔七','语文',30,'1986-03-15') insert into jiaoshibiao values('孔八','数学',29,'1987-04-08') insert into jiaoshibiao values('孔九','英语',28,'1988-05-01') --学生表 insert into studentbiao values('张三','','一班',1001,1002,1003) insert into studentbiao values('李四','','一班',1001,1002,1003) insert into studentbiao values('王五','','一班',1001,1002,1003) insert into studentbiao values('赵六','','一班',1001,1002,1003) insert into studentbiao values('冯七','','一班',1001,1002,1003) insert into studentbiao values('张哇','','二班',1004,1005,1006) insert into studentbiao values('李哇','','二班',1004,1005,1006) insert into studentbiao values('王哇','','二班',1004,1005,1006) insert into studentbiao values('赵哇','','二班',1004,1005,1006) insert into studentbiao values('冯哇','','二班',1004,1005,1006) insert into studentbiao values('张蛋','','三班',1007,1008,1009) insert into studentbiao values('李蛋','','三班',1007,1008,1009) insert into studentbiao values('王蛋','','三班',1007,1008,1009) insert into studentbiao values('赵蛋','','三班',1007,1008,1009) insert into studentbiao values('冯蛋','','三班',1007,1008,1009) --分数表 insert into fenshubiao values(1,90,95,90) insert into fenshubiao values(2,92,93,91) insert into fenshubiao values(3,94,96,90) insert into fenshubiao values(4,90,99,93) insert into fenshubiao values(5,97,95,99) insert into fenshubiao values(6,80,85,70) insert into fenshubiao values(7,70,65,60) insert into fenshubiao values(8,88,93,87) insert into fenshubiao values(9,92,98,55) insert into fenshubiao values(10,97,95,92) insert into fenshubiao values(11,75,79,80) insert into fenshubiao values(12,80,75,80) insert into fenshubiao values(13,92,94,79) insert into fenshubiao values(14,94,91,70) insert into fenshubiao values(15,98,95,60) --查询三个表全部信息 select*from jiaoshibiao select*from studentbiao select*from fenshubiao --查询此次语文成绩最高的学生的信息 select *from studentbiao where xcode=--通过学号在学生表中查询这个学生信息 (select xuehao from fenshubiao where yufen=--通过下面获取到的分数在分数表中查询这个学生的学号 (select MAX(yufen)from fenshubiao))--获取分数表中语文最高分数 --方法二: select *from studentbiao where xcode=--通过下面获得的学生学号在学生表中查询这个学生信息 (select top 1 xuehao from fenshubiao order by yufen desc)--通过分数把分数表中学生进行排序、降序,获取第一个学生学号 --查询此次数学成绩最高的学生的信息 select *from studentbiao where xcode= (select xuehao from fenshubiao where shufen= (select MAX(shufen)from fenshubiao)) --查询此次英语成绩最高的学生的信息 select *from studentbiao where xcode= (select xuehao from fenshubiao where yingfen= (select MAX(yingfen)from fenshubiao)) --查询此次语文成绩最低的学生的信息 select *from studentbiao where xcode= (select xuehao from fenshubiao where yufen= (select min(yufen)from fenshubiao)) --查询此次数学成绩最低的学生的信息 select *from studentbiao where xcode= (select xuehao from fenshubiao where shufen= (select min(shufen)from fenshubiao)) --查询此次英语成绩最低的学生的信息 select *from studentbiao where xcode= (select xuehao from fenshubiao where yingfen= (select min(yingfen)from fenshubiao)) --查询此次语文成绩最低的学生所任课教师的信息 select *from jiaoshibiao where tcode= (select yujiao from studentbiao where xcode= ((select xuehao from fenshubiao where yufen= (select min(yufen)from fenshubiao)))) --查询此次数学成绩最低的学生所任课教师的信息 select *from jiaoshibiao where tcode= (select shujiao from studentbiao where xcode= ((select xuehao from fenshubiao where shufen= (select min(shufen)from fenshubiao)))) --查询此次英语成绩最低的学生所任课教师的信息, select *from jiaoshibiao where tcode= (select yingjiao from studentbiao where xcode= ((select xuehao from fenshubiao where yingfen= (select min(yingfen)from fenshubiao)))) --查询此次语文成绩最高的学生所任课教师的信息 select *from jiaoshibiao where tcode= (select yujiao from studentbiao where xcode= ((select xuehao from fenshubiao where yufen= (select max(yufen)from fenshubiao)))) --查询此次数学成绩最高的学生所任课教师的信息 select *from jiaoshibiao where tcode= (select shujiao from studentbiao where xcode= ((select xuehao from fenshubiao where shufen= (select max(shufen)from fenshubiao)))) --查询此次英语成绩最高的学生所任课教师的信息 select *from jiaoshibiao where tcode= (select yingjiao from studentbiao where xcode= ((select xuehao from fenshubiao where yingfen= (select max(shufen)from fenshubiao)))) --查询学生信息,将所有语文任课教师编号改为该科目的任课教师名字显示 select xcode,name,sex,banji, (select tname from jiaoshibiao where jiaoshibiao.tcode=studentbiao.yujiao)as 语文老师, shujiao,yingjiao from studentbiao select studentbiao.xcode,name,sex,banji,shujiao,yingjiao,jiaoshibiao.tname,kecheng,age,birth from studentbiao join jiaoshibiao on studentbiao.yujiao=jiaoshibiao.tcode --查询学生信息,将所有任课教师编号改为该科目的任课教师名字显示 select xcode,name,sex,banji, (select tname from jiaoshibiao where jiaoshibiao.tcode=studentbiao.yujiao)as 语文老师, (select tname from jiaoshibiao where jiaoshibiao.tcode=studentbiao.shujiao)as 数学老师, (select tname from jiaoshibiao where jiaoshibiao.tcode=studentbiao.yingjiao)as 英语老师 from studentbiao --查询各个学生的学号,姓名,语文分数,数学分数,英语分数,以及三门课里面每一门课的任课教师姓名 select xcode,name, (select yufen from fenshubiao where fenshubiao.xuehao=studentbiao.xcode ) as 语文成绩 , (select tname from jiaoshibiao where jiaoshibiao.tcode=studentbiao.yujiao)as 语文老师, (select shufen from fenshubiao where fenshubiao.xuehao=studentbiao.xcode ) as 数学成绩 , (select tname from jiaoshibiao where jiaoshibiao.tcode=studentbiao.shujiao)as 数学老师, (select yingfen from fenshubiao where fenshubiao.xuehao=studentbiao.xcode ) as 英语成绩 , (select tname from jiaoshibiao where jiaoshibiao.tcode=studentbiao.yingjiao)as 英语老师 from studentbiao --查询每个班级里的语文最高分 select top 1 yufen from fenshubiao where xuehao in (select xcode from studentbiao where banji='一班')order by yufen desc select MAX(yufen) from fenshubiao where xuehao in(select xcode from studentbiao where banji='二班') select MAX(yufen) from fenshubiao where xuehao in(select xcode from studentbiao where banji='三班') --查询每个班级里的数学最高分 select top 1 shufen from fenshubiao where xuehao in (select xcode from studentbiao where banji='一班')order by shufen desc select MAX(shufen) from fenshubiao where xuehao in(select xcode from studentbiao where banji='二班') select MAX(shufen) from fenshubiao where xuehao in(select xcode from studentbiao where banji='三班') --查询每个班级里的英语最高分 select top 1 yingfen from fenshubiao where xuehao in (select xcode from studentbiao where banji='一班')order by yingfen desc select MAX(yingfen) from fenshubiao where xuehao in(select xcode from studentbiao where banji='二班') select MAX(yingfen) from fenshubiao where xuehao in(select xcode from studentbiao where banji='三班') --查看每个班的语文平均分 select avg(yufen)as 一班 from fenshubiao where xuehao in(select xcode from studentbiao where banji='一班') select avg(yufen)as 二班 from fenshubiao where xuehao in(select xcode from studentbiao where banji='二班') select avg(yufen)as 三班 from fenshubiao where xuehao in(select xcode from studentbiao where banji='三班') --查询语文课程平均分最高的班级的语文教师的信息 declare @a decimal(18,2)--先定义一个变量a,用来接收数据,数据库变量前面要加@,要用declare声明一下才可以使用 select @a= avg(yufen) from fenshubiao where xuehao in--把一班的语文平均分赋值给变量@a (select xcode from studentbiao where banji='一班') declare @b decimal(18,2)--再定义一个变量b,用来接收二班的平均分 select @b= avg(yufen) from fenshubiao where xuehao in--把二班平均分接收到变量@b中 (select xcode from studentbiao where banji='二班') declare @c decimal(18,2)--再定义一个变量c,这三个变量因为要接收分数,所以用decimal类型 select @c= avg(yufen) from fenshubiao where xuehao in--接收三班平均分 (select xcode from studentbiao where banji='三班') declare @class varchar(20)--定义一个变量名@class,用来赋值班级,属于字符串类型,数据类型varchar if @a>@b and @a>@c--如果一班的平均分最高 set @class='一班' --set 设置,意思是设置把'一班'赋值到变量@class中 else if @b>@a and @b>@c --如果二班平均分最高 set @class='二班'--@class赋值为二班 else if @c>@a and @c>@b--如果三班平均分最高 set @class='三班'--@class赋值为三班 select * from jiaoshibiao where tcode in--因为下面学生表中老师的编号是教师表中教师编号的外键, --所以用下面获取到的教师编号在教师表中查询教师信息 (select yujiao from studentbiao where banji=@class)--通过变量@class接收到的班级在学生表中查询语文教师的编号 --查询数学课程平均分最高的班级的数学教师的信息 declare @a decimal(18,2) select @a= avg(shufen) from fenshubiao where xuehao in (select xcode from studentbiao where banji='一班') declare @b decimal(18,2)-- select @b= avg(shufen) from fenshubiao where xuehao in (select xcode from studentbiao where banji='二班') declare @c decimal(18,2) select @c= avg(shufen) from fenshubiao where xuehao in (select xcode from studentbiao where banji='三班') declare @class varchar(20) if @a>@b and @a>@c set @class='一班' else if @b>@a and @b>@c set @class='二班' else if @c>@a and @c>@b set @class='三班' select * from jiaoshibiao where tcode in (select shujiao from studentbiao where banji=@class) --查询英语课程平均分最高的班级的英语教师的信息 declare @a decimal(18,2)--先定义一个变量@a select @a= avg(yingfen) from fenshubiao where xuehao in (select xcode from studentbiao where banji='一班')--把一班的平均分赋值到变量@a中 declare @b decimal(18,2)--再定义一个变量@b select @b= avg(yingfen) from fenshubiao where xuehao in (select xcode from studentbiao where banji='二班')--把二班的平均分赋值到变量@b中 declare @c decimal(18,2)--再定义一个变量@c select @c= avg(yingfen) from fenshubiao where xuehao in (select xcode from studentbiao where banji='三班')--把三班的平均分赋值到变量@c中 declare @class varchar(20)--定义一个字符串类型的的变量@class用来接收班级 if @a>@b and @a>@c--如果变量@a最大,之前变量@a已被一班平均分赋值,则一班平均分最高 set @class='一班' --设置把'一班'赋值到变量@class中 else if @b>@a and @b>@c --如果@b最大,则二班平均分最高 set @class='二班'--变量@class则接收的是'二班' else if @c>@a and @c>@b--如果@c最大,则三班平均分最高 set @class='三班'--变量@class接收的是'三班' select * from jiaoshibiao where tcode in--通过下面学生表中获得的英语教师的编号在教师表中查询英语老师的全部信息 (select yingjiao from studentbiao where banji=@class)--括号里在学生表通过变量@class获得的班级查询英语教师的编号 --测试孔六老师的教课达标率,学生分数大于70分 --有三个及以上学生大于70分老师达标 declare @g int --定义一个变量@g用来接收大于70分的学生数量 select @g=COUNT(*) from fenshubiao where xuehao in--通过下面学生编号在分数表中查询这些编号所对应的分数大于70分的学生数量放在变量@g中 (select xcode from studentbiao where yingjiao in--通过下面教师表中查到的老师编号在学生表中查询他所教班级的所有学生编号 (select tcode from jiaoshibiao where tname='孔六')) and yingfen>=70--通过孔六老师的姓名在教师表里查询他的编号 if @g>=3--如果@g也就是超过70分的学生数量大于等于三个 begin print'孔六老师达标'--则孔六老师达标 end else print'孔六老师不达标'--否则,孔六老师不达标
原文地址:https://www.cnblogs.com/zyg316/p/5582444.html