在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
【实验目的】:了解SQL语言的使用,进一步理解关系运算,巩固数据库的基础知识。 select * from teacher select tno, tname from teacher 如要查询时改变列标题的显示,则从teacher表中分别检索出教师教工号、姓名、电子邮箱信息并分别加上‘教师姓名'、‘教工号'、'电子邮箱'等标题信息。 select tno 工号, tname 姓名, temail 电子邮箱 from teacher 使用TOP关键字:分别从teacher中检索出前2条及前面67%的教师的信息。 select top 2 * from teacher select top 67 percent * from teacher 使用DISTINCT关键字:从teacher表中检索出教师的职称并且要求显示的职称不重复。select distinct tposition from teacher 2.用计算列:将teacher表中各教师的姓名、教工号及工资按95%发放的信息,第2条语句将工资按95%发放后列名该为‘预发工资'。语句如下: select tno tname ,tsalary*0.95 from teacher select tno工号, tname姓名, tsalary*0.95 AS 预发工资 from teacher 3.使用ORDER BY子句对查询的结果进行排序 select tno, tname from teacher WHERE tsalary>2800 order by tsalary ASC 4.条件查询 select * from teacherWHERE tamount>288 order by tamount DESC (2)使用BETWEEN AND谓词:从teacher表中查询出工作量界于144和288之间的教师资料,语句如下: select * from teacher WHERE tamount between 144 and 288 (3) 使用IN谓词:从teacher表中查询出职称为“教授”或“副教授”的教师的教工号、教师姓名、职称及家庭住址,语句如下: select tno,tname,tposition, taddress from teacher WHERE tposition in ('教授','副教授') (4)使用LIKE谓词:从teacher表中分别检索出姓'王'的教师的资料,或者姓名的第2个字是'莉'或'轩'的教师的资料,语句如下: select * from teacher WHERE tname like '王%' select * from teacher WHERE tname like '_[莉,轩]%' 二、多表查询 select student.sno, sname, cno, grade from student inner join SC on student.sno=SC.sno select student.sname,sc.grade from student,sc WHERE student.sno=sc.sno select student.sno,student.sname,sc.cno,course.cname,sc.grade from student,sc,course WHERE student.sno=sc.sno and sc.cno=course.cno 2.使用UNION子句进行查询 select sno AS 学号或工号, sname AS 姓名 from student union select tno, tname from teacher 3.用GROUP子句进行查询 select '学号'=sno,'总成绩'=SUM(Grade) from SC group by Sno (2)使用带HAVING 的GROUP BY子句。 select '学号'=sno,'总成绩'=SUM(Grade) from SC group by Sno Having SUM(Grade)>160 4.用COMPUTE和COMPUTE BY子句进行查询 select '学号'=sno,'成绩'=Grade from SC order by sno COMPUTE SUM(Grade) (2)用COMPUTE BY 子句按学号汇总出SC表中每个学生的学号及总成绩,语句如下: select '学号'=sno,'成绩'=Grade from SC ORDER BY Sno COMPUTE SUM(Grade) by sno 观察执行COMPUTE和COMPUTE BY子句的结果有什么不同? 5.嵌套查询 select SC.sno , SC.cno,SC.grade FROM SC WHERE sno IN ( SELECT sno FROM student WHERE Sclass='j10011' AND Ssex='男') 使用IN关键字查询出与教师不同名的学生(学号,姓名),语句如下: select sno 学号, sname 姓名 from student where sname not in (select tname from teacher) (2) 使用EXISTS 或NOT EXISTS关键字。 使用EXISTS关键字查询出‘j10011'班的学生的学号、课程号及相应的成绩,语句如下: SELECT SC.sno,SC.cno,SC.grade FROM SC WHERE EXISTS ( SELECT * FROM student WHERE SC.sno=student.sno AND student.sclass=‘j10011' ) 以上就是表的查询操作实验全部内容,希望对大家的学习有所帮助,大家亲自动手实践一下。 |
请发表评论