High Performance MySQL Chapter2

本文介绍了MySQL中InnoDB和BDB类型数据表支持的事务处理特性,包括事务隔离级别的设置与查询方法,并通过示例展示了如何使用存储过程实现事务控制及游标的使用技巧。
  • MYSQL中只有INNODB和BDB类型的数据表才能支持事务处理!其他的类型是不支持!
SELECT @@global.tx_isolation;  //查看系统当前隔离级别
SELECT @@session.tx_isolation; //session级别的事务隔离级别
SELECT @@tx_isolation;  //查看当前会话隔离级别
  • 需注意的是若指定了GLOBAL关键字则会对所有后续的session生效而对当前session无效。若指定了SESSION关键字则会对当前session中的后续的事务生效而对当前事务无效。若没有指定任何关键字则仅对当前session中接下来的一个事务生效。
设置当前会话隔离级别
set session transaction isolatin level repeatable read;

设置系统当前隔离级别
set global transaction isolation level repeatable read;
  • 用户可以用SET TRANSACTION语句改变单个会话或者所有新进连接的隔离级别
    SET [SESSION | GLOBAL] TRANSACTION ISOLATION LEVEL {READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE}
    默认的行为(不带session和global)是为下一个(未开始)事务设置隔离级别。如果你使用GLOBAL关键字,语句在全局对从那点开始创建的所有新连接(除了不存在的连接)设置默认事务级别。你需要SUPER权限来做这个。使用SESSION 关键字为将来在当前连接上执行的事务设置默认事务级别。 任何客户端都能自由改变会话隔离级别(甚至在事务的中间),或者为下一个事务设置隔离级别。

  • 网上有人使用set tx_isolation命令:
    mysql> set tx_isolation=’read-committed’;
    事务隔离变了。
    网上还有人这样写 set @@tx_isolation命令,但这个命令是有问题的。
    set @@tx_isolation=’read-committed’;
    session事物的隔离级别并没有改变。

  • 新建存储过程,输入如下代码,这即时一个事务的例子

BEGIN
        DECLARE t_error INTEGER DEFAULT 0;
        DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1;

        START  TRANSACTION;

        INSERT INTO TExchangeInfo2(FExchangeNo) VALUES('2DCE');
        INSERT INTO TExchangeInfo2(FExchangeNo) VaLUES('2ZCE');
        INSERT INTO TExchangeInfo2(FExchangeNo) VaLUES('2ZCE');

        IF t_error = 1 THEN
                ROLLBACK;
        ELSE
                COMMIT;
        END IF;

        select t_error; 
END
  • 当事务用如下方式时,删除数据经常性卡死,暂时不知道原因,测试上次事务并未提交导致
BEGIN
        DECLARE t_error INTEGER DEFAULT 0;
        DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1;

        START  TRANSACTION;

        INSERT INTO TExchangeInfo2(FExchangeNo) VALUES('2DCE');
        INSERT INTO TExchangeInfo2(FExchangeNo) VaLUES('2ZCE');
        SAVEPOINT save1;
        INSERT INTO TExchangeInfo2(FExchangeNo) VaLUES('2ZCE');

        IF t_error = 1 THEN
                ROLLBACK TO save1;
        ELSE
                COMMIT;
        END IF;

        select t_error; 
END
  • 游标
BEGIN
    DECLARE CurDone1 INT DEFAULT 0;
    DECLARE ttt VARCHAR(20);
    DECLARE PositionCursor CURSOR FOR SELECT FExchangeNo from TExchangeInfo;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET CurDone1 = 1;

    OPEN PositionCursor;
    FETCH PositionCursor INTO ttt;
    while not CurDone1 DO
        SELECT ttt;
        SET CurDone1 = 0;
        FETCH PositionCursor INTO ttt;
    END WHILE;
END

BEGIN
    DECLARE CurDone1 INT DEFAULT 0;
    DECLARE ttt VARCHAR(20);
    DECLARE PositionCursor CURSOR FOR SELECT FExchangeNo from TExchangeInfo ORDER BY FExchangeNo;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET CurDone1 = 1;

    OPEN PositionCursor;
    FETCH PositionCursor INTO ttt;
    while not CurDone1 DO
        IF ttt = '11DCE' THEN
            update TExchangeInfo SET FExchangeNo = CONCAT('1',ttt) WHERE FExchangeNo = ttt;
        END IF;

        SELECT ttt;
        SET CurDone1 = 0;
        FETCH PositionCursor INTO ttt;

    END WHILE;
END
  • mysql 不支持Current of cursor_name这种用法
    update TExchangeInfo SET FExchangeNo = CONCAT(‘1’,ttt) WHERE CURRENT OF PositionCursor;
    MySQL的游标是向前只读的,也就是说,你只能顺序地从开始往后读取结果集,不能从后往前,也不能直接跳到中间的记录.
-- COLLATE  指定字符排序方式
-- UNIQUE   约束唯一标识数据库表中的记录,UNIQUE和PRIMARY KEY 约束列或列集合提供了唯一性的保证
-- PRIMARY KEY拥有自定义的UNIQUE约束,每个表可以有多个UNIQUE约束,但是每个表只能有一个PRIMARY KEY约束

CREATE TABLE `TExchangeInfo`
(
`FSerialID`                     INT(10)                                          NOT NULL AUTO_INCREMENT,
`FExchangeNo`               VARCHAR(10) COLLATE utf8_bin NOT NULL DEFAULT '',
`FExchangeName`             VARCHAR(20) COLLATE utf8_bin NOT NULL DEFAULT '',
`FExchangeReportNo`     VARCHAR(20) COLLATE utf8_bin NOT NULL DEFAULT '',
`FOperatorNo`                   VARCHAR(20) COLLATE utf8_bin NOT NULL DEFAULT '',
`FExchangeDescribe`     VARCHAR(50) COLLATE utf8_bin                    DEFAULT '',
`FOperatorTime`             TIMESTAMP                                        NOT NULL   DEFAULT CURRENT_TIMESTAMP,
`FExchangeSequenceNo` INT(11)                                            NOT NULL DEFAULT 0,
PRIMARY KEY (`FExchangeNo`),
UNIQUE  KEY `FSerialID` (`FSerialID`)
)ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_bin


DROP PROCEDURE IF EXISTS `PM_Add_ExchangeInfo`;
CREATE DEFINER = `root`@`%` PROCEDURE `PM_Add_ExchangeInfo`(ExchangeNo varchar(10),ExchangeName varchar(20),ExchangeReportNo varchar(20),OperatorNo varchar(20),ExchangeDescribe varchar(50),OperatorTime timestamp,ExchangeSequenceNo  int(11))
BEGIN
    DECLARE OperatorLogInfo  VARCHAR(5000);
    DECLARE OperateTime          TIMESTAMP;

    SET OperateTime = NOW();

    INSERT INTO FExchangeInfo(FSerialID,FExchangeNo,FExchangeName,FExchangeReportNo,FOperatorNo,FExchangeDescribe,FOperatorTime,FExchangeSequenceNo)
    VALUES(ExchangeNo,ExchangeName,ExchangeReportNo,OperatorNo,ExchangeDescribe,OperatorTime,ExchangeSequenceNo);

END;
  • 创建表时只能指定一个主键,当需要使用多个字段作为联合主键时,可以使用这种方式
CREATE TABLE ttt
(
    FID INT,
    FName VARCHAR(20),
    FEnglishName VARCHAR(20),
    PRIMARY KEY(FID, FName)
);

如果这样写会报错
CREATE TABLE ttt
(
    FID INT PRIMARY KEY,
    FName VARCHAR(20) PRIMARY KEY,
    FEnglishName VARCHAR(20)
);

如果在创建表时候没有指定主键,也可以在创建完成之后,对标进行修改,其增加主键的格式如下
CREATE TABLE ttt
(
    FID INT,
    FName VARCHAR(20),
    FEnglishName VARCHAR(20)
);

ALTER TABLE ttt ADD PRIMARY KEY(FID, FName);
  • 返回自增流号的方法 select LAST_INSERT_ID
We had several goals in mind for this book. Many of them were derived from think- ing about that mythical perfect MySQL book that none of us had read but that we kept looking for on bookstore shelves. Others came from a lot of experience helping other users put MySQL to work in their environments. We wanted a book that wasn’t just a SQL primer. We wanted a book with a title that didn’t start or end in some arbitrary time frame (“...in Thirty Days,” “Seven Days To a Better...”) and didn’t talk down to the reader. Most of all, we wanted a book that would help you take your skills to the next level and build fast, reliable systems with MySQL—one that would answer questions like “How can I set up a cluster of MySQL servers capable of handling millions upon millions of queries and ensure that things keep running even if a couple of the servers die?” We decided to write a book that focused not just on the needs of the MySQL appli- cation developer but also on the rigorous demands of the MySQL administrator, who needs to keep the system up and running no matter what the programmers or users may throw at the server. Having said that, we assume that you are already rela- tively experienced with MySQL and, ideally, have read an introductory book on it. We also assume some experience with general system administration, networking, and Unix-like operating systems. This revised and expanded second edition includes deeper coverage of all the topics in the first edition and many new topics as well. This is partly a response to the changes that have taken place since the book was first published: MySQL is a much larger and more complex piece of software now. Just as importantly, its popularity has exploded. The MySQL community has grown much larger, and big corporations are now adopting MySQL for their mission-critical applications. Since the first edi- tion, MySQL has become recognized as ready for the enterprise.* People are also * We think this phrase is mostly marketing fluff, but it seems to convey a sense of importance to a lot of people. using it more and more in applications that are exposed to the Internet, where down- time and other problems cannot be concealed or tolerated. As a result, this second edition has a slightly different focus than the first edition. We emphasize reliability and correctness just as much as performance, in part because we have used MySQL ourselves for applications where significant amounts of money are riding on the database server. We also have deep experience in web applications, where MySQL has become very popular. The second edition speaks to the expanded world of MySQL, which didn’t exist in the same way when the first edition was written.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值