什么是事务处理呢??事务处理是用来维护数据库的完整性的,它保证成批的MySQL操作要么完全执行,要么完成不执行。
一、Mysql事务概念
MySQL 事务主要用于处理操作量大,复杂度高的数据。由一步或几步数据库操作序列组成逻辑执行单元,这系列操作要么全部执行,要么全部放弃执行。在 MySQL 中只有使用了 Innodb 数据库引擎的数据库或表才支持事务。事务用来管理 insert,update,delete 语句。
二、事务特性:Atomicity(原子性)、Consistency(稳定性,一致性)、隔离性(Isolation)和Durability(持续性,可靠性)。这四个特性也简称ACID性。
1.原子性:事务是应用中最小的执行单位,就如原子是自然界最小颗粒,具有不可再分的特征一样。事务是应用中不可再分的最小逻辑执行体,一组事务,要么成功;要么撤回。
2.稳定性,一致性:事务执行的结果,必须使数据库从一个一致性状态,变到另一个一致性状态。当数据库中只包含事务成功提交的结果时,数据库处于一致性状态。一致性是通过原子性来保证的。有非法数据(外键约束之类),事务撤回。
3.隔离性:各个事务的执行互不干扰,任意一个事务的内部操作对其他并发的事务,都是隔离的。也就是说:并发执行的事务之间不能看到对方的中间状态,并发执行的事务之间不能相互影响。事务独立运行。一个事务处理后的结果,影响了其他事务,那么其他事务会撤回。事务的100%隔离,需要牺牲速度。
4.持续性,可靠性:持续性也称为持久性,指事务一旦提交,对数据所做的任何改变,都要记录到永久存储器中,通常是保存进物理数据库。软、硬件崩溃后,InnoDB数据表驱动会利用日志文件重构修改。可靠性和高速度不可兼得, innodb_flush_log_at_trx_commit 选项 决定什么时候吧事务保存到日志里。
注意事项:存储引擎MyISAM不支持事物,存储引擎InnoDB支持事物。事务只针对对数据数据产生影响的语句有效。show engines 查看mysql锁支持的数据引擎。
三、读取数据概念
1.脏读(Dirty Reads):所谓脏读就是对脏数据的读取,而脏数据所指的就是未提交的数据。一个事务正在对一条记录做修改,在这个事务完成并提交之前,这条数据是处于待定状态的(可能提交也可能回滚),这时,第二个事务来读取这条没有提交的数据,并据此做进一步的处理,就会产生未提交的数据依赖关系。这种现象被称为脏读。
2.不可重复读(Non-Repeatable Reads):一个事务先后读取同一条记录,但两次读取的数据不同,我们称之为不可重复读。也就是说,这个事务在两次读取之间该数据被其它事务所修改。
3.幻读(Phantom Reads):一个事务按相同的查询条件重新读取以前检索过的数据,却发现其他事务插入了满足其查询条件的新数据,这种现象就称为幻读。
四、事务隔离级别
修改事务隔离级别语法:
SET [SESSION | GLOBAL] TRANSACTION ISOLATION LEVEL {READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE}
1、Read Uncommitted(未授权读取、读未提交):这是最低的隔离等级,允许其他事务看到没有提交的数据。这种等级会导致脏读。如果一个事务已经开始写数据,则另外一个事务则不允许同时进行写操作,但允许其他事务读此行数据。该隔离级别可以通过“排他写锁”实现。避免了更新丢失,却可能出现脏读。也就是说事务B读取到了事务A未提交的数据。SELECT语句以非锁定方式被执行,所以有可能读到脏数据,隔离级别最低。
SET session transaction isolation level read uncommitted ; SET global transaction isolation level read uncommitted;/*全局建议不用*/ SELECT @@global.tx_isolation; SELECT @@session.tx_isolation; SELECT @@tx_isolation;
新建一个简单的student表,设置id和name,num字段,开启事务1对表新增通过存储过程,事务不提交,查看当前数据库事务状态,可以看到一条数据事务,事务级别为READ UNCOMMITTED:
drop table if exists student; create table student( id int primary key auto_increment comment 'id', name varchar(100) comment '名称', num int ); drop procedure if exists proc_on_sw; delimiter ;; create procedure proc_on_sw() begin start transaction; insert into student(name,num) value('aaa',1); select * from information_schema.INNODB_TRX; end ;; delimiter ;; call proc_on_sw();
新建事务2,查询student表,我们在READ UNCOMMITTED级别下,可以看到其他事务未提交的数据:再去查看数据库事务状态,我们会看到状态正常。
start transaction ; select * from student; commit; select * from information_schema.INNODB_TRX;
2.Read Committed(授权读取、读提交):读取数据的事务允许其他事务继续访问该行数据,但是未提交的写事务将会禁止其他事务访问该行。该隔离级别避免了脏读,但是却可能出现不可重复读。事务A事先读取了数据,事务B紧接了更新了数据,并提交了事务,而事务A再次读取该数据时,数据已经发生了改变。
SET session transaction isolation level read committed ; SET global transaction isolation level read committed; /*全局建议不用*/ drop procedure if exists proc_on_up; delimiter ;; create procedure proc_on_up() begin set autocommit=0; update student set name='cc' where id=1; commit; set autocommit=1; end ;; delimiter ;; call proc_on_up(); select * from student;
3.repeatable read(可重复读取):就是在开始读取数据(事务开启)时,不再允许修改操作,事务开启,不允许其他事务的UPDATE修改操作,不可重复读对应的是修改,即UPDATE操作。但是可能还会有幻读问题。因为幻读问题对应的是插入INSERT操作,而不是UPDATE操作。避免了不可重复读取和脏读,但是有时可能出现幻读。这可以通过“共享读锁”和“排他写锁”实现。
set session transaction isolation level repeatable read;
4.串行化、序列化:提供严格的事务隔离。它要求事务序列化执行,事务只能一个接着一个地执行,但不能并发执行。如果仅仅通过“行级锁”是无法实现事务序列化的,必须通过其他机制保证新插入的数据不会被刚执行查询操作的事务访问到。序列化是最高的事务隔离级别,同时代价也花费最高,性能很低,一般很少使用,在该级别下,事务顺序执行,不仅可以避免脏读、不可重复读,还避免了幻像读。
set session transaction isolation level serializable;
隔离等级 脏读 不可重复读 幻读
读未提交 YES YES YES
读已提交 NO YES YES
可重复读 NO NO YES
串行化 NO NO NO
五、完整例子包括提交和回滚完整例子
drop procedure if exists pro_new; delimiter;; create procedure pro_new(out rtn int) begin declare err INT default 0; -- 如果出现异常,会自动处理并rollback declare exit handler for sqlexception ROLLBACK ; -- 启动事务 set autocommit=0; start transaction; insert into student(name,num) values(NULL,2.3); -- set err = @@IDENTITY; -- = 获取上一次插入的自增ID; set err =last_insert_id(); -- 获取上一次插入的自增ID insert into student(name,num) VALUEs('ccc',err); -- 运行没有异常,提交事务 commit; -- 设置返回值为1 set rtn=1; set autocommit=1; end ;; delimiter ;; set @n=1; call pro_new(@n); select @n;
更多相关教程请访问 MySQL视频教程

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver Mac version
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function