mysql 触发器,事务,存储过程,光标
时间:2023-3-1 22:08 作者:wen 分类: MySQL
回顾:
视图:虚表 由可执行的sql语句中的select语句组成虚表
备份数据库:
mysqldump -uroot -p12345678 test > c:/a.sql
还原数据库:
新的数据库中 创建同名数据库 test
mysql -uroot -p12345678 test<c:\a.sql
触发器:
create trigger 触发器名称 触发的时间 触发的动作 on 引起触发器的表名称 for each row sql
触发器时机: before 之前 after 之后
触发器动作:激发触发器时,语句类型 insert update delete
create trigger delTest1 before delete on Test1 for each row delete from Test2;
事务:(用来面试的)
可以确保所有的操作都能够执行成功,否则就不更新数据
官方:任务分成单个逻辑单元,每个单元都成执行成功,最终的目标才成功,否则全部失败
要么全部成功 要么全部失败
1.存储引擎:
mysql: 在实际中 数据类型很多,表也很多--》数据处理上 存在差异
mysql针对差异性,提供不同的存储引擎(mysql核心),对数据不同的处理要求,选择不同存储引擎
myisam 默认:中小型项目 不支持事务
一张表 存成3个文件:
.frm 表定义
myd 数据
myi 索引
innodb 事务型引擎(支持事务) 提交 回滚 ,有了崩溃回复能力
.frm 表定义 数据 索引
create table test1(
表定义...
)engine=引擎类型
engine=引擎分类
2.事务: innodb表 安装 appserv 注意: enable innodb 要选择
事务的4个特征:
原子性 : 逻辑单元为最小单位 一件--》逻辑单元 insert update delete
一致性 : 执行sql 必须和 数据库中的数据对应
隔离性 : 每个单元之间的工作相对独立的
持续性 : 在事务中 处理的数据 是永久保存的
3.案例
开启事务 start transaction /begin
编写sql语句 insert updata delete
执行上方的所有语句 commit
SAVEPOINT identifier 设置回滚点
rollback 回滚到,回滚点的位置
start transaction;
insert into test3(name,pass) values("zhangsna","123456");
savepoinit name
insert into test3(name,pass) values("zhangsna1","123456");
insert into test3(name,pass) values("zhangsna2","123456");
if(){
rollback;
}
commit;
缺点:
1.innodb 降低mysql运行效率
2.对于大多数行业 不太适合
常用于:库房 金融 ...
3.存储过程
是一组为了完成特定功能的sql语句,经过编译后存在数据库中,用户可以调用代码来执行
delimiter 自定义执行符号
create procedure 函数名()
begin
sql语句,每条命令都要有分号
end
call 函数名() 自定义执行符号
delimiter //
create procedure doBooks()
begin
insert into books(bName,bTypeId,publishing,price,pubDate,author,LSBN) values('Liunx傻瓜书',4,'家里蹲大学出版社',903,20040301,'张三','7505380796');
select * from books;
end//
call dobooks()//
drop procedure dobooks;
create procedure delbooks()
begin
delete from books where bname like '%傻瓜书%';
select * from books;
end//
call delbooks()//
参数
in 传入
out 传出
inout 传入传出
into 赋值
creat procedure 函数名(in 参数名 数据类型)
begin
end
set @变量名称=value 定义变量
select @变量名称 打印
select count(bid) into @a from books;
create procedure test1(in str varchar(50))
begin
declare aa int(4);
set aa = (select btypeid from btype where btypename=str);
select * from books where btypeid = aa;
end
call test1("黑客")
declare 变量名称 数据类型[default value];
repeat 循环:
[标签(自定义单词词组)]repeat
循环体
until 终止添加
end repeat[标签]
光标
declare 光标名称 cursor for sql(select)
打开光标:
open 光标名称
抓取数据:
fatch 光标名称 into 变量列表
关闭光标
close 关闭名称
create procedure test()
begin
declare done int default 0;
declare n varchar(50);
declare p varchar(50);
declare ops cursor for select name,pass from test1;
declare continue handler for sqlstate '02000' set done=1;
open ops;
repeat
fetch ops into n,p;
select n,p,done;
until done
end repeat;
close ops;
end//
create table test1(
id int(4) auto_incement primary key,
name char(50),
pass int(50),
);
4. if语句:
if 条件 then 执行语句
elseif 条件 then 执行语句
......
else 执行语句
end if
5 repeat
loop
[标签]loop
循环体
end loop[标签]
leave 终止循环
iterate 继续循环
create procedure test7(in p1 int)
begin
lb:loop
set p1 = p1+1;
select p1;
if p1<10 then
iterate lb;
end if;
leave lb;
end loop lb;
end
作业
delimiter //
create procedure getbooks()
begin
declare done int default 0;
declare bid1 int(4);
declare bname1 varchar(255);
declare btypeid1 enum('1','2','3','4','5','6','7','8','9','10');
declare publishing1 varchar(255);
declare price1 int(4);
declare pubdate1 date;
declare author1 varchar(30);
declare lsbn1 varchar(255);
declare ops cursor for select bid,bname,btypeid,publishing,price,pubdate,author,lsbn from books;
declare continue handler for sqlstate '02000' set done=1;
open ops;
repeat
fetch ops into bid1,bname1,btypeid1,publishing1,price1,pubdate1,author1,lsbn1;
select bid1,bname1,btypeid1,publishing1,price1,pubdate1,author1,lsbn1,done;
until done
end repeat;
close ops;
end//
call getbooks();//
create procedure setbooks(in num int(4))
begin
declare bid2 int(4);
declare bname2 varchar(255);
declare btypeid2 enum('1','2','3','4','5','6','7','8','9','10');
declare publishing2 varchar(255);
declare price2 int(4);
declare pubdate2 date;
declare author2 varchar(30);
declare lsbn2 varchar(255);
declare ops cursor for select bid,bname,btypeid,publishing,price,pubdate,author,lsbn from books where bid = num;
open ops;
fetch ops into bid2,bname2,btypeid2,publishing2,price2,pubdate2,author2,lsbn2;
select bid2,bname2,btypeid2,publishing2,price2,pubdate2,author2,lsbn2;
close ops;
end//
call setbooks(4);