mysql触发器

mysql触发器语法如下:

CREATE TRIGGER 触发器名称 触发时刻 触发事件 ON 表名 FOR EACH ROW 触发语句;

其中触发时刻有2种:BEFORE,AFTER
触发事件有3种:INSERT,UPDATE,DELETE

举例:
CREATE TRIGGER t1 AFTER INSERT ON data FOR EACH ROW UPDATE chars SET countcount = count + CHAR_LENGTH(NEW.name);
create trigger t2 before delete on data for each row update chars set countcount=count-char_length(old.name);

CREATE TRIGGER t3 AFTER UPDATE ON accounts FOR EACH ROW INSERT INTO audit (id, balance, user, time) VALUES (OLD.id, NEW.balance, CURRENT_USER(), NOW());


触发器
delimiter //
CREATE TRIGGER t AFTER INSERT ON accounts FOR EACH ROW
BEGIN
DECLARE i int;
SET i=6;
insert into audit(accountid,balance,user,time,tick) values (NEW.id,NEW.balance,CURRENT_USER(), NOW(),i);
END;//
delimiter ;

insert into accounts(label,balance) values ('www',7000);

drop trigger IF EXISTS t;

show triggers;


视图
CREATE VIEW v AS SELECT * FROM accounts WHERE balance > 7000;
select * from v;

alter view v as select label from accounts WHERE balance > 7000;
DROP VIEW IF EXISTS v;
SHOW CREATE VIEW v;


存储过程
delimiter //
create procedure p(label varchar(10),balance float) 
begin
insert into accounts(label,balance) values (label,balance);
end;//
delimiter ;

call p('zhangsan',9000);

select * from accounts;
SHOW PROCEDURE STATUS like 'p';  or SHOW CREATE PROCEDURE p;
DROP PROCEDURE p;


清除记录
truncate table accounts; 
delete from accounts;

来自:
http://database.51cto.com/art/201005/200671.htm

教程:
http://dev.mysql.com/doc/refman/5.1/zh/triggers.html#create-trigger

你可能感兴趣的:(html,mysql)