一、简单的存储过程
1、无参存储过程
(1)编写
-
create procedure student()
-
begin
-
select * from student;
-
end;
(2)调用存储过程
call student();
2、有参存储过程
这个参数分为:传入参数、传出参数
(1)编写
-
create procedure study(
-
out l1 decimal(8,2),
-
out l2 decimal(8,2),
-
in l3 int
-
)
-
begin
-
select sum(uid) into l1 from student where order_name=l3;
-
select avg(uid) into l2 from student;
-
end;
注意:l1和l2是用来存储要传出去的值,而l3则是存储传入进存储过程作为条件的值
(2)调用有参存储过程
call study(@stuSum,@stuAvg,1511010);
(3)查看存储过程运行完的结果
select @stuSum,@stuAvg;
3、删除存储过程
drop procedure study;
一、索引的介绍
1、优点:提高MySQL的检索速度,使之高效运行。
2、索引分类:
1)单列索引:一个索引只包含单个列
2)组合索引:一个索引包含多个列
3、索引本质:索引也是一张表,该表保存了主键与索引字段,并且指向实体表的记录,索引应用在SQL查询语句的条件中(where的条件)。
4、缺点:索引会降低表进行insert、update、delete的速度(需要对数据库中的表和索引都进行对应的操作),索引文件会占用磁盘空间,所以索引不能滥用,应该适当。
二、索引的使用
1、普通索引
1)创建索引(三种)
(1)最基本的索引(注意:char/varchar类型的字段,length可以小于字段实际长度;blob/text类型字段,必须指定length)
create index indexName on firstIndex(name(length));
(2)添加索引(修改表结构)
alter firstIndex add index [indexName] on(name(length));
(3)在创建表的同时创建索引(也称指定索引)
create table firstIndex (id bigint not null,name varchar(16) not null,index[indexName] (name(length)));
2)删除索引
drop index [indexName] on myfirstIndex;
2、唯一索引(索引列的值(或索引组合列的值)必须唯一)
1)创建索引(三种)
(1)直接创建索引
create unique index indexName on firstIndex(name(length));
(2)修改表结构时添加
alter table firstIndex add unique [indexName] (name(length));
(3)创建表时直接指定
create table firstIndex(id bigint not null,username varchar(16) not null,unique [indexName] (username(length)));
3、添加和删除索引(使用alter进行添加的四种方式)
1)由于添加了主键,索引值必须是唯一的(NOT NULL)
alter table tbl_name add primary key(column_list);
2)创建索引的值必须是唯一的。
alter table tbl_name add unique index_name(column_list);
3)添加普通索引(索引值不唯一)
alter table tbl_name add index index_name(column_list);
4)索引为fulltext(用于全文索引)
转载:https://blog.csdn.net/tree_ifconfig/article/details/81449218