zoukankan      html  css  js  c++  java
  • mysql数据库 学习(实训练习 二)

    测试、练习

    1、用sql建表student:
    学号 姓名 性别 生日 专业
    1 张三 男 1980-12-03 1
    2 王武 女 1980-09-22 3
    3 李四 女 1981-03-04 2
    4 赵六 女 1981-05-24 1
    5 张建国 男 1980-06-02 4
    6 赵娟 女 1980-08-30 2

    -- 创建数据库
    create database webapp2 charset utf8mb4;
    -- 创建用户名、密码
    create user'webapp2'@'localhost'identified by'webapp2';
    -- 授权
    grant all on webapp2.*to'webapp2'@'localhost';
    -- 用用户名、密码登录
    mysql -uwebapp2 -pwebapp2
    -- 创建表
    create table student
    (
    	s_id int not null primary key auto_increment,
    	s_name varchar(20),
    	s_sex varchar(5),
    	s_birthday date,
    	s_professional int
    );
    -- 添加数据
    insert into student(s_name,s_sex,s_birthday,s_professional) 
    values
    ('张三','男','1980-12-03',1),
    ('王武','女','1980-09-22',3),
    ('李四','女','1980-03-04',2),
    ('赵六','女','1980-05-24',1),
    ('张建国','男','1980-06-02',4),
    ('赵娟','女','1980-08-30',2);
    

    2、用sql建表course:
    学号 学期 课程编号 分数
    1 1 2 92.0
    1 2 2 76.0
    2 1 3 60.0
    2 2 3 90.0
    3 4 1 66.0
    3 4 2 NULL
    3 4 4 81.0
    3 4 6 95.0
    5 1 2 67.0
    6 1 2 50.0
    6 2 2 87.0
    6 2 3 86.0

    -- 创建表
    create table course
    (
    	c_id int,
    	c_semester int,
    	c_course int,
    	c_score double(3,1),
    	constraint fk_student_course foreign key (c_id) references student(s_id)
    );
    -- 添加数据
    insert into course(c_id,c_semester,c_course,c_score)
    values
    (1,1,2,92),
    (1,2,2,76),
    (2,1,3,60),
    (2,2,3,90),
    (3,4,1,66),
    (3,4,2,null),
    (3,4,4,81),
    (3,4,6,95),
    (5,1,2,67),
    (6,1,2,50),
    (6,2,2,87),
    (6,2,3,86);
    

    要求:不考虑考试科目和学期,列出不同学生所有考试成绩中,成绩为优的分数的累加值,和成绩为良的分数的平均值。优和良的界线是90分和80分。

    select * from student s left join course c on c.c_id = s.s_id
    union all
    select * from student s right join course c on c.c_id = s.s_id
    
    
    select sum(c_score) as '累加值' from course c,student s WHERE c.c_id = s.s_id and c_score>=90;
    select avg(c_score) as '平均值' from course c,student s WHERE c.c_id = s.s_id and 80<c_score<90;
    
    
    select s_name,sum(c_score) as '累加值' from course c,student s WHERE c.c_id = s.s_id and c_score>=90 GROUP BY s_name;
    select s_name,avg(c_score) as '平均值' from course c,student s WHERE c.c_id = s.s_id and 80<c_score<90 GROUP BY s_name;
    
    
    
    

    1.建表:
    Student(
    学号:s_id 整型 主键 ,
    姓名:name 字符型 ,
    电话:Tel 字符型,
    学历:Content 字符型,
    毕业日期:Date 日期型,
    教师编号:teacher_id 整型,外键)

    -- 创建表
    create table student02
    (
    	s_id int not null primary key auto_increment,
    	name varchar(20),
    	tel varchar(20),
    	content varchar(20),
    	date date,
    	teacher_id int,
    	constraint fk_student02_teacher01 foreign key (teacher_id) references teacher(t_id)
    );
    
    

    2.插入记录:

    s_id Name Tel Content Date teacher_id

    1 张三 13333663366 大专毕业 2006-10-11 1
    2 张三 13612312331 本科毕业 2006-10-15 2
    3 张四 021-55665566 中专毕业 2006-10-16 3
    4 张五 021-55665566 大专毕业 2006-10-16 2
    5 张六 021-55665566 大专毕业 2006-10-18 3
    6 张七 021-55665566 中专毕业 2006-10-12 1

    -- 添加数据
    insert into student02(name,tel,content,date,teacher_id) 
    values
    ('张三',13333663366,'大专毕业','2006-10-11',1),
    ('张三',13612312331,'本科毕业','2006-10-15',2),
    ('张四',021-55665566,'中专毕业','2006-10-16',3),
    ('张五',021-55665566,'大专毕业','2006-10-16',2),
    ('张六',021-55665566,'大专毕业','2006-10-18',3),
    ('张七',021-55665566,'中专毕业','2006-10-12',1);
    

    3.建表:
    Teacher(
    教师编号:t_id 整型 主键 ,
    姓名:name 字符型 ,
    年龄:age 整型,
    地址:address 字符型)

    -- 创建表
    create table teacher(
    	t_id int not null primary key auto_increment,
    	name varchar(20),
    	age int,
    	address varchar(20)
    );
    

    2.插入记录:
    t_id name age address

    1 张老师 38 成都
    2 李老师 42 北京
    3 王老师 35 上海

    -- 添加数据
    insert into teacher
    (name,age,address) 
    values
    ('张老师',38,'成都'),
    ('李老师',42,'北京'),
    ('王老师',35,'上海');
    

    要求:

    (a) 有一新记录(小王 13254748547 高中毕业 2007-05-06 1)请用SQL语句新增至表student中;

    insert into student02
    (name,tel,content,date,teacher_id) 
    values
    ('小王','13254748547','高中毕业','2007-05-06',1);
    

    (b)请统计不同教师所教授学生的总数以及学历为大专以上的学生总数,并按照教师姓名进行分组显示;

    select t.name,count(*) from student s left join teacher t on s.teacher_id = t.t_id where s.content like '本科%' group by t.t_id;
    
    select t.name as '教师姓名', COUNT(s.name) as '大专以上人数' from student02 s,teacher t where s.teacher_id = t.t_id and (s.content <> '大专毕业' and s.content <> '高中毕业' and s.content <> '中专毕业')   GROUP BY t.name HAVING COUNT(s.name);
    
    
  • 相关阅读:
    易宝支付Demo,生产中封装成简洁的代付接口,不用request如何获取项目运行时的真实路径(转)
    java之IO流的关闭
    Java IO包装流如何关闭?
    qt5.9模块
    九款免费轻量的 AutoCAD 的开源替代品推荐
    QT pro 添加带空格的路径以及添加库文件的正确方法
    QT添加openssl的方法
    手机芯战!麒麟与骁龙上演难分胜负的技术竞速赛(2013以后,芯片和基带都集成到一起去了)
    使用redis缓存加索引处理数据库百万级并发
    TF.Learn
  • 原文地址:https://www.cnblogs.com/d534/p/15239244.html
Copyright © 2011-2022 走看看