zoukankan      html  css  js  c++  java
  • 数据库实验2

    1.建学生表语句

    CREATE TABLE  `student` (
    Sno INT NOT NULL ,
    Sname VARCHAR( 20 ) NOT NULL ,
    Ssex VARCHAR( 3 ) ,
    Sage INT( 2 ) NOT NULL ,
    Sdept VARCHAR( 20 ) ,
    Sclass VARCHAR( 30 ) ,
    Intime DATE ,
    PRIMARY KEY ( Sno )
    ) ENGINE = INNODB DEFAULT CHARSET = utf8

       有约束条件的创建表的语句

    ALTER TABLE  `student` 
    ADD CONSTRAINT Sex_rule CHECK (Ssex IN ('','')),
    ADD CONSTRAINT Age_rule CHECK (Sage BETWEEN 15 AND 30),
    ADD CONSTRAINT Enter_University_date_rule CHECK (
    Intime BETWEEN 1923 --30 AND 2015 -11 -05);
     1 CREATE TABLE  `student` (
     2 Sno INT NOT NULL ,
     3 Sname VARCHAR( 20 ) NOT NULL ,
     4 Ssex VARCHAR( 3 ) constraint Sex_rule check(Ssex IN('','')),
     5 Sage INT( 2 ) constraint Age_rule check(Sage BETWEEN 15 AND 30) ,
     6 Sdept VARCHAR( 20 ) ,
     7 Sclass VARCHAR( 30 ) ,
     8 Intime DATE constraint Enter_University_date_rule check(Intime BETWEEN 1923-4-30 AND 2015-11-05),
     9 PRIMARY KEY ( Sno )
    10 ) ENGINE = INNODB DEFAULT CHARSET = utf8;

    1.1数据插入语句

    INSERT INTO  `data`.`student` (
    `Sno` ,`Sname` ,`Ssex` ,`Sage` ,`Sdept` ,`Sclass` ,`Intime`)
    VALUES (
    
    '20009001''葛文卿''女''22''国际贸易''国贸2班''2000-8-29'
    )(
    '20014019''郑秀丽''女''21''会计学''会计1班''2001-9-2'
    )(
    '20023001''刘成凯''男''18''计算机''软件2班''2002-8-27'
    )(
    '20026002''李涛''女''19''电子学''电子1班''2002-8-27'
    )(
    '20023002''沈香娜''女''18''计算机''软件2班''2002-8-27'
    )(
    '20026003''李涛''男''19''计算机''软件1班''2002-8-27'
    )(
    '20023003''肖一竹''女''19''计算机''软件2班''2002-8-27'
    );

    2. 课程表建立

    CREATE TABLE  `course` (
    Cno VARCHAR( 10 ) NOT NULL ,
    Cname VARCHAR( 20 ) NOT NULL ,
    Cpno VARCHAR( 10 ) ,
    Ccredit FLOAT( 1 ) NOT NULL ,
    PRIMARY KEY ( Cno )
    ) ENGINE = INNODB DEFAULT CHARSET = utf8

    2.1 添加数据

    INSERT INTO `data`.`course` (`Cno`, `Cname`, `Cpno`, `Ccredit`) VALUES 
    ('C801', '高等数学', NULL, '4'), 
    ('C802', 'C++语言', 'C807', '3'), 
    ('C803', '数据结构', 'C802', '4'), 
    ('C804', '数据库原理', 'C803', '4'),
    ('C805', '操作系统', 'C807', '4'), 
    ('C806', '编译原理', 'C803', '4'),
    ('C807', '离散数学', NULL, '4');

    3. 创建成绩表

    CREATE TABLE  `grade` (
    Sno INT NOT NULL ,
    Cno VARCHAR( 10 ) ,
    Ggrade INT( 3 ) DEFAULT  '0',
    Ccredit FLOAT( 1 ) NOT NULL ,
    PRIMARY KEY ( Sno, Cno )
    ) ENGINE = INNODB DEFAULT CHARSET = utf8

     3.1 插入数据

    INSERT INTO  `data`.`grade` (
    `Sno` ,
    `Cno` ,
    `Ggrade` ,
    `Ccredit`
    )
    VALUES (
    '20023001',  'C801',  '98',  '4'
    ), (
    '20023002',  'C804',  '70',  '4'
    ), (
    '20026001',  'C801',  '85',  '4'
    ), (
    '20023001',  'C802',  '99',  '3'
    ), (
    '20026002',  'C803',  '82',  '4'
    );

    4.授课表

    CREATE TABLE  `teach` (
    Tname VARCHAR( 10 ) NOT NULL ,
    Cno VARCHAR( 10 ) ,
    Ttime INT( 3 ) DEFAULT  '0',
    Tclass VARCHAR( 20 ) ,
    PRIMARY KEY ( Cno, Tclass )
    ) ENGINE = INNODB DEFAULT CHARSET = utf8

    4.1数据插入

    INSERT INTO  `data`.`teach` (
    `Tname` ,`Cno` ,`Ttime` ,`Tclass`)
    VALUES (
    '苏亚步',  'C801',  '72',  '软件1班'
    ), (
    '苏亚步',  'C801',  '72',  '软件2班'
    ), (
    '王文山',  'C802',  '64',  '软件2班'
    ), (
    '张珊',  'C803',  '72',  '软件2班'
    ), (
    '王文山',  'C804',  '64',  '软件2班'
    );

     

    1.  select * from `student` where Ssex = '' and Sclass = '软件2班';
    
    2.  select * from `student` where Intime < '2002-01-01';
    
    3.  select * from `student` where Sage<19 OR Ssex = '';
    
    4.  select `Cname` from  `course` where Cpno IS NULL;
    
    5.  select `Sno`,`Sname`,`Sdept` from `student` where Sdept not in ('电子学','会计学');
    
    6.  select MAX(Ggrade) from `grade` where Cno = 'C801';
    
    7.  select COUNT(*) '男生总人数', AVG(Sage) '男生平均年龄'from `student` where Ssex = '' ;
        select COUNT(*)  '女生总人数', AVG(Sage)'女生平均年龄' from `student` where Ssex = '' ;
        select Ssex , COUNT(Sno) As Number , AVG(Sage) As AvogofAge from student group by Ssex;
    
    列出选修了一门以上课程的学生学号,及其选修门数   
    8.  select Sno,COUNT(*)'课程数' from grade GROUP BY Cno HAVING COUNT(*)>1; 
    
    9.  select `Sno`,`Sname` from `student` where `Sclass` IN (select `Sclass` from `student` where `Sname` = '沈香娜');//嵌套查询
       select `Sno`,`Sname` from `student` where Sname<>'沈香娜' AND `Sclass` IN (select `Sclass` from `student` where `Sname` = '沈香娜')
    
    统计每一年龄段选修课程的学生人数
    10  SELECT  Sage, COUNT(DISTINCT student.Sno) '人数' FROM student, grade WHERE student.Sno = grade.Sno GROUP BY Sage;

    难题:

    1.在基本表“成绩表”中检索平均成绩最高的学生学号;
     select Sno ,AVG(Ggrade) from `grade` group by Sno having AVG(Ggrade)>=ALL(select AVG(Ggrade) from `grade` group by Sno );
    
    2.SELECT Sage, COUNT( Sno ) AS  '人数' FROM student GROUP BY Sage HAVING COUNT( * ) >2 order by Sage ASC,Sno DESC;
    
    3. 列出选修了全部课程的学生学号和姓名;
       select Sname ,Sno
       from student
       where NOT EXISTS(SELECT * FROM course where not exists(
       select * from grade where grade.Sno = student.Sno and grade.Cno = course.Cno
    ));
    
    4. 查询这样的学生,该生至少选修了学生20026001所选修的全部课程。
    SELECT DISTINCT Sno
    FROM grade gradeX
    WHERE NOT 
    EXISTS (
    
    SELECT * 
    FROM grade gradeY
    WHERE gradeY.Sno =  '20026001'
    AND NOT 
    EXISTS (
    
     SELECT * 
     FROM grade gradeZ
     WHERE gradeZ.Sno = gradeX.Sno
     AND gradeZ.Cno = gradeY.Cno
     )
    )
  • 相关阅读:
    html5 保存图片到服务器本地
    html5 canvas分层
    webstorm配置scss自动编译路径
    微信开发测试号配置
    html5手机网站需要加的那些meta/link标签,html5 meta全解
    css去掉iPhone、iPad默认按钮样式
    如何激活webstorm 11
    min-height在安卓下不起作用
    基于字符串模式的路由路径的一些示例。
    nodeJS搭建本地服务器
  • 原文地址:https://www.cnblogs.com/xs-yqz/p/4900586.html
Copyright © 2011-2022 走看看