im new in SQL programming. I need to create a trigger which after delete on "czytelnik" will also delete all records from "wypożyczył" which have the same CzytelnikID.
My code:
create trigger zadanie10
after update on czytelnik
for each row begin
if(old.Nazwisko != new.Nazwisko) then
delete from wypożyczył
where CzytelnikID=old.CzytelnikID;
end if;
end;
I got no idea what is worng in this code. Getting error after
"old.CzytelnikID;"
"end if;"
"end;"
I bypassed my problem this way:
create trigger zadanie10
after update on czytelnik
for each row
delete from wypożyczył
where old.CzytelnikID=wypożyczył.CzytelnikID and old.Nazwisko!=new.Nazwisko;
But if anybody know why i couldnt do the first way, please tell me.
Related
im trying to make my first delete trigger but im having a lot of errors. My goal is to create a trigger that prevents some rows to be deleted from a table, where the "created_id" equals 0.
I'm creating it on PHPMyAdmin. First i wasn't able to even create the trigger because i got a lot of errors. I was finally able to create this trigger:
CREATE TRIGGER `prevenirBorrado`
BEFORE DELETE ON `entidades_tipos`
FOR EACH ROW
BEGIN
IF entidades_tipos.created_by = 0 THEN
SIGNAL SQLSTATE 'U0042'
SET MESSAGE_TEXT = 'STRSQL No puedes borrar este registro!' ;
END IF ;
END
But now it won't let me delete any record from my table, getting this error:
SQLSTATE[42S02]: Base table or view not found: 1109 Unknown table 'entidades_tipos' in where clause
I was thinking about making AFTER DELETE and use 'ROLLBACK' but im not able to create the trigger without errors.
Thanks.
The SQLSTATE will raise an error. You could try adding an empty CONTINUE HANDLER immediately after the BEGIN statement.
Something like:
BEGIN
DECLARE CONTINUE HANDLER FOR SQLSTATE 'U0042' BEGIN END;
Also you need a semicolon at the end of your trigger declaration.
I've been trying to create a simple BEFORE INSERT trigger on a database table (MySQL v 5.7 ) but I keep receiving a vague "#1064 ... syntax error" message which doesn't help resolve the issue.
Here's the SQL:
CREATE OR REPLACE TRIGGER `CREATE_QUIZ_TRIG` BEFORE INSERT ON `quiz`
FOR EACH ROW BEGIN
SET NEW.ACTIVE = UPPER(NEW.ACTIVE);
SET NEW.CREATED = NOW();
END
/
All I'm trying to do is enforce a column to uppercase and then insert the current date & time into a timestamp column. I've been following the documentation from:
https://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html
and realise that for multi-statement expression I have to redefine the delimiter at the beginning of the trigger's creation but the same '#1064' error occurs.
This is made even more confusing because when I use phpmyadmin's interface for creating the same trigger it works fine - but won't when I export the generated SQL and try to create the trigger using that!?
Thanks for any help
I didn't realise that, by default, phpmyadmin adds a ; delimiter which was breaking the ; used to end a statement within the BEGIN END block.
CREATE TRIGGER backupFIDE AFTER UPDATE ON player
FOR EACH ROW
BEGIN
IF (OLD.FIDERating <> NEW.FIDERating) THEN
INSERT INTO playerbackup(PlayerName, OldFIDERating, NewFIDERating)
VALUES(OLD.PlayerName, OLD.FIDERating, NEW.NewFIDERating)
END;
Hi guys, receiving syntax error at line 6 for some reason, been looking online for solution to what might be the cause. Basically looking a simple trigger, that will copy a change change of FIDERating in Player table to a backup table containing OldFIDERating before it was changed, and the NewFIDERating along with the date change was made.
DELIMITER //
CREATE TRIGGER backupFIDE AFTER UPDATE ON player
FOR EACH ROW
BEGIN
IF NEW.FIDERating <> OLD.FIDERating
THEN
INSERT INTO playerbackup(PlayerName, OldFIDERating, NewFIDERating)
VALUES(OLD.PlayerName, OLD.FIDERating, NEW.NewFIDERating);
END IF;
END;
//
DELIMITER ;
I'm trying to create a trigger that everytime that insert a new data in my table tb_produto_parent, I need to update the columm cod_prod add one more.
Follow the trigger :
DELIMITER $$
create trigger trgAdicionaUm after insert
on tb_produto_parent
for each row
BEGIN
select cod_prod from tb_produto_parent;
update
tb_produto_parent set cod_prod = cod_prod +1;
END;
When I try to execute the code, MySQL show me a error :
Error Code: 1415 Not allowed to return a result set from a trigger.
Thanks !
There are two major problems with your code
you can't use SELECT on it own in a trigger because a trigger doesn't return a resultset to the client
you can't use DML statements (UPDATE in your case) on the same table (tb_produto_parent) on which you have your trigger in MySQL. Therefore even if you fix the first problem you still won't be able to update any row in tb_produto_parent within the trigger.
The only thing you can do in MySQL trigger is to alter values of columns of a row being inserted by using a BEFORE event for a trigger.
A possible solution is to use a stored procedure instead.
Error Code: 1415 Not allowed to return a result set from a trigger.
Looking at your trigger:
BEGIN
select cod_prod from tb_produto_parent;
update
tb_produto_parent set cod_prod = cod_prod +1;
END;
It would seem the select statement is the cause of this error. Remove it.
Being a complete newbie to this, I thought I might be able to save my self a lot of headache by just asking for help...
When I try to run this I'm getting an Error 1064: You have an error in your SQL Syntax...
CREATE TRIGGER payroll_lock BEFORE DELETE OR INSERT OR
UPDATE ON timesheet_entry FOR EACH ROW IF entry_date < '2013-07-25'
THEN raise_application_error(-20001, 'Cannot modify old records.');
I would also consider other options for stopping the insert/update/delete if the record is before a given fixed date.
Thanks for any help in fixing this! And explanations are appreciated, I don't know much in this particular area.
you should CALL keyword before raise_application_error function.
another tip is you should use NEW or OLD keyword for update trigger.
I'm not sure but I don't think you can create multiple trigger at same command with OR(this is first time I see that).
DELIMITER //
CREATE TRIGGER payroll_lock BEFORE UPDATE ON wp_links
FOR EACH ROW
BEGIN
IF NEW.entry_date < '2013-07-25' THEN
CALL raise_application_error(-20001, 'Cannot modify old records.');
END IF;
END//