您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

MySQL学习之路7-索引、事务、函数、存储过程、游标

bubuko 2022/1/25 20:05:40 mysql 字数 2921 阅读 808 来源 http://www.bubuko.com/infolist-5-1.html

索引 使用索引快速定位某列中特定值的行,不需要遍历数据表所有行。 创建索引的数据结构:BTREE and HASH。 主键也是一种索引,Primary key。 show index from orders;#显示索引 alter table orders add index oid_index(o ...

索引

  • 使用索引快速定位某列中特定值的行,不需要遍历数据表所有行。
  • 创建索引的数据结构:BTREE and HASH
  • 主键也是一种索引,Primary key。
show index from orders;#显示索引
 
alter table orders add index oid_index(oid);#添加索引

drop index oid_index on orders;#删除索引

 

自定义函数

#自定义函数
delimiter //
create function linearfunction(x int)
returns int
begin 
declare y int;
set y= 2*x+1;
return y;
end //
delimiter ;

select linearfunction(5);#调用自定义函数
drop function linearfunction;#删除自定义函数

 

存储过程

  • 完成特定功能的SQL语句集,经编译创建并保存在数据库中,用户可通过指定存储过程的名字并给定参数来调用执行。
#存储过程
delimiter //
create procedure info(score int)
begin 
select * from stuscore where Math>score;
end //
delimiter ;
call info(90);#调用存储过程
drop procedure info;#删除存储过程

 

事务

  • 维护数据库的完整性,保证成批的SQL语句要么完全执行,要么都不执行。
create table bank_account(
	id int unsigned auto_increment primary key,
	account_name varchar(10),
	account_balance decimal(10,2)
);
show tables;
insert into bank_account(account_name,account_balance)
values
(‘客户A‘,500),
(‘客户B‘,300);
begin;#开启事务
update  bank_account set account_balance = 400 where account_name = ‘客户A‘;
update  bank_account set account_balance = 400 where account_name = ‘客户B‘;
rollback;#回退
commit;#提交
select * from bank_account;

 

游标

  • 每一行的显示select查询的结果集。
delimiter //
create procedure info_cursor()
begin
	declare var_id int(10);
	declare var_stuname varchar(20);
	declare var_math decimal(5,1);
	
	#创建游标
	declare score_cursor cursor FOR
	select stuid,stuname,Math from stuscore where class = 1;
	
	#打开游标
	open score_cursor;
	
	#使用游标
	fetch score_cursor into var_id,var_stuname,var_math;
	select var_id,var_stuname,var_math;
	
	#关闭游标
	close score_cursor;
	
end //
delimiter ;
call info_cursor();#调用游标

 

2020-03-14 19:54

MySQL学习之路7-索引、事务、函数、存储过程、游标

原文:https://www.cnblogs.com/fuyusheng/p/12493861.html


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶