2
I am trying to create a Trigger/Stored Function for when the user changes the value of a column from 0 to 1 it changes the feature of another column
CREATE TABLE `aluno` (
`CPF_ALU` varchar(11) NOT NULL,
`PRIORIDADE` tinyint(1) NOT NULL,
`CURSO` int(4) NOT NULL,
`ANO_ENTRADA` int(4) NOT NULL,
`SEMESTRE_ENTRADA` int(1) NOT NULL,
`TURNO` varchar(7) NOT NULL,
`MONITOR` tinyint(1) NOT NULL,
`VOLUNTARIO` tinyint(1) NOT NULL,
`BOLSISTA` tinyint(1) NOT NULL,
`DESISTIU` tinyint(1) NOT NULL,
`MEDIA_GERAL` float DEFAULT NULL,
`PROJETO_C_BOLSA` int(4) DEFAULT NULL,
`VALOR_BOLSA` float DEFAULT NULL,
`ORIENTADOR_TCC` varchar(11) DEFAULT NULL,
PRIMARY KEY (`CPF_ALU`),
KEY `fk_curso_aluno_idx` (`CURSO`),
KEY `fk_orientador_tcc_idx` (`ORIENTADOR_TCC`),
KEY `fk_projeto_c_bolsa_idx` (`PROJETO_C_BOLSA`),
CONSTRAINT `fk_cpf_aluno` FOREIGN KEY (`CPF_ALU`) REFERENCES `pessoa` (`CPF_P`) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT `fk_curso_aluno` FOREIGN KEY (`CURSO`) REFERENCES `curso` (`ID`) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT `fk_orientador_tcc` FOREIGN KEY (`ORIENTADOR_TCC`) REFERENCES `professor` (`CPF_PROF`) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT `fk_projeto_c_bolsa` FOREIGN KEY (`PROJETO_C_BOLSA`) REFERENCES `projeto` (`CODIGO`) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
This is the student table script, in which case when the user set the stock column value to 1 (true) it would change the PROJETO_C_BOLSA
and VALOR_BOLSA
for NOT NULL
.
DELIMITER $$
CREATE PROCEDURE alterarAluno ()
BEGIN
START transaction;
ALTER TABLE deinfo.aluno
CHANGE COLUMN PROJETO_C_BOLSA PROJETO_C_BOLSA INT(4) NOT NULL ,
CHANGE COLUMN VALOR_BOLSA VALOR_BOLSA FLOAT NOT NULL ;
COMMIT;
END $$
DELIMITER ;
DELIMITER $$
CREATE trigger configurarAlunoBolsistaInsert
before insert on deinfo.aluno for each row
begin
if (new.bolsista = 1) then
CALL deinfo.alterarAluno();
end if;
end $$
DELIMITER ;
DELIMITER $$
CREATE trigger configurarAlunoBolsistaUpdate
before update on aluno for each row
begin
if (new.bolsista = 1) then
CALL deinfo.alterarAluno();
end if;
end $$
DELIMITER ;
I created these Triggers/Procedure to make it work but when I try to update a row of the student table it returns me this error:
UPDATE `aluno` SET `BOLSISTA`='1', `PROJETO_C_BOLSA`='1', `VALOR_BOLSA`='400' WHERE `CPF_ALU`='14114631887' 1422: Explicit or implicit commit is not allowed in stored function or trigger.
Could someone give me a hand.
I’m using Mysql57, I’ve read the documentation but I haven’t found anything that could help me in this sense.
In place of CALL deinfo.alterarSilver(); do the column SET on Trigger itself , in addition to this sp alterarSilver calls Rigger itself which can generate a recursive call
– Motta
It doesn’t work either, but I’ve arranged another way to do the check on Rigger and if it wasn’t filled returns an exception to the application and it handles the error.
– ThiagoGomes
There is no design reason to change the type of a column for the entire table by changing a single record.
– Mateus