An Image/Link below is provided (as is) to download presentation
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.Content is provided to you AS IS for your information and personal use only.Download presentation by click this link.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.During download, if you can't get a presentation, the file might be deleted by the publisher.
单表查询(续III) • 查询选修了课程的学生学号 • Select Distinct Sno From STUDENT; • 查询计算机系全体学生的姓名 • Select Sname From Student where Sdept=‘CS’; • 查询所有年龄在20岁以下的学生姓名及其年龄 • Select Sname From Student where Sage<20;
单表查询(续IV) • 查询年龄在20~23(含20和23岁)之间的学生姓名、性别和年龄。 • Select Sname, Ssex, Sage From Student Where Sage BETWEEN 20 AND 23; • 查询数学系和计算机系的学生的姓名和性别。 • Select Sname, Ssex From Student Where Sdept IN (‘MA’, ‘CS’);
单表查询(续V) • 查询所有姓刘的学生的学号、姓名 • Select Sno, Sname From Student Where Sname Like ‘刘%’ • 查询所有姓刘,并且名字为两个汉字的学生的学号、姓名 • Select Sno, Sname From Student Where Sname Like ‘刘__’
单表查询(续VI) • 查询所选课程没有成绩的学生的学号,所选课程号。 • Select Sno, Cno From SC Where Grade IS NULL
单表查询(续VII) • ORDER BY子句可以将查询结果按照一个或多个属性列的升序(ASC)或降序(DESC)排序。 • 查询全体学生的情况,并按所在系的升序和年龄的降序排列 • Select * From Student Order By Sdept Asc, Sage Desc
单表查询(续IX) • 查询学生总数 • Select Count(*) From Student; • 查询选修了课程的学生人数 • Select Count(Distinct Sno) From SC; • 计算1号课程的学生平均成绩 • Select AVG(Grade) From SC Where Cno=‘1’; • 查询选修1号课程的学生最高分数 • Select MAX(Grade) From SC Where Cno=‘1’;
单表查询(续X) • 查询各门课及其相应选课人数。 Select Cno, Count(Sno) From SC Group By Cno; • 查询选课至少为三门的学生的学号及其选课门数。 Select Sno, Count(Sno) From SC Group By Sno Having Count(Sno)>=3;
嵌套查询(续II) • 查询与刘晨在同一个系学习的学生。 • Select Sno, Sname, Sdept From Student Where Sdept = (Select Sdept From Student Where Sname = ‘刘晨’);
嵌套查询(续III) • 查询选修了课程名为‘信息系统’的学生的学号和姓名。 Select Sno, Sname From Student Where Sno IN (Select Sno From SC Where Cno IN (Select Cno From Course Where Cname = ‘信息系统’));
嵌套查询(续IV) • 查询其他系中比信息系中某一学生年龄小的学生姓名和年龄。 Select Sname, Sage From Student Where Sdept <> ‘IS’ And Sage < Any(Select Sage From Student Where Sdept = ‘IS’)
嵌套查询(续V) • 查询其他系中比信息系中所有学生年龄都小的学生姓名和年龄。 Select Sname, Sage From Student Where Sdept <> ‘IS’ And Sage < All(Select Sage From Student Where Sdept = ‘IS’)
嵌套查询(续VI) • 查询选择了1号课程的学生姓名。 Select Sname From Student Where Exists (Select * from SC Where Sno=Student.Sno And Cno=‘1’) • 查询没有选择1号课程的学生姓名。 Select Sname From Student Where Not Exists (Select * from SC Where Sno=Student.Sno And Cno=‘1’)
嵌套查询(续VII) • 查询选修了全部课程的学生姓名 Select Sname From Student Where Not Exists (Select * from Course Where Not Exists (Select * from SC Where Sno=Student.Sno And Cno=Course.Cno));
集合查询 • 查询选修了1号或2号课程的学生 Select Sno From SC Where Cno=‘1’ Union Select Sno From SC Where Cno=‘2’
集合查询(续I) • 查询选修了1号课程的学生和选修了2号课程的学生的交集 Select Sno From SC Where Cno=‘1’ And Sno In (Select Sno From SC Where Cno=‘2’)
集合查询(续II) • 查询计算机系的学生与年龄不大于19岁的学生的差集 Select * from Student Where Sdept = ‘CS’ And Sage > 19;
Insert语句 • 插入数据 Insert Into <表名>[(<属性列>[{,<属性列>}])] Values(<值>[{,<值>}]) • 插入子查询结果 Insert Into <表名>[(<属性列>[{,<属性列>}])] <子查询>
Insert语句(续I) Insert Into Student Values (‘95020’, ‘陈冬’, ‘IS’, 18); Insert Into SC (Sno, Cno) Values(‘95020’, ‘1’); Insert Into Dept_Age (Sdept, Avgage)Select Sdept, AVG(Sage) From Student Group By Sdept;
Update语句 • 修改数据 Update <表名> Set <列名>=<表达式>[{, <列名>=<表达式>}] [Where <条件>]
Update语句(续I) • 将学生95001的年龄改为22岁 Update Student Set Sage = 22 Where Sno=‘95001’ • 将所有学生的年龄增加1岁 Update Student Set Sage=Sage+1
Update语句(续II) • 将计算机系全体学生的成绩置零。 Update SC Set Grade = 0 Where ‘CS’ = (Select Sdept From Student Where Student.Sno = SC.Sno)