æ¦å¿µ
MySQL äºå¡ä¸»è¦ç¨äºå¤çæä½é大ï¼å¤æ度é«çæ°æ®ãæ¯å¦è¯´ï¼å¨äººå管çç³»ç»ä¸ï¼ä½ å é¤ä¸ä¸ªäººåï¼ä½ æ¢éè¦å é¤äººåçåºæ¬èµæï¼ä¹è¦å é¤å该人åç¸å ³çä¿¡æ¯ï¼å¦ä¿¡ç®±ï¼æç« ççï¼è¿æ ·ï¼è¿äºæ°æ®åºæä½è¯å¥å°±ææä¸ä¸ªäºå¡ï¼
- å¨ MySQL ä¸åªæ使ç¨äº Innodb æ°æ®åºå¼æçæ°æ®åºæ表ææ¯æäºå¡ã
- äºå¡å¤çå¯ä»¥ç¨æ¥ç»´æ¤æ°æ®åºçå®æ´æ§ï¼ä¿è¯ææ¹ç SQL è¯å¥è¦ä¹å ¨é¨æ§è¡ï¼è¦ä¹å ¨é¨ä¸æ§è¡ã
- äºå¡ç¨æ¥ç®¡ç insert,update,delete è¯å¥
ä¸è¬æ¥è¯´ï¼äºå¡æ¯å¿ 须满足4个æ¡ä»¶ï¼ACIDï¼ï¼ï¼ååæ§ï¼Atomicityï¼æ称ä¸å¯åå²æ§ï¼ãä¸è´æ§ï¼Consistencyï¼ãé离æ§ï¼Isolationï¼å称ç¬ç«æ§ï¼ãæä¹ æ§ï¼Durabilityï¼ã
- **ååæ§ï¼**ä¸ä¸ªäºå¡ï¼transactionï¼ä¸çæææä½ï¼è¦ä¹å ¨é¨å®æï¼è¦ä¹å ¨é¨ä¸å®æï¼ä¸ä¼ç»æå¨ä¸é´æ个ç¯èãäºå¡å¨æ§è¡è¿ç¨ä¸åçé误ï¼ä¼è¢«åæ»ï¼Rollbackï¼å°äºå¡å¼å§åçç¶æï¼å°±åè¿ä¸ªäºå¡ä»æ¥æ²¡ææ§è¡è¿ä¸æ ·ã
- **ä¸è´æ§ï¼**å¨äºå¡å¼å§ä¹ååäºå¡ç»æ以åï¼æ°æ®åºçå®æ´æ§æ²¡æè¢«ç ´åãè¿è¡¨ç¤ºåå ¥çèµæå¿ é¡»å®å ¨ç¬¦åææçé¢è®¾è§åï¼è¿å å«èµæç精确度ã串èæ§ä»¥ååç»æ°æ®åºå¯ä»¥èªåæ§å°å®æé¢å®çå·¥ä½ã
- **é离æ§ï¼**æ°æ®åºå 许å¤ä¸ªå¹¶åäºå¡åæ¶å¯¹å ¶æ°æ®è¿è¡è¯»ååä¿®æ¹çè½åï¼é离æ§å¯ä»¥é²æ¢å¤ä¸ªäºå¡å¹¶åæ§è¡æ¶ç±äºäº¤åæ§è¡è导è´æ°æ®çä¸ä¸è´ãäºå¡é离å为ä¸å级å«ï¼å æ¬è¯»æªæ交ï¼Read uncommittedï¼ã读æ交ï¼read committedï¼ãå¯éå¤è¯»ï¼repeatable readï¼å串è¡åï¼Serializableï¼ã
- **æä¹ æ§ï¼**äºå¡å¤çç»æåï¼å¯¹æ°æ®çä¿®æ¹å°±æ¯æ°¸ä¹ çï¼å³ä¾¿ç³»ç»æ éä¹ä¸ä¼ä¸¢å¤±ã
å¨ MySQL å½ä»¤è¡çé»è®¤è®¾ç½®ä¸ï¼äºå¡é½æ¯èªå¨æ交çï¼å³æ§è¡ SQL è¯å¥åå°±ä¼é©¬ä¸æ§è¡ COMMIT æä½ãå æ¤è¦æ¾å¼å°å¼å¯ä¸ä¸ªäºå¡å¡é¡»ä½¿ç¨å½ä»¤ BEGIN æ START TRANSACTIONï¼æè æ§è¡å½ä»¤ SET AUTOCOMMIT=0ï¼ç¨æ¥ç¦æ¢ä½¿ç¨å½åä¼è¯çèªå¨æ交ã
- äºå¡æ§å¶è¯å¥
- BEGIN æ START 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 READ å SERIALIZABLEã
-- äºå¡æä½
select * from stu;
-- æ¥çææåå¨å¼æï¼åªæinnodb æ¯æäºå¡
show engines;
-- å¼å§äºå¡
begin;
delete from stu where name like 'æ%';
select * from stu;
delete from stu where name like 'èµµ%';
-- åç°æä½æ误ï¼rollback åæ»
rollback;
-- æ交确认
commit;
show create table stu;
CREATE TABLE `stu` (
`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'å¦å·',
`name` varchar(30) NOT NULL COMMENT 'å§å',
`gender` enum('ç·','女') DEFAULT '女',
`score` tinyint unsigned DEFAULT NULL COMMENT 'æ绩',
`dept` varchar(100) DEFAULT NULL COMMENT 'ä¸ä¸',
PRIMARY KEY (`id`),
KEY `name_index` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=202153 DEFAULT CHARSET=utf8mb3 COMMENT='å¦çä¿¡æ¯è¡¨'
MySQLäºå¡é离级å«
äºå¡éç¦»çº§å« | è读 | ä¸å¯éå¤è¯» | 幻读 |
---|---|---|---|
读æªæ交ï¼read-uncommittedï¼ | æ¯ | æ¯ | æ¯ |
ä¸å¯éå¤è¯»ï¼read-committedï¼ | å¦ | æ¯ | æ¯ |
å¯éå¤è¯»ï¼repeatable-readï¼ | å¦ | å¦ | æ¯ |
串è¡åï¼serializableï¼ | å¦ | å¦ | å¦ |
æ¥çå½åç³»ç»é»è®¤çäºå¡é离级å«
read uncommited
select @@transaction_isolation;
show variables like '%transaction_isolation%';
select user(),database(),version(),now();
select user(),database(),version(),now();
create table cf_user(
id int unsigned auto_increment,
name varchar(30),
money int,
primary key(id)
)
insert into cf_user values(null,'æå½',1000),(null,'å¼ ä¸ä¸°',600);
select * from cf_user;
start transaction;
select * from cf_user;
update cf_user set money = money - 100 where id = 1;
commit;
select * from cf_user;
set session transaction isolation level read committed ;
start transaction ;
update cf_user set money = money - 50 where id =1;
select * from cf_user;