mysql练习笔记

Student:
学号(Sno) 姓名 Sname      性别 Ssex      年龄 Sage      所在系 Sdept
Course:
课程号Cno      课程名Cname      先行课Cpno      学分Credit
SC:
学号Sno      课程号Cno      成绩Grade

create table student(
     sno varchar(20) primary key,
     sname varchar(20),
     ssex varchar(20),
     sage int,
     sdept varchar(20)
);
create table course(
     cno varchar(20) primary key,
     cname varchar(20),
     cpno varchar(20),
     credit int
);
create table sc(
     sno varchar(20),
     cno varchar(20),
     grade int,
     primary key(sno,cno),
     foreign key(sno) references student(sno),
     foreign key(cno) references course(cno)
);
insert into student(sno,sname,ssex,sage,sdept) values
('95001','张三','男',20,'CS'),
('95002','李四','女',19,'IS'),
('95003','小华','女',18,'MA'),
('95004','王五','男',19,'IS');
insert into course(cno,cname,cpno,credit) values
('1','数据库','5',4),
('2','数学',null,2),
('3','数据结构','1',3),
('4','操作系统','6',4);
insert into sc(sno,cno,grade) values
('95001','1',92),
('95001','2',85),
('95001','3',88),
('95002','2',90),
('95002','3',95);

一:查询表中的列和行
1.查询全体学生的详细记录
select * from student;
2.查询全体学生的姓名及出生年份
select sname,Year(now())-sage+1 from student;
3.查询选修了课程的学生姓名
select distinct s.sname from student as s,sc where s.sno=sc.sno;

二:条件查询:
常用的查询条件

查询条件谓词
比较=,<,>,>=,<=,!=,<>,!>,!<;
not+上述比较运算符
确定范围Between and,Not between And,
确定集合IN,not IN
字符匹配Like,Not Like
空值IsNull,ISNOTNULL
多重条件AND,OR

1:查询计算机系全体学生的姓名
select sname from student where sdept='CS';
2.查询考试成绩有不及格的学生的学号
select sno from sc where grade<60;
3.查询年龄在20到23间的学生的姓名,系别及年龄
select sname,sdept,sage from student where sage between 20 and 30;
4.查询信息系(IS),数学系(MA)和计算机系(CS)学生的姓名和性别
select sname,ssex from student where sdept in("IS","MA","CS") ;
5.查询所有姓张的学生的姓名,学号和性别(where name like ‘张%’)
select sname,sno,ssex from student where sname like '张%';
6.查询姓”李”且命名为两个汉字的学生的姓名
select sname from student where sname like '李_';
7.查询缺少成绩的学生的学号和相应的课程号(where grade is not null)
select sno,cno from sc where grade is null;
8.查询有成绩的学生学号和课程号
select sno,cno from sc where grade is not null;
9.查询选修了3号课程的学生的学号及其成绩,分数降序排列
select student.sno,grade from student,sc where student.sno=sc.sno and sc.cno=3 order by grade desc;
10.查询全体学生情况,结果按所在系的号升序排列,同一系中的学生按年龄降序
select * from student order by sdept,sage desc;


三:使用集函数

count,sum,avg,max,min

1.查询学生的总人数
select count(sno) from student;
2:查询选修了课程的学生人数(select count(distinct sno))
select count(distinct sno) from sc;
3:计算1号课程的学生平均成绩
select avg(grade) from sc where cno='1';
4:查询选修1号课程的学生最高分数
select max(grade) from SC where cno='1';
5:求各个课程号及相应的选课人数
select cno,count(sno) from sc group by cno;
6:查询选修了3门以上的课程的学生学号
select sno from sc group by sno having count(*)>3;


四:连接查询:

<1>等值与非等值的连接查询
在连接查询中用来连接两个有的条件称为连接条件或连接谓词,,当连接运算符号为”=”时,称为等值连接,使用如,=,<,>,<=,>=,!=连接时称非等值连接
1.
1:查询每个学生及其选修课程的情况
select student.*,sc.*
from student,sc
where student.sno=sc.sno
<2>自身连接
连接操作在同一个表中进行连接查询
2:查询每一门课的间接先修课(即先修课的先修课)
select first .cno,second.cpno
from course first ,course second
where first.cpno=second.cno;


五:复合条件连接
1:查询选修2号课程且成绩在80分以上的所有学生。
select student.sname from student,sc where student.sno=sc.sno and sc.cno='2' and sc.grade>80;


六:嵌套查询

1:带有谓词in的子查询

<1>查询与“张三”在同一个系学习的学生
select sno,sname,sdept
from student
where sdept in(
select sdept
from student
where sname='张三');

<2>查询选修了课程名为“信息系统”的学生学号和姓名
select sno,sname
from student
where sno in
( select sno
from sc
  where cno in
     (select cno
        from course
          where cname='数据库'));
或:
select student.sno,sname
   from student,sc,course
  where student.sno=sc.sno and
       sc.cno=course.cno and
       course.cname='数据库';

2:带有Any 或all谓词的子查询

<1>查询其他系中比信息系中某一学生年龄小的学生姓名和年龄
select sname, sage
from student
where sage
       from student
where sdept='IS')
and sdept<>'IS';
或用集函数:
select sname, sage
from student
where sage<
(select max(sage)
from student
where sdept='IS')
and sdept<>'IS';

<2> 查询其他系中比信息系所有学生年龄都小的学生姓名及年龄
select sname, sage
from student
where sage
        (select sage
        from student
       where sdept='IS')
    and sdept<>'IS';

3 带有Exitst谓词的子查询
<1>查询所有选修了1号课程的学生姓名
select sname
from student
where exists
        (select *
         from sc
         where sno=student.sno and cno='1');

<2>查询没有选修1号课程的学生姓名
select sname
from student
where not exists
          (select *
            from sc
             where sc.sno=student.sno and sc.cno='1');

<2>查询选修所有全部课程的学生姓名
select sname
from student
where not exists
        (select *
          from course
            where not exists
                  (select *
                     from sc
                       where sc.sno=student.sno
              and sc.cno=course.cno));

<3>查询只选修了学生95002选修的全部课程的一部分的学生号码

select distinct sno
from sc scx
where not exists
       ( select *
          from sc scy
           where scy.sno='95002' and
              not exists
                  ( select *
                     from sc scz
                      where scz.sno=scx.sno and
                         scz.cno=scy.cno)

       );

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