Question Locked

Help Creating an SQL Trigger Using Notepad

0 helpful
1 reply
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi everyone,

I’m currently learning SQL and trying to create a trigger using Notepad for my MySQL database. I wrote the trigger script in Notepad and saved it as a .sql file. However, when I try to run it using the MySQL command line, I’m not sure if the syntax or formatting is correct.

Here’s what I’m trying to do: I want to automatically update a column in a table whenever a new row is inserted.

This is my sample trigger code:

DELIMITER //

CREATE TRIGGER update_timestamp
BEFORE INSERT ON employees
FOR EACH ROW
BEGIN
SET NEW.created_at = NOW();
END;
//

DELIMITER ;

When I run the file using this command:

bash
Copy
Edit
mysql -u root -p -D mydatabase < trigger_script.sql
I get an error, and I’m not sure if I’m missing something in Notepad formatting or syntax. Any advice or corrections would be appreciated!

Thanks in advance.
Dede

1 reply

by kees_b

What is the exact error you get?

Try to type the code directly into MySQL in stead of using a script. Copy-paste will be fine for a test.

Why not use something like DEFAULT CURRENT_TIMESTAMP in the field definition? Then you don't need a script.

This discussion is closed to new replies.