use srs
--例4-1 向数据库s中添加一条学生记录('11003','拉姆','女','计算机','16')insertinto s values('11003','拉姆','女','计算机','16');--例4-4 将学生“雷姆”转入自动化update s
set dept='自动化'where sn='雷姆'--例4-5 将所有学生的年龄增加1岁update s
set age=age+1--例4-6 删除学生“雷姆”的记录deletefrom s
where sn='雷姆'--例4-7 删除数据表s的所有记录deletefrom s
--例4-8 从数据表s中查询所有学生的学号、姓名和性别select sno,sn,sex
from s
--例4-9 从数据表sc中查询成绩大于80分的学生选课信息。select*from sc
where grade >80--例4-10 从数据表sc中查询选修了课程“1”并成绩高于80分的学生的学号和成绩select sno,grade
from sc
where cno='1'and grade>80--例4-11从数据表t中查询工资在5000元到7000元之间的教师的教师号、姓名与职称select tno,tn,prof
from t
where sal between5000and7000--例4-12从数据表sc中查询选修了课程‘1’或‘2’的学生的选课信息select*from sc
where cno in('1','2')--例4-13.1数据表t中查询所有李姓教师和教师号和姓名select tno,tn
from t
where tn like'李%'--例4-13.2去重distinct:adj 清楚的,清晰的,去掉相同年龄的selectdistinct age
from s
--例4-14 从数据表sc中查询考试成绩有效的学生选课信息select*from sc
where grade isnotnull--例4-15 从数据表t中查询计算机系教师的人数,以及这些教师工资的总和与平均值selectcount(*)as num,sum(sal)as sumsal,avg(sal)as avgsal
from t
where dept ='计算机'--例4-16 从数据表sc中查询选修了课程"1"且有成绩的学生人数,以及成绩中的最高分和最低分。selectcount(grade)as num,max(grade)as maxgrade,min(grade)as mingrade
from sc
where cno='1'--例4-17 从数据表sc中查询选修每门课程的学生人数--group by子句将查询结果按照属性cno进行了分组--因为select后面有两个字段,我们用group by对cno进行分组select cno,count(sno)as num
from sc
groupby cno
--例4-18从数据表sc中查询选修两门及以上课程的学生学号及选课门数--使用grout by子句将查询结果按照属性sno进行分组--使用count(*)函数对各组个数进行统计,having子句限制筛选条件select sno,count(*)as num
from sc
groupby sno
having(count(*)>=2)--例4-19 从数据表t中查询每个教师的教师号、姓名及年薪,并按照年薪值降序排列select tno,tn,sal*12as income
from t
orderby income desc--降序 高-低--只有order by子句可以调用列的别名--例4-20 从数据表sc中查询选修了课程"1"或"2"的学生选课信息,查询结构按照学号升序排列,学号相同再按照排序排列select*from sc
where cno in('1','2')orderby sno,grade desc--例4-21 从数据表t、tc中查询教师“肖坚”所讲授的课程,列出其教师号和所授的课程编号。select t.tno,cno
from t,tc
where t.tno=tc.tno and tn='肖坚'select t.tno,cno
from t innerjoin tc
on t.tno=tc.tno
where tn='肖坚'--on主要实在表之间进行连接时指明连接条件的--例4-22 从数据表c,sc中查询每门课程的课程编号,课程名称及选课人数select c.cno,cn,count(sc.sno)as num
from c,sc
where sc.cno=c.cno
groupby c.cno,cn
--例4-23 从数据表c中查询所有选课信息,并列出课程名称select sno,c.cno,cn,grade
from c
rightjoin sc
on c.cno=sc.cno
--例4-24 从数据表c中查询所有课程名称,并分别列出它们的选课信息select cn,c.cno,sno,grade
from c
leftjoin sc
on c.cno=sc.cno
--例4-25 对学生表s与教师表t进行交叉查询select*from s crossjoin t
--例4-26 从数据表s中查询所有年龄比“加藤惠”大的学生姓名、年龄。--例4-27 从数据表T中查询与教师“肖坚”同系的教师名称select tn,prof
from t
where dept=(select dept
from t
where tn ='肖坚')--例4-28 从数据表s,sc中查询选修了课程‘2’的学生姓名select sn
from s
where sno=any(select sno
from sc
where cno='2')--使用子查询在数据表sc中查询选修了课程'2'的学生学号为结果--用于在父查询的where子句中构造查询条件。查询这些学生学号--在数据表s中对应的学生学号,any:任意一个