加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

数据库查询语句

(2015-06-25 15:17:49)
标签:

教育

it

         

             

 

--1、检索student2010表中学制(XZ)为年的学生信息

--select * from student2010 where xz = '2'

 

--2、检索course表中学分(credit)小于的所有课程

--select * from course where credit < '3'

 

--3、检索course表中学分(credit)在.5和之间的所有课程

--select * from course where credit between '2.5' and '4'

 

--4、检索course表中学分(credit)小于2.5或大于4的所有课程

--select * from course where credit not between '2.5' and '4'

 

--5、检索student2010表中班级名称(BJMC)为“计算机网络技术班”的学生信息

--select * from student2010 where BJMC = '2010计算机网络技术班'

 

--6、检索stucou表中选了课程编号从005到012的所有记录

--select * from stucou where couno between '005' and '012'

 

--7、检索stucou表中没有选修课程编号从005到012的所有记录

--select * from stucou where couno not between '005' and '012'

 

--8、检索student2010表中和你同一个市的学生信息

--select * from student2010 where jtdz like '%韶关市'

 

--9、检索student2010表中专业名称(ZYMC)为“计算机网络技术”的学生信息,只显示学号、姓名、班级三列

--select XH,XM,BJMC from student2010  where ZYMC = '计算机网络技术'

 

--10、检索student2010表中专业名称(ZYMC)为“计算机网络技术”的学生的学号、姓名字段,字段名用中文显示

--select XH AS 学号,XM AS 姓名from student2010  where ZYMC = '计算机网络技术'

 

--11、检索stucou表中选修了、'004','009','010','015'、及课程的记录

--select * from stucou where couno in ('004','009','010','015')

 

--12、显示student2010表中的所有系部名称(不重复显示)

--select distinct xymc from student2010

 

--13、显示stucou表中所有willorder为1且couno为003的记录

--select * from stucou where willorder='1' and couno='003'

 

--14、检索student表中你的记录行

--select * from student where stuname = '钟红连'

 

--15、检索student表中和你同姓的记录行

--select * from student where stuname like '钟%'

 

--16、显示stucou表中所有willorder为2到4的记录

--select * from stucou where willorder between '2' and '4'

 

--17、显示student表中所有姓张、李、刘的记录

--select * from student where stuname like '[张,李,刘]%'

 

--18、显示student表中所有姓张、李、刘记录的学号、姓名两个字段

--select stuno,stuname from student where stuname like '[张,李,刘]%'

 

 

               【  二  】表连接

 

--1、显示每个学生所属的班级名称及姓名

--select classname,stuname from class inner join student on class.classno=student.classno

 

--2、显示所有与你同姓的学生所属的班级名称及姓名

--select classname,stuname from class inner join student on class.classno=student.classno where stuname like '钟%'

 

--3、显示网络班所有与你同姓的学生所属的班级名称及姓名

--select classname,stuname from class inner join student on class.classno=student.classno where stuname like '钟%' and classname like '%网络%'

 

--4、显示每个班级所属的院系编号、院糸名称及班级名称

--select department.departno,departname,classname from department inner join class on class.departno=department.departno

 

--5、显示每个学生所属的院系名称、班级名称、学号及姓名

--select departname,classname,stuno,stuname from class inner join department on department.departno=class.departno inner join student on class.classno=student.classno

 

--6、显示信息科技系每个学生所属的院系名称、班级名称、学号及姓名

--select departname,classname,stuno,stuname from class inner join department on department.departno=class.departno inner join student on class.classno=student.classno where departname like '信息科技系'

 

--7、显示信息科技系每个与你同姓学生所属的院系名称、班级名称、学号及姓名

--select departname,classname,stuno,stuname from class inner join department on department.departno=class.departno inner join student on class.classno=student.classno where stuname like '钟%'

 

--8、显示每个学生所属的院系编号、院系名称、班级编号、班级名称、学号及姓名六个字段

--select department.departno,departname,class.classno,classname,stuno,stuname from class inner join department on department.departno=class.departno inner join student on class.classno=student.classno

 

--9、显示院系编号为的院系的学生的院系名称、班级编号、班级名称、学号及姓名五个字段

--select departname,class.classno,classname,stuno,stuname from class inner join department on department.departno=class.departno inner join student on class.classno=student.classno where department.departno = '01'

 

--10、显示每个学生的班级名称、学号、姓名、选修的课程号

--select classname,student.stuno,stuname,couno from student inner join stucou on student.stuno=stucou.stuno inner join class on class.classno=student.classno

 

--11、显示所有选了课程的学生的班级名称、学号、姓名、选修的课程号

--select classname,student.stuno,stuname,couno from student inner join stucou on student.stuno=stucou.stuno inner join class on class.classno=student.classno where couno like '002'

 

--12、显示所有选了002、005、008三门课程的学生的班级名称、学号、姓名、选修的课程号

--select classname,student.stuno,stuname,couno from student inner join stucou on student.stuno=stucou.stuno inner join class on class.classno=student.classno where couno in ('002','005','008')

 

--13、显示所有选了、、三门课程的学生的班级名称、学号、姓名、选修的课程号,

------查询结果先按课程号排序,课程号相同的再按学生姓名排序

--select classname,stucou.stuno,stuname,couno from student inner join stucou on student.stuno=stucou.stuno inner join class on class.classno=student.classno where couno in ('002','005','008') order by couno,stuname

 

--14、显示“00电子商务”班选修了课程002的学生的班级名称、学号、姓名、选修的课程编号

--select classname,student.stuno,stuname,couno from student inner join stucou on student.stuno=stucou.stuno inner join class on class.classno=student.classno where couno like '002' and classname like '00电子商务'

 

                    【   三 

 

--1、查询stucou表中选了,004,008,010课程,志愿号为或或的记录

--select * from stucou where couno in('001','004','008','010') and willorder in('1','2','3')

--2、查询student2010表中sfzh字段值以1、3、5、7、9结尾的记录

--select * from student2010 where sfzh like '%[1,3,5,7,9]'

 

-----------聚合函数使用------------------------

--1、查询stucou表中第一志愿选了,004,008,010课程的人数

--select count(*) from stucou where couno in('001','004','008','010') and willorder=1

 

--2、统计student2010表中籍贯与你相同(同一县、区或市)且性别相同的学生人数,列名为“人数”

--select * from student2010

--select count(*) from student2010 where jtdz like '%陆丰%' and xb='女''

--3、统计student2010表中与你同姓的学生人数,列名为“同姓人数”

 

--select count(*) 同姓人数 from student2010 where xm like '庄%'

 

---------分组统计(group by子句使用)--------------------

--1、统计student2010表中男、女生人数

--select xb,count(*) from student2010 group by xb

 

--2、统计stucou表中各门课程的选修人数

--select * from stucou

--select couno, count(*) from stucou  group by couno

 

--3、统计stucou 表中每个学生选修的课程数量

--select stuno, count(*) from stucou group by stuno

 

--4、统计student2010表中每个院系的学生人数

--select * from student2010

--select xymc,zymc, count(*) from student2010 group by xymc,zymc

 

--5、统计student2010表中每个班的学生人数,显示yxmc,bj及对应的人数,并按人数由多到少排序

--select xymc,bjmc, count(*) rs from student2010 group by xymc,bjmc order by rs desc

 

--6、统计student2010表中各民族学生人数,并按人数由少到多排序

--select mz,count(*) rs from student2010 group by mz order by rs

 

--7、在student2010表分专业统计男、女生人数,按专业名称排序

--select zymc,xb,count(*) from student2010 group by zymc,xb order by zymc

--8、统计student2010表中每个班的学生人数,显示院系名称、班级名称及班级人数三列,并把检索结果保存到一名

-----为class2010的新表中

--select xymc,bjmc,count(*) rs into class2010 from student2010 group by xymc,bjmc order by rs

 

-------------------对分组统计的结果进一步筛选(having子句使用)------------------------------

--1、统计student2010表中各院系学生人数,只显示人数多于400的记录

--select xymc,count(*) from student2010 group by xymc having count(*)>400

 

--2、统计student2010表中各院系学生人数,只显示人数多于400的记录,按人数由多到少显示

--select xymc,count(*) rs from student2010 group by xymc having count(*)>400 order by rs desc

 

--3、统计stucou表中各门课程的选修人数,只显示人数少于30的记录(显示couno及对应的人数)

--select couno,count(*) from stucou group by couno having count(*)<30

 

--4、统计stucou表中各门课程的选修人数,只显示人数少于的记录,按人数由少到多显示

--select couno,count(*) from stucou group by couno having count(*)<30 order by count(*)

 

--5、查询student2010表中人数多于人的班级的xymc、zymc、bjmc及rs(人数)四个字段

--select xymc,zymc,bjmc,count(*) rs from student2010 group by xymc,zymc,bjmc having count(*)>60

 

--6、查询student2010表中人数多于人的班级的xymc、zymc、bjmc及rs(人数)四个字段,

-----并把检索结果保存到一名为class60的新表中

--select xymc,zymc,bjmc,count(*) rs into class60 from student2010 group by xymc,zymc,bjmc having count(*)>60

 

                

                    【  四 

 

--1、统计级学生人数

--select count(*) from student2010

 

--2、统计级学生中护理学院人数

--select count(*) from student2010 where xymc='护理学院'

 

--3、统计护理学院各专业人数,显示专业名称及人数

--select zymc,xymc,count(*) from student2010 group by zymc,xymc having xymc='护理学院'

 

--4、统计护理学院各专业人数,显示院系名称、专业名称及人数三列

--select zymc 专业名称,xymc 院系名称,count(*) 人数from student2010 group by zymc,xymc having xymc='护理学院'

 

--5、统计护理学院各专业人数,显示院系名称、专业名称及人数三列,只显示人数多于人的结果

--select zymc 专业名称,xymc 院系名称,count(*) 人数from student2010 group by zymc,xymc having xymc='护理学院' and count(*)>100

 

--6、统计护理学院各专业人数,显示院系名称、专业名称及人数三列,只显示人数多于人的结果,按人数由多到少显示

--select zymc 专业名称,xymc 院系名称,count(*) 人数from student2010 group by zymc,xymc having xymc='护理学院' and count(*)>100 order by count(*) desc

 

--------------------------------------------------------------------------

------(以下操作涉及department、class、student、course及stucou等表)--------

--------------------------------------------------------------------------

--1、显示每个班级所属的院系名称(departname)及班级名称(classname)

--select departname,classname from department inner join class on department.departno=class.departno

 

--2、显示信息科技系每个班级所属的院系名称(departname)及班级名称(classname)

--select departname,classname from department inner join class on department.departno=class.departno where departname='信息科技系'

 

--3、显示每个学生所属的班级名称(classname)及姓名(stuname)

--select class.classname,stuname from class inner join student on class.classno=student.classno

 

--4、显示每个学生所属的班级名称(classname)、学号(stuno)及姓名(stuname)

--select class.classname,stuno,stuname from class inner join student on class.classno=student.classno

 

--5、显示每个学生所属的classno、classname及stuno、stuname四字段,显示结果按姓名排序

--select class.classno,class.classname,stuno,stuname from class inner join student on class.classno=student.classno order by stuname

 

--6、显示网络专业各个学生所属的classname、stuno及stuname三字段

--select class.classname,stuno,stuname from class inner join student on class.classno=student.classno where classname like'%网络%'

 

--7、显示网络专业各个学生所属的classname、stuno及stuname三字段,字段名使用中文别名(班级、学号及姓名)

--select class.classname 班级,stuno 学号,stuname 姓名from class inner join student on class.classno=student.classno where classname like'%网络%'

 

--8、显示网络专业各个学生所属的classname、stuno及stuname三字段,字段名使用中文别名(班级、学号及姓名),显示结果按姓名排序

--select class.classname 班级,stuno 学号,stuname 姓名from class

 

 

                 【  五 

 

--1、显示每个学生所属的院系名称、班级编号、班级名称、学号、姓名、所选课程编号,按课程号排序

--select departname,class.classno,classname,student.stuno,stuname,couno from department inner join class on department.departno=class.departno inner join student on class.classno=student.classno inner join stucou on student.stuno=stucou.stuno order by couno

 

2、显示每个学生所属的院系名称、班级编号、班级名称、学号、姓名、所选课程名称,按课程名称排序

--select departname,class.classno,classname,student.stuno,stuname,couname from department inner join class on department.departno=class.departno inner join student on class.classno=student.classno inner join stucou on student.stuno=stucou.stuno inner join course on stucou.couno=course.couno order by couname

 

3、显示每个学生所属的院系名称、班级编号、班级名称、学号、姓名、所选课程名称、任课老师,按课程名称排序

--select departname,class.classno,classname,student.stuno,stuname,couname,teacher from department inner join class on department.departno=class.departno inner join student on class.classno=student.classno inner join stucou on student.stuno=stucou.stuno inner join course on stucou.couno=course.couno order by couname

 

4、显示院系编号为的每个学生所属的院系名称、班级编号、班级名称、学号、姓名、所选课程名称、任课老师,按课程名称排序

--select departname,class.classno,classname,student.stuno,stuname,couname,teacher fromdepartment inner join class on department.departno=class.departno inner join student on class.classno=student.classno inner join stucou on student.stuno=stucou.stuno inner join course on stucou.couno=course.couno where department.departno='01' order by couname

 

5、显示选修了课程的学生所属的院系名称、班级编号、班级名称、学号、姓名、所选课程编号,按志愿号排序

--select departname,class.classno,classname,student.stuno,stuname,couno from department inner join class on department.departno=class.departno inner join student on class.classno=student.classno inner join stucou on student.stuno=stucou.stuno where couno='008' order by willorder

 

6、显示选修了课程的学生所属的院系名称、班级编号、班级名称、学号、姓名、所选课程名称,按志愿号排序

--select departname,class.classno,classname,student.stuno,stuname,couname from department inner join class on department.departno=class.departno inner join student on class.classno=student.classno inner join stucou on student.stuno=stucou.stuno inner join course on stucou.couno=course.couno where stucou.couno='008' order by willorder

 

7、显示每个院系的编号、名称及对应的班级名称(分别使用左外连接、右外连接完成)

--select department.departno,departname,classname  from department left join class on department.departno=class.departno

--select department.departno,departname,classname  from department right join classon department.departno=class.departno

 

8、使用全外连接显示每个院系的编号、名称及对应的班级名称

--select department.departno,departname,classname from department full join classon department.departno=class.departno

 

9、将class表及student表进行交叉连接,写出命令并执行(看有没有实际的使用意义)

--select class.*,student.* from class cross join student

 

 

            【  六  

 

--1、显示每个学生所属的班级名称及姓名

--select classname,stuname from class inner join student on class.classno=student.classno

 

--2、显示所有与你同姓的学生所属的班级名称及姓名

--select classname,stuname from class inner join student on class.classno=student.classno where stuname like '钟%'

 

--3、显示网络班所有与你同姓的学生所属的班级名称及姓名

--select classname,stuname from class inner join student on class.classno=student.classno where stuname like '钟%' and classname like '%网络%'

 

--4、显示每个班级所属的院系编号、院糸名称及班级名称

--select department.departno,departname,classname from department inner join class on class.departno=department.departno

 

--5、显示每个学生所属的院系名称、班级名称、学号及姓名

--select departname,classname,stuno,stuname from class inner join department on department.departno=class.departno inner join student on class.classno=student.classno

 

--6、显示信息科技系每个学生所属的院系名称、班级名称、学号及姓名

--select departname,classname,stuno,stuname from class inner join department on department.departno=class.departno inner join student on class.classno=student.classno where departname like '信息科技系'

 

--7、显示信息科技系每个与你同姓学生所属的院系名称、班级名称、学号及姓名

--select departname,classname,stuno,stuname from class inner join department on department.departno=class.departno inner join student on class.classno=student.classno where stuname like '钟%'

 

--8、显示每个学生所属的院系编号、院系名称、班级编号、班级名称、学号及姓名六个字段

--select department.departno,departname,class.classno,classname,stuno,stuname from class inner join department on department.departno=class.departno inner join student on class.classno=student.classno

 

--9、显示院系编号为的院系的学生的院系名称、班级编号、班级名称、学号及姓名五个字段

--select departname,class.classno,classname,stuno,stuname from class inner join department on department.departno=class.departno inner join student on class.classno=student.classno where department.departno = '01'

 

--10、显示每个学生的班级名称、学号、姓名、选修的课程号

--select classname,student.stuno,stuname,couno from student inner join stucou on student.stuno=stucou.stuno inner join class on class.classno=student.classno

 

--11、显示所有选了课程的学生的班级名称、学号、姓名、选修的课程号

--select classname,student.stuno,stuname,couno from student inner join stucou on student.stuno=stucou.stuno inner join class on class.classno=student.classno where couno like '002'

 

--12、显示所有选了、、三门课程的学生的班级名称、学号、姓名、选修的课程号

--select classname,student.stuno,stuname,couno from student inner join stucou on student.stuno=stucou.stuno inner join class on class.classno=student.classno where couno in ('002','005','008')

 

--13、显示所有选了、、三门课程的学生的班级名称、学号、姓名、选修的课程号,

------查询结果先按课程号排序,课程号相同的再按学生姓名排序

--select classname,stucou.stuno,stuname,couno from student inner join stucou on student.stuno=stucou.stuno inner join class on class.classno=student.classno where couno in ('002','005','008') order by couno,stuname

 

--14、显示“电子商务”班选修了课程的学生的班级名称、学号、姓名、选修的课程编号

--select classname,student.stuno,stuname,couno from student inner join stucou on student.stuno=stucou.stuno inner join class on class.classno=student.classno where couno like '002' and classname like '00电子商务'

 

 

             【  七  

 

 

--1、在student2010表中查询和“王珊珊”在同一班级的所有同学的信息。

--select * from student2010 where bjmc in(select bjmc from student2010 where xm='王珊珊')

 

--2、在student2010表中查询和“田丽丽”在同一班级的所有男同学的信息。

--select * from student2010 where bjmc in(select bjmc from student2010 where xm='田丽丽') and xb='男'

 

--3、在student2010表中查询和“沈雪梅”在同一院系的所有女同学的信息。

--select * from student2010 where xymc in(select xymc from student2010 where xm='沈雪梅') and xb='女'

 

--4、在student2010表中查询和“巫莎莎”在同一天生日有学生的信息。

--select * from student2010 where substring (sfzh,4,3) like (select substring (sfzh,4,3) from student2010 where xm='巫莎莎')

 

--5、在student2010表中查询和“袁冬琳”在同一院系的所有陈姓女学生的信息。

--select * from student2010 where xymc in(select xymc from student2010 where xm='袁冬琳') and xm like '陈%' and xb='女' 

 

--6、查询course表中最多人选修的课程信息(willnum最大)

--select * from course willnum

 

--7、查询course表中最少人选修的课程信息(willnum最小)

--select * from course willnum

 

--8、查询course表中选修人数(willnum)大于平均选修数的课程信息

--select * from course willnum

 

---9、在student2010表中查询和“黄丽君”在同一院系的所有学生信息

--select * from student2010 where xymc in(select xymc from student2010 where xm='黄丽君')

 

---10、在student2010表中查询和“黄丽君”同班的所有学生信息

--select * from student2010 where bjmc in(select bjmc from student2010 where xm='黄丽君')

 

--11、查询所有有选修课的学生信息

--select * from student where stuno in (select stuno from stucou)

 

--12、查询没有选修课程的学生信息

--select * from student where stuno not in (select stuno from stucou)

 

--13、查询没有人选修的课程信息

--select * from stucou where stuno not in (select stuno from student)

 

--14、查找选修了课程号为的课程的学生信息

select * from student where stuno in (select stuno from stucou) and stuno='002'

 

 

          【   八  

 

 

--1、使用SSMS创建视图,要求数据表的来源为:class,student两个表

-----显示学生每个学生所属的班级名称、学号及姓名、性别,视图保存为v_stud(要求视图的定义加密)

----(此小题不用输入命令)

USE [Xk]

GO

 

--SET ANSI_NULLS ON

--GO

--SET QUOTED_IDENTIFIER ON

--GO

--ALTER VIEW [dbo].[v_stud]

--with encryption

--AS

--SELECT     dbo.Class.ClassName, dbo.Student.StuNo, dbo.Student.StuName, dbo.Student.Gender, dbo.StuCou.CouNo

--FROM         dbo.Class INNER JOIN

                      --dbo.Student ON dbo.Class.ClassNo = dbo.Student.ClassNo INNER JOIN

                      --dbo.StuCou ON dbo.Student.StuNo = dbo.StuCou.StuNo

 

--2、用命令查看视图V_stud的所有数据

--select * from V_stud

 

--3、用命令查看所有学生的班级名称、学号及姓名及所选课程号(使用视图V_stud与表stucou连接),结果按姓名排序

--SELECT     dbo.Class.ClassName, dbo.Student.StuNo, dbo.Student.StuName, dbo.Student.Gender, dbo.StuCou.CouNo

---FROM         dbo.Class INNER JOIN

                    --dbo.Student ON dbo.Class.ClassNo = dbo.Student.ClassNo INNER JOIN

                    --dbo.StuCou ON dbo.Student.StuNo = dbo.StuCou.StuNo order by stuname

 

--第二题

--1、使用SSMS创建视图,数据表的来源为:department,class两个表

-----显示每个班级的院系编号、院落系名称、班级编号及名称,视图保存为v_depcla

--------(此小题不用输入命令)

 

--2、用命令查看视图V_depcla的所有数据

--select * from v_depcla

 

--3、使用insert命令对v_depcla视图进行数据插入操作,插入一院系信息:编号:院系编号、名称:生物制药系

-----完成后查看department表看是否有记录插入

--insert into v_depcla(departno,departname) values ('07','生物制药系')

--select * from department

 

-------------------------------写出数据查询代码---------------------------------

------(以下操作涉及department、class、student、course及stucou等表)--------

--1、显示每个学生所属的院系名称、班级编号、班级名称、学号、姓名、所选课程编号,按课程号排序

--SELECT     dbo.Department.DepartName, dbo.Student.ClassNo, dbo.Student.StuName, dbo.Student.StuNo, dbo.StuCou.CouNo, dbo.Class.ClassName

--FROM         dbo.Class INNER JOIN

                      --dbo.Department ON dbo.Class.DepartNo = dbo.Department.DepartNo INNER JOIN

                      --dbo.Student ON dbo.Class.ClassNo = dbo.Student.ClassNo INNER JOIN

                      --dbo.StuCou ON dbo.Student.StuNo = dbo.StuCou.StuNo order by couno

 

--2、显示每个学生所属的院系名称、班级编号、班级名称、学号、姓名、所选课程名称,按课程名称排序

--SELECT     dbo.Department.DepartName, dbo.Student.ClassNo, dbo.Student.StuName, dbo.Student.StuNo, dbo.Class.ClassName, dbo.Course.CouName

--FROM         dbo.Class INNER JOIN

                      --dbo.Department ON dbo.Class.DepartNo = dbo.Department.DepartNo INNER JOIN

                      --dbo.Student ON dbo.Class.ClassNo = dbo.Student.ClassNo INNER JOIN

                      --dbo.Course ON dbo.Department.DepartNo = dbo.Course.DepartNo order by couname

 

 

--3、显示每个学生所属的院系名称、班级编号、班级名称、学号、姓名、所选课程名称、任课老师,按课程名称排序

--SELECT     dbo.Department.DepartName, dbo.Student.ClassNo, dbo.Student.StuName, dbo.Student.StuNo, dbo.Class.ClassName, dbo.Course.CouName,

                      --dbo.Course.Teacher

--FROM         dbo.Class INNER JOIN

                      --dbo.Department ON dbo.Class.DepartNo = dbo.Department.DepartNo INNER JOIN

                      --dbo.Student ON dbo.Class.ClassNo = dbo.Student.ClassNo INNER JOIN

                      --dbo.Course ON dbo.Department.DepartNo = dbo.Course.DepartNo order by couname

 

--4、显示院系编号为的每个学生所属的院系名称、班级编号、班级名称、学号、姓名、所选课程名称、任课老师,按课程名称排序

--SELECT     dbo.Department.DepartName, dbo.Student.ClassNo, dbo.Student.StuName, dbo.Student.StuNo, dbo.Class.ClassName, dbo.Course.CouName,

                      --dbo.Course.Teacher

--FROM         dbo.Class INNER JOIN

                      --dbo.Department ON dbo.Class.DepartNo = dbo.Department.DepartNo INNER JOIN

                      --dbo.Student ON dbo.Class.ClassNo = dbo.Student.ClassNo INNER JOIN

                      --dbo.Course ON dbo.Department.DepartNo = dbo.Course.DepartNo where DepartNo like '01' order by couname

 

--5、显示选修了课程的学生所属的院系名称、班级编号、班级名称、学号、姓名、所选课程编号,按志愿号排序

--SELECT     dbo.Department.DepartName, dbo.Student.ClassNo, dbo.Student.StuName, dbo.Student.StuNo, dbo.StuCou.CouNo, dbo.Class.ClassName

--FROM         dbo.Class INNER JOIN

                     -- dbo.Department ON dbo.Class.DepartNo = dbo.Department.DepartNo INNER JOIN

                     -- dbo.Student ON dbo.Class.ClassNo = dbo.Student.ClassNo INNER JOIN

                     -- dbo.StuCou ON dbo.Student.StuNo = dbo.StuCou.StuNo where stucou.couno='008' order by willorder 

 

--6、显示选修了课程的学生所属的院系名称、班级编号、班级名称、学号、姓名、所选课程名称,按志愿号排序

--SELECT     dbo.Department.DepartName, dbo.Student.ClassNo, dbo.Student.StuName, dbo.Student.StuNo, dbo.Class.ClassName, dbo.Course.CouName,

                      --dbo.Course.Teacher

--FROM         dbo.Class INNER JOIN

                      --dbo.Department ON dbo.Class.DepartNo = dbo.Department.DepartNo INNER JOIN

                      --dbo.Student ON dbo.Class.ClassNo = dbo.Student.ClassNo INNER JOIN

                      --dbo.Course ON dbo.Department.DepartNo = dbo.Course.DepartNo where stucou.couno='008' order by willorder

 

 

              【  九  

 

--1、使用SSMS创建视图,要求数据表的来源为:department,class,student三个表

-----显示学生每个学生所属的院系名称、班级名称、学号及姓名、性别,视图保存为v_stu

----(此小题不用输入命令)

 

--2、用命令查看视图V_stu的所有数据

--select * from v_stu

 

--3、用命令查看所有学生的院系名称、班级名称、学号及姓名(使用视图V_stu),结果按姓名排序

--select * from v_stu order by stuname

 

--4、用命令查看所有姓陈、张、李的学生的院系名称、班级名称、学号及姓名(使用视图V_stu),结果按姓名排序

--select * from v_stu where stuname like '[陈张李]%'

 

--5、用内连接命令查看所有姓陈、张、李的学生的院系名称、班级名称、学号及姓名(使用department、class、student三个表连接),结果按姓名排序

--SELECT dbo.Department.DepartName, dbo.Class.ClassName, dbo.Student.StuName, dbo.Student.StuNo, dbo.Student.Gender FROM dbo.Class INNER JOIN dbo.Department ON

--dbo.Class.DepartNo = dbo.Department.DepartNo INNER JOIN dbo.Student ON dbo.Class.ClassNo = dbo.Student.ClassNo and stuname like '[陈张李]%'

 

--第二题

--1、使用SSMS创建视图,数据表的来源为:department,class,student,stucou四个表

-----显示每个学生的院系名称、班级名称、学号、选修的课程编号、志愿号,视图保存为v_cou

--------(此小题不用输入命令)

 

--2、用命令查看视图V_cou的所有数据

--select * from v_cou

 

--3、用命令查看“信息科技系”每个学生的院系名称、班级名称、学号、选修的课程编号、志愿号信息,列名使用中文显示

--select departname as 院系名称,classname as 班级名称,stuno as 学号,couno as 课程编号,willorder as 志愿号from v_cou where departname like '信息科技系'

 

--4、使用creat view命令创建视图v_cou2:显示每个学生的院系名称、班级名称、学号、选修的课程编号、志愿号

--select * from v_cou2

--SELECT DepartName, ClassName, StuNo, WillOrder, CouNo FROM dbo.v_cou

 

--第三题

--1、使用SSMS创建视图,数据表的来源为:department,class,student,course,stucou五个表

-----显示每个学生所属系部名称,班级名称、学号、姓名、选修的课程编号、课程名称、志愿号等七个字段,列名使用中文,视图保存为v_stc

 

--2、用命令查看视图V_stc的数据

--select * from v_stc

 

--3、用命令查看v_stc视图中所有第志愿选了课程的学生信息

--select * from v_stc where couno like '008'

 

--4、用内连接命令查看每个学生所属系部名称,班级名称、学号、姓名、选修的课程编号、课程名称、志愿号等七个字段

------(department,class,student,course,stucou五个表),列名使用中文表示

--SELECT     dbo.Department.DepartName, dbo.Class.ClassName, dbo.Student.StuNo, dbo.Student.StuName, dbo.Course.CouName, dbo.Course.CouNo,

--dbo.StuCou.WillOrder FROM dbo.Department INNER dbo.Course ON dbo.Department.DepartNo = dbo.Course.DepartNo INNER JOIN

--dbo.StuCou ON dbo.Course.CouNo = dbo.StuCou.CouNo INNER JOIn dbo.Student ON dbo.StuCou.StuNo = dbo.Student.StuNo INNER JOIN

--dbo.Class N dbo.Department.DepartNo = dbo.Class.DepartNo AND dbo.Student.ClassNo = dbo.Class.ClassNo

 

--5、修改视图v_stc的定义,除了原来显示的七个字段,增加一个性别字段

-----使用命令查看所有女性学生的信息

--SELECT     dbo.Department.DepartName, dbo.Class.ClassName, dbo.Student.StuNo, dbo.Student.StuName, dbo.StuCou.WillOrder, dbo.StuCou.CouNo,

                   --dbo.Course.CouName, dbo.Student.Gender

--FROM         dbo.Class INNER JOIN

                   --dbo.Student ON dbo.Class.ClassNo = dbo.Student.ClassNo INNER JOIN

                   -- dbo.Department ON dbo.Class.DepartNo = dbo.Department.DepartNo INNER JOIN

                   --dbo.Course ON dbo.Department.DepartNo = dbo.Course.DepartNo INNER JOIN

                   --dbo.StuCou ON dbo.Student.StuNo = dbo.StuCou.StuNo AND dbo.Course.CouNo = dbo.StuCou.CouNo

 

--6、使用creat view命令创建视图,数据表的来源为:department,class,student,course,stucou五个表

-----显示每个学生所属系部名称,班级名称、学号、姓名、选修的课程编号、课程名称、志愿号等七个字段,视图保存为v_stc2

--SELECT     dbo.Department.DepartName, dbo.Student.StuNo, dbo.Student.StuName, dbo.StuCou.WillOrder, dbo.Class.ClassNo, dbo.Course.CouName,

                      --dbo.Course.CouNo

--FROM         dbo.Class INNER JOIN

                      --dbo.Department ON dbo.Class.DepartNo = dbo.Department.DepartNo INNER JOIN

                      --dbo.Course ON dbo.Department.DepartNo = dbo.Course.DepartNo INNER JOIN

                      --dbo.StuCou ON dbo.Course.CouNo = dbo.StuCou.CouNo INNER JOIN

                      --dbo.Student ON dbo.Class.ClassNo = dbo.Student.ClassNo AND dbo.StuCou.StuNo = dbo.Student.StuNo

 

               【  十  程序设计练习 

 

-----------------------------------局部变量--use xk

--declare @bjh varchar(10)

--set @bjh='20000002'

--select classname from class where classno=@bjh

-----------------------------------------------------------------------declare @var1 varchar(8)

--select @var1='班级名称'

--print @var1

-----------------------------------------------------------------------试分析以下代码运行的结果

--use xk

--declare @bjh varchar(10)

--set @bjh='20000002'

--set @bjh='20000001'

--select classname from class where classno=@bjh

-----------------------------------------------------------------------试分析以下代码运行的结果

--use xk

--declare @bjh varchar(10)

--set @bjh='20000002'

--go

--select classname from class where classno=@bjh

-----------------------------------------------------------------------流程控制语句

--检索信息技术类课程的数量(例子)

 

 

--使用IF....Else流程控制语句完成以下练习

--1、检索course表中"信息技术类课程的数量,如果查不到,则提示“没有此类课程”

 

 

 

--2、检索student表中所有姓陈的学生信息,如果查不到,则提示“没有姓陈的学生”

 

 

--3、检索stucou表中课程编号为的课程的选修人数,如果没有人选修,提示“没人选修”

 

 

--4、检索stucou表中同时选修了及课程的学生人数,如果没有人同时选修此两门课程,

-----提示“没人同时选修及”

 

 

           【  十一  

 

--标量值函数练习

--例:创建一函数xkrs,输入课程号,返回报名人数(willnum)      (course表)

 

 

--调用函数xkrs,查询课程号为的报名人数

--select dbo.xkrs('005') as 人数

 

--1、创建一函数xm,输入学生的学号,返回该学生的姓名,(student表)

 

--调用函数xm,查询学号为学生的姓名

--select dbo.xma('00000000005') as stuname

 

--2、创建一函数kcmc,输入课程的编号,返回该课程的课程名称(course表)

/*

create function kcmca

(@couno char(3))

returns char(30)

begin

declare @name char(30)

select @name=couname from course where couno=@couno

return @name

 

 

 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有