mysql中的存储过程与索引

一、存储过程

创建存储过程:设置定界符$$,存储体避免在客户端直接操作sql,而是直接将存储过程写入服务器端


>  delimiter $$

>     create procedure delete_matches(In p_playrno Integer)

>     begin

>               delete from matches

>                where playerno=p_playerno;

>      end$$

>delimiter;

调用存储过程:
(1) 设置参数

set @p_playrno =1;

(2)调用存储过程

call delete_matches(p_playrno)

参数的类型有三种:in(给定参数调用存储过程,输出);
              out(存储过程向外输出,给定参数调用,则输出为空);
              inout(存储过程既可以输入参数,也可以输出)


二、索引

索引分为单列索引和组合索引,单列索引即索引包含单个列,一张表可以有多个单列索引,
但这不是组合索引,组合索引即一个索引包含多个列。

查询速度快,但更新比较慢

使用show index 命令来显示索引的相关信息,\G是格式化索引信息
例如:show index from mytable;\G


(1)创建索引:(在username为Blob或Text类型时,需要指定length的长度)
create index indexName on mytable(username(length))
(2)修改表结构时候添加索引
alert table mytable add index indexName(username)
(3)创建表的时候直接给定
>  create table mytable(
>  myid int not null,
>  myname varchar(18) not null,
>  index[indexName](username(length))
>  )
(4)删除索引
drop INDEX[indexName] on mytable;

(5)唯一索引:索引列的值必须是唯一的,允许为空值,组合索引列值的索引组合值必须是唯一的
与上面大致相同,在增加的索引字段前加UNIQUE,如果是主键则标明primary key



猜你喜欢

转载自blog.csdn.net/lei_1994/article/details/79537295