数据库复习

     今日复习收获:

 1、之前一直用extract和diffdate来获取datetime类型中的年。今天复习数据库实验报

       告时,忽然发现了这样一代码:(select 性别,year(生日)

 

原来我之前一直用year来提取数据!! 

 

感悟:很多时候,当我回过头来看我当初所做的事情时,我总是惊叹:我当初是怎么做出

来的!江山打了下来,却没有时间去欣赏、去体会,确实是一种遗憾,抑或是一种损失。2、复习很有必要:

 

 

k)查询选修了所有课程的学生的学号和姓名。

select distinct 姓名, student.学号

from student,preCourse 

where  student.学号 = preCourse.学号 and

           not exists(

                select 课程号

                from course

                except

                select 课程号

                from preCourse

                     where student.学号 = preCourse.学号

                    ) 

今天看完之后,发觉有冗余,在第一个from中preCourse完全多余。这道题的思路应该是:在not exists中求出选修了所有课程的学生,然后通过外围student的学号等于PreCourse中的学号即可将其名字打印出来(因为preCourse中没有name属性,故须与student连接)。

修改后:

select distinct 姓名, student.学号
    from student
    where   not exists(
              select 课程号
              from course
        except
              select 课程号
              from preCourse
              where student.学号 = preCourse.学号
                      ) 

 

你可能感兴趣的:(数据库)