MySQL触发器和过程无法正确更新数据

时间:2015-12-14 17:23:36

标签: mysql

我有一个名为software_product的表的小型数据库,该表具有名为“build_status”的属性。这个值可以是“可用的”,“准备好的”,“未准备好”。此值来自所有软件产品组件的最低状态,这些组件保存在另一个表中(组件状态来自检查)。

我正在努力工作的是我的表格检查的触发器,当插入发生时,它设置新的组件状态,然后在需要时更新软件产品构建状态。下面是我的MySQL语句。如果有人能帮助我找到为什么我的build_status仍为NULL的问题,我将非常感激。

谢谢!

表格创建:

create table Software_Product (
productID int,
name varchar(100),
version varchar(20),
build_status varchar(15),
primary key (productID)
);

create table People (
personID int,
name varchar(40),
seniority date,
primary key (personID)
);

create table Components (
componentID int,
name varchar(100),
ownerID int,
status varchar(15),
size int,
programming_language varChar(15),
primary key (componentID),
foreign key (ownerID) references People(personID)
);

create table Software_Build (
productID int,
componentID int,
foreign key (componentID) references Components(componentID),
foreign key (productID) references Software_Product(productID)
);

create table Inspection (
personID int,
componentID int,
score int,
description text,
date date,
foreign key (personID) references People(personID),
foreign key (componentID) references Components(componentID)
);

触发:

DELIMITER $$
create trigger updateStatus1
after insert
on inspection
    for each row begin
        declare foo varchar(15);
        if(new.score > 100 or new.score < 0)
        then
            signal sqlstate '45000';
        end if;
        if(new.score >= 90)
        then
            update components
            set status = 'ready'
            where new.componentID = components.componentID;
        end if;
        if(new.score < 90 or new.score >= 75)
        then
            update components
            set status = 'usable'
            where new.componentID = components.componentID;
        end if;
        if(new.score < 75)
        then
            update components
            set status = 'not-ready'
            where new.componentID = components.componentID;
        end if;
        call getLowestStatus(new.componentID,@output);
        select @output into foo;
        call updateStatusOnSP(new.componentID,foo);
END $$
DELIMITER ;

getLowestStatusProcedure

DELIMITER $$
create procedure getLowestStatus(
    in icomponentID int, out ostatus varchar(15))
begin
    if((select distinct 1 from (select status
        from components
        where components.componentID = icomponentID) as newTable
        where status = 'ready') = 1)
    then
        set ostatus = 'ready';
    end if;
    if((select distinct 1 from (select status
        from components
        where components.componentID = icomponentID) as newTable
        where status = 'usable') = 1)
    then
        set ostatus = 'usable';
    end if;
    if((select distinct 1 from (select status
        from components
        where components.componentID = icomponentID) as newTable
        where status = 'not-ready') = 1)
    then
        set ostatus = 'not-ready';
    end if;
END $$
DELIMITER ;

updateStatusOnOP

DELIMITER $$
create procedure updateStatusOnSP(in icomponentID int,
    in istatus varchar(15))
begin
    update software_product
    inner join software_build on
    software_build.componentID = icomponentID
    set software_product.build_status = istatus;
END $$
DELIMITER ;

0 个答案:

没有答案