技术开发 频道

浅析MySQL InnoDB数据库引擎

我们下边的例子中读取数据的时候对数据加Read locks for Updating锁.

SESSION 1 mysql> delete from t where id=2; Query OK, 2 rows affected (0.09 sec) mysql> select * from t; +------+ | id | +------+ | 1 | +------+ 1 row in set (0.00 sec) mysql> begin; Query OK, 0 rows affected (0.00 sec) mysql> select max(id) from t for update; +---------+ | max(id) | +---------+ | 1 | +---------+ 1 row in set (0.00 sec) mysql> insert into t values(2); Query OK, 1 row affected (0.00 sec) mysql> select * from t; +------+ | id | +------+ | 1 | | 2 | +------+ 2 rows in set (0.00 sec) SESSION 2 mysql> select max(id) from t for update; ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction SESSION 1 mysql> commit; Query OK, 0 rows affected (0.09 sec) SESSION 2 mysql> select max(id) from t for update; +---------+ | max(id) | +---------+ | 2 | +---------+ 1 row in set (0.02 sec) mysql> insert into t values(3); Query OK, 1 row affected (0.09 sec) mysql> commit; Query OK, 0 rows affected (0.00 sec) mysql> select * from t; +------+ | id | +------+ | 1 | | 2 | | 3 | +------+ 3 rows in set (0.00 sec)
四、InnoDB外键约束

InnoDB引擎的表支持外键约束,和其它数据库一样,子表INSERT的数据必须要在主表中存在。其它引擎是否支持外键约束没有做测试。
0
相关文章