• SQL语句 多表基本操作


    创建四张表
    学生表:学号(Sno)、姓名(Sname)、性别(Ssex)、年龄(Sage)
    教师表:教师编号(Tno)、教师姓名(Tname)
    课程表:课程编号(Cno)、课程名(Cname)、教师编号(Tno)
    成绩表:学号(Sno)、课程编号(Cno)、成绩(Sscore)

    1.在学生表中插入以下数据:
       S0001,刘一,男,18;
       S0002,钱二,女,19;
       S0003,张三,男,17;
       S0004,李四,男,18;
       S0005,王五,男,17;
       S0006,赵六,女,19;
    2.在教师表中插入一下数据:
       T0001,叶平;
       T0002,贺高;
       T0003,杨艳;
       T0004,周艳;

    3.在课程表中插入以下数据:
       C0001,语文, T0001;
       C0002,数学, T0002 ;
       C0003,英语, T0003 ;
       C0004,物理, T0004 ;
    4.在成绩表中插入一下数据:
      S0001,C0001,56;
      S0001,C0002,78;
      S0001,C0003,67;
      S0001,C0004,90;
      S0002,C0001,79;         S0005,C0001,80;
      S0002,C0002,76;         S0005,C0002,92;
      S0002,C0003,80;         S0005,C0004,94;
      S0002,C0004,45;         S0006,C0001,55;
      S0003,C0001,81;         S0006,C0002,55;
      S0003,C0002,81;         S0006,C0003,62;
      S0003,C0003,92;        
      S0003,C0004,55;        
      S0004,C0001,59;
      S0004,C0002,76;
      S0004,C0003,80;
      S0004,C0004,45;

    SQL練習2
    1.查询学生表的所有数据。
    2.查询学生表所有学生的姓名和年龄,并且按照年龄的升序排列。
    3.查询年龄为18岁的学生学号和姓名
    4.查询名字中有“艳”的老师的教师编号和姓名。
    5.计算班里有几个 男生,几个女生。(显示形式:男生人数,女生人数)
    6.在学生表中添加字段“专业”( 字符串型)
    6.删除添加字段“专业”
    8.把李四的所有成绩按照降序排列。
    9.查询数学成绩在60到80分之间的学生的姓名和数学成绩。

    SQL練習3
    1.查询平均成绩大于60的学生的学号、姓名和平均成绩,并且按照成绩的升序排序。
    2.查询所有同学的学号、姓名、选课数、以及总成绩。
    3.查询语文成绩比数学成绩高的学生的姓名年龄。
    4.查询数学成绩在前三名的学生的学号、姓名、年龄和数学成绩。
    5.查询班里物理成绩及格的男生的学号、姓名以及物理成绩 。
    6.查询各科目的最高分和最低分。(以如下形式显示:课程编号,最低分,最高分)
    7.更新李四的英语成绩为90。
    8.删除钱二的物理成绩记录。
    9.查询成绩表中最低分的课程是哪个老师教的,最低分成绩是哪个学生的(以如下形式显示:课程名、学生名、分数、老师名、 )


    CREATE TABLE STUDENT
    (
     Sno   varchar(10),
     Sname varchar(20),
     Ssex  varchar(10),
     Sage  numeric(10)
    );

    CREATE TABLE teacher
    (
     Tno   varchar(10),
     Tname varchar(20)
    );


    create table Form
    (
    Cno    varchar(20),
    Cname  varchar(20),
    Tno   varchar(10)
    );


    create table scorre
    (
    Sno    varchar(20),
    Cno    varchar(20),
    Sscore numeric(20)
    );


    /*删除表中的字段*/
    alter table student
    drop column Sage;

    /*在表中添加字段*/
    alter table student
    add Ssex varchar(10),
        Sage numeric(10)


    /* 为STUDENT表中插入数据 */
    INSERT INTO STUDENT(Sno,Sname,Ssex,Sage) VALUES ('S0001','刘一','男',18);
    insert into student(Sno,Sname,Ssex,Sage) VALUES ('S0002','钱二','女',19);
    insert into student(Sno,Sname,Ssex,Sage) VALUES ('S0003','张三','男',17);
    insert into student(Sno,Sname,Ssex,Sage) VALUES ('S0004','李四','男',18);
    insert into student(Sno,Sname,Ssex,Sage) VALUES ('S0005','王五','男',17);
    insert into student(Sno,Sname,Ssex,Sage) VALUES ('S0006','赵六','女',19);


    /* 为Teacher表中插入数据 */
    INSERT INTO Teacher(Tno,Tname) VALUES ('T0001','叶平');
    INSERT INTO Teacher(Tno,Tname) VALUES ('T0002','贺高');
    INSERT INTO Teacher(Tno,Tname) VALUES ('T0003','杨艳');
    INSERT INTO Teacher(Tno,Tname) VALUES ('T0004','周艳');

    /* 为Form表中插入数据 */
    INSERT INTO Form(Cno,Cname,Tno) VALUES ('C0001','语文', 'T0001');
    INSERT INTO Form(Cno,Cname,Tno) VALUES ('C0002','数学', 'T0002');
    INSERT INTO Form(Cno,Cname,Tno) VALUES ('C0003','英语', 'T0003');
    INSERT INTO Form(Cno,Cname,Tno) VALUES ('C0004','物理', 'T0004');

    /* 为scorre表中插入数据 */
    INSERT INTO scorre(Sno,Cno,Sscore) VALUES ('S0001','C0001', 56);
    INSERT INTO scorre(Sno,Cno,Sscore) VALUES ('S0001','C0002', 78);
    INSERT INTO scorre(Sno,Cno,Sscore) VALUES ('S0001','C0003', 67);
    INSERT INTO scorre(Sno,Cno,Sscore) VALUES ('S0001','C0004', 90);
    INSERT INTO scorre(Sno,Cno,Sscore) VALUES ('S0002','C0001', 79);
    INSERT INTO scorre(Sno,Cno,Sscore) VALUES ('S0002','C0002', 76);
    INSERT INTO scorre(Sno,Cno,Sscore) VALUES ('S0002','C0003', 80);
    INSERT INTO scorre(Sno,Cno,Sscore) VALUES ('S0002','C0004', 45);
    INSERT INTO scorre(Sno,Cno,Sscore) VALUES ('S0003','C0001', 81);
    INSERT INTO scorre(Sno,Cno,Sscore) VALUES ('S0003','C0002', 81);
    INSERT INTO scorre(Sno,Cno,Sscore) VALUES ('S0003','C0003', 92);
    INSERT INTO scorre(Sno,Cno,Sscore) VALUES ('S0003','C0004', 55);
    INSERT INTO scorre(Sno,Cno,Sscore) VALUES ('S0004','C0001', 59);
    INSERT INTO scorre(Sno,Cno,Sscore) VALUES ('S0004','C0002', 76);
    INSERT INTO scorre(Sno,Cno,Sscore) VALUES ('S0004','C0003', 80);
    INSERT INTO scorre(Sno,Cno,Sscore) VALUES ('S0004','C0004', 45);
    INSERT INTO scorre(Sno,Cno,Sscore) VALUES ('S0005','C0002', 80);
    INSERT INTO scorre(Sno,Cno,Sscore) VALUES ('S0005','C0003', 92);
    INSERT INTO scorre(Sno,Cno,Sscore) VALUES ('S0005','C0004', 94);
    INSERT INTO scorre(Sno,Cno,Sscore) VALUES ('S0006','C0002', 55);
    INSERT INTO scorre(Sno,Cno,Sscore) VALUES ('S0006','C0003', 55);
    INSERT INTO scorre(Sno,Cno,Sscore) VALUES ('S0006','C0004', 62);

    /*查询学生表的所有数据*/
    select * from student;

    /*查询学生表所有学生的姓名和年龄,并且按照年龄的升序排列*/
    SELECT Sname,Sage
    FROM STUDENT
    ORDER BY SAGE asc;

    /*查询年龄为18岁的学生学号和姓名*/
    SELECT Sno,Sname
    FROM STUDENT
    WHERE Sage=18;

    /*.查询名字中有“艳”的老师的教师编号和姓名(模糊查询)*/
    SELECT Tno,Tname
    FROM Teacher
    where tname like '%艳%';

    /*计算班里有几个 男生,几个女生。(显示形式:男生人数,女生人数)*/
    /*SELECT  count(Ssex='男') 男生人数, count(Ssex='女') 女生人数
    FROM STUDENT;
    (count 为计数)*/
    SELECT  sum( CASE WHEN Ssex = '男' THEN 1 ELSE 0 END ) 男生人数,
            sum( CASE WHEN Ssex = '女' THEN 1 ELSE 0 END ) 女生人数
    FROM STUDENT


    /*在学生表中添加字段“专业”( 字符串型)*/
    alter table student
    add   专业 varchar(20);

    /*删除添加字段“专业”*/
    alter table student
    drop  column 专业;

    /*把李四的所有成绩按照降序排列*/
    SELECT Sscore
    FROM STUDENT A,SCORRE B
    WHERE A.Sname='李四' AND A.Sno = B.Sno
    ORDER BY Sscore
    DESC;

    /*查询数学成绩在60到80分之间的学生的姓名和数学成绩*/
    SELECT Sname,Sscore
    FROM STUDENT A,FORM B,SCORRE C
    WHERE A.Sno = C.Sno and B.Cno = C.Cno and B.Cname='数学'
          and C.Sscore BETWEEN 60 AND 80;
          /*and C.Sscore > 60 AND C.Sscore <80; */

     
     
     2
     /*查询平均成绩大于60的学生的学号、姓名和平均成绩,并且按照成绩的升序排序*/
     SELECT Sno 学号,Sname 姓名, sum(Sscore)平均分
     FROM STUDENT A,SCORRE B
     WHERE A.Sno = B.Sno
     GROUP BY Sno,Sname,SUM(Sscore)
     ORDER BY SUM(Sscore)
     
     
     
    SELECT student.sno, student.sname, avg(scorre.sscore)
    FROM student
    LEFT JOIN scorre
    ON student.sno = scorre.sno
    GROUP BY student.sno,student.sname
    HAVING avg(scorre.sscore) > 60



    /*查询所有同学的学号、姓名、选课数、以及总成绩*/

     select a.Sno 学号, a.Sname  姓名,count(b.Cno)  选课总数,sum(Sscore) 总成绩
     from STUDENT a,SCORRE b
     where a.Sno = b.Sno
     group by a.SNo,a.Sname
     order by a.Sno;

     /*查询语文成绩比数学成绩高的学生的姓名年龄*/
    SELECT a.*, b.Sscore 语文,c.Sscore 数学
    FROM Student a , SCORRE b , SCORRE c
    WHERE a.Sno = b.Sno
    and a.Sno = c.Sno and b.Cno = 'C0001'
    and c.Cno = 'C0002' and b.Sscore > c.Sscore

    /*查询数学成绩在前三名的学生的学号、姓名、年龄和数学成绩*/

    (个人感觉这个SQL语句更简单些)
    SELECT m.sscore
    FROM (select c.Sscore
    from student a,form b,scorre c
    where a.Sno = c.Sno and b.Cno = c.Cno and b.Cname = '数学'
    group by c.Sscore
    order by c.Sscore desc
    )m
    limit 3

    /*利用左右关联*/
    SELECT student.sno,student.sname,student.sage,scorre.sscore
    FROM student
    LEFT JOIN scorre
    ON student.sno = scorre.sno
    LEFT JOIN form course
    ON course.cno = scorre.cno
    WHERE course.cname ='数学'
    GROUP BY student.sno,student.sname,student.sage,scorre.sscore
    ORDER BY scorre.sscore desc
    limit 3




    /*查询班里物理成绩及格的男生的学号、姓名以及物理成绩*/
    SELECT a.Sno 学号,a.Sname 名字,b.Sscore 物理成绩
    FROM Student a, Scorre b,Form c
    WHERE a.Sno = b.Sno
        AND c.Cname = '物理'
        AND b.Sscore>60
        AND b.Cno = c.Cno

        
    /*查询各科目的最高分和最低分。(以如下形式显示:课程编号,最低分,最高分)*/
    SELECT max(A.sSCORE),max(B.sSCORE),max(C.sSCORE),max(D.sSCORE),min(A.sSCORE),min(B.sSCORE),min(C.sSCORE),min(D.sSCORE)
    FROM  (SELECT a.Cname, b.Sscore,a.Cno FROM Form a, Scorre b WHERE   a.Cno = b.Cno AND b.Cno = 'C0001')A,
          (SELECT a.Cname, b.Sscore,a.Cno FROM Form a, Scorre b WHERE   a.Cno = b.Cno AND b.Cno = 'C0002')B,
          (SELECT a.Cname, b.Sscore,a.Cno FROM Form a, Scorre b WHERE   a.Cno = b.Cno AND b.Cno = 'C0003')C,
          (SELECT a.Cname, b.Sscore,a.Cno FROM Form a, Scorre b WHERE   a.Cno = b.Cno AND b.Cno = 'C0004')D
     
     

    /*更新李四的英语成绩为90*/

    UPDATE SCORRE
    SET sscore = '90'
    WHERE sno = (select sno from student where sname = '李四')
    AND cno = (select cno from form where cname = '英语')

    下方为实验
    SELECT m.sname
    FROM ( SELECT Sscore,Sname
            FROM   Student a, Scorre b,Form c
            WHERE a.Sno = b.Sno
        AND c.Cname = '物理'
        AND a.Sname='李四'
        AND b.Cno = c.Cno) m
        WHERE Sscore = 45

        
        
    /*删除钱二的物理成绩记录*/
    DELETE
    FROM SCORRE
    WHERE cno = (select cno from Form where Cname = '物理')
    and sno = (select sno from student where Sname = '钱二')



    /*.查询成绩表中最低分的课程是哪个老师教的,最低分成绩是哪个学生的(以如下形式显示:课程名、学生名、分数、老师名)*/
    SELECT c.Cname 课程名, a.Sname 学生名, d.Sscore 分数,b.Tname 老师名
    FROM student a, teacher b,form c,scorre d,(SELECT min(Sscore)AS sc FROM scorre )e
    WHERE e.sc = d.Sscore AND a.Sno = d.Sno AND b.Tno = c.Tno AND c.Cno = d.Cno






  • 相关阅读:
    利用win10自带的虚拟机Hyper-V安装Centos7的步骤教程
    Java元组Tuple介绍与使用
    Fiddler高级用法-设置断点
    Fiddler高级用法-抓取手机app数据包
    Fiddler基础用法-抓取浏览器数据包
    dig 命令
    curl 命令
    vmware虚拟机三种网络连接方式
    解决虚拟机vmware虚拟机安装64位系统“此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态”的问题
    linux 文件系统 xfs、ext4、ext3 的区别
  • 原文地址:https://www.cnblogs.com/TrustBelieve/p/5897203.html
Copyright © 2020-2023  润新知