一个简单的外键加事务mysql代码

mysql

什么叫外键:保持表中数据的一致性,完整性。使两张表可以极联更新,和极联删除。 具体分化为:RESTRICT,NO ACTION,CASCADE ,SET NULL 事务:主要用于同步数据的同步性,防止由于忽发状况,数据之间出现断层。


drop database if exists nosay1;
create database nosay1 character set utf8;
use nosay1;
create
 table usertable (id int primary key auto_increment,username varchar(10)
 not null default 'nodefine',password varchar(36) not null default 
'12345')character set utf8 engine innodb;
create table userinfo(id 
int primary key auto_increment,`money` decimal(8) not null default 
'0',usertable_id int ,foreign key (usertable_id) references 
usertable(id) on delete cascade on update cascade )character set utf8 
engine innodb;
create table `class`(id int primary key 
auto_increment,`money` decimal(8) not null default '0',`count` 
tinyint(4) not null default '0')character set utf8 engine innodb;
start transaction;
insert into `class` values ('','','');
commit;
delimiter $$
create trigger insert_usertable after insert on usertable for each row
begin
insert into userinfo values (null,'2000',new.id);
update userinfo set `money` = `money`-200 where usertable_id=new.id;
update `class` set `money` = `money`+200,`count` = `count`+1;
end
delimiter ;
delimiter $$
create trigger delete_usertable after delete on usertable for each row
begin
update `class` set `money` = `money`-200,`count` = `count`-1;
end
delimiter ;
insert into usertable values (null,'nosay','12345');




2014-10-08 13:41:56

数据库
数据库

这是介绍的地方

数据库的标签

本文相关标签

推荐应用

友情链接


皖ICP备14007051号-2 关于穆子龙