标签:触发器 mit rollback cte 处理 支持 acid charlie 用户名
start transaction;
开启commit;
提交rollback;
回滚第一步: 开启事务, 执行操作
# alpha 对 bravo 转账 100
mysql> select * from balance;
+----+-------+-------+
| id | name | money |
+----+-------+-------+
| 1 | alpha | 1000 |
| 2 | bravo | 1000 |
+----+-------+-------+
2 rows in set (0.00 sec)
# 开启事务
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> update balance set money=1000-100 where name='alpha';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update balance set money=1000+100 where name='bravo';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from balance;
+----+-------+-------+
| id | name | money |
+----+-------+-------+
| 1 | alpha | 900 |
| 2 | bravo | 1100 |
+----+-------+-------+
2 rows in set (0.00 sec)
第二步: 此时我们还未提交事物, 我们新开一个窗口, 查看一下balance表
mysql> select * from balance;
+----+-------+-------+
| id | name | money |
+----+-------+-------+
| 1 | alpha | 1000 |
| 2 | bravo | 1000 |
+----+-------+-------+
2 rows in set (0.00 sec)
第三步: 然后我们提交事物
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from balance;
+----+-------+-------+
| id | name | money |
+----+-------+-------+
| 1 | alpha | 900 |
| 2 | bravo | 1100 |
+----+-------+-------+
2 rows in set (0.00 sec)
第四步: 在新的窗口查看balance表, 数据修改成功
mysql> select * from balance;
+----+-------+-------+
| id | name | money |
+----+-------+-------+
| 1 | alpha | 900 |
| 2 | bravo | 1100 |
+----+-------+-------+
2 rows in set (0.00 sec)
如果我们在第三步的时候不提交, 回滚一下事务
mysql> select * from balance;
+----+-------+-------+
| id | name | money |
+----+-------+-------+
| 1 | alpha | 1000 |
| 2 | bravo | 1000 |
+----+-------+-------+
2 rows in set (0.00 sec)
# 开启事务
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> update balance set money=1000-100 where name='alpha';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update balance set money=1000+100 where name='bravo';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from balance;
+----+-------+-------+
| id | name | money |
+----+-------+-------+
| 1 | alpha | 900 |
| 2 | bravo | 1100 |
+----+-------+-------+
2 rows in set (0.00 sec)
# 回滚事务
mysql> rollback;
Query OK, 0 rows affected (0.01 sec)
# 数据没有被修改
mysql> select * from balance;
+----+-------+-------+
| id | name | money |
+----+-------+-------+
| 1 | alpha | 1000 |
| 2 | bravo | 1000 |
+----+-------+-------+
2 rows in set (0.00 sec)
MyISAM (我的阿萨姆?)
create view 视图名 as select查询语句
select * from 视图名
drop view 视图名
mysql> select * from user_info;
+----+---------+----------+
| id | name | password |
+----+---------+----------+
| 1 | bigb | 111111 |
| 2 | blake | 222222 |
| 3 | black | 333333 |
| 4 | alpha | 111111 |
| 5 | bravo | 222222 |
| 6 | charlie | 333333 |
| 7 | delta | 111111 |
| 8 | echo | 222222 |
| 9 | foxtrot | 333333 |
+----+---------+----------+
9 rows in set (0.00 sec)
mysql> select * from user_info where name='alpha';
+----+-------+----------+
| id | name | password |
+----+-------+----------+
| 4 | alpha | 111111 |
+----+-------+----------+
1 row in set (0.00 sec)
# 创建视图
mysql> create view v1 as select * from user_info where name='alpha';
Query OK, 0 rows affected (0.01 sec)
# 使用视图
mysql> select * from v1;
+----+-------+----------+
| id | name | password |
+----+-------+----------+
| 4 | alpha | 111111 |
+----+-------+----------+
1 row in set (0.00 sec)
# 如果我们对原表记录进行了修改, 发现视图中的数据也进行了相应的修改, 因此通过视图得到是一个动态的数据集
mysql> update user_info set password='888888' where name='alpha';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from v1;
+----+-------+----------+
| id | name | password |
+----+-------+----------+
| 4 | alpha | 888888 |
+----+-------+----------+
1 row in set (0.00 sec)
# 删除视图
mysql> drop view v1;
Query OK, 0 rows affected (0.00 sec)
delimiter 结束符号(自定义)
create trigger 触发器名 before/after 触发事件 on 表名 for each row
begin
触发器触发的内容;
end 结束符号
delimiter; # 将结束符号改回;
# t1 和 t2 为两个空表
mysql> select * from t1;
Empty set (0.00 sec)
mysql> select * from t2;
Empty set (0.00 sec)
# 创建触发器
mysql> delimiter //
mysql> create trigger tri_nb after insert on t1 for each row
-> begin
-> insert into t2 (name) values ('bravo');
-> end //
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;
# 对 t1 进行操作
mysql> insert into t1 (name) values ('alpha');
Query OK, 1 row affected (0.01 sec)
mysql> select * from t1;
+----+-------+
| id | name |
+----+-------+
| 1 | alpha |
+----+-------+
1 row in set (0.00 sec)
mysql> select * from t2;
+----+-------+
| id | name |
+----+-------+
| 1 | bravo |
+----+-------+
1 row in set (0.00 sec)
delimiter 结束符号
create procedure p1(IN 参数 int) # IN表示传入参数, 参数类型为int
begin
sql语句;
end 结束符号
delimiter 结束符号
使用
call p1(参数);
drop procedure p1;
例子
+----+---------+----------+
| id | name | password |
+----+---------+----------+
| 1 | bigb | 111111 |
| 2 | blake | 222222 |
| 3 | black | 333333 |
| 4 | alpha | 888888 |
| 5 | bravo | 222222 |
| 6 | charlie | 333333 |
| 7 | delta | 111111 |
| 8 | echo | 222222 |
| 9 | foxtrot | 333333 |
+----+---------+----------+
# 创建
mysql> delimiter //
mysql> create procedure p1(IN p_in int)
-> begin
-> select * from user_info where id = p_in;
-> end //
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;
# 调用
mysql> call p1(3);
+----+-------+----------+
| id | name | password |
+----+-------+----------+
| 3 | black | 333333 |
+----+-------+---------+
1 row in set (0.00 sec)
mysqldump -u用户名 -p密码 数据库名 > 备份文件.sql
mysqldump -u用户名 -p密码 数据库名 表1 表2 > 备份文件.sql
对库下面的表进行备份mysqldump -u用户名 -p密码 --databases 数据库1 数据库2 > 备份文件.sql
mysqldump -u用户名 -p密码 --all-databases > 备份文件.sql
source D:\all.sql
标签:触发器 mit rollback cte 处理 支持 acid charlie 用户名
原文地址:https://www.cnblogs.com/bigb/p/11779709.html