[TOC]
First, the transaction
1. What matters
- A transaction is concurrency control units, refers to a set of operations are executed either succeed or both fail.
2. What is the use affairs
- Ensure the integrity of the database data.
3. Use
grammar:
start transaction; sql语句 commit/rollback; # commit 的意思是上面的处理都正确,现在提交,修改本地数据 # rollback 的意思是上面的处理有错误或者问题,取消上面所有的操作,回到start transaction 之前的状态
4. Features transaction (the ACID)
(1) atom of (Atomicity)
- A transaction is a logical unit of work database, all operations in the transaction included either do or do not do
(2) Consistency (Consistency)
- After the transaction occurs and former refers to the occurrence, the total data still matches
(3) isolation (Isolation)
- Execution of a transaction can not interfere with other transactions. I.e., operation and use of the data inside a transaction other concurrent transactions are isolated and can not interfere with each other concurrently executing transactions between the various
(4) persistent (Durability)
- Refers to a transaction once submitted, change the data in a database it should be permanent
Two, MySQL storage engine
1. InnoDB
- High-performance, support services, support row lock
- MySQL5.0 version and above is the default storage engine
2. MyIsam
- Low performance, does not support transactions, supports table locks
- Now also gradually eliminated
Three View
1. What is the view
A view is a virtual table (non-real), it is to obtain a dynamic data set based on a SQL statement, and name it. After using the name you can get the result set, and the result set can be used as a table
However, the view will increase the burden on the database system, now basically no one to use.
2. Use the view
-
#增加视图: create view 视图名 as SQL语句; #删除: drop view v1; #查看视图: select * from 视图名;
Fourth, the flip-flop
1. What is the trigger
- A trigger is a grammar. Its function is to associate multiple SQL statements together, when we execute the specified SQL statement will execute other SQL statements simultaneously.
2. The use of triggers
-
两张表: 订单表 库存表 场景: 当我下一个订单的时候, 订单表中需要增加一个记录, 同时库存表中需要减1 这两个操作是同时发生的, 并且前一个操作出发后一个操作 # 使用方法: # 例子:当向tb1表中添加一条数据的同时, 向tb2表添加一条数据 增加: delimiter // CREATE TRIGGER 触发器名 BEFORE INSERT ON t2 FOR EACH ROW #触发条件 BEGIN INSERT INTO t3 (NAME) VALUES ('aa'); # 触发后执行的sql语句 END // delimiter ; #删除: drop trigger 触发器名; # 查看 show triggers\G
V. Stored Procedures
1. What is a stored procedure
- That is a function of the database system, SQL statements thrown into a pile of stored procedure, and then to call a stored procedure by this name inside the actuator in python similar functions, stored procedures, SQL statements.
2. The use of stored procedures
-
#创建: delimiter // create procedure 存储过程名() BEGIN select * from user where id=2; # SQL语句 END // delimiter ; # 调用存储过程: call 存储过程名(); # 删除: drop procedure 存储过程名;
Six built-in functions (common)
CHAR_LENGTH(str)
返回值为字符串str 的长度,长度的单位为字符。一个多字节字符算作一个单字符。
对于一个包含五个二字节字符集, LENGTH()返回值为 10, 而CHAR_LENGTH()的返回值为5。
CONCAT(str1,str2,...)
字符串拼接
如有任何一个参数为NULL ,则返回值为 NULL。
FORMAT(X,D)
将数字X 的格式写为'#,###,###.##',以四舍五入的方式保留小数点后 D 位, 并将结果以字符串的形式返回。若 D 为 0, 则返回结果不带有小数点,或不含小数部分。
例如:
SELECT FORMAT(12332.1,4); 结果为: '12,332.1000'
INSTR(str,substr)
返回字符串 str 中子字符串的第一个出现位置。
LEFT(str,len)
返回字符串str 从开始的len位置的子序列字符。
LOWER(str)
变小写
UPPER(str)
变大写
LTRIM(str)
返回字符串 str ,其引导空格字符被删除。
RTRIM(str)
返回字符串 str ,结尾空格字符被删去。
SUBSTRING(str,pos,len)
获取字符串子序列
LOCATE(substr,str,pos)
获取子序列索引位置
REPEAT(str,count)
返回一个由重复的字符串str 组成的字符串,字符串str的数目等于count 。
若 count <= 0,则返回一个空字符串。
若str 或 count 为 NULL,则返回 NULL 。
REPLACE(str,from_str,to_str)
返回字符串str 以及所有被字符串to_str替代的字符串from_str 。
REVERSE(str)
返回字符串 str ,顺序和字符顺序相反。
RIGHT(str,len)
从字符串str 开始,返回从后边开始len个字符组成的子序列
Seven, database backup
1. Why should data be backed up
- A large number of value-added data, for unexpected accidents result in loss of data, so to back it up
2. How to back up
用法:
#语法:
# mysqldump -h 服务器 -u用户名 -p密码 数据库名 表名, 表名,.... > 备份文件的路径(如:aaa.sql , 这个是相对路径,也可以填绝对路径,都要以 .sql 格式结尾。)
#示例:
#单库备份
mysqldump -uroot -p123 db1 > db1.sql
mysqldump -uroot -p123 db1 table1 table2 > db1-table1-table2.sql
#多库备份
mysqldump -uroot -p123 --databases db1 db2 mysql db3 > db1_db2_mysql_db3.sql
#备份所有库
mysqldump -uroot -p123 --all-databases > all.sql
重新导入:
mysql> source D:/test3.sql;