Chinaunix首页 | 论坛 | 博客
  • 博客访问: 396797
  • 博文数量: 102
  • 博客积分: 1395
  • 博客等级: 中尉
  • 技术积分: 1050
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-05 18:09
文章分类

全部博文(102)

文章存档

2013年(25)

2012年(77)

分类: Mysql/postgreSQL

2012-12-12 13:29:01

三种出发器: insert 触发器, delete触发器, update触发器

eg:

CREATE TABLE tableA
(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
tablename VARCHAR(20));

CREATE TABLE tableB
(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
tablename VARCHAR(20));

CREATE TRIGGER t_insert_tableA
AFTER INSERT ON tableA
FOR EACH ROW
INSERT INTO tableB(tablename) VALUES(new.tablename);
INSERT INTO tableA VALUES(1,'aa');
CREATE TRIGGER t_inbefore_tableA
BEFORE INSERT ON tableA
FOR EACH ROW
INSERT INTO tableB(tablename) VALUES(new.tablename);
INSERT INTO tableA VALUES(2, 'bb');

CREATE TRIGGER t_del_tableA
BEFORE DELETE ON tableA
FOR EACH ROW
DELETE FROM tableB WHERE id=old.id;
DELETE FROM tableA WHERE id=1;

DELIMITER //
CREATE TRIGGER t_update_tableA
BEFORE UPDATE ON tableA
FOR EACH ROW
BEGIN
SET new.tablename = UPPER(new.tablename);
END //

DELIMITER ;

UPDATE tableA SET tablename='tian' WHERE id=2;
阅读(1821) | 评论(0) | 转发(0) |
0

上一篇:SQL触发器

下一篇:事务

给主人留下些什么吧!~~