mysql基础教程(独家整理) 联系客服

发布时间 : 星期三 文章mysql基础教程(独家整理)更新完毕开始阅读

name varchar(20), p int

)engine=innodb;

insert into bank values(1,'yao',50); insert into bank values(2,'tom',0);

1:php 操作事务 2:存储过程操作事务

news09 test.php

delimiter //

create procedure up1() begin

declare a int; declare b int; declare rs int; start transaction;

update bank set p = p + 50 where id = 1; set a = row_count();

update bank set p = p - 50 where id = 2; set b = row_count(); set rs = a + b; if rs = 2 then commit; else

rollback; end if;

end // delimiter ;

事务:保存点

start transaction select * from test;

update bank p = p - 50 where id = 1; savepoint haha;//..

delete from bbs; select * from t; rollback to haha;

有一些特殊语句-->执行直接提交 commit; 1:truncate table 表名; 2:create 3:drop 4:alter

总结:

事务:多条不可分割sql语句 作用:保存数据完整性,正确性

特点:原子性,一致性,永久性,持久性 innodb

start transaction begin commit rollback;

savepoint 保存点

最好方式 mysql_query(\1:表引擎不是innodb 2:出错

DBUtils.class.php

//-------------------------

问题:面试问题->myisam引擎保证数据完整性 解决:锁

表存储引擎

innodb:支持事务,外键,

myisam:不支持事务,不支外键[查询速度] memory:所有数据保存内存->查询,更新

create table t( )enging=innodb;

create table t( id int

)enging=myisam;

begin insert delete commit;

方法提供事务完整性,正确性; myisam

语法: 加锁

lock tables 表名 read|write 解锁

unlock tables;

图示:

create table r1( id int,

name varchar(20), p int

)engine=myisam;

insert into r1 values(1,'tom',100); insert into r1 values(2,'jerry',0);

lock tables r1 write;

update r1 set p = p - 100 where id = 1; update r1 set p = p + 100 where id = 2;

unlock tables;

创建读锁:

1:其它用可以对表读操作 select ,不能作更新操作; 2:自己不能对表insert,update,delete

lock tables r1 read; select * from r1; unlock tables;

加读锁目的:多次读取内容内容不会改变;

总结:

1:创建'写'锁,其它用户对表不可读不可写 2:创建锁,锁定多个表[小心] lock tables r1 write,r2 write; 3:创建读锁 多次读取数据完整;

如果当你认真的读到这里,那么你的数据库基本操作将不会有大的问题,更高级和实战的文档,将在China Partner群里公布。 希望可以给你的学习带来帮助 !