实验一(1)无条件单表查询select sname NAME,'year of birth:' BIRTH,2004-sage BIRTHDAY,LOWER(sdept) DEPARTMENT FROM student;(2)有条件单表查询SELECT sname,sdept,sage FROM student WHERE sage NOT BETWEEN 20 AND 23;(3)单表嵌套(一层)查询SELECT sno,sname,sdept FROM student WHERE sdept IN(SELECT sdept FROM student WHERE sname='刘晨');(4)复合条件多表查询SELECT student.sno,sname,cname,grade FROM student ,sc,course WHERE student.sno=sc.sno AND o=o;(5)使用COUNT()的单表查询SELECT COUNT(*) FROM student;(6)使用AVG()的单表查询SELECT AVG(grade) '平均成绩' from SC where CNO='1';(7)查询结果分组SELECT cno,COUNT(sno) '人数' FROM sc GROUP BY cno;(8)查询结果排序SELECT * FROM student ORDER BY sdept,sage DESC;(9)使用通配符的查询SELECT sname,sno,ssex FROM student WHERE sname NOT LIKE'刘%';(10)使用换码字符的单表查询SELECT cno,ccredit FROM course WHERE cname LIKE 'DB\_Design'ESCAPE'\';(11)插入单个元组插入一个新学生元组Insert into student (sno,sname,ssex,sdept,sage) values ('200215128','陈冬','男','IS',18)(12)插入子查询结果对每一个系,求学生平均年龄,并把结果存入数据库Create table dept_age(sdept char(15),avg_age int)Insert into dept_age(sdept,avg_age) select sdept,avg(sage) from student group by sdept(13)修改某个元组的值将学生200215121的年龄改为22岁Update student set sage=’22’ where sno=’200215121’(14)修改多个元组的值将所有学生的年龄增加一岁Update student set sage=sage+1(15)删除一个元组的值删除学号为200215128的学生记录delete from student where sno='200215128'(16)建立视图建立信息系学生的视图create view is_student as select sno,sname,sage from student where sdept='IS'×(17)查询视图查询选修了1号课程的信息系学生信息Select is_student.sno,sname from is_student,sc where is_student.sno=sc.sno and o=’1’×(18)更新视图将信息系学生视图is_student中学号为95001的学生姓名改为李楠update is_student set sname='李楠' where sno='95002'将下列问题用SQL命令表示:1.查询‘IS’系学生的学号、姓名、性别。
SELECT sno,sname,ssex FROM student WHERE sdept='IS';2.查询‘IS’系年龄在20岁以下的学生。
SELECT * FROM student WHERE sdept='IS'AND sage<20;3.查询所有不姓‘刘’的学生的学号、姓名、性别。
SELECT sname,sno,ssex FROM student WHERE sname NOT LIKE'刘%';4. 查询student表中学生的总人数。
SELECT COUNT(*) '总人数' FROM student;5. 查询和‘李勇’同性别的所有同学的姓名。
SELECT sname from student where ssex in(select ssex from student where sname='李勇');6. 查询和‘李勇’同性别并同系的所有同学的姓名。
Select sname from student where ssex in (select ssex from student where sname='李勇') and sdept in (select sdept from student where sname='李勇')7. 查询选修2号课程的学生的学号。
Select sno from sc where cno='2'8. 求3号课程的平均成绩。
Select avg(grade) from sc where cno=’3’9.查询选修2号课程的学生的最高分。
Select max(grade) from sc where cno=’2’10.按成绩降序排列,输出‘IS’系学生选修了2号课程的学生的姓名和成绩。
Select sname,grade from student,sc where sdept='IS' and cno='2' and student.sno=sc.sno order by gradeSQL查询分析器下建数据库的命令代码:create database 霍双双200826352on(name='霍双双200826352_data',filename='E:\霍双双20082635\霍双双200826352_data.mdf',size=10mb,maxsize=50mb,filegrowth=10%)log on(name='霍双双200826352_log',filename='E:\霍双双20082635\霍双双200826352_log.ldf',size=10mb,maxsize=50mb,filegrowth=10%)在查询分析器重建立各表的命令代码:建立student表:create table student(sno char(5) primary key,sname char(20),ssex char(2),sage int,sdept char(15))建立course表:create table course(cno char(2)primary key,cname char(15),cpno char(2),ccredit int)建立cs表:use 霍双双200826352create table sc(sno char(5),cno char(2),grade smallint,primary key(sno,cno),foreign key(sno)references student(sno),foreign key(cno)references course(cno))实验二T-SQL查询、存储过程、触发器、完整性上机作业题第一部分:T-SQL程序设计(1).如果3号课程的平均成绩在80分以上,则输出“3号课程成绩良好”,否则输出“3号成绩一般”declare @avg float set @avg=(select avg(grade)from sc where cno='3')if @avg>80print'3号课程成绩良好'else print'3号成绩一般'(2)计算并输出95003号学生的平均成绩,若无该生信息,则显示“该生未选课”,提示信息.declare @avg float if(select count(*)from sc where sno='95003')=0 print '该生未选课' else begin select @avg=avg(grade)from sc where sno='95003' print'95003号学生平均成绩' print @avg end(3).如果有成绩在90分以上的学生,则显示他的学号,课程和成绩,否则显示“没有学生的课程成绩在90分以上”提示信息declare @text char(10) if exists(select grade from SC where grade>90)select Sno,Cno,Grade from SC where Grade>90 else begin set @text='没有学生的课程成绩在90分以上' print @text end×(4).利用游标逐行显示student表中的记录。
declare stu cursor for select *from student open stu fetch next from stu while @@fetch_status=0 fetch next from stu close stu deallocate stu(5).用自定义函数计算全体男生的平均年龄create function avg_age(@sex char(2)) returns intas begin declare @aver int select @aver=(select avg(Sage) from Student where Ssex=@sex )return @aver enddeclare @aver1 int,@sex char(2) set @sex='男' select @aver1=dbo.avg_age(@sex)select @aver1 as '全体男生的平均年龄'go(6).显示course表中课程名的前2个字符。