数据库表的查询操作实践演练(实验三)
数据库 操作 查询 实践 实验 演练
2023-06-13 09:19:43 时间
继前两次的实验,本次实验以熟练掌握利用select语句进行各种查询操作:单表查询、多表连接及查询、嵌套查询、集合查询等,巩固数据库查询操作。
下面就跟着小编一起练习吧!
在实验一创建并插入数据的表(Student, Course,SC,Teacher,TC)的基础上,完成以下操作。
(1)将教师‘罗莉 的名字改为‘罗莉莉 。
插入: insert into Score(sno,cno,grade) values ( 04261006 , C003 , 64 )
insert into Score(sno,cno,grade) values( 04261007 , C004 , 79 ) 查询: select sno 学号,cno 课程号,grade 分数from Score where sno=04261006 or sno=04261007; 删除: delete from Score where sno=04261006 or sno=04261007; (3)求每门课的平均成绩,并把结果存入average表(自行设计并创建); CREATE TABLE average
(
cno CHAR(8),
avscore numeric(5,2),
constraint a1 primary key (cno),
constraint a2 foreign key (cno) references Course(cno),
)
insert into average(cno,avscore)
select distinct cno ,avg(grade) from Score group by cno (4)将学生“马丽”的年龄改为24; Update Student set 2014-year(Sbirth) 年龄 where Sname= 马丽 (5)将所有学生的szipcode属性列值填补上; update Student set szipcode= 221000 (6)将average表中的所有课程的平均成绩置零; update average set avscore= 0 (7)删除average表中的课程号为‘C007 的平均成绩记录; delete from average where cno= C007 (8)删除所有average表中平均成绩记录; delete from average; (9)建立一个临时学生信息表(tstudent),删除该表中的学号含‘101 的所有学生记录。 create table tstudent ( Sno char(8) primary key, Sname varchar(8) unique );
Delete from tstudent where Sno like 001011% (10)查询全体学生的学号与姓名; select sno 学号,sname 姓名from Student (11)查询全体学生的学号、姓名、所属系; select sno 学号,sname 姓名,sdept 系from Student (12)查询全体学生的详细记录; select * from Student (13)查询全体学生的姓名及其年龄; select sname 姓名,2014-year(sbirth) 年龄from Student (14)查询全体学生的姓名、出生年份; select sname 姓名,year(sbirth) 出生年份from Student (15)查询所有修过课的学生的学号; select distinct sno from Score
select distinct student.sno from Student,Score where Student.sno=Score.sno and Score.grade (16)查询“计算机系”班全体学生名单; select sno,sname from Student where sdept= 计算机系 (17)查询查询所有年龄在23岁以下的学生姓名及其年龄; select sname 姓名,2014-year(sbirth) 年龄from Student where 2014-year(sbirth) (18)查询考试成绩有不及格的学生的学号; select distinct sno from Score where grade (19)查询年龄在20至22岁之间的学生姓名、系和年龄; select sname 姓名,sdept 系,2014-year(sbirth) 年龄from student where 2014-year(sbirth) between 20 and 22; (20)查询年龄不在20至22岁之间的学生姓名、系和年龄;
select sname 姓名,sdept 系,2014-year(sbirth) 年龄from student where 2014-year(sbirth) not between 20 and 22; (21)查询“计算机系”和“电商系”的学生的姓名; select sname from Student where sdept= 计算机系 or s > (22)查询既不是“计11”也不是“计61”班的学生的姓名和班级信息; select sname,sclass from Student where sclass not in( 计 , 计 );
(23)查询学号为“04262002”的学生的详细情况;
[code]select student.sno,sname,ssex,2014-year(sbirth),sclass,grade from Student,Score where Student.sno=Score.sno and Student.sno= 04262002 (24)查询学号以“04262”打头的学生信息; select * from Student where sno like 04262% (25)查询所有姓“张”学生的学号、姓名、性别、年龄; select sno 学号,sname 姓名,ssex 性别,2011-year(sbirth) 年龄from Student where sname like 王% (26)查询名字中第二个字有“海”字的学生的学号、姓名、性别、年龄; select sno 学号,sname 姓名,ssex 性别,2011-year(sbirth) 年龄from Student where sname like _田% (27)查询所有不姓“刘”学生的姓名; select sname 姓名from Student where sname not like 刘% (28)查询课程号以“C”开头的最后两个字母为“05”的课程号和课程名; select cno,cname from Course where cno like C%05 (29)某些学生选修某门课程后没有参加考试,所以有选修课记录,但没有考试成绩,试查找缺少考试成绩的学生和相应的课程号; select Student.sno,sname,cno from Student,Score where Student.sno=Score.sno and grade is NULL; (30)查找全部有成绩记录的学生学号、课程号; select sno, cno from Score where grade is not NULL; (31)查找“计算机系”年龄在22岁以下的学生学号、姓名; select sno ,sname from Student where sdept= 计算机系 and 2014-year(sbirth) 22 (32)查找选修了“C001”号课程的学生学号及其成绩,查询结果按分数降序排序; select student.sno,grade from student,Score where Student.sno=Score.sno and cno= C001 order by grade desc; (33)查询全体学生情况,查询结果按所在系升序排列,对同一系中的学生按年龄降序排列; select * from student order by sdept asc,2014-year(sbirth) desc; (34)查询学生总人数; select count(*) 人数from Student; (35)查询选修了课程的学生人数; select count(distinct sno)人数from Score; (36)在所有课程中查询最高分的学生学号和成绩; select sno,grade from Score where grade =(select max(grade)from Score )
select distinct a.* from Score a where a.sno IN (select top 1 Score.sno from Score where Score.cno = a.cno order by grade desc)
(37)查询学习“C001”课程的学生最高分数;
(select Student.sno from Student,Score where
sdept= 计算机系 and Student.sno=Score.sno group by Student.sno having count(cno) (40)自然连接student和score表; select student.*,Score.grade from student ,Score where student.sno=Score.sno; (41)使用自身连接查询每一门课程的间接先行课(即先行课的先行课) select a.cno,b.cpno from Course a,Course b where a.cpno=b.cno; (42)使用复合条件连接查询选修“c001”号课程且成绩在90分以上的所有同学; select sname,grade from student,Score where Student.sno=Score.sno and cno= C001 and grade (43)使用复合条件连接查询每个学生选修的课程名及其成绩;
select Student.sno,sname,cname,grade from Course,Score,Student where Course.cno=Score.cno and student.sno=Score.sno; (44)查询选修了全部课程的学生; select Sname from Student where not exists (select * from Course where not exists(select * from Score where Sno=Student.Sno and Cno=Course.Cno)) (45)查询所有选修了C001号课程的学生学号、姓名; select student.sno,sname from student,Score where student.sno=Score.sno and cno= C001
(46)查询选修了课程C001或C007的学生学号、姓名;
[code]select student.sno,sname,cno from student,Score where student.sno=Score.sno and cno in ( C001 , C007 );
[/code]
(47)查询“计算机系”的学生及年龄不大于23岁的学生;
as select sno,sname,ssex,sbirth,sclass from student where s > (53)建立“计算机系”学生的视图2,并要求进行修改与插入时,仍须保证该视图只有“计算机系”班学生; create view view_student2
as select sno,sname,ssex,sbirth,sclass from student where s > (54)建立“计算机系”选修了“C001”课程的学生的视图,定义视图名为“v_cs_C001_student1”; create view v_cs_C001_student1
as select student.sno,sname,ssex,sbirth,sclass from Student ,Score where
student.sno=Score.sno and s > (55)建立“计算机系”班选修了“C001”课程且成绩在90分以上的学生的视图,定义视图名为“cs_c001_student2”; create view cs_c001_student2
as
select student.sno,sname ,ssex,sbirth,sclass,cno from student,Score where
student.sno=Score.sno and cno= C001 and s > (56)定义一个反映学生年龄的视图,定义视图名为“v_birth_student”; create view v_birth_student
as
select sno,sname,2014-year(sbirth) age from student (57)将学生表中所有女生记录定义为一个视图,视图名为“v_female_student”; create view v_female_student
as
select * from student where ssex= 女 (58)将学生的学号及其平均成绩定义为一个视图,视图名为“v_average_student”; create view v_average_student
as
select sno,avg(grade) avscore from Score group by sno; (59)在“计算机系”学生视图中找出年龄小于22岁的学生; select * from view_student where 2014-year(sbirth) (60)利用视图查询“计算机系”选修了“C001”课程的学生; select * from v_cs_C001_student1; (61)通过(52)中的“计算机系”视图修改某个学生的名字; update view_student set sname= 王某某 where sno=04261001; (62)通过(53)中的“计算机系”视图,插入一个新学生记录。 insert into view_student2(sno,sname,ssex,sbirth,sclass) values ( 04262004 , 张某某 , 男 , 1987/11/09 , 计 ); (63)通过(53)中的“计算机系”视图,删除一个学生记录。 delete from view_student2 where sno= 04262004 and sname= 张某某
实验课结束了,相信通过本节课的实践操作,小伙伴们都对数据库表的操作有了更进一步的了解。
以上就是查询数据库表的基本操作,几乎涵盖了各种查询操作所遇到的情况,值得大家亲自操作一下,相信对大家的学习有所帮助。
我想要获取技术服务或软件
服务范围:MySQL、ORACLE、SQLSERVER、MongoDB、PostgreSQL 、程序问题
服务方式:远程服务、电话支持、现场服务,沟通指定方式服务
技术标签:数据恢复、安装配置、数据迁移、集群容灾、异常处理、其它问题
本站部分文章参考或来源于网络,如有侵权请联系站长。
数据库远程运维 数据库表的查询操作实践演练(实验三)
相关文章
- 数据库——操作数据库语句(select单表查询)
- excel菜鸟级mysql数据库mysql for excel操作2021.7.23
- 【说站】python执行数据库的查询操作
- 【Redis】Redis 数据库操作 ② ( 数据库操作 | 切换数据库 | 查询当前数据库键个数 | 清空当前数据库 | 清空所有数据库 )
- postgresql 数据库 查询集合结果如何用逗号分隔返回字符串处理的操作
- postgresql数据库根据年月查询出本月的所有数据操作
- PostgreSQL 查找当前数据库的所有表操作
- MySQL数据库学习之查询操作详解
- mysql锁表查询和解锁操作详解数据库
- 使用Java操作MySQL数据库快速精准查询(java查询mysql数据库)
- 操作Oracle数据库中MINUS操作的使用(oracle的minus)
- 数据库使用JDBC操作Oracle数据库(jdbc使用oracle)
- 使用Scala操作MySQL数据库(scalamysql)
- 化查询C操作MySQL数据库之参数化查询(cmysql参数)
- Oracle删除数据库中的记录:操作指南(oracle如何删除)
- MySQL 存储过程声明:优化数据库操作(mysql存储过程声明)
- MySQL命令清空数据库操作实例(mysql 命令 清空)
- C语言操作Oracle数据库之精彩绝伦(c操作oracle数据库)
- 查看MySQL数据库版本的简易操作(如何查看mysql数据库版本)
- Oracle Q操作指南:成功实现数据库查询(oracle q 操作)
- cmd终端删除MySQL数据库命令操作指南(cmd删除mysql命令)
- 为 Oracle 数据库优化操作伪列行数分析法(oracle 伪列行数)
- MySQL数据库不支持全连接操作,详解查询中的连接方式(mysql 不支持全连接)
- Oracle数据库中的内连接操作一种有效的数据集合连接方式(oracle中的内连接)
- Oracle数据库不支持OR操作(oracle不用or查询)
- PHP文本文章分页代码按标记或长度(不涉及数据库)
- sql2000清空后让表的id从1开始等数据库操作
- Python使用MySQLdbforPython操作数据库教程