主键外键练习

Posted 枫炎

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了主键外键练习相关的知识,希望对你有一定的参考价值。

use xinxi2
go

--创建院系表
create table xiyuan
(
pcode int primary key not null,--主键
pname varchar(20),
pteacher varchar(20),
ptel     varchar(20),
)
--创建可选修课程表
create table kecheng
(
kcode int primary key not null,--主键
kname varchar(20),
kteacher varchar(20),
ktel     varchar(20),
)

--创建学生列表

create table xuesheng
(
xcode int primary key identity(001,1) not null,--主键
xname varchar(20),
xsex   char(10),
xpart   int,--外键
xlesson int,--外键
)

--输入院系信息
insert into xiyuan values(101,计算机系,王法,12345678)
insert into xiyuan values(102,外语系,张能,23456789)
insert into xiyuan values(103,数学系,赵强,34567890)
insert into xiyuan values(104,历史系,钱进,12345678901)

--输入选修课数据
insert into kecheng values(201,计算机工程,王强,123123123)
insert into kecheng values(202,国防安全,刘发,321321321)
insert into kecheng values(203,文艺鉴赏,齐值,456456456)
insert into kecheng values(204,科技创新,李志,654654654)
insert into kecheng values(205,旅游管理,王明,258258258)

--输入学生数据
insert into xuesheng values(张三,,101,201)
insert into xuesheng values(李四,,101,202)
insert into xuesheng values(王五,,101,203)
insert into xuesheng values(赵六,,101,204)
insert into xuesheng values(张七,,101,205)
insert into xuesheng values(李五,,101,202)
insert into xuesheng values(王五,,101,203)
insert into xuesheng values(赵丽,,101,205)

insert into xuesheng values(赵颖,,102,204)
insert into xuesheng values(张丽丽,,102,201)
insert into xuesheng values(李莉,,102,201)
insert into xuesheng values(王器,,102,203)
insert into xuesheng values(王丽,,102,205)

insert into xuesheng values(张甜甜,,103,201)
insert into xuesheng values(任盈盈,,103,203)
insert into xuesheng values(周芷若,,103,205)

insert into xuesheng values(李莫愁,,104,201)
insert into xuesheng values(黄蓉,,104,202)
insert into xuesheng values(小龙女,,104,202)
insert into xuesheng values(林胡冲,,104,203)
insert into xuesheng values(林平之,,104,205)

go
select * from xiyuan
select * from kecheng
select * from xuesheng

--查看选修人数最多的课程的名称
select kname from kecheng where kcode=
(select top 1 xlesson from xuesheng group by xlesson order by COUNT(*)desc )



--查看男生选修 女生选修课程最多课程的详细信息
select xlesson,COUNT(*) from xuesheng where xsex= group by xlesson
select xlesson,COUNT(*) from xuesheng where xsex= group by xlesson order by COUNT(*)desc
select top 1 xlesson,COUNT(*) from xuesheng where xsex= group by xlesson order by COUNT(*)desc
select top 1 xlesson from xuesheng where xsex = group by xlesson order by COUNT(xlesson)desc
select * from kecheng where kcode=(select top 1 xlesson from xuesheng where xsex = group by xlesson order by COUNT(*)desc)


--查看计算机系人数
select xsex,COUNT(*) from xuesheng where xpart=(select pcode from xiyuan where pname=计算机系) group by xsex

--查看计算机系男生,女生人数
select COUNT(*) from xuesheng where xpart=(select pcode from xiyuan where pname=计算机系)and xsex=

--5查看哪个系院的男生最多、女生最多
select pname from xiyuan where pcode=
(select top 1 xpart from xuesheng  where xsex=group by xpart  order by COUNT(*) desc)

select pname from xiyuan where pcode=
(select top 1 xpart from xuesheng where xsex= group by xpart order by COUNT(*) desc)
--6、查看王强老师的课程有多少人选修
select COUNT(*) from xuesheng where xlesson=
(select kcode from kecheng where kteacher=王强)

--7、查看张丽丽同学的系院的电话
select ptel from xiyuan where pcode=
(select xpart from xuesheng where xname=张丽丽)

--8、查看周芷若同学的选修课程任课老师的名字及联系方式
select kteacher,ktel from kecheng where kcode=
(select xlesson from xuesheng where xname=周芷若)

 

以上是关于主键外键练习的主要内容,如果未能解决你的问题,请参考以下文章

主键外键和子查询

在MySQL中如何设置主键和外键

主键外键和索引的区别?

主键外键和索引的区别?

3.啥是键、候选键、主键和外键?

Mysql主键外键和索引的区别