1
2
3
4
5
Transaction(事务)
原子性(Atomicity)确保工作单元内的所有操作都能成功完成。如若不然,事务在遭受失败时就会被放弃,之前的种种操作就会被撤销回它们之前的状态。
一致性(Consistency)确保数据库能够在成功提交的事务的基础上正确改变状态。
隔离性(Isolation)使事务能够独立操作,事务之间彼此透明。
持久性(Durability)确保当系统发生失败时,已提交的事务的结果或者说效果能够持续存在。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
BEGINSTART TRANSACTION;显式地开启一个事务;

COMMIT;也可以使用COMMIT WORK,不过二者是等价的。COMMIT会提交事务,并使已对数据库进行的所有修改称为永久性的;

ROLLBACK;有可以使用ROLLBACK WORK,不过二者是等价的。回滚会结束用户的事务,并撤销正在进行的所有未提交的修改;

SAVEPOINT identifier;SAVEPOINT允许在事务中创建一个保存点,一个事务中可以有多个SAVEPOINT

RELEASE SAVEPOINT identifier;删除一个事务的保存点,当没有指定的保存点时,执行该语句会抛出一个异常;

ROLLBACK TO identifier;把事务回滚到标记点;

SET TRANSACTION;用来设置事务的隔离级别。InnoDB存储引擎提供事务的隔离级别有READ UNCOMMITTED、READ COMMITTED、REPEATABLE READSERIALIZABLE
通过设定会话变量 AUTOCOMMIT 可以控制事务行为。如果 AUTOCOMMIT 被设为1(默认值),则每一个 SQL 语句(无论是否在事务中)都会被认为是一个完成的事务,则默认当它结束时予以提交。当 AUTOCOMMIT 被设为0(通过命令 SET AUTOCOMMIT=0)时,后续一系列语句就像是一个事务,直到 COMMIT 语句执行为止,不再提交任何行为。

今天写的一些脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* Author: Chenhui Wang
Date: 2018-05-25 */

-- IS NULL的用法
/*
假设有张left_join_result的表
+-------------+------------------+----------------+
| tutorial_id | tutorial_title | tutorial_count |
+-------------+------------------+----------------+
| 10 | Norwegian Wood | 10 |
| 11 | Ass we Can! | 15 |
| 12 | Motherfucker | 15 |
| 13 | Hell of the Fame | NULL |
+-------------+------------------+----------------+
*/
select * from left_join_result where tutorial_count = NULL; -- 会返回Empty set,因为不管什么运算与NULL做都是false
select * from left_join_result where tutorial_count != NULL; -- 也会返回Empty set

select * from left_join_result where tutorial_count is NULL; -- 会返回id为13的元组
select * from left_join_result where tutorial_count is not NULL; -- 会返回其余结果

-- REGEXP正则表达式的用法
/*
模式 模式匹配对象
^ 字符串的开始位置
$ 字符串的结尾
. 单个字符
[...] 一对方括号之间的字符
[^...] 未在一对方括号之间的字符
p1|p2|p3 交替匹配模式1、模式2或模式3
* 匹配前面元素的零个或多个实例
+ 匹配前面元素的一个或多个实例
{n} 匹配前面元素的n个实例
{m,n} 匹配前面元素的m~n个实例,m <= n
*/
select tutorial_title from left_join_result where tutorial_title REGEXP "^Ass"; -- 以Ass开头的tutorial_title字段
select tutorial_title from left_join_result where tutorial_title REGEXP "d$"; -- 以d结尾的tutorial_title字段

-- transaction事务
create table transaction_test(
id int(5) not null,
primary key (id)
);
begin; -- 开启事务
insert into transaction_test (id) values (5); -- 插入5
insert into transaction_test (id) values (66); -- 插入66
commit; -- 完成插入,结束事务。
/*
+----+
| id |
+----+
| 5 |
| 66 |
+----+
*/
rollback; -- 回滚,在commit之前用rollback会取消所有刚才的insert之类的命令
-- Groupby 分组
/*
现在有一个表 asshole
+----+---------+---------------------+--------+
| id | name | date | singin |
+----+---------+---------------------+--------+
| 1 | Pornhub | 2016-04-22 15:25:33 | 1 |
| 2 | Fuck | 2016-04-20 15:25:47 | 3 |
| 3 | Hell | 2016-04-19 15:26:02 | 2 |
| 4 | Dick | 2016-04-07 15:26:14 | 4 |
| 5 | Redtube | 2016-04-11 15:26:40 | 4 |
| 6 | Link | 2016-04-04 15:26:54 | 2 |
+----+---------+---------------------+--------+
*/
select name, count(*) from asshole group by name; -- 按照name属性来group,count(*)是计数函数,用来统计各name的出现次数
/*
+---------+----------+
| name | count(*) |
+---------+----------+
| Pornhub | 1 |
| Fuck | 1 |
| Hell | 1 |
| Dick | 1 |
| Redtube | 1 |
| Link | 1 |
+---------+----------+
*/

-- 修改元组类型用alter modify, 修改元组名字用alter change oldname newname newtype;
-- 修改元组NULL为not null用alter modify

update asshole set length=0;
alter table asshole modify length bigint(30) not null default 100;