您现在的位置是:网站首页> 编程资料编程资料
数据库表的查询操作实践演练(实验三)_MsSql_
2023-05-26
463人已围观
简介 数据库表的查询操作实践演练(实验三)_MsSql_
继前两次的实验,本次实验以熟练掌握利用select语句进行各种查询操作:单表查询、多表连接及查询、嵌套查询、集合查询等,巩固数据库查询操作。
下面就跟着小编一起练习吧!
在实验一创建并插入数据的表(Student, Course,SC,Teacher,TC)的基础上,完成以下操作。
(1)将教师‘罗莉'的名字改为‘罗莉莉'。
复制代码 代码如下:
update Teacher set tname='罗莉莉' where tname='罗莉'
(2)将两个同学(数据自己临时设置,用后即删除)的两门课程的成绩以运行sql程序文件的形式插入score表中。该题用以验证、理解和掌握关系模型的完整性规则;
插入:
复制代码 代码如下:
insert into Score(sno,cno,grade) values ('04261006','C003','64')
insert into Score(sno,cno,grade) values('04261007','C004','79')
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
(
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%';
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>0 ;
select distinct student.sno from Student,Score where Student.sno=Score.sno and Score.grade>0 ;
(16)查询“计算机系”班全体学生名单;
复制代码 代码如下:
select sno,sname from Student where sdept='计算机系'
(17)查询查询所有年龄在23岁以下的学生姓名及其年龄;
复制代码 代码如下:
select sname 姓名,2014-year(sbirth) 年龄from Student where 2014-year(sbirth)<23;
(18)查询考试成绩有不及格的学生的学号;
复制代码 代码如下:
select distinct sno from Score where grade<60;
(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 sclass='电商系'
(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';
(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 )
相关内容
- 数据库表的查询操作(实验二)_MsSql_
- MySql删除和更新操作对性能有影响吗_MsSql_
- 数据库表的创建、管理和数据操作(实验一)_MsSql_
- MySql更新优化策略_MsSql_
- 解决SQL SERVER数据库备份时出现“操作系统错误5(拒绝访问)。BACKUP DATABASE 正在异常终止。”错误的解决办法_MsSql_
- 如何调优SQL Server查询_MsSql_
- SQL优化经验总结_MsSql_
- 获取SQL Server数据库元数据的几种方法_MsSql_
- sql server Bulk Insert命令详细_MsSql_
- SQL2005、SQL2008允许远程连接的配置说明(附配置图)_MsSql_
