10.三表,索引

Student学生表(学号、姓名、性别、年龄、编辑)

Course课程表(编号、课程名称)

sc选课表(选课编号、学号、课程编号、成绩)

create table student(

stu_no int,

stu_name varchar(10),

-- sex char(1),

-- age int(3),

-- edit varchar(20) )

-- DEFAULT charset=utf8;

-- insert into student values

(1,'wang','男',21,'hello'),

-- (2,'小明','女',22,'haha2'),

-- (3,'hu','女',23,'haha3'),

-- (4,'li','男',25,'haha4');

-- create table course(

-- c_no int,

-- c_name varchar(10) )

-- DEFAULT charset=utf8;

-- insert into course values

-- (1,'计算机原理'),-

-- (2,'java'),

-- (3,'c'),

-- (4,'php'),

-- (5,'py');

-- #drop table sc;

-- create table sc(

-- sc_no int,

-- stu_no int,

-- c_no int,

-- score int(3))

-- DEFAULT charset=utf8;

-- insert into sc values

-- (1,1,1,80),

-- (2,2,2,90),

-- (3,2,1,85),

-- (4,2,3,70),

-- (5,2,4,95),

-- (6,2,5,89);

=============================================

select * from student;
select * from course;
select * from sc;

学生表: student; as a 表

选课表: as b 表

成绩表 as c表

三表连接的格式:

1、

格式:select * from 表1,表2,表3 where 表1.关联字段1=表3.关联字段3 AND 表2.关联字段2=表3.关联字段 3;

案例:select * from student as a ,course as b,sc as c where a.stu_no=c.stu_no AND b.c_no=c.c_no ;

2、

三表内连接

内连接

格式:

select * from 表1 INNER JOIN 表3 on 表1.关联字段1=表3.关联字段3 INNER JOIN表2 on 表2.关联字段2=表3.关联字段3

案例:select * from student as a INNER JOIN sc as c on a.stu_no=c.stu_no INNER JOIN course as b on b.c_no=c.c_no

3、三表左连接

格式:select * from 表1 left JOIN 表3 on 表1.关联字段1=表3.关联字段3 left JOIN表2 on 表2.关联字段2=表3.关联字段3

案例:select * from student as a left JOIN sc as c on a.stu_no=c.stu_no left JOIN course as b on b.c_no=c.c_no

4、三表右连接

格式:格式:select * from 表1 right JOIN 表3 on 表1.关联字段1=表3.关联字段3 right JOIN表2 on 表2.关联字段2=表3.关联字段3

案例:select * from student as a right JOIN sc as c on a.stu_no=c.stu_no right JOIN course as b on b.c_no=c.c_no

5、先合两表在和一表

格式:select * from (select * from 表1 right JOIN 表3 on 表1.关联字段1=表3.关联字段3 ) 临时表s inner 表2 on 临时表. 字段3=表2.字段2

案例:
select * from (select c.stu_no,stu_name,sex,age,edit,sc_no,c_no,score from student as a right JOIN sc as c on a.stu_no=c.stu_no ) s INNER JOIN course as b on s.c_no =b.c_no ;

==========================================

练习:

1)写一个SQL语句,查询选修了“计算机原理”的学生学号和姓名

方法:SELECT a.stu_no,a.stu_name from student a,course b,sc c where a.stu_no=c.stu_no and b.c_no=c.c_no AND c_name='计算机原理'

(2)写一个SQL语句,查询“小明”同学选修的课程名称

方法: SELECT c_name FROM student a,course b,sc c where a.stu_no=c.stu_no and b.c_no=c.c_no and stu_name='小明'

(3)写一个SQL语句,查询选修了5门课程的学生学号和姓名

方法1:SELECT a.stu_name,a.stu_no FROM student a,course b,sc c where a.stu_no=c.stu_no and b.c_no=c.c_no group by stu_name HAVING count(stu_name)=5,

方法2:

SELECT a.stu_name ,a.stu_no FROM student a,course b,sc c where a.stu_no=c.stu_no and b.c_no=c.c_no group by a.stu_no HAVING count(a.stu_no)=5

注意点:

1、在合表的过程中右重复列的情况,请注意显示的字段

select * from (select a.*,sc_no,c_no ,score from student a inner JOIN sc c on a.stu_no=c.stu_no)s ,course b where s.c_no=b.c_no

索引

一、索引的介绍

1、什么是索引?

(1)定义:索引是一种数据结构

一个索引在存储的表中的数据结构;

(2)索引是在表的字段上创建的

(3)索引包含了一列值,这个值保存在一个数据结构中

2、索引作用?

(1)保证数据记录的唯一性

(2)实现表与表之间的参照性

(3)减少排序和分组的时间(例如在使用order by ,group by 查询语句中进行数据检索)
(4)可以使用索引快速访问数据库中指定信息

3、索引的缺点?

(1)索引要占物理内存

(2)索引对表进行增删改查,索引要动态维护,降低数据的维护速度

4、索引的分类

(1)普通索引

index 简称 mul 最基本的索引,没有任何限制

(2)主键索引

primary key 简称 pri 是一种唯一索引,不能为空

(3)唯一索引

unique 简称 uni 是一种唯一索引,可为空,一个表中可以有多个唯一索引


以下作为了解下:

(4)全文索引

(5)组合索引

(6)单列索引

(7)聚焦索引

(8)非聚焦索引

==========================================

二、索引的应用

1、索引的查询

方法一:

格式1:

格式:show INDEX from 表名;

案例:show INDEX from emp ;

方法二:

格式:show keys from 表名;

案例:show KEYS from student2 ;

(2)查看表结构,通过表结构查看索引

desc 表名

(3)创建普通索引

第一种情况:索引名和字段名不一致

格式:ALTER table 表名 add INDEX 索引名(字段名);

案例:ALTER table student2 add INDEX sym(sex);

简写:mul

第二种情况:索引名和字段名一致

格式:ALTER table 表名 add INDEX (字段名);

案例:ALTER table student2 add INDEX (age);

(4)唯一索引(唯一,为空,在一个表可以有多个唯一索引)

单词:unique

简写:uni

第一种情况:添加唯一索引名和字段名不一致

格式:

alter table 表名 add UNIQUE 索引名(字段名)

案例:

alter table student2 add UNIQUE aa(name)

第二种情况:添加唯一索引名和字段名一致

格式:

alter table 表名 add UNIQUE (字段名)

案例:

alter table student2 add UNIQUE (name)

(5) 添加主键索引 (唯一,不能为空,一个表中只有一个主键)

单词:primary key 主键

简写:pri

格式:

ALTER table 表名 add PRIMARY key (字段名) ;

案例:

ALTER table student2 add PRIMARY key (id) ;

(6)删除索引:

1、第一种情况:删除普通索引和唯一索引是通一种方法

格式:

alter table 表名 drop INDEX 索引名

案例:
alter table student2 drop INDEX aa;

2、第二种情况:删除主键索引

格式:

alter table 表名 drop primary key;

案例:

alter table student2 drop primary key ;

===============================================

二、创建方法二

格式:create INDEX 索引名 on 表名 (字段名)

案例:create INDEX aa on student2 (english)

===============================================

三、建表时创建索引

格式:

CREATE table 表名( 字段名 字段类型(字符长度) PRIMARY key ,字段名 字符类型(字符长度) UNIQUE )) ;

案例:

CREATE table wzx( id int(10) PRIMARY key ,name VARCHAR(20) UNIQUE ,age int(10)) ;

索引是对表的一列数据起到约束作用

===========================================

面试题:

1、什么是索引?

2、索引的作用

3、索引有哪些?

4、如何创建索引?

5、主键索引和唯一索引的区别?

posted @ 2024-07-22 18:02  藕丝鲜芋  阅读(2)  评论(0编辑  收藏  举报