view
create view
1
2
3
4
5
6
7
8
9drop view student_view;
create view student_view as select *from t_student where id%2=0;
-- insert into t_student
insert into t_student values(5,'java',20),(6,'javab',25),(7,'sad',20),(8,'jksd',56);
-- update student_view
update student_view set age=100 where id=8;create complex view
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41-- 有关系的两张表(fk)
drop view student_view2;
create view student_view2 as select s.*,sc.c_id from t_student s left join t_sc sc on s.id=sc.s_id;
-- update view
update student_view2 set name='as' ;
ERROR 1288 (HY000): The target table student_view2 of the UPDATE is not updatable
-- 无关系的两张表
drop table tt;
drop table ta;
create table tt(
id int(7) primary key,
tname varchar(20)
);
create table ta(
taid int(7) primary key,
tt_id int(7)
);
insert into tt values(1,'aa');
insert into tt values(2,'aa');
insert into tt values(3,'aa');
insert into tt values(4,'aa');
insert into ta values(1,1);
insert into ta values(2,2);
insert into ta values(3,3);
insert into ta values(4,4);
drop view t_view;
create view t_view as select * from tt t join ta a on a.tt_id=t.id;
update t_view set tname='as';
create view t_view2 as select count(*)from tt t join ta a on a.tt_id=t.id;
update t_view set count=10;
视图作用:1.封装sql 2.屏蔽底层数据库结构 3.定制数据 4.安全性(grant)索引
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69-- function:提高查询效率
底层:b+数 myisam(数据,索引分别存储),innodb(数据,索引合二为一)
-- 主键列默认索引列
show index from 表名
-- 索引分类
1.主键索引(primary key)
2.unique
3.全文索引(elasticsearch)
4.复合索引
create index index_name on tablename(column 1,column 2)
5.普通索引
create index index_name on tablename(colunmn)
drop table index_test;
create table index_test(
id int(7) primary key auto_increment,
a int(7),
b int(7),
c varchar(20),
d varchar(20)
);
insert into index_test values(1,2,3,'20w','cc'),(null,22,93,'2s0','dd'),(null,22,33,'w20','ss'),(null,22,53,'201','hh'),(null,72,43,'200','gg'),(null,2,3,'20i','ff');
drop index t_index;
create index t_index on index_test(a,b,c);
-- jude index valid
explain +select....
explain select * from index_test where a=2 and b=3 and c='20w';(ref) len=93
+----+-------------+------------+------------+------+---------------+---------+---------+-------------------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+------+---------------+---------+---------+-------------------+------+----------+-------+
| 1 | SIMPLE | index_test | NULL | ref | t_index | t_index | 93 | const,const,const | 1 | 100.00 | NULL |
+----+-------------+------------+------------+------+---------------+---------+---------+-------------------+------+----------+-------+
-- 索引失效*
1.复合索引最左匹配原则
explain select *from index_test where b=3 and c='20w';(All len=0)
explain select *from index_test where a=3 and b=2;(ref len=10)
explain select *from index_test where c='sd' and a=10 and b=2;(ref len=93),调优
2.模糊匹配
-- %在末尾生效
explain select * from index_test where a=1 and b=2 and c like 's%';(ref len=93)
%在前部分生效同(%?%)
explain select * from index_test where a=1 and b=2 and c like '%s';(ref len=10)-- a,b走索引
3.查询范围
explain select * from index_test where a=1 and b>2 and c='21';(ref len=10)
4.索引计算
explain select* from index_test where id+1=1;(all)
5.索引使用函数
explain select* from index_test where abs(id)=1;(all)
6.c查询数据超过百分之30(数据量足够大)
7.is null and is not null
(is null 不走索引,is not null 走索引)
8.in and not in
before 5.7 in(走索引) not in(不走)
after 8.0 not in ( allow index)
-- 索引的创建策略
-- fit create index
1.primary key or unique
2.重复性少,经常被查询,但是更新少
3.order by 列
4.join on 列
-- unfit create index
1.null值列
2.重复列
3.更新频繁列
锁
1 | 1.行锁(两线程不能同时修改某一行的值) |
索引底层原理
1 | myisam(mai se meng),innoDB----->B+树 |
B+树
– 聚簇索引
聚簇索引与非聚簇索引
1 | innoDB为引擎的表,表中的数据都会有主键,即使不主动声明,系统会创建一个隐式的主键.B+树的键值就是主键.这种**以主键为B+树索引而构成的B+树***索引,称为聚簇索引 |
1 | select * from xx where id>=18 and id<41; |
非聚簇索引查找图
myisam查找图
1 | key - 索引列值,value - 数据行的物理地址. |
函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44delimiter $$
create function dy_add(a int ,b int) returns int
begin
return a+b;
end $$
delimiter ;
select dy_add(5,2);
delimiter $$
create function dy_add(a int ,b int) returns int
begin
return a+b;
end $$
delimiter ;
select dy_add(5,2);
-- 触发器
drop trigger customer_trriger;
delimiter //
create trigger customer_trriger
before delete on s_customer
for each row
begin
delete from s_ord where customer_id=old.id;
end //
delimiter ;
-- 触发器
drop trigger customer_trriger;
delimiter //
create trigger customer_trriger
before insert on s_customer
for each row
begin
if new.id<5
select 'error';
end //
delimiter ;