本教程来自我的github nightttt7/MySQL-tutorial, 使用python脚本定时更新在此
本教程用于:
1. 入门学习
2. 快速查询
- 初稿完成于2018/3/13 by nightttt7 and lotus3333
todo
- 完善 游标
- 触发器
- 函数
- 引擎
- 数据库设置与安全
- 范式
- 更多的实战
目录
事务
- TRANSACTION
- 管理必须成批执行的SQL语句,要么全部成功,要么全部失败
- 事务开启
START TRANSACTION;
# 或者
BEGIN;
- 事务提交
COMMIT;
- 事务回滚
ROLLBACK;
- 设置保存点
SAVEPOINT 保存点名称;
- 回滚到保存点
ROLLBACK TO SAVEPOINT 保存点名称;
- 删除保存点
RELEASE SAVEPOINT 保存点名称;
- 创建/删除/更改结构等语句不能被回滚
- 事务不能嵌套
示例代码
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test value('英文');
Query OK, 1 row affected (0.05 sec)
mysql> insert into test value('挪威语');
Query OK, 1 row affected (0.01 sec)
mysql> commit;
Query OK, 0 rows affected (0.05 sec)
mysql> select * from test;
+-----------+
| name |
+-----------+
| 中文 |
| 英文 |
| 挪威语 |
+-----------+
3 rows in set (0.00 sec)
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test value('法语');
Query OK, 1 row affected (0.05 sec)
mysql> rollback;
Query OK, 0 rows affected (0.07 sec)
mysql> select * from test;
+-----------+
| name |
+-----------+
| 中文 |
| 英文 |
| 挪威语 |
+-----------+
3 rows in set (0.00 sec)
mysql> begin ;
Query OK, 0 rows affected (4.01 sec)
mysql> insert into test value('俄罗斯语');
Query OK, 1 row affected (0.01 sec)
mysql> savepoint yaou;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test value('西班牙语');
Query OK, 1 row affected (0.00 sec)
mysql> savepoint xiou;
Query OK, 0 rows affected (0.02 sec)
mysql> insert into test value('阿拉伯语');
Query OK, 1 row affected (0.00 sec)
mysql> savepoint dongou;
Query OK, 0 rows affected (0.00 sec)
mysql> rollback to savepoint xiou;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test;
+--------------+
| name |
+--------------+
| 中文 |
| 英文 |
| 挪威语 |
| 法语 |
| 粤语 |
| 瑞典语 |
| 芬兰语 |
| 俄罗斯语 |
| 西班牙语 |
+--------------+
9 rows in set (0.00 sec)
1. 数值类型
类型 | 大小 | 备注 |
---|---|---|
BIT[(M)] | ||
TINYINT[(M)] | 1字节 | (-128,127) |
SMALLINT[(M)] | 2字节 | (-32 768,32 767) |
MEDIUMINT[(M)] | 3字节 | (-8 388 608,8 388 607) |
INT或INTEGER[(M)] | 4字节 | (-2 147 483 648,2 147 483 647) |
BIGINT[(M)] | 8字节 | 极大整数值 |
DECIMAL[(M[,D])] | M是数字的总数,D是小数点后的数字 | |
FLOAT[(M,D)] | 4字节 | 单精度浮点数值 |
DOUBLE[(M,D)] | 8字节 | 双精度浮点数值 |
FLOAT[(p)] | 0-24单精度,25-53双精度 |
- bool值等于tinyint(1)。0是False,非0是True;但是False是0,True是1。
- 个人看法是尽量不要使用UNSIGNED,因为可能会带来一些意想不到的的效果,对于INT类型可能存放不了的数据,INT UNSIGNED同样可能存放不了,与其如此,还不如在数据库设计阶段将INT类型提升为BIGINT类型 来源
- [(M)] 对于bit指bit数,最大63,默认1。M indicates the maximum display width for integer types. For floating-point and fixed-point types, M is the total number of digits that can be stored (the precision). For string types, M is the maximum length. The maximum permissible value of M depends on the data type.
- [UNSIGNED]无符号 [ZEROFILL]自动补零(自动unsigned)只有BIT没有这两个选项
BIGINT FLOAT DOUBLE