连接查询.docx

上传人:b****5 文档编号:14487959 上传时间:2023-06-23 格式:DOCX 页数:13 大小:152.63KB
下载 相关 举报
连接查询.docx_第1页
第1页 / 共13页
连接查询.docx_第2页
第2页 / 共13页
连接查询.docx_第3页
第3页 / 共13页
连接查询.docx_第4页
第4页 / 共13页
连接查询.docx_第5页
第5页 / 共13页
连接查询.docx_第6页
第6页 / 共13页
连接查询.docx_第7页
第7页 / 共13页
连接查询.docx_第8页
第8页 / 共13页
连接查询.docx_第9页
第9页 / 共13页
连接查询.docx_第10页
第10页 / 共13页
连接查询.docx_第11页
第11页 / 共13页
连接查询.docx_第12页
第12页 / 共13页
连接查询.docx_第13页
第13页 / 共13页
亲,该文档总共13页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

连接查询.docx

《连接查询.docx》由会员分享,可在线阅读,更多相关《连接查询.docx(13页珍藏版)》请在冰点文库上搜索。

连接查询.docx

连接查询

《数据库原理及应用》电子实验报告

题目:

SQL语言(3)数据查询—连接查询

日期

201..

姓名

QQ

实验环境:

WindowsXP,SqlServer2005

实验目的:

1.理解连接查询的意义

2.理解连接操作的三种方法:

嵌套循环法、排序合并法、索引连接法

3.理解SQL中连接查询的主要类型:

广义笛卡尔积、等值连接(含自然连接)、非等值连接查询、自身连接查询、外连接查询和复合条件连接查询。

实验内容:

1.连接查询

1)连接查询的定义一个查询同时涉及两个以上的表,称之为连接查询

2)采用连接查询的原因为了简化在多表间来回查找的繁琐及数据的不明确,在一个视图里多表间,简结寻找信息及多个表之间挖取自己的需要的信息

2.连接操作的方法

1)嵌套循环法

2)排序合并法

3)索引连接法

3.采用的数据库表

1)库表结构定义

2)库表记录

4.广义笛卡尔积(写出题目、关系代数表达式和sql语句)

1)广义笛卡尔积的定义

2)查看学生、课程、选修课三者之间的映射

3)Select*fromstudent,course,sc;

【实例1】

5.等值连接(含自然连接)查询

1)等值连接的定义——当连接运算符为=s时成为等值连接

2)非等值连接的定义——处使用=连接的运算叫分等值连接

3)实例操作(写出题目、关系代数表达式和sql语句及sql环境运算截图)

【实例1】查询每个学生的选修课课的情况

selectstudent.*,sc.*

fromstudentstudent,sc

wherestudent.sno=sc.sno;

【实例2】查询选修课里的信息

selectstudent.sno,sname,ssex,sage,sdept,cno,gradefromstudent,scwherestudent.sno=sc.sno;

【实例3】查询成绩等于90分的名单

selectsnofromscwheregrade='90';

4查询计算机学院的全体成员;

selectsnamefromstudentwheresdept='CS';

实例5:

查询没门课程的名单

Selectstudent.snofromstudent,course,scwherestudent.sno=sc.snoando=ogroupbystudent.sno;

(28)计算1号课程的平均成绩

selectavg(grade)fromscwherecno='1';

计算1号课程的最大成绩

selectmax(grade)fromsc

wherecno='1’

查询200215012的选修课的学分

selectsum(ccredit)fromsc,course

wheresno='200215012'andSC.Cno=o;

查询选修2号课程的名单

selectsno,snamefromstudentwheresnoin(selectsnofromscwherecno='2');

查看刘晨在那个学院

selectsdeptfromstudentwheresname='莉莉';

掉看CS的同学

selectsno,sname,sdeptfromstudentwheresdeptin('CS');

selectsname,sno,sdeptfromstudentwheresdept='CS';

查询每门课的简接先修课程

selecto,second.cpnofromcoursefirst,coursesecond

wherefirst.cpno=o;

6.非等值连接查询(写出题目、关系代数表达式和sql语句及sql环境运算截图)

1)非等值连接查询的定义——处用=号连接的外的语句运算

2)实例操作【实例1】查找年龄在20岁一下的名单

selectsnofromstudentwheresage<='20';

3)查找成绩在在90分下的名单

4)selectsnofromscwheregrade<='90'

5)

5、查询比其他学院某学生年龄比计算机学元的的学生的姓名及年龄

selectsname,sagefromstudentwheresage

andsde

pt<>'cs';

6、查询非计算机系年龄在20速之下的学生

selectsname,sagefromstudentwheresage<(selectmax(sage)fromstudentwheresdept='CS')andsdept<>'cs';

7查看平均成绩在90岁以上的学号和平均成绩

selectsno,avg(grade)fromscgroupbysnohavingavg(grade)>=90

7.自身连接查询(写出题目、关系代数表达式和sql语句及sql环境运算截图)

1)自身连接查询的定义

2)实例操作

【实例1】查询每门CNO的间接先修课

selecto,second.cpnofromcoursefirst,coursesecond

wherefirst.cpno=o;

【实例2】

8.外连接查询(写出题目、关系代数表达式和sql语句及sql环境运算截图)

1)外连接查询的定义——只有满足连接条件的元祖才能作为结果输出

实例操作

【实例1】将向学生表里插入SC里的数据

selectstudent.sno,sname,ssex,sage,sdept,cno,grade

fromstudentleftjoinscon(student.sno=sc.sno);

【实例2】将spj表连接到s表中

selects.Sno,sname,jno,qtyfromsleftjoinspjon(s.sno=spj.sno);

【实例4】查询P,spj表的类容

selectp.pno,pname,jnofromPleftjoinspjon(p.pno=spj.pno);

显示J、SPJ两表的所有信息

selectJ.jno,jname,sno,cityfromJleftjoinspjon(j.jno=spj.jno);

9.复合条件连接查询(写出题目、关系代数表达式和sql语句及sql环境运算截图)

1)复合条件连接查询的定义——在select语句里的WHERE中有多个连接条件的语句

2)实例操作

【实例1】查看刘晨所在院系的所用成员

selectsno,sname,sdeptfromstudentwheresdeptin(selectsdeptfromstudentwheresname='莉莉');

实例二、查询选修课名为信息系统的学号及其名字

selectsno,snamefromstudentwherestudent.snoin(selectsnofromscwherecnoin(selectcnofromcoursewherecname='信息系统'));

selectstudent.sno,snamefromstudent,course,scwherestudent.sno=sc.snoando=oandame='信息系统';

selectstudent.sno,snamefromstudent,coursewherecname='信息系统';

三、查找平均成绩超过自己的学生

selectsno,cnofromscx

wheregrade>=(selectavg(grade)fromscywherey.sno=x.sno);

四、查询比计算机学院某学生年龄小的学生的姓名及年

selectsname,sagefromstudentwheresage

六、查询比其他学院某学生年龄比计算机学元的的学生的姓名及年龄

selectsname,sagefromstudentwheresage

andsdept<>'cs';

七、查询非计算机系年龄在20速之下的学生

selectsname,sagefromstudentwheresage<(selectmax(sage)fromstudentwheresdept='CS')andsdept<>'cs';

八、查询其他院系比计算机学院每一位学生都小的名单

selectsname,sagefromstudentwheresage'CS';

selectsname,sagefromstudentwheresage'CS';

九、查询生意选修了1号课程的学生的姓名

selectsnamefromstudentwhereexists(select*fromscwheresno=student.snoandcno='1');

selectsnamefromstudentwherestudent.snoin(selectsnofromscwheresno=student.snoandcno='1');

查询2002级数据库的平均成绩级、课程号、学号

selecto,cnamefromsc,coursewhereo=oandcname='数据库'andsnolike('2002%');

完成情况:

10.1完成了数据连接查询的自身连接、外连接、符合连接

 

出现的问题:

(小四宋体)

查询时表的定义不明确时在相关字段加上X、X前缀的字段名

解题措施——写注释

分布查询——添加字段元素——信息比对

边写边保存

Selectstudent.snofromstudent,course,scwherestudent.sno=sc.snoando=ogroupbysno;

消息209,级别16,状态1,第1行

列名'sno'不明确。

Selectstudent.snofromstudent,course,scwherestudent.sno=sc.snoando=ogroupbystudent.sno;

 

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2