mysql预处理视频_mysql预处理事务处理实例讲解

本文通过三个实验详细介绍了MySQL的预处理语句使用,包括传入单个值、多个值以及删除操作。同时,探讨了MySQL的事务处理,特别是InnoDB引擎对事务的支持,展示了关闭自动提交、设置保存点、回滚和提交事务的步骤。实验中通过删除和恢复数据展示了事务的回滚功能,强调了事务在数据库操作中的重要性。
摘要由CSDN通过智能技术生成

6.9 mysql 预处理

实验一:传入一个值

mysql> prepare stu_pre from 'select * from student where id>?';

Query OK, 0 rows affected (0.00 sec)

Statement prepared

mysql> set @i=3;

Query OK, 0 rows affected (0.00 sec)

mysql> select @i;

+------+

| @i |

+------+

| 3 |

+------+

1 row in set (0.00 sec)

mysql> execute stu_pre using @i;

+------+------+------+-------+

| id | name | sex | score |

+------+------+------+-------+

| 4 | dd | male | 3.5 |

| 5 | ee | male | 3.6 |

| 6 | ff | male | 3.9 |

+------+------+------+-------+

3 rows in set (0.00 sec)

实验二:传入多个值

mysql> select * from student;

+------+------+--------+-------+

| id | name | sex | score |

+------+------+--------+-------+

| 1 | aa | female | NULL |

| 2 | bb | male | NULL |

| 3 | cc | male | 3.1 |

| 4 | dd | male | 3.5 |

| 5 | ee | male | 3.6 |

| 6 | ff | male | 3.9 |

+------+------+--------+-------+

mysql> set @id=3;

Query OK, 0 rows affected (0.00 sec)

mysql> set @sex='male';

Query OK, 0 rows affected (0.00 sec)

mysql> prepare stu_pre from 'select * from student where id execute stu_pre using @id,@sex;

+------+------+------+-------+

| id | name | sex | score |

+------+------+------+-------+

| 2 | bb | male | NULL |

+------+------+------+-------+

1 row in set (0.00 sec)

实验三:删除预处理

mysql> execute stu_pre using @id,@name;

+------+------+------+-------+

| id | name | sex | score |

+------+------+------+-------+

| 2 | bb | male | NULL |

+------+------+------+-------+

1 row in set (0.00 sec)

mysql> drop prepare stu_pre;

Query OK, 0 rows affected (0.00 sec)

mysql> execute stu_pre using @id,@name;

ERROR 1243 (HY000): Unknown prepared statement handler (stu_pre) given to EXECUTE

6.10 mysql 事务处理

MyISAM引擎不支持事务,innodb支持事务

修改表引擎:

mysql> show create table student;

+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| Table | Create Table |

+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| student | CREATE TABLE `student` (

`id` int(11) DEFAULT NULL,

`name` varchar(30) DEFAULT NULL,

`sex` varchar(10) DEFAULT NULL,

`score` float DEFAULT NULL

) ENGINE=MyISAM DEFAULT CHARSET=latin1 |

+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

mysql> alter table student engine=innodb;

Query OK, 4 rows affected (0.05 sec)

Records: 4 Duplicates: 0 Warnings: 0

mysql> show create table student;

+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| Table | Create Table |

+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| student | CREATE TABLE `student` (

`id` int(11) DEFAULT NULL,

`name` varchar(30) DEFAULT NULL,

`sex` varchar(10) DEFAULT NULL,

`score` float DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

6.10.1 关闭自动提交功能

set autocommit=0;

6.10.2 保存还原点

savepoint pointName;

6.10.3 还原至某个点

rollback to pointName;

6.10.4 还原到原始点

rollback

实验一:

#设置事务不自动提交

mysql> set autocommit=0;

Query OK, 0 rows affected (0.00 sec)

mysql> select * from student;

+------+------+--------+-------+

| id | name | sex | score |

+------+------+--------+-------+

| 1 | aa | female | NULL |

| 2 | bb | male | NULL |

| 3 | cc | male | 3.1 |

| 6 | ff | male | 3.9 |

+------+------+--------+-------+

4 rows in set (0.00 sec)

#删除id=1的数据

mysql> delete from student where id=1;

Query OK, 1 row affected (0.00 sec)

#设置还原点

mysql> savepoint p1;

Query OK, 0 rows affected (0.00 sec)

#可以看到id=1的数据已经消失

mysql> select * from student;

+------+------+------+-------+

| id | name | sex | score |

+------+------+------+-------+

| 2 | bb | male | NULL |

| 3 | cc | male | 3.1 |

| 6 | ff | male | 3.9 |

+------+------+------+-------+

3 rows in set (0.00 sec)

#再删除id=2的数据

mysql> delete from student where id=2;

Query OK, 1 row affected (0.00 sec)

#设置还原点

mysql> savepoint p2;

Query OK, 0 rows affected (0.00 sec)

#可以看到id=5的数据已经消失

mysql> select * from student;

+------+------+------+-------+

| id | name | sex | score |

+------+------+------+-------+

| 3 | cc | male | 3.1 |

| 6 | ff | male | 3.9 |

+------+------+------+-------+

2 rows in set (0.00 sec)

#回滚至第一个还原点

mysql> rollback to p1;

Query OK, 0 rows affected (0.00 sec)

#可以看到id=2的数据恢复了

mysql> select * from student;

+------+------+------+-------+

| id | name | sex | score |

+------+------+------+-------+

| 2 | bb | male | NULL |

| 3 | cc | male | 3.1 |

| 6 | ff | male | 3.9 |

+------+------+------+-------+

3 rows in set (0.00 sec)

#回滚到初始点

mysql> rollback;

Query OK, 0 rows affected (0.01 sec)

#可见数据全部恢复

mysql> select * from student;

+------+------+--------+-------+

| id | name | sex | score |

+------+------+--------+-------+

| 1 | aa | female | NULL |

| 2 | bb | male | NULL |

| 3 | cc | male | 3.1 |

| 6 | ff | male | 3.9 |

+------+------+--------+-------+

4 rows in set (0.00 sec)

#提交事务

mysql> commit ;

Query OK, 0 rows affected (0.00 sec)

mysql> select * from student;

+------+------+--------+-------+

| id | name | sex | score |

+------+------+--------+-------+

| 1 | aa | female | NULL |

| 2 | bb | male | NULL |

| 3 | cc | male | 3.1 |

| 6 | ff | male | 3.9 |

+------+------+--------+-------+

4 rows in set (0.00 sec)

注意:再进行实验时需要将事务的自动提交关闭,否则不能进行该实验。

希望与广大网友互动??

点此进行留言吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值