Home > Database > Mysql Tutorial > body text

mysql-MySQL5.0,使用触发器修改表结构

WBOY
Release: 2016-06-06 09:42:29
Original
1186 people have browsed it

mysql触发器

需求:
表t_fault_code记录故障信息编码,
表t_fault_record记录故障记录,
表t_fault_count记录每日的故障统计
现在,code表中增加一条记录时,需要在count表中新增一列,用来存储record表中该故障的出现次数。
我的思路:
创建3个触发器,
trigger 1:code表insert一条记录后,查询count表中是否已经存在该列(列名:t_fault_code.Source)。如果不存在,新增一列。
trigger 2:count表中insert一条记录后,在count表中对应的那条记录下+1
trigger 3:count表中delete一条记录后,在count表中对应的那条记录下-1
问题:
创建第一个触发器时,代码如下:

<code> DELIMITER $CREATE TRIGGER t_fault_code_alter_trigger AFTER INSERT ON t_fault_code FOR EACH ROW BEGIN SET @columns_count = ( #确定count表中是否存在指定列    SELECT COUNT(*)    FROM information_schema.COLUMNS    WHERE table_schema='vhis' AND table_name='t_fault_count' AND column_name= NEW.Source);IF @coulmns_count = 0 THEN     ALTER TABLE t_fault_count ADD COLUMN NEW.Source INT DEFAULT 0 AFTER TrainNo;END IF;END DELIMITER $</code>
Copy after login

执行后出现如下错误提示:图片说明

现在不解的是,
1. 在触发器中是否不可以执行修改表结构的操作?
2. 如果可以的话,应该如何实现trigger 1?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template