什么是索引(Index)?
索引是一种数据结构,排好序的快速查找数据结构,在于提高查找数据的效率
索引一般存储在磁盘上,平常所说的索引,都是指B树索引。
为什么要设计索引?
索引分类:
单值索引—-
唯一索引—-索引列的值必须唯一,允许空值
复合索引—-
索引结构:
一般在什么字段上建索引(过滤数据最多的字段)
基本语法:
创建索引 create [unique] index indexName on mytable(columnname(length));
删除 drop index【indexName】 on mytable;
查看 show index from table_name:
多表联合查询
select from table1 where id>21 union select from table2 where id>21 union select * from table3 where id>21;
一般先写谁列名就是谁的 可以起别名。
起别名:select last_name as 姓,first_name as 名 from empees;
数据结构:B Trees和B+ Trees、哈希
什么是事务?
一个或一组SQL语句组成一个执行单元,这个执行单元要么全部执行,要么全部不执行。
三、详细主从复制过程
(1)Master开启bin-log功能,binlog日志文件用于记录数据库的增删改操作。
(2)需要开启三个线程,Master:I/O线程;Slave:I/O线程,SQL线程。
(3)Slave start;通过I/O线程连接Master,并且请求某个bin-log,position之后的内容。
(4)Master服务器收到Slave I/O线程发过来的日志请求信息,然后Master I/O线程将bin-log内容、position返回给Slave IO线程。
(5)Slave服务器收到bin-log日志内容,将bin-log日志内容写入到relay-log中继日志,创建一个master.info文件,该文件记录master IP、用户名、密码、master bin-log名称、bin-log position。
从机配置:
建表
create table ‘student’(
‘s_id’ vachar(20),
‘s_name’ vachar(20) not null default ‘’,
‘t_id’ vachar(20) not null default ‘’,
primary key(‘s_id’)
);
插入
insert into student values(‘01’,’tom’,’1990-0-0’,’男’);
insert into score values(‘01’,’01’,80);
查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号。
内连接查询同一学生
select from score a inner join score b on (a.s_id = b.s_id and a.c_id=’01’ and b.c_id = ‘02’ and a.s_score>b.s_score);
select from score a inner join score b where(a.s_id = b.s_id and a.c_id=’01’ and b.c_id=’02’ and a.s_score>b.s_score);
2、查询平均成绩大于60分的学生的学号和平均成绩
select s_id,avg(s_score) as avgs from score group by s_id having avg(s_score)>60;
select a.s_id,a.s_score from (select s_id,avg(s_score) as avgs from score group by s_id) as a where a.avgs>60;
— 3、查询所有学生的学号、姓名、选课数、总成绩
select stu.sid,stu.s_name,count(stu.s_id),sum(sc.s_score) from student as stu inner join score as sc on sc.s_id=stu.s_id group by (s_id);
4、查询姓“张”的老师的个数
select count(t_id) from teacher where t_name like “张%”;
5.查询没学过“张三”老师课的学生的学号、姓名(重点)sc.s_id,stu.s_name
四表查询
select s_id,s_name from student where s_id not in (select s_id from score join course on score.c_id=course.c_id jion tescher on course.t_id = teacher.t_id where t_name=”张三”);
select s _id,s_name from student where s_id not in(select s_id score join courser on score.c_id=course.id join teacher on course.t_id = teacher.t_id where t_name=’’)
查询学过“张三”老师所教的所有课的同学的学号、姓名
select s_id,s_name from student where s_id in (select s_id from score join course on score.c_id=course.c_id jion teacher on teacher.t_id=course.t_id where t_name=”张三”);
7、查询学过编号为“01”的课程并且也学过编号为“02”的课程的学生的学号、姓名
select student.s_id,student.s_name from student where s_id in(select s_id from score where score.c_id=’01’ ) and s_id in(_select s_id from score where score.c_id=’02’);
— 8、查询课程编号为“02”的总成绩
select sum(s_score ) from score wher c_id=’02’;
— 9、查询所有课程成绩小于60分的学生的学号、姓名
select student.s_id,student.s_name form student where student.s_id not in(select score.s_score from score wher s_score>=60);
— 10、查询没有学全所有课的学生的学号、姓名(题目没有明确要不要完全没学的学生)
select s_id,s_name from student where s_id not in (select s_id form score group by s_id having count(*) = 3);
— 11、查询至少有一门课与学号为“01”的学生所学课程相同的学生的学号和姓名
select student.s_id,student,s_name from score join student on student.s_id=score.s_id where score.c_id in (select c_id from score where s_id=’01’) and score.s_id!=’01’ group by score.s_id;
— 12、查询和“01”号同学所学课程完全相同的其他同学的学号
select sid from student where sid in (select sid from score where sid=’01’)
13.把“Score”表中“张三”老师教的课的成绩都更改为此课程的平均成绩(难点)
update score as A join (select __avg(s-score) as T,score.c-id from score join course on score.c-id=course.c-id join tecaher on course.t-id = teahcer.t-id where t-name=’zhansan’ group by c-id ) as B __on A.c-id=B.c-id set A.score=B.T;
15、删除学习“张三”老师课的SC表记录
delete from score where c-id =(select c-id from course join teacher on course.t-id=teacher.t-id);
— 16.检索”01”课程分数小于60,按分数降序排列的学生信息
select from score join student on score.s-id=student.s-id where score.s-score<60 and score.c-id='01' order by score.s-score desc;
17、按平均成绩从高到低显示所有学生的“数据库”(c_id=’04’)、“企业管理”(c_id=’01’)、“英语”(c_id=’06’)三门的课程成绩,按如下形式显示:学生ID,数据库,企业管理,英语,有效课程数,有效平均分
18、查询各科成绩最高和最低的分: 以如下的形式显示:课程ID,最高分,最低分
select c-id ‘课程id’,max(s-score) ‘最高分’,min(s-score) ‘最低分’ from score group by c-id;
19、按各科平均成绩从低到高和及格率的百分数从高到低排列,以如下形式显示:课程号,课程名,平均成绩,及格百分数
select course.c-id ‘课程号’,course.c-name ‘课程名’,avg(sc.s-score) ‘平均成绩’,
concat((select count() from score sa sc1 where sc1.s-score>=60 sc1.c-id=sc.c-id)/count()100,”%”) ‘格百分数’ from score as sc join course on course.c-id =sc.c-id group by sc.c-id order by avg(sc.s-score);
— 21、查询不同老师所教不同课程平均分从高到低显示
select teacher.t-name,course.c-id,course.c-name,avg(sc.score),
select name,min(time),max(time) from tab