1 DAY-03作业

1.1考察内容

  • Mysql数据库简介
  • Navicat操作数据库

1.2 理论题

  1. 请列出几款典型的关系型和非关系型数据库。
    答:关系型数据库:Oracle、MySQL、SQLite、Microsoft SQL server;非关系型数据库:MangoDB、redis

  2. 请列出mysql数据库的特点。
    自己写的:
    答:开源免费。
    答案:
    可移植性好、支持多操作系统、支持多语言、开源社区版本免费、支持多线程

  3. Mysql中常用的数据类型有哪些?
    答:整型int、字符型varchar、日期date、小数decimal

  4. Mysql中常用的约束类型有哪些?
    答案:主键、非空、惟一、默认值、外键

  5. 请指出mysql和navicat两款软件,谁是客户端,谁是服务端,为什么?
    答:MySQL是客户端,因为其可以操作数据,对数据进行增删改查。Navicat是服务端,因为其作用是展示MySQL的数据。 (大错特错)

答:MySQL是服务端,Navicat是客户端。
因为MySQL是用来存储数据的,而Navicat是用来编写SQL语句,并发送给mysql执行并展示返回的数据。

1.3 实操题

  1. 通过SQL创建雇员表employees,字段要求如下:姓名(长度为10), 年龄,身高(保留小数点2位) 。
create table employees(id int unsigned primary key auto_increment,name varchar(10),age int(5),height decimal(5,2));
  1. 通过SQL在employees中新增数据行(张三,18,170.5)。
insert into employees values('张三',18,170.5);
  1. 通过navicat修改张三的年龄为20。
update employees set age=20 where name='张三' ;
  1. 通过navicat删除张三的这条记录。
delete from employees where name='张三';

在表中选择该行的记录右击删除。

  1. 通过SQL删除employees表。
drop table employees;
-- 或者
drop table if exists employees;

1.4 预习题

  1. 重复完成实操题中的第6、7题后,查询出姓名为张三的学生记录
select * from employees where name='张三';

1.5 总结

  1. 更新数据语法:updata 表名 set 字段=... where...
  2. 删除记录语法:
delete from 表名 where...

2 DAY-04作业

2.1 考察内容

  • SQL简单数据操作(增、删、改、查)
  • SQL条件查询
  • SQL聚合函数
  • SQL分组、分页查询

2.2 理论题

  1. delete和drop删除命令有什么区别?
    答:delete 是删除表中的数据,drop是删除表。Delete和drop都是物理删除。
    如:
delete from students where id = 6;
drop table students;

答案:delete可以删除部分或者全部表的记录,删除之后,主键的值不会重1开始,drop删除的是整个表的结构及数据。

  1. where条件查询有哪几种方式?
    答:5种查询方式。分别是:
    ① 条件查询(比较运算符:>>=<<=!=<>=)、
    ② 条件查询(逻辑运算符:andornot)、
    ③ 模糊查询(关键字like:多字符%、单个字符_)、
    ④ 范围查询(非连续:in、连续:between…and…)、
    ⑤ 空判断(is nullis not null)。

  2. MySQL中常用的聚合函数哪些?
    答:count()max()min()avg()sum()

  3. 起别名用什么命令,可以给什么起别名,分别有什么注意点?
    答:命令:as

给表起别名:select * from students as stu;
给字段起别名:select studentNum as Snum from students;

2.3 实操题

  1. 查询1班除外其他班级学生的平均年龄、最大年龄、最小年龄。
select avg(age),max(age),min(age) from students where class !='1班';--或者
select avg(age),max(age),min(age) from students where class <>'1班';
  1. 查询学生“百里守约”的基本信息。
select * from students where name ='百里守约';
  1. 查询学生“百里守约”或“百里玄策”的基本信息。
select * from students where name ='百里守约' or name= '百里玄策';
  1. 查询姓“张”学生的姓名,年龄,班级。
select name, age, class from students where name like '张%';
  1. 查询姓名中含有“约”字的学生的基本信息。
select * from students where name like '%约%';
  1. 查询姓名长度为三个字,姓“孙”的学生的学号,姓名,年龄,班级,身份证号。
select studentNum, name, age, class, card from students where name like='孙__';
  1. 查询姓“百”或者姓“孙”的学生的基本信息。
select * from students where name like ’百%’ or name like '孙%';
  1. 查询姓“百”并且家乡是“山西”的学生信息。
select * from students where name like '百%' and hometown='山西';
  1. 查询家乡是“北京”、“新疆”、“山东”或者“上海”的学生的信息。
-- 太麻烦了
select * from students where hometown='北京' or hometown='新疆' or hometown='山东' or hometown='上海';--简单写法
select * from students where hometown in ('北京','新疆','山东','上海');
  1. 查询姓“孙”,但是家乡不是“河北”的学生信息。
select * from students where name like '孙%' and hometown!='河北';
  1. 查询家乡不是“北京”、“新疆”、“山东”、“上海”的学生的信息。
-- 错的
select * from students where not hometown in('北京','新疆','山东','上海');-- 对的!!!!!!
select * from students where hometown not in ('北京','新疆','山东','上海');
  1. 查询全部学生信息,并按照“性别”排序。
select * from students group  order by sex;
  1. 查询现有学生都来自于哪些不同的省份。
select hometown from students group by hometown;(没去重)select distinct hometown from students;
  1. 查询所有男生,并按年龄升序排序。
select * from students where sex='男' order by age;
  1. 统计共有多少个学生。
select count(*) from students;
  1. 统计年龄大于20岁的学生有多少个。
select count(*) from students where age>20;
  1. 统计男生的平均年龄。
select avg(age) from students group by sex='男';
  1. 查询1班学生中的最大年龄是多少。
select max(age) from students where class ='1班';
  1. 统计2班男女生各有多少人。
select sex, count(*) from students where class='2班' group by sex;select sex, class, count(*) from students where class='2班' group by sex;
  1. 统计每个班级中每种性别的学生人数,并按照班级升序排序。
select sex, count(*) from students group by sex order by class;select class, sex, count(*) from students group by class,sex order by class;

2.4 预习题

  1. 多表查询有哪几种常用的方式?
    答:内连接、左连接、右连接。

2.5 总结

  1. like后面不需要=
  2. Where不能加聚合函数,having可以
  3. Where可以和having一起用
  4. Distinct 在字段前面
  5. 字段 not in (…)
  6. Group by 字段1,字段2…
  7. Where … order by …中间不需要and

3 DAY-05作业

  • 联表查询(内连接、左连接、右连接)
  • 自关联
  • 子查询

3.1 理论题

  1. 内连接、左连接、右连接有什么区别?
    答:
    内连接:表与表的交集;
    左连接:以左表为标准与右表连接,右表没有的数据记为null;
    右连接:以右表为标准与左表连接,左表没有的数据记为null。

  2. 自关联用在什么场景中?
    答:用在省市区的地方查询。
    答案:在同一张表中,用两个字段来表示记录之间的层级关系时,可以使用自关联。比如地址信息中的,省、市的信息。

3.2 实操题

添加数据

  1. 建学生信息表student
create table student
( sno varchar(20) not null primary key, sname varchar(20) not null, ssex varchar(20) not null, sbirthday datetime, class varchar(20)
);
  1. 建立教师表
create table teacher
( tno varchar(20) not null primary key, tname varchar(20) not null, tsex varchar(20) not null, tbirthday datetime, prof varchar(20), depart varchar(20) not null
);
  1. 建立课程表course
create table course
( cno varchar(20) not null primary key, cname varchar(20) not null, tno varchar(20) not null, foreign key(tno) references teacher(tno)
);
  1. 建立成绩表
CREATE TABLE score ( sno VARCHAR (20) NOT NULL , FOREIGN KEY (sno) REFERENCES student (sno), cno VARCHAR (20) NOT NULL, FOREIGN KEY (cno) REFERENCES course (cno), degree DECIMAL
);
  1. 添加学生信息
insert into student values('108','曾华','男','1977-09-01','95033');
insert into student values('105','匡明','男','1975-10-02','95031');
insert into student values('107','王丽','女','1976-01-23','95033');
insert into student values('101','李军','男','1976-02-20','95033');
insert into student values('109','王芳','女','1975-02-10','95031');
insert into student values('103','陆君','男','1974-06-03','95031');
  1. 添加教师表
insert into teacher values('804','李诚','男','1958-12-02','副教授','计算机系');
insert into teacher values('856','张旭','男','1969-03-12','讲师','电子工程系');
insert into teacher values('825','王萍','女','1972-05-05','助教','计算机系');
insert into teacher values('831','刘冰','女','1977-08-14','助教','电子工程系');
  1. 添加课程表
insert into course values('3-105','计算机导论','825');
insert into course values('3-245','操作系统','804');
insert into course values('6-166','数字电路','856');
insert into course values('9-888','高等数学','831');
  1. 添加成绩表
insert into score values('103','3-245','86');
insert into score values('105','3-245','75');
insert into score values('109','3-245','68');
insert into score values('103','3-105','92');
insert into score values('105','3-105','88');
insert into score values('109','3-105','76');
insert into score values('103','3-105','64');
insert into score values('105','3-105','91');
insert into score values('109','3-105','78');
insert into score values('103','6-166','85');
insert into score values('105','6-166','79');
insert into score values('109','6-166','81');

题目

  1. 查询Student表中的所有记录的Sname、Ssex和Class列。
select Sname, Ssex, class from Student;
  1. 查询教师所有的单位即不重复的Depart列。
select distinct depart from teacher;
  1. 查询Student表的所有记录。
select * from student;
  1. 查询Score表中成绩在60到80之间的所有记录。
select * from score where degree between 60 and 80;select * from score where degree>=60 and degree <=80;
  1. 查询Score表中成绩为85,86或88的记录。
select * from score where degree in (85,86,88);
  1. 查询Student表中“95031”班或性别为“女”的同学记录。
select * from student where class = '95031' or ssex='女';
  1. 以Class降序查询Student表的所有记录。
select * from student order by class desc;
  1. 以Cno升序、Degree降序查询Score表的所有记录。
select * from score order by Cno,degree desc;
  1. 查询“95031”班的学生人数。
select count(*) from student where class='95031';

10. 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)

-- 子查询
select sno,cno from score where degree in (select max(degree) from score);-- 排序
select sno,cno from score order by degree desc limit 0,1;

11. 查询每门课的平均成绩。

select course.cname,avg(score.degree) from course
left join score on course.cno=score.cno
GROUP BY course.cno;

12. 查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

select avg(degree) from score
where cno like '3%'
group by cno
having count(cno)>5;
  1. 查询分数大于70,小于90的Sno列。
select sno from score where degree between 70 and 90;select sno,cno from score where degree>70 and degree<90;
  1. 查询所有学生的Sname、Cno和Degree列。
select student.sname,score.sno,score.degree from student
left join score on student.sno=score.sno;
  1. 查询所有学生的Sno、Cname和Degree列。
select student.sno,course.cname,score.degree from student
left join score on student.sno=score.sno
left join course on score.cno=course.cno;
  1. 查询“95031”班学生的平均分。
select class,avg(degree) from score
inner join student on student.sno = score.sno
where class='95031'

17. 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。

select * from student
inner join score on student.sno=score.sno
where score.cno='3-105' and score.degree>(SELECT max(degree) from score
where sno='109' and cno='3-105');
  1. 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
    – 与17一样

3.3 总结

总结

  1. Where 字段>(查询语句)
  2. Between可以这样用between >12 and <10
  3. Select distinct 字段 from 表,distinct在前
  4. 10,11,12重点看

【数据库】03 数据库作业问答相关推荐

  1. MySQL数据库03 数据库查询语句汇总

    DQL 查询 基础数据 CREATE TABLE `dept`(`deptno` INT(2) NOT NULL, `dname` VARCHAR(14),`loc` VARCHAR(13),CONS ...

  2. MYSQL:餐厅点菜、管理员工的数据库。大学数据库课程大作业(初学者,入门,用的基础知识)

    mysql数据库:点餐系统和管理员工的数据库 大二做的数据库课程大作业,作者是初学者,分享给大家参考,内容参考了很多篇数据库文章才拼凑出来,所以本数据库很粗糙,很简单,需要的同学复制粘贴然后自行修改交 ...

  3. 在SQLserver数据库里设置作业的步骤

    在SQLserver数据库里设置作业(对数据库的表定期进行数据清理)的步骤 1.首先,要打开sql server代理的服务,在我的电脑,右键管理的服务打开,SQL Server 代理 (MSSQLSE ...

  4. oracle数据库作业1,北京语言20秋《Oracle数据库开发》作业1(100分)

    -[北京语言大学]20秋<Oracle数据库开发>作业1 试卷总分:100    得分:100 第1题,Orcacle提供了(),用于支持采用向导方式创建数据库. A.SQL*Plus B ...

  5. 北语20春oracle数据开发2,北语20春《Oracle数据库开发》作业3题目【标准答案】

    20春<Oracle数据库开发>作业3 试卷总分:100  得分:100 一.单选题 (共 17 道试题,共 85 分) 1.当启动Oracle实例,如果不需要装置数据库,可以使用如下() ...

  6. 关系型数据库和MySQL作业及答案

    关系型数据库和MySQL作业 作业:使用下面的SQL在MySQL中创建数据库.二维表并插入数据,然后完成下面的查询操作. create database hrs default charset utf ...

  7. [数据库03]-约束(唯一性-主键-外键/存储引擎/事务/索引/视图/DBA命令/数据库设计三范式

    [数据库03]-约束(唯一性-主键-外键)/存储引擎/事务/索引/视图/DBA命令/数据库设计三范式 一.约束 1.1 唯一性约束(unique) 1.2 主键约束 1.3 外键约束 二.存储引擎 2 ...

  8. 西工大数据库实验大作业 火锅店菜品管理系统

    西工大数据库实验大作业 火锅店菜品管理系统 文章目录 西工大数据库实验大作业 火锅店菜品管理系统 前言: 一.大作业简单的需求分析: 二.数据流图: 三.数据字典: 四.E-R图: 五.关系模式设计: ...

  9. 数据库入门第一次作业

    数据库入门第一次作业 1.在某大学的<<单身群体资料库>>中,用如下表来存储学生信息.其中,用户信息.星座信息和血型信息分别采用Users.Star和Blood三个表来保存,其 ...

  10. mysql的第一次作业_数据库入门第一次作业 - osc_2frf70qv的个人空间 - OSCHINA - 中文开源技术交流社区...

    数据库入门第一次作业 1.在某大学的<>中,用如下表来存储学生信息.其中,用户信息.星座信息和血型信息分别采用Users.Star和Blood三个表来保存,其中Users表引用了Star和 ...

最新文章

  1. sola ris 简单命令
  2. Keras学习笔记---保存model文件和载入model文件
  3. spcomm控件的使用
  4. 我的世界服务器设置op显示,我的世界设置op权限 | 手游网游页游攻略大全
  5. java- WatchService监控
  6. mysql无法添加或更新子行_MySQL错误1452-无法添加或更新子行:外键约束失败?
  7. 华为透露中国首个开源基金会将于近期正式运营!
  8. Ubuntu 17安装Virtual Box
  9. 护士如何预防职业病:下肢静脉曲张-健康小常识
  10. spring+ibatis+注解开发
  11. 【UOJ#310】【UNR#2】黎明前的巧克力(FWT)
  12. 看完这一篇,解决Mac电脑90%的软件烦恼
  13. 论文笔记_S2D.46_2013-3DV_基于点融合的动态场景实时三维重建
  14. gitlab主备同步_gitlab实现主备切换集群
  15. gridcontrol值为0时设置为空_汇总:MySQL 8.0 运维便捷命令
  16. 1001系列之案例0003如何对欧洲人口普查数据集整理挖掘
  17. 13个有意思的网站,你一定要看
  18. centos安装Docker与使用构建业务镜像挂载卷harbor仓库的高可用及网络模式和资源限制介绍...
  19. Medical image segmentation
  20. 安卓逆向 -- Xposed模块编写

热门文章

  1. 看财经郎眼,深感数据挖掘意义重大
  2. android tablayout 自定义,TabLayout用法详解及自定义样式
  3. 【Scratch-动作模块】位置移动分析(二)
  4. Java获取世界各国各城市代码_java获取中国城市代码 中国城市ID
  5. WPF---->自定义控件添加Popup弹出框
  6. GAN、DCGAN、WGAN、SRGAN
  7. idea破解教程,实测可用!
  8. 同城本地信息发布系统
  9. icp许可证办理的条件
  10. 何红辉设计模式之六大原则