主键 外键 子查询 练习 学生选修课

create database xuankebiao--创建一个选课表的数据库
go
use xuankebiao--使用这个数据库
go
create table kechengbiao--添加一个课程表的表
(
  kcode int primary key not null,--课程编号,主键
  kname varchar(20),
  klaoshi varchar(20),
  kdh varchar(20),
)

create table xiyuanbiao--添加一个院系的表
(
  xicode int primary key not null,--系院编号,主键
  xiname varchar(20),
  xilaoshi varchar(20),
  xidh varchar(20),
)
create table xueshengbiao--添加一个学生编号的表
(
  xuehao int primary key not null,
  name varchar(20),
  xingb char(10),
  xibie int,--院系表的外键
  xuanxiuk int,--课程表的外键
)

--向课程表中添加信息
insert into kechengbiao values(1,'语文','鲁达','12345')
insert into kechengbiao values(2,'数学','牛顿','23456')
insert into kechengbiao values(3,'英语','乔布斯','34567')
insert into kechengbiao values(4,'计算机','盖茨','45678')

--向系院表中添加信息
insert into xiyuanbiao values(101,'人文系','王大','232323')
insert into xiyuanbiao values(102,'数理系','李二','323232')
insert into xiyuanbiao values(103,'艺术系','张三','343434')
insert into xiyuanbiao values(104,'计算机系','赵四','434343')

--向学生表中添加信息

--人文系
insert into xueshengbiao values(001,'张小明','',101,1)--因为系别是院系表中的外键,直接用主键中每个系的编号
insert into xueshengbiao values(002,'李小鹏','',101,1)--选修课是课程表中课程的外键,直接用主键中课程的编号
insert into xueshengbiao values(003,'孙小菲','',101,1)
insert into xueshengbiao values(004,'王梦璐','',101,1)
insert into xueshengbiao values(005,'周小刚','',101,1)
insert into xueshengbiao values(006,'张全蛋','',101,1)

--数理系
insert into xueshengbiao values(007,'张明','',102,2)
insert into xueshengbiao values(008,'李冉','',102,2)
insert into xueshengbiao values(009,'孙菲','',102,2)
insert into xueshengbiao values(010,'王璐','',102,2)
insert into xueshengbiao values(011,'周刚','',102,2)
insert into xueshengbiao values(012,'张蛋','',102,2)

--艺术系
insert into xueshengbiao values(013,'张红','',103,3)
insert into xueshengbiao values(014,'李菲','',103,3)
insert into xueshengbiao values(015,'孙冉','',103,3)
insert into xueshengbiao values(016,'王梦','',103,3)
insert into xueshengbiao values(017,'周晨','',103,3)
insert into xueshengbiao values(018,'张二蛋','',103,3)

--计算机系
insert into xueshengbiao values(019,'张哇','',104,4)
insert into xueshengbiao values(020,'李哇','',104,4)
insert into xueshengbiao values(021,'孙哇','',104,4)
insert into xueshengbiao values(022,'王哇','',104,4)
insert into xueshengbiao values(023,'周哇','',104,4)
insert into xueshengbiao values(024,'张哇','',104,4)
insert into xueshengbiao values(025,'赵哇','',104,4)
insert into xueshengbiao values(026,'钱哇','',104,4)

--1.查看选修最多的课程名称
select kname from kechengbiao where kcode=
 (select top 1 xuanxiuk from xueshengbiao group by xuanxiuk order by count(*) desc)

--2.查看男生选修/女生选修最多的课程的所有信息
select * from kechengbiao where kcode=
(select top 1 xuanxiuk from xueshengbiao where xingb='' group by xuanxiuk order by count(*) desc)
select * from kechengbiao where kcode=
(select top 1 xuanxiuk from xueshengbiao where xingb='' group by xuanxiuk order by count(*) desc)
--3、查看计算机系的人数
select count(xibie) from xueshengbiao where xibie=
(select xicode from xiyuanbiao where xiname='计算机系')
--4、查看计算机系女生人数、男生人数
select count(xingb) from xueshengbiao where xingb='' and xibie=
(select xicode from xiyuanbiao where xiname='计算机系')
select count(xingb) from xueshengbiao where xingb='' and xibie=
(select xicode from xiyuanbiao where xiname='计算机系')
--5、查看哪个系院的男生最多、女生最多
select xiname from xiyuanbiao where xicode=
(select top 1 xibie from xueshengbiao where xingb='' group by xibie order by COUNT(*)desc)

select xiname from xiyuanbiao where xicode=
(select top 1 xibie from xueshengbiao where xingb='' group by xibie order by COUNT(*)desc)

--6、查看乔布斯老师的课程有多少人选修
select count(xuanxiuk) from xueshengbiao where xuanxiuk=
(select kcode from kechengbiao where klaoshi='乔布斯')
--7、查看王哇同学的系院的电话
select xidh from xiyuanbiao where xicode=
(select xibie from xueshengbiao where name='王哇')
--8、查看孙小菲同学的选修课程任课老师的名字及联系方式
select klaoshi,kdh from kechengbiao where kcode=
(select xuanxiuk from xueshengbiao where name='孙小菲')
原文地址:https://www.cnblogs.com/zyg316/p/5571496.html