0

I'm not receiving an error currently when running the query to create a trigger but after running it I can't execute another query. It is as if I haven't closed some encapsulation:

DELIMITER // CREATE TRIGGER trigger_name BEFORE DELETE ON existingtable for each row begin INSERT INTO new_delete_table (column) values(old.column) end; END DELIMITER;

I am using command line and start with:

mysql>

I execute and receive a new:

mysql>

which is the normal behavior when a query is successful. If I then try to see my triggers I end up in an infinite loop where it is waiting for me to enter some character to close something.

mysql> show triggers;
    ->

I can use ctrl + c to exit the function but that boots me out of MySQL as well. When I log back in my trigger is not present and I can't find any errors.

3
  • I've tried it as // DELIMITER; after the first end;. Is that correct? The behavior seemed the same. Commented Sep 1, 2020 at 15:37
  • The published code is syntactically incorrect end; end is just wrong - did you transcribe correctly? And no the first end should not be there, the insert should be terminated and the begin should be ended (with an end //) and there should be a space between the delimiter keyword and the delimiter, Commented Sep 1, 2020 at 15:39
  • @P.Salmon Yes, this is my actual code, with exception of column/table names. Commented Sep 1, 2020 at 15:47

2 Answers 2

1

The DELIMITER command is special. All characters following the command until the end of line are interpreted as a string that you want to use as the new delimiter.

You must not put code on the same line, because all of that code will become part of the new delimiter. This is why you got no error, but no CREATE TRIGGER statement was executed. It only became part of a very long delimiter string.

The reason that DELIMITER must interpret the end-of-line as the end of the command is that it doesn't accept ; as the end of the DELIMITER command. If it did, there would be no way to reset the delimiter back to ;.

You asked in a comment if you need newlines. Aside from the newlines after DELIMITER commands, you do not need newlines. You can do this:

DELIMITER // 
CREATE TRIGGER trigger_name BEFORE DELETE ON existingtable FOR EACH ROW BEGIN INSERT INTO new_delete_table (column) VALUES(old.column) END //
DELIMITER ;

(Remember to use // as the delimiter at the end of the CREATE TRIGGER statement.)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this worked. The first end also had to removed.
0

Youhave some errors in your code

every code line must end in a semicolon and you have an END to much

DELIMITER // 
CREATE TRIGGER trigger_name BEFORE DELETE ON existingtable 
for each row 
begin 
    INSERT INTO new_delete_table (`column`) values
    (old.column) ;
end// 
DELIMITER;

3 Comments

This performs the same. Do I need actual new lines for each statement? I'm going from windows to linux so I'm smushing the syntax to avoid new line errors.
This code did not execute. It had the same behavior as the original.
check the answer the was a typo, try it please again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.