MYSQL入门到精通【6】:数据库事务 / 视图 / 触发器过程函数
1. 事务控制语言DTL
1.1 基本介绍
一个sql语句就是一个事务
事务可以保证一组sql语句要么成功要么都失败
默认自动提交,但必须保证引擎是InnoDB
1.2 使用方法
start transaction;
sql语句
如果上面的sql语句没有问题,commit
如果上面的sql语句有问题,rollback
1.3 自动提交关闭
可以关闭 set autocommit=0 关闭自动提交
最后 插入或者修改时 只有 输入commit才会最终存入,否则不存
show variables like '%commit%'; #图①
set autocommit=OFF; #关闭自动提交
show variables like '%commit%'; #图②
图①
图②
1.3 基本原则
原子性:一个事务中的所有语句,应该做到:要么全做,要么一个都不做;
一致性:让数据保持逻辑上的合理性,比如:一个商品出库时,既要让商品库中的该商品数量减1,又要让对应用户的购物车中的该商品加1;
隔离性:如果多个事务同时并发执行,但每个事务就像各自独立执行一样。
持久性:一个事务执行成功,则对数据来说应该是一个明确的硬盘数据更改(而不仅仅是内存中的变化)
#模拟转账
show databases;
create database bank;
use bank;
show tables;
create table user(
id int unsigned not null auto_increment primary key,
name varchar(20) not null,
fee decimal(6,2) not null
);
insert into user (name,fee) values ('xiaoliu',4000.00);
insert into user (name,fee) values ('xiaozhang',1000.00);
select * from user;
#图①
#模拟小刘给小张转2000元,分两次:
#流程1:小刘的账户减2000元
update user set fee=2000 where id=1;
select * from user;
#流程2:小张的账户加2000元
update user set fee=3000 where id=2;
select * from user;
#图②
#开启事务,防止流程中断
start transaction;
update user set fee=1500 where id=1;
select * from user;
update user set fee=3500 where id=22;
select * from user;
rollback; #流程中断,重新计算
select * from user;
#图③
#开启事务,防止流程中断
start transaction;
update user set fee=1500 where id=1;
select * from user;
update user set fee=3500 where id=2;
select * from user;
commit; #流程正常,提交数据
select * from user;
#图④
图①
图②
图③
图④
2. 视图的使用场景view
2.1 数据库编程
存储过程函数:
delimiter // 修改结束符 避免冲突
create procedure he(a int)
begin
select * from student where id=a;
end//
delimiter;
call he(1);
2.2 视图view
视图的使用:
create view 视图名 [(列名1,列名2,…)] as select 语句;
其实就是当作一个查询表来用
修改视图:
alter view 视图名 [(列名1,列名2,…)] as select 语句 ;
删除视图:
drop view [if exists] 视图名;
show databases;
use shop;
show tables;
select * from student; #图①
create view test_view (name,age) as select name,age from student where id=2;
show create view test_view\G #test_view视图名,展示视图②
select name from test_view; #图③
图①
图②
图③
3. 触发器的使用场景
3.1 触发器
语法 :
create trigger 触发器名 after/before insert/update/delete on 表名 for each
begin
sql语句
end
show databases;
use shop;
show tables;
drop table student;
drop table test_view;
show tables; #空表
create table article(
id int unsigned not null auto_increment primary key,
title varchar(20) not null
);
desc article;
#创建统计文章的表
create table total_num(
id int unsigned not null auto_increment primary key,
type tinyint unsigned not null comment '1文章2用户',
num int unsigned not null
);
desc total_num;
insert into total_num(type,num) values(1,0);
insert into total_num(type,num) values(2,0);
select * from total_num;
#创建触发器
delimiter //
create trigger insert_total_num after insert on article for each row
begin
update total_num set num=num+1 where type=1;
end//
#输出Query OK
select * from total_num;
//
delimiter;
select * from article;
//
#使用
insert into article (title) values ('title1');
select * from total_num;
insert into article (title) values ('title2');
select * from total_num;
#创建触发器
delimiter //
create trigger delete_total_num after delete on article for each row
begin
update total_num set num=num11 where type=1;
end//
#输出Query OK
delimiter;
select * from article;
select * from total_num;
insert into article (title) values ('title3');
select * from total_num;
delete from article where id=1;
select * from total_num;
#查看触发器
show create trigger delete_total_num;
#删除触发器
drop create trigger delete_total_num;