MySQL 05

Affairs

basic concepts

  • Transaction: Transaction
  • A transaction is a logical unit of one or more sql statement constituted, is an overall concept
  • Role : make a series of sql statements either completed ISSUE not all completed, to ensure the integrity of the database

Four properties (ACID)

  • Atomicity ( A tomicity): A transaction is the smallest unit of execution can not be divided, either all of the operations of a transaction, either completed or not completed all
  • Consistency ( C onsistency): the transaction before the start and after the end of the integrity of the database is not corrupted
  • Isolation ( the I solation): Support for multiple database transaction concurrency, inconsistent transaction isolation can be prevented due to the implementation of the cross
    • Uncommitted Read
    • READ COMMITTED
    • Repeatable read
    • Serialization
  • Persistent ( D urability): After completion of the transaction, when the data changes permanent

Instructions

  • start transaction; Open
  • commit; submit
  • rollback; Rollback

The first step: Turn on the transaction, perform the operation

# 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)

Step Two: At this point we have not submitted something, we open a new window, look at the balance sheet

mysql> select * from balance;
+----+-------+-------+
| id | name  | money |
+----+-------+-------+
|  1 | alpha |  1000 |
|  2 | bravo |  1000 |
+----+-------+-------+
2 rows in set (0.00 sec)

The third step: Then we submitted thing

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)

Step 4: Check balance sheet in a new window, data modification success

mysql> select * from balance;
+----+-------+-------+
| id | name  | money |
+----+-------+-------+
|  1 | alpha |   900 |
|  2 | bravo |  1100 |
+----+-------+-------+
2 rows in set (0.00 sec)

If we do not commit, rollback, about the time the transaction in the third step

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)

Storage Engine

  • InnoDB (MySQL by default)
  • MyISAM (My Assam?)

  • the difference
    • InnoDB supports transactions
    • InnoDB supports row locks (concurrent), MyISAM only supports table locks

view

basic concepts

  • select query dynamic data sets obtained having a name based on, as long as the user can view acquired by the data set name

Instructions

  • Increase views create view 视图名 as select查询语句
  • Use view (equivalent to a table) select * from 视图名
  • Delete View 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)

trigger

basic concepts

  • Trigger certain operations on the table 2 of Table 1 when certain operations

Instructions

  • Creating Triggers
delimiter 结束符号(自定义)
create trigger 触发器名 before/after 触发事件 on 表名 for each row
begin
触发器触发的内容;
end 结束符号

delimiter;  # 将结束符号改回;
  • Example: When adding records to achieve t1, t2 add a record to
# 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)

Stored Procedures

basic concepts

  • Sql statement on the package, to facilitate post called, is equivalent function in MySQL

Instructions

  • create
delimiter 结束符号

create procedure p1(IN 参数 int)  # IN表示传入参数, 参数类型为int
begin
sql语句;
end 结束符号

delimiter 结束符号
  • use

    • call p1(参数);
  • delete
    • drop procedure p1;
  • example

+----+---------+----------+
| 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)
  

data backup

Backup

  • Single database backup
    • mysqldump -u用户名 -p密码 数据库名 > 备份文件.sql
    • mysqldump -u用户名 -p密码 数据库名 表1 表2 > 备份文件.sql The following table database backup
  • Multi-database backup mysqldump -u用户名 -p密码 --databases 数据库1 数据库2 > 备份文件.sql
  • Back up all mysqldump -u用户名 -p密码 --all-databases > 备份文件.sql

Importing

  • source D:\all.sql

function

Guess you like

Origin www.cnblogs.com/bigb/p/11779709.html
05