三.database阶段回顾

阶段回顾:

1.mysql:文件管理软件
2.三部分:
  服务端
  sql语句
  客户端
3.客户端
  mysql
  navicat
4.授权操作
  用户操作
  授权操作
5.sql语句
  数据库操作
    create database xx default charset utf8;
    drop database xx;
  数据表
    列
      数字 decimal
      整数
      小数
      字符串
      时间 datatime
      其他:引擎,字符表,起始值
      主键,一个表只能有一个主键,非空且唯一,自增。主键索引。索引是加速查找
      唯一索引:独立存在,可以和外键联合起来,可以为空,有加速查找特性。
      外键:具有约束功能,解决重复文字的问题
        一对多
        一对一
        多对多:至少要3张表
      数据行:

        查
        in not in
        between and
        limit
        group by having
        order by
        like "%a"
        left join xx on 关系
        笛卡尔积关系
          a表三条数据:1 2 3
          select * from a as a1,a as a2;
          产生9条冗余数据:
          1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3
        临时表:保持临时数据,将结果再进行操作

 练习:

7.查找既选择课程1又选择课程2的学生ID与姓名
select * from score where course_id=1 and course_id=2;
同一列既是1又是2这么写有问题
select * from score where course_id=1 or course_id=2
select * from score where course_id=1 or course_id=2 group by student_id having count(student_id)>1;
select student_id from score where course_id=1 or course_id=2 group by student_id having count(student_id)>1;

select score.student_id,student.sname from score
left join student on score.student_id=student.sid
where course_id=1 or course_id=2 group by student_id having count(student_id)>1;

8.查询学过xx老师的所有课程,的学生学号,姓名
select * from course
left join teacher on course.tearch_id=teacher.tid
where teacher.tname="空空";

select * from course
left join teacher on course.tearch_id=teacher.tid
where teacher.tname="空空";

select student_id from score where course_id in
(
select cid from course
left join teacher on course.tearch_id=teacher.tid
where teacher.tname="空空"
)
group by student_id having count(course_id)=
(
select count(cid) from course
left join teacher on course.tearch_id=teacher.tid
where teacher.tname="空空"
)

10.查询成绩小于60的学生,的学生学号,姓名
select * from score
left join student on score.student_id=student.sid where score.number>60

select * from score
left join student on score.student_id=student.sid where score.number>60
group by student_id

select score.student_id,student.sname from score
left join student on score.student_id=student.sid where score.number>60
group by student_id

distinct可以分组去重,但是使用group by也可以去重,最好用group by,distinct效率不高
select distinct student_id from score
left join student on score.student_id=student.sid where score.number>60

posted on 2022-08-20 17:18  老憨豆  阅读(18)  评论(0编辑  收藏  举报